-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagex_workflow.module
224 lines (211 loc) · 9.18 KB
/
imagex_workflow.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* @file
* Code for the imagex_workflow feature.
*/
/**
* Denotes the vocabulary machine of the taxonomy vocabulary used for access.
*/
define('IMAGEX_WORKFLOW_ACCESS_TAXONOMY_VOCABULARY_MACHINE_NAME', 'workbench_section');
include_once 'imagex_workflow.features.inc';
// Forced the inclusion of the workbench_moderation states and transitions
// as they are not auto-included during site install feature building using the
// `features_include_defaults` function as the component is not recognized as
// includable. If these files are not included, the foreach loop in
// `workbench_moderation_states_features_rebuild` presents a warning due to
// `$defaults` equalling false.
include_once 'imagex_workflow.features.workbench_moderation_states.inc';
include_once 'imagex_workflow.features.workbench_moderation_transitions.inc';
/**
* Implements hook_module_implements_alter().
*/
function imagex_workflow_module_implements_alter(&$implementations, $hook) {
if ('modules_enabled' == $hook) {
$group = $implementations['imagex_workflow'];
unset($implementations['imagex_workflow']);
$implementations['imagex_workflow'] = $group;
}
}
/**
* Implements hook_modules_enabled().
*/
function imagex_workflow_modules_enabled($modules) {
// If the `imagex_workflow` is not one of the modules enabled, exit early.
if (!in_array('imagex_workflow', $modules)) {
return;
}
// Attempt to laod the vocabulary access section and should it not exist,
// attempt to create it IF the taxonomy vocabulary exists.
$taxonomy_access = workbench_access_load('taxonomy', IMAGEX_WORKFLOW_ACCESS_TAXONOMY_VOCABULARY_MACHINE_NAME);
$vocabulary = NULL;
if (empty($taxonomy_access)) {
$vocabulary = taxonomy_vocabulary_machine_name_load(IMAGEX_WORKFLOW_ACCESS_TAXONOMY_VOCABULARY_MACHINE_NAME);
if (!$vocabulary) {
imagex_log(WATCHDOG_DEBUG, t('ImageX Workflow has been enabled but default workbench sections taxonomy vocabulary cannot be loaded by machine name: @machine_name.'), array(
'@machine_name' => IMAGEX_WORKFLOW_ACCESS_TAXONOMY_VOCABULARY_MACHINE_NAME,
));
return;
}
workbench_access_section_save(array(
'access_id' => $vocabulary->machine_name,
'access_scheme' => 'taxonomy',
'access_type' => 'taxonomy',
'access_type_id' => $vocabulary->machine_name,
));
$taxonomy_access = workbench_access_load('taxonomy', 'workbench_section');
}
else {
$vocabulary = taxonomy_vocabulary_machine_name_load($taxonomy_access['access_id']);
}
if (!empty($vocabulary)) {
// Attempt to load the `administrator` role for inserting this role as part
// as a role who has access (or is an editor) of all sections.
// TODO: Determine best approach as administrator doesn't exist until AFTER.
$administrator_role = user_role_load_by_name(IMAGEX_USER_ROLES_ADMINISTRATOR_ROLE);
// Retrieve all taxonomy terms that belong to the taxonomy access
// vocabulary in which we will ensure that the access is and saved.
$entity_query = new EntityFieldQuery();
$tids = $entity_query->entityCondition('entity_type', 'taxonomy_term')
->propertyCondition('vid', $vocabulary->vid)
->execute();
if (!empty($tids['taxonomy_term'])) {
$tids = array_keys($tids['taxonomy_term']);
$terms = taxonomy_term_load_multiple($tids);
foreach ($terms as $term) {
// Foreach term, ensure that the access exists and if not, save it.
$term_access = workbench_access_load('taxonomy', $term->tid);
if (!$term_access) {
workbench_access_section_save(array(
'access_id' => $term->tid,
'access_scheme' => 'taxonomy',
'access_type' => 'taxonomy',
'access_type_id' => $vocabulary->machine_name,
));
}
}
// By default, add the super user (uid = 1) to all workbench access terms.
workbench_access_user_section_save(IMAGEX_ADMINISTRATOR_UID, $term->tid, 'taxonomy');
if (!empty($administrator_role->rid)) {
workbench_access_role_section_save($administrator_role->rid, $term->tid, 'taxonomy');
}
}
workbench_access_user_section_save(IMAGEX_ADMINISTRATOR_UID, $vocabulary->machine_name, 'taxonomy');
if (!empty($administrator_role->rid)) {
workbench_access_role_section_save($administrator_role->rid, $vocabulary->machine_name, 'taxonomy');
}
}
// Attempt to retrieve the active workbench access tree and log errors should
// we not be able to successfully load the data.
$workbench_access_tree = workbench_access_get_active_tree();
if (FALSE === $workbench_access_tree) {
imagex_log(WATCHDOG_DEBUG, t('Unable to generate workbench access active tree.'));
return;
}
if (imagex_drupal_is_installing()) {
// TODO: Test the use case of where the module is installed AFTER Drupal.
// Save the access section for reach of the active tree elements.
foreach ($workbench_access_tree['tree'] as $access_id => $access_info) {
// TODO: Determine the most appropriate method of handling this.
// At times, default access_type and access_scheme may NOT be a taxonomy.
workbench_access_section_save(array(
'access_id' => $access_id,
'access_type' => 'taxonomy',
'access_scheme' => 'taxonomy',
'access_type_id' => $access_info['access_type_id'],
));
}
}
}
/**
* Implements hook_date_popup_pre_validate_alter().
*
* Assists in providing a default time for scheduler's publish_on and unpublish_on
* fields should only a date be presented. The date_popup api will present a form error
* should only the date be present and not the time during validation.
*/
function imagex_workflow_date_popup_pre_validate_alter(&$element, &$form_state, &$input) {
if (in_array($element['#name'], array('publish_on', 'unpublish_on')) && isset($form_state['values'][$element['#name']])) {
if (!empty($form_state['values'][$element['#name']]['date']) && empty($form_state['values'][$element['#name']]['time'])) {
$form_state['values'][$element['#name']]['time'] = '00:00:00';
}
$input = $form_state['values'][$element['#name']];
}
}
/**
* Implements hook_form_alter().
*/
function imagex_workflow_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'workbench_access_section_form':
$form['#submit'][] = 'imagex_workflow_workbench_access_section_form_submit';
break;
}
}
/**
* Submit handler for the workbench access section form.
*
* @param array $form
* The Drupal Form API array.
* @param array $form_state
* The current form's state.
*/
function imagex_workflow_workbench_access_section_form_submit(&$form, &$form_state) {
if (!empty($form_state['input']['sections']) && db_table_exists('workbench_access_user')) {
foreach ($form_state['input']['sections']['workbench_section'] as $key => $row) {
if (!empty($row)) {
db_merge('workbench_access_user')
->key(array('access_id' => $key))
->fields(array(
'uid' => 1,
'access_scheme' => 'taxonomy',
))
->execute();
}
else {
db_delete('workbench_access_user')
->condition('uid', 1)
->condition('access_id', $key)
->execute();
}
}
}
}
/*
* Implementation of hook_views_default_views_alter().
*/
function imagex_workflow_views_default_views_alter(&$views) {
// Alter only the 'admin_views_node' view.
if (array_key_exists('admin_views_node', $views)) {
/* Display: Defaults */
$handler =& $views['admin_views_node']->display['default'];
/* Filter criterion: Workbench Moderation: State */
$handler->display_options['filters']['state']['id'] = 'state';
$handler->display_options['filters']['state']['table'] = 'workbench_moderation_node_history';
$handler->display_options['filters']['state']['field'] = 'state';
$handler->display_options['filters']['state']['exposed'] = TRUE;
$handler->display_options['filters']['state']['expose']['operator_id'] = 'state_op';
$handler->display_options['filters']['state']['expose']['label'] = 'State';
$handler->display_options['filters']['state']['expose']['operator'] = 'state_op';
$handler->display_options['filters']['state']['expose']['identifier'] = 'state';
$handler->display_options['filters']['state']['expose']['remember_roles'] = array(
2 => '2',
1 => 0,
3 => 0,
7 => 0,
5 => 0,
6 => 0,
4 => 0,
8 => 0,
);
/* Filter criterion: Workbench Moderation: Current */
$handler->display_options['filters']['current']['id'] = 'current';
$handler->display_options['filters']['current']['table'] = 'workbench_moderation_node_history';
$handler->display_options['filters']['current']['field'] = 'current';
$handler->display_options['filters']['current']['value'] = '1';
$handler->display_options['filters']['current']['expose']['operator_id'] = '';
$handler->display_options['filters']['current']['expose']['label'] = 'Current revision';
$handler->display_options['filters']['current']['expose']['operator'] = 'current_op';
$handler->display_options['filters']['current']['expose']['identifier'] = 'current';
$handler->display_options['filters']['current']['expose']['required'] = TRUE;
}
}