-
Notifications
You must be signed in to change notification settings - Fork 7
/
islandora_workflow.rules.inc
313 lines (293 loc) · 9.12 KB
/
islandora_workflow.rules.inc
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* @file
* Provides Rules events and an action for integration with workflow.
*/
define('ISLANDORA_RULES_MANAGER', 'islandora_workflow_Manager');
define('ISLANDORA_RULES_EDITOR', 'islandora_workflow_Editor');
define('ISLANDORA_RULES_SUBMITTER', 'islandora_workflow_Submitter');
define('ISLANDORA_RULES_ADMINISTRATOR', 'islandora_workflow_Administrator');
/**
* Implements hook_rules_action_info().
*/
function islandora_workflow_rules_action_info() {
return array(
'islandora_workflow_rules_email_someone' => array(
'label' => t('Email Someone'),
'module' => 'Islandora Rules',
'arguments' => array(
'object' => array(
'type' => 'fedora_object',
'label' => t('The object that has been modified'),
),
),
'eval input' => array('message'),
),
);
}
/**
* Get a list of recipients for a email notification on an object change.
*
* @param Fedora_Item $object
* a Fedora object
* @param array $settings
* an array of Rules settings
*
* @return array
* an array of Drupal user ids
*/
function islandora_workflow_rules_email_recipients($object, $settings) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('permissions.inc', 'islandora_workflow');
module_load_include('inc', 'islandora_workflow');
// Variable in which to accumulate the array of recipients. (user ids)
$recipients = array();
// Get everyone who has the given workflow roles for the parent collection.
if ($settings['roles']) {
$collections = array();
$parents = $object->get_relationships('fedora:isMemberOf');
foreach ($parents as $parent) {
$collections[] = $parent['object'];
}
$parents = $object->get_relationships('fedora:isMemberOfCollection');
foreach ($parents as $parent) {
$collections[] = $parent['object'];
}
$options = array(
'collection' => $collections,
'permissions' => $settings['roles'],
);
$user_permissions = islandora_workflow_get_user_permissions($options);
// Return an array with the keys [user,collection,permission].
foreach ($user_permissions as $user_permission) {
$uid = $user_permission['user'];
$recipients[] = $uid;
}
}
if ($settings['notify_creator']) {
$creator_name = islandora_workflow_get_object_creator($object->pid);
$creator = user_load(array('name' => $creator_name));
if ($creator) {
$recipients[] = $creator->uid;
}
}
if ($settings['notify_assignee']) {
$assignee_name = islandora_workflow_get_object_assignee($object->pid);
$assignee = user_load(array('name' => $assignee_name));
if ($assignee) {
$recipients[] = $assignee->uid;
}
}
return array_filter(array_unique($recipients));
}
/**
* A Rules action to send email to people.
*
* @param Fedora_Item $object
* The object that has been altered
* @param array $settings
* An array of Rules settings
*/
function islandora_workflow_rules_email_someone($object, $settings) {
$recipients = islandora_workflow_rules_email_recipients($object, $settings);
foreach ($recipients as $recipient) {
$account = user_load($recipient);
// TODO: Find some way to make the language actually send the message in
// the preferred language of the user.
drupal_mail('islandora_workflow', NULL, $account->mail, user_preferred_language($account), $settings);
watchdog('iw_rules', 'Sending message about %object to %address', array(
'%object' => $object->pid,
'%address' => $account->mail,
));
}
}
/**
* Form definition.
*
* Settings form for the Rules action to send people email.
*
* @param array $settings
* An array of existing settings
* @param array $form
* The settings form to modify
*/
function islandora_workflow_rules_email_someone_form($settings, &$form) {
$settings += array(
'subject' => '',
'html_message' => TRUE,
'message' => '',
'roles' => array('admin'),
'notify_creator' => TRUE,
'notify_assignee' => FALSE,
);
$form['settings']['subject'] = array(
'#type' => 'textfield',
'#title' => t('Message subject'),
'#default_value' => $settings['subject'],
'#description' => t('The subject to set on the sent message.'),
);
$form['settings']['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#default_value' => $settings['message'],
'#description' => t('The message to be sent.'),
);
$form['settings']['html_message'] = array(
'#type' => 'checkbox',
'#title' => t('Send as HTML message'),
'#default_value' => $settings['html_message'],
'#description' => t('Adjusts the e-mail headers so HTML tags should be properly used.'),
);
$role_options = array(
ISLANDORA_RULES_ADMINISTRATOR => t('Workflow Administrator'),
ISLANDORA_RULES_MANAGER => t('Manager'),
ISLANDORA_RULES_EDITOR => t('Editor'),
);
$form['settings']['roles'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('Workflow Role'),
'#description' => t('Select the roles to be notified.'),
'#options' => $role_options,
'#default_value' => $settings['roles'],
);
$form['settings']['notify_creator'] = array(
'#type' => 'checkbox',
'#title' => t("Notify the object's creator"),
'#default_value' => $settings['notify_creator'],
);
$form['settings']['notify_assignee'] = array(
'#type' => 'checkbox',
'#title' => t("Notify the user assigned to the object"),
'#default_value' => $settings['notify_assignee'],
);
}
/**
* Form validation function.
*
* Ensure that either:
* a) At least one workflow role, or
* b) The creator
* Has been selected as email recipients.
* If not, display a form error.
*
* @param array $form
* The form.
* @param array $form_state
* The form state.
*/
function islandora_workflow_rules_email_someone_validate($form, &$form_state) {
$settings = $form_state['values']['settings'];
$has_recipient = !empty($settings['roles']) || $settings['notify_creator'];
if (!$has_recipient) {
form_set_error('settings', t('Please select at least one workflow role or creator'));
}
}
/**
* Implements hook_rules_event_info().
*
* @return array
* An array of event information.
*/
function islandora_workflow_rules_event_info() {
$arguments = array(
'object' => array(
'label' => t('Fedora Object'),
'type' => 'fedora_object',
),
);
return array(
'islandora_workflow_rules_reject_object' => array(
'label' => t('After rejecting a Fedora object'),
'module' => 'Islandora Rules',
'arguments' => $arguments,
),
'islandora_workflow_rules_approve_object' => array(
'label' => t('After approving a Fedora object'),
'module' => 'Islandora Rules',
'arguments' => $arguments,
),
'islandora_workflow_rules_submit_object' => array(
'label' => t('After submitting a Fedora object'),
'module' => 'Islandora Rules',
'arguments' => $arguments,
),
'islandora_workflow_rules_assign_object' => array(
'label' => t('After assigning a Fedora object'),
'module' => 'Islandora Rules',
'arguments' => $arguments,
),
'islandora_workflow_rules_delete_object' => array(
'label' => t('After deleting a Fedora object'),
'module' => 'Islandora Rules',
'arguments' => $arguments,
),
);
}
/**
* Implements hook_token_values().
*/
function islandora_workflow_token_values($type, $object = NULL, $options = array()) {
$values = array();
if ('fedora_object' == $type) {
$values['fedora-object-pid'] = $object->pid;
$values['fedora-object-title'] = $object->objectProfile->objLabel;
$values['fedora-object-creator'] = islandora_workflow_get_object_creator($object->pid);
}
return $values;
}
/**
* Implements hook_token_list().
*/
function islandora_workflow_token_list($type = 'all') {
$tokens = array();
if ('fedora_object' == $type) {
$tokens['fedora_object']['fedora-object-pid'] = t("The object PID");
$tokens['fedora_object']['fedora-object-title'] = t("The object's title");
$tokens['fedora_object']['fedora-object-creator'] = t("The object's creator");
}
return $tokens;
}
/**
* Implements hook_rules_data_type_info().
*/
function islandora_workflow_rules_data_type_info() {
$data_types = array();
$data_types['fedora_object'] = array(
'label' => t('Fedora object'),
'class' => 'FedoraObjectRulesDataType',
'saveable' => FALSE,
'identifiable' => TRUE,
'uses_input_form' => FALSE,
'module' => 'Islandora Workflow',
);
return $data_types;
}
class FedoraObjectRulesDataType extends rules_data_type {
/**
* Load a Fedora object.
*
* @param string $pid
* The Fedora PID of the object.
*
* @return Fedora_Item
* A Fedora object.
*/
function load($pid) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$object = new Fedora_Item($pid);
return $object;
}
/**
* Get the identifier for the Fedora object.
*
* @return mixed
* The PID of the Fedora object if it exists; NULL otherwise
*/
function get_identifier() {
$fedora_object = $this->get();
if ($fedora_object) {
return $fedora_object->pid;
}
}
}