-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_workbench.module
158 lines (144 loc) · 5.13 KB
/
scheduler_workbench.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Implements hook_form_FORM_ID_alter().
*
* Alters node_type forms to add fields to scheduler.
*
* @param array $form
* @param array $form_state
*/
function scheduler_workbench_form_node_type_form_alter(&$form, $form_state) {
$states = array('' => t('--None--')) + workbench_moderation_state_labels();
if (TRUE === isset($form['scheduler']['unpublish'])) {
$form['scheduler']['unpublish']['scheduler_unpublish_moderation_state'] = array(
'#type' => 'select',
'#title' => t('Moderation State'),
'#options' => $states,
'#description' => t('Moderation state to be placed in after unpublishing'),
'#default_value' => variable_get('scheduler_unpublish_moderation_state_' . $form['#node_type']->type, '')
);
$form['scheduler']['unpublish']['scheduler_unpublish_default_time'] = array(
'#type' => 'textfield',
'#title' => t('Default Time'),
'#description' => t('An offset from the current time such as "+1 day" or "-2 hours -30 minutes"'),
'#default_value' => variable_get('scheduler_unpublish_default_time_' . $form['#node_type']->type, '')
);
}
if (TRUE === isset($form['scheduler']['publish'])) {
$form['scheduler']['publish']['scheduler_publish_moderation_state'] = array(
'#type' => 'select',
'#title' => t('Moderation State'),
'#options' => $states,
'#description' => t('Moderation state to be placed in after publishing'),
'#default_value' => variable_get('scheduler_publish_moderation_state_' . $form['#node_type']->type, '')
);
}
}
/**
* Implements hook_permission().
*
* @return array
*/
function scheduler_workbench_permission() {
return array(
'override default scheduler time' => array(
'title' => t('Override default scheduler time'),
'description' => t('Allow users to override the default un-publish schedule time'),
),
);
}
/**
* Implements hook_form_alter().
*
* @param array $form
* @param array $form_state
*/
function scheduler_workbench_form_alter(&$form, $form_state) {
// Make sure we have scheduler settings.
if (TRUE === isset($form['scheduler_settings'])) {
$node = $form['#node'];
// And we have unpublish settings.
if (TRUE === isset($form['scheduler_settings']['unpublish_on'])) {
$unpublish_time = variable_get('scheduler_unpublish_default_time_' . $node->type, '');
// Only proceed if a default time has been set.
if (FALSE === empty($unpublish_time)) {
// Only set the default value on node creation.
if (TRUE === empty($node->nid)) {
// Make sure that the strtotime call doesn't throw an error.
try {
// Add the default to the created date and set the unpublish date.
$default_unpublish = date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . ' ' . $unpublish_time));
$form['scheduler_settings']['unpublish_on']['#default_value'] = $default_unpublish;
}
catch (Exception $e) {
// If we get an error log it.
watchdog('scheduler', $e->getMessage(), WATCHDOG_ERROR);
}
}
}
// If the user isn't allowed to override the default.
if (FALSE === user_access('override default scheduler time')) {
$unpublish_date = $form['scheduler_settings']['unpublish_on']['#default_value'];
if (TRUE === empty($unpublish_date)) {
$unpublish_date = t('Never');
}
// Hide the form element and show it as text only.
$form['scheduler_settings']['unpublish_on']['#access'] = FALSE;
$form['scheduler_settings']['unpublish_on']['#type'] = 'textfield';
$form['scheduler_settings']['unpublish_on_text'] = array(
'#markup' => t('<strong>Set to unpublish on:</strong> %date', array('%date' => $unpublish_date)),
);
}
}
}
}
/**
* Implements hook_module_implements_alter().
*
* Moves form alter hooks to the end to make sure they are after scheduler.
*
* @param array $implementations
* @param string $hook
*/
function scheduler_workbench_module_implements_alter(&$implementations, $hook) {
switch ($hook) {
case 'form_node_type_form_alter':
case 'form_alter':
$group = $implementations['scheduler_workbench'];
unset($implementations['scheduler_workbench']);
$implementations['scheduler_workbench'] = $group;
break;
}
}
/**
* Implements hook_scheduler_api().
*
* @param stdClass $node
* @param string $action
*/
function scheduler_workbench_scheduler_api($node, $action) {
$func = "_scheduler_workbench_scheduler_$action";
if (function_exists($func)) {
$func($node);
}
}
/**
* Handles when scheduler unpublishes a node.
*
* @param stdClass $node
*/
function _scheduler_workbench_scheduler_unpublish($node) {
if ($state = variable_get('scheduler_unpublish_moderation_state_' . $node->type, FALSE)) {
workbench_moderation_moderate($node, $state);
}
}
/**
* Handles when scheduler publishes a node.
*
* @param stdClass $node
*/
function _scheduler_workbench_scheduler_publish($node) {
if ($state = variable_get('scheduler_publish_moderation_state_' . $node->type, FALSE)) {
workbench_moderation_moderate($node, $state);
}
}