forked from nigelgbanks/cwrc_workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora_workflow_rest.module
551 lines (517 loc) · 21.5 KB
/
islandora_workflow_rest.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
/**
* @file
* The main module file for the Islanora workflow rest interface.
*/
define('ISLANDORA_WORKFLOW_REST_SOLR_DEFAULT_FIELD', 'workflow_userID_ms');
define('ISLANDORA_WORKFLOW_REST_ISLANDORA_ENTITIES_ACCESS_ALL_REPORTS', 'access all saved reports');
define('ISLANDORA_WORKFLOW_REST_ISLANDORA_ENTITIES_VIEW_REPORTS', 'view saved reports');
define('ISLANDORA_WORKFLOW_REST_ISLANDORA_MODIFY_WORKFLOW_DATASTREAMS', 'modify workflow datastreams');
/**
* Implements hook_permission().
*/
function islandora_workflow_rest_permission() {
return array(
ISLANDORA_WORKFLOW_REST_ISLANDORA_ENTITIES_ACCESS_ALL_REPORTS => array(
'title' => t('View all Workflow Reports'),
'description' => t('View, edit or delete all reports configured in workflow reports.'),
),
ISLANDORA_WORKFLOW_REST_ISLANDORA_ENTITIES_VIEW_REPORTS => array(
'title' => t('View Workflow Reports'),
'description' => t('View, edit or delete reports configured in workflow reports.'),
),
ISLANDORA_WORKFLOW_REST_ISLANDORA_MODIFY_WORKFLOW_DATASTREAMS => array(
'title' => t('Workflow REST Datastream access'),
'description' => t('View, edit, add or update workflow datastreams via REST interface.'),
),
);
}
/**
* Implements hook_menu().
*/
function islandora_workflow_rest_menu() {
return array(
'cwrc_workflow' => array(
'title' => 'My Workflow Logs',
'description' => 'Manage logged workflow events',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_workflow_rest_cwrc_workflow_management_form'),
'file' => 'includes/cwrc_workflow_status.inc',
'access arguments' => array('access content'),
),
// Workflow datastream form entry point tab.
'islandora/object/%islandora_object/workflow' => array(
'title' => 'Workflow',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_workflow_rest_entry_form', 2),
'type' => MENU_LOCAL_TASK,
'access arguments' => array(2),
'access callback' => TRUE,
'file' => 'includes/workflow.form.inc',
),
'islandora_workflow_rest/reports' => array(
'title' => 'Workflow Reports',
'description' => 'Configure new and existing Workflow reports.',
'page callback' => 'drupal_get_form',
'access arguments' => array(ISLANDORA_WORKFLOW_REST_ISLANDORA_ENTITIES_VIEW_REPORTS),
'page arguments' => array('islandora_workflow_rest_reports_form'),
'file' => 'includes/reports.form.inc',
'type' => MENU_NORMAL_ITEM,
),
// Administer/add new workflow stamps.
'admin/islandora/islandora_workflow_rest' => array(
'title' => 'Workflow Rest',
'description' => 'Configure custom Workflow Stamps.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('islandora_workflow_rest_admin'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
),
// Add workflow Step.
'islandora_workflow_rest/v1/add_workflow' => array(
'page callback' => 'islandora_workflow_rest_add_workflow_step',
'page arguments' => array(3),
'access callback' => 'islandora_workflow_rest_modify_workflow_ds',
'type' => MENU_CALLBACK,
),
// Get last workflow item.
'islandora_workflow_rest/v1/get_last_workflow' => array(
'page callback' => 'islandora_workflow_rest_get_last_workflow_step',
'access callback' => 'islandora_workflow_rest_modify_workflow_ds',
'type' => MENU_CALLBACK,
),
// Get full workflow datastream.
'islandora_workflow_rest/v1/get_full_workflow' => array(
'page callback' => 'islandora_workflow_rest_get_full_workflow',
'access callback' => 'islandora_workflow_rest_modify_workflow_ds',
'type' => MENU_CALLBACK,
),
// Test for stamp/category on object WORKFLOW datastream.
'islandora_workflow_rest/v1/has_entry' => array(
'page callback' => 'islandora_workflow_rest_test_workflow_step',
'access callback' => 'islandora_workflow_rest_modify_workflow_ds',
'type' => MENU_CALLBACK,
),
// Find all pids with given stamp/category, within an optional collection.
'islandora_workflow_rest/v1/workflow_query' => array(
'page callback' => 'islandora_workflow_rest_find_workflows',
'access callback' => 'islandora_workflow_rest_modify_workflow_ds',
'type' => MENU_CALLBACK,
),
'islandora_workflow_rest/user/autocomplete' => array(
'page callback' => 'islandora_workflow_rest_user_autocomplete',
'access arguments' => array('search islandora solr'),
'file' => 'includes/workflow.form.inc',
'type' => MENU_CALLBACK,
),
// Configure default SOLR Fields.
'islandora_workflow_rest/autocomplete_luke' => array(
'title' => 'Islandora Solr Luke autocomplete',
'description' => 'Autocomplete callback to populate solr text fields.',
'page callback' => 'islandora_workflow_rest_autocomplete_luke',
'access arguments' => array('administer islandora solr'),
'file' => 'includes/utilities.inc',
'type' => MENU_CALLBACK,
),
'islandora_workflow_rest/skip_entries/%' => array(
'title' => 'Skip checked entries',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_workflow_rest_cwrc_workflow_skip_entries_form', 2),
'access arguments' => array('administer islandora solr'),
'file' => 'includes/cwrc_workflow_status.inc',
'type' => MENU_CALLBACK,
),
'islandora_workflow_rest/delete_report/%' => array(
'title' => 'Delete report',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_workflow_rest_cwrc_workflow_delete_reports_form', 2),
'access callback' => 'islandora_workflow_rest_can_delete_report',
'access arguments' => array(2),
'file' => 'includes/reports.form.inc',
'type' => MENU_CALLBACK,
),
'islandora_workflow_rest/formatted_workflow/%islandora_object' => array(
'title' => 'Workflow',
'page callback' => 'islandora_workflow_rest_show_formatted_workflow',
'page arguments' => array(2),
'access callback' => 'islandora_workflow_rest_can_access_formatted',
'access arguments' => array(2),
'file' => 'includes/formatted_workflow.inc',
'type' => MENU_CALLBACK,
),
);
}
/**
* CRUD access callback for workflow datastreams.
*
* @return bool
* TRUE if access is allowed, FALSE otherwise.
*/
function islandora_workflow_rest_modify_workflow_ds() {
module_load_include('inc', 'islandora_workflow_rest', 'includes/utilities');
$params = islandora_workflow_rest_get_request_parameters();
$object = islandora_object_load($params['PID']);
$info = islandora_object_access_callback(
ISLANDORA_WORKFLOW_REST_ISLANDORA_MODIFY_WORKFLOW_DATASTREAMS, $object);
if (!$info) {
echo drupal_json_output(array("Error" => "Access Denied."));
exit();
}
return $info;
}
/**
* Delete reports access callback.
*
* @param string $lid
* The list id for the given report.
*
* @return bool
* TRUE if access is granted, FALSE otherwise.
*/
function islandora_workflow_rest_can_delete_report($lid) {
module_load_include('inc', 'islandora_workflow_rest', 'includes/report_utilities');
global $user;
$record = islandora_workflow_rest_get_record('lid', $lid);
if (!$record) {
return FALSE;
}
$edit_roles = unserialize($record['editroles']);
$can_edit = count(array_intersect($edit_roles, $user->roles)) ||
user_access(ISLANDORA_WORKFLOW_REST_ISLANDORA_ENTITIES_ACCESS_ALL_REPORTS);
return $can_edit;
}
/**
* Implements hook_mail().
*/
function islandora_workflow_rest_mail($key, &$message, $params) {
global $base_url;
$formatted_name = format_username($params['mail_account']);
$data['user'] = $params['mail_account'];
$options['language'] = $message['language'];
$variables = array(
"@username" => $formatted_name,
"@site" => $base_url,
"@message" => $params['body'],
"@link" => $base_url . $params['link'],
);
user_mail_tokens($variables, $data, $options);
$langcode = $message['language']->language;
$message['subject'] = t('Notification from @site', $variables, array('langcode' => $langcode));
$message['body'][] = t("Dear @username\n\n@message\n\n@link\n\n", $variables, array('langcode' => $langcode));
$pm_message = "";
$success;
$private_msg_available = module_exists('privatemsg');
if ($private_msg_available) {
$pm_message = "Private message notification sent";
// Utilize the Privatemsg module notification system.
// Send a message to the user with privatemsg_new_thread.
$success = privatemsg_new_thread(
array($params['mail_account']), t('Notification from @site', $variables, array('langcode' => $message['language']->language)), t("Dear @username\n\n@message\n\n@link\n\n", $variables, array('langcode' => $langcode)
)
);
if (!$success['success']) {
$pm_message = "";
foreach ($success['messages']['error'] as $error_message) {
$pm_message .= $error_message . " ";
}
}
else {
$pm_message .= " and email notification set to $formatted_name";
}
}
$message['send'] = $params['force_email'];
drupal_set_message(
t("@pmsg", array(
'@pmsg' => ($pm_message == "" ? "Enable Private Message module to receive message notifications." : $pm_message),
)
), 'status'
);
if ($message['send'] && (!$success['success'])) {
drupal_set_message(
t("@email", array(
'@email' => "Email force sent to $formatted_name",
)
), 'status'
);
}
}
/**
* Test a workflow for particular attributes.
*/
function islandora_workflow_rest_test_workflow_step() {
try {
module_load_include('inc', 'islandora_workflow_rest', 'includes/utilities');
module_load_include('inc', 'islandora_workflow_rest', 'includes/cwrc_workflow');
module_load_include('inc', 'islandora_workflow_rest', 'includes/basic_enum');
module_load_include('inc', 'islandora_workflow_rest', 'includes/workflow_item');
$params = islandora_workflow_rest_get_request_parameters();
if (!isset($params['PID'])) {
return drupal_json_output(array("Error" => "PID was not supplied for this request."));
}
$object = islandora_object_load($params['PID']);
if (!$object['WORKFLOW']) {
return drupal_json_output("$object->id does not have a workflow datastream.");
}
$workflow = CWRCWorkflowAPI::fromDatastream($object['WORKFLOW']);
$response = array("response" => array());
// Test if the given object has the given stamp/category/status.
foreach ($params as $key => $value) {
if ($key != "PID" && $key != "simple") {
$response["response"][$key] = $workflow->getWorkflowStepsByAttributeValue($key, $value);
}
}
// Just a simple true or false response.
if (isset($params['simple']) && strtoupper($params['simple']) == "TRUE") {
foreach ($response["response"] as $key => $value) {
if (isset($response["response"][$key]) && count($response["response"][$key]) > 0) {
$response["response"][$key] = "TRUE";
}
else {
$response["response"][$key] = "FALSE";
}
}
}
unset($response["response"]["q"]);
return drupal_json_output($response);
}
catch (Exception $e) {
return drupal_json_output($e);
}
}
/**
* Return the last workflow step for an islandora object.
*/
function islandora_workflow_rest_get_last_workflow_step() {
try {
module_load_include('inc', 'islandora_workflow_rest', 'includes/utilities');
module_load_include('inc', 'islandora_workflow_rest', 'includes/cwrc_workflow');
module_load_include('inc', 'islandora_workflow_rest', 'includes/basic_enum');
module_load_include('inc', 'islandora_workflow_rest', 'includes/workflow_item');
$params = islandora_workflow_rest_get_request_parameters();
if (!isset($params['PID'])) {
return drupal_json_output(array("Error" => "PID was not supplied for this request."));
}
$object = islandora_object_load($params['PID']);
if (!$object['WORKFLOW']) {
return drupal_json_output("$object->id does not have a workflow datastream.");
}
$workflow = CWRCWorkflowAPI::fromDatastream($object['WORKFLOW']);
return drupal_json_output($workflow->entryToArray());
}
catch (Exception $e) {
return drupal_json_output($e);
}
}
/**
* Return the full workflow datastream for an islandora object.
*/
function islandora_workflow_rest_get_full_workflow() {
try {
module_load_include('inc', 'islandora_workflow_rest', 'includes/utilities');
module_load_include('inc', 'islandora_workflow_rest', 'includes/cwrc_workflow');
module_load_include('inc', 'islandora_workflow_rest', 'includes/basic_enum');
module_load_include('inc', 'islandora_workflow_rest', 'includes/workflow_item');
$params = islandora_workflow_rest_get_request_parameters();
if (!isset($params['PID'])) {
return drupal_json_output(array("Error" => "PID was not supplied for this request."));
}
$object = islandora_object_load($params['PID']);
if (!$object['WORKFLOW']) {
return drupal_json_output("$object->id does not have a workflow datastream.");
}
$workflow = CWRCWorkflowAPI::fromDatastream($object['WORKFLOW']);
echo $workflow->workflowToJson();
}
catch (Exception $e) {
return drupal_json_encode($e);
}
}
/**
* Rest end-point for workflow entries.
*/
function islandora_workflow_rest_add_workflow_step() {
try {
$use_realname = module_exists('realname');
module_load_include('inc', 'islandora_workflow_rest', 'includes/cwrc_workflow');
module_load_include('inc', 'islandora_workflow_rest', 'includes/basic_enum');
module_load_include('inc', 'islandora_workflow_rest', 'includes/workflow_item');
module_load_include('inc', 'islandora_workflow_rest', 'includes/utilities');
$params = islandora_workflow_rest_get_request_parameters();
if (!isset($params['PID'])) {
return drupal_json_output(array("Error" => "PID was not supplied for this request."));
}
// Assign parameter values from GET request.
$activity_params = (isset($params['activity']) ? json_decode($params['activity'], TRUE) : array());
$tool_id = (isset($params['toolID']) ? array("toolID" => json_decode($params['toolID'])) : array());
$assigned_params = (isset($params['assigned']) ? json_decode($params['assigned'], TRUE) : array());
$assigned_accounts = array();
$activity_accounts = array();
if (isset($activity_params['recipient'])) {
$activity_accounts = islandora_workflow_rest_load_users_from_csv($activity_params['recipient']);
$activity_recipients = "";
foreach ($activity_accounts as $account) {
if (!$account) {
return drupal_json_output(array("Error" => "Supplied user is not in this system"));
}
$activity_recipients .= $account->name . ",";
}
$activity_params['recipient'] = rtrim($activity_recipients, ",");
}
if (isset($assigned_params['recipient'])) {
$assigned_accounts = islandora_workflow_rest_load_users_from_csv($assigned_params['recipient']);
$assigned_recipients = "";
foreach ($assigned_accounts as $account) {
if (!$account) {
return drupal_json_output(array("Error" => "Supplied user is not in this system"));
}
$assigned_recipients .= $account->name . ",";
}
$assigned_params['recipient'] = rtrim($assigned_recipients, ",");
}
$islandora_object = islandora_object_load($params['PID']);
if (!$islandora_object['WORKFLOW']) {
$workflow = CWRCWorkflowAPI::fromTemplate();
CWRCWorkflowAPI::updateDatastream($workflow, $islandora_object);
}
else {
$workflow = CWRCWorkflowAPI::fromDatastream($islandora_object['WORKFLOW']);
}
$activity = new WorkflowItem(WorkflowConst::ACTIVITY, $activity_params
);
$assigned = new WorkflowItem(WorkflowConst::ASSIGNED, $assigned_params);
$entry = new WorkflowItem(WorkflowConst::WORKFLOW, $tool_id);
// Get the last workflow step, if exists.
$last_workflow = $workflow->entryToArray();
if ($last_workflow) {
$last_wf_string = islandora_workflow_rest_prepare_workflow_string($last_workflow, "previous");
}
else {
$last_wf_string = "";
}
$workflow->addWorkflowStep($islandora_object->id, $entry, $activity, $assigned);
CWRCWorkflowAPI::updateDatastream($workflow, $islandora_object);
// Get the current workflow step.
$this_workflow = $workflow->entryToArray();
$this_wf_string = islandora_workflow_rest_prepare_workflow_string($this_workflow, "current");
if (isset($activity->typeAttributes[WorkflowConst::BODY]) &&
isset($activity->typeAttributes[WorkflowConst::SUBJECT]) &&
isset($activity->typeAttributes[WorkflowConst::RECIPIENT])) {
// Email activity notification.
$params['body'] = $activity->typeAttributes[WorkflowConst::BODY] . "\n\n" .
$this_wf_string . "\n\n" . $last_wf_string;
$params['link'] = url("islandora/object/$islandora_object->id/workflow");
$params['subject'] = $activity->typeAttributes[WorkflowConst::SUBJECT];
$params['accounts'] = islandora_workflow_rest_load_users_from_csv($activity->typeAttributes[WorkflowConst::RECIPIENT], $use_realname);
$params['force_email'] = 1;
islandora_workflow_rest_notify($params);
}
if (isset($assigned->typeAttributes[WorkflowConst::BODY]) &&
isset($assigned->typeAttributes[WorkflowConst::SUBJECT]) &&
isset($assigned->typeAttributes[WorkflowConst::RECIPIENT])) {
// Email Assigned notification.
$params['body'] = $assigned->typeAttributes[WorkflowConst::BODY] . "\n\n" .
$this_wf_string . "\n\n" . $last_wf_string;
$params['link'] = url("islandora/object/$islandora_object->id/workflow");
$params['subject'] = $assigned->typeAttributes[WorkflowConst::SUBJECT];
$params['accounts'] = islandora_workflow_rest_load_users_from_csv($assigned->typeAttributes[WorkflowConst::RECIPIENT], $use_realname);
$params['force_email'] = 1;
islandora_workflow_rest_notify($params);
}
return drupal_json_output($workflow->entryToArray());
}
catch (Exception $e) {
return drupal_json_output($e);
}
}
/**
* Callback to retrieve workflow pids with given attributes.
*/
function islandora_workflow_rest_find_workflows() {
try {
module_load_include('inc', 'islandora_workflow_rest', 'includes/utilities');
module_load_include('inc', 'islandora_workflow_rest', 'includes/cwrc_workflow');
module_load_include('inc', 'islandora_workflow_rest', 'includes/basic_enum');
module_load_include('inc', 'islandora_workflow_rest', 'includes/workflow_item');
$params = islandora_workflow_rest_get_request_parameters();
$collection_pid = (isset($params['PID']) ? $params['collection_pid'] : NULL);
$required = (isset($params['required']) ? json_decode($params['required'], TRUE) : array());
$custom_query = (isset($params['query']) ? $params['query'] : NULL);
$workflow_pids = CWRCWorkflowAPI::findWorkflowPids($custom_query, $collection_pid, $required, 0, 5, TRUE);
return drupal_json_output($workflow_pids);
}
catch (Exception $e) {
return drupal_json_output($e);
}
}
/**
* Implements hook_islandora_datastream_ingested().
*/
function islandora_workflow_rest_islandora_datastream_ingested(AbstractObject $object, AbstractDatastream $datastream) {
module_load_include('inc', 'islandora_workflow_rest', 'includes/utilities');
islandora_workflow_add_or_modify($object, $datastream, 'created');
}
/**
* Implements hook_islandora_datastream_modified().
*/
function islandora_workflow_rest_islandora_datastream_modified(AbstractObject $object, AbstractDatastream $datastream) {
module_load_include('inc', 'islandora_workflow_rest', 'includes/utilities');
islandora_workflow_add_or_modify($object, $datastream, 'modified');
}
/**
* Implements hook_menu_alter().
*/
function islandora_workflow_rest_menu_alter(&$items) {
$items['islandora/search']['access callback'] = 'islandora_workflow_rest_solr_access';
}
/**
* Denies access to roles not associated with report.
*
* @global user $user
*
* @return bool
* Access
*/
function islandora_workflow_rest_solr_access() {
global $user;
module_load_include('inc', 'islandora_workflow_rest', 'includes/report_utilities');
if (isset($_GET['workflow_report'])) {
$record = islandora_workflow_rest_get_record('lid', $_GET['workflow_report']);
$view_roles = unserialize($record['viewroles']);
if (count(array_intersect($view_roles, $user->roles)) || user_access(ISLANDORA_WORKFLOW_REST_ISLANDORA_ENTITIES_ACCESS_ALL_REPORTS)) {
return TRUE;
}
else {
return FALSE;
}
}
return user_access('search islandora solr');
}
/**
* Implements hook_islandora_solr_primary_display().
*/
function islandora_workflow_rest_islandora_solr_primary_display() {
return array(
'cwrc_report' => array(
'name' => t('CWRC Report'),
'module' => 'islandora_workflow_rest',
'file' => 'includes/solr_results.inc',
'class' => 'IslandoraSolrResultsWorkflowRest',
'function' => 'displayResults',
'description' => t('Search results for CWRC reporting.'),
),
);
}
/**
* Access callback for viewing a formatted WORKFLOW datastream.
*
* @param AbstractObject $object
* An AbstractObject representing a Fedora object.
*
* @return bool
* TRUE if the user has access, FALSE otherwise.
*/
function islandora_workflow_rest_can_access_formatted(AbstractObject $object) {
return islandora_datastream_access('access content', $object['WORKFLOW']);
}