-
Notifications
You must be signed in to change notification settings - Fork 8
/
islandora_webform.module
799 lines (723 loc) · 28.6 KB
/
islandora_webform.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
<?php
/**
* @file
* Implements hooks and theme preprocess functions for this module.
*/
/** Internal predicates used to mark objects created by a Webform
*/
define('ISLANDORA_WEBFORM_ISIW_PREDICATE', 'isIWContributed');
define('ISLANDORA_WEBFORM_HASIW_PREDICATE', 'hasIW');
define('ISLANDORA_WEBFORM_SUBMIT_PER_OBJECT_LIMIT_ID', -910);
//define('ISLANDORA_WEBFORM_SUBMIT_ATATIME_LIMIT_ID', -911);
/**
* Hook implementations...
*/
/**
* Implements hook_menu().
*/
function islandora_webform_menu() {
$items['admin/islandora/configure/iw'] = array(
'title' => 'Islandora Webform',
'description' => 'Configuration page for Islandora Webform module',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_webform_admin'),
'file' => 'includes/admin.form.inc',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'weight' => -1,
);
$items['node/%webform_menu/webform/islandora'] = array(
'title' => 'Islandora settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_webform_configure_stepped_form', 1),
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'file' => 'includes/webform.configure.inc',
'weight' => 5,
'type' => MENU_LOCAL_TASK,
);
$items['islandora/object/%islandora_object/submissions'] = array(
'title' => 'Submissions',
'page callback' => 'islandora_webform_results_object_submissions_page',
'page arguments' => array(2),
'access callback' => 'islandora_webform_object_results_access',
'access arguments' => array(2),
'file' => 'includes/islandora_webform.report.inc',
'weight' => 4,
'type' => MENU_LOCAL_TASK,
);
$items['islandora_webform/%/tag_object/%/nojs'] = array(
'page callback' => 'islandora_webform_action_tag_object',
'page arguments' => array(1, 3, 4),
'access callback' => '_islandora_webform_user_access_or',
'access arguments' => array(
'manage islandora webform',
'islandora webform link objects',
),
'type' => MENU_CALLBACK,
'file' => 'tag_object.inc',
'file path' => drupal_get_path('module', 'islandora_webform') . '/includes',
);
$items['islandora_webform/%/tag_object/%/ajax'] = array(
'delivery callback' => 'ajax_deliver',
)
+ $items['islandora_webform/%/tag_object/%/nojs'];
$items['islandora_webform/%/untag_object/%/nojs'] = array(
'page callback' => 'islandora_webform_action_untag_object',
'page arguments' => array(1, 3, 4),
'access callback' => '_islandora_webform_user_access_or',
'access arguments' => array(
'manage islandora webform',
'islandora webform link objects'
),
'type' => MENU_CALLBACK,
'file' => 'tag_object.inc',
'file path' => drupal_get_path('module', 'islandora_webform') . '/includes',
);
$items['islandora_webform/%/untag_object/%/ajax'] = array(
'delivery callback' => 'ajax_deliver',
) + $items['islandora_webform/%/untag_object/%/nojs'];
// Callback for displaying a webform.
$items['islandora_webform/retrieve_form/%node/nojs'] = array(
'page callback' => '_iw_inline_webform',
'page arguments' => array(2,3),
'access callback' => 'node_access',
'access arguments' => array('view', 2),
'file' => 'inline_webform.inc',
'file path' => drupal_get_path('module', 'islandora_webform') . '/includes',
);
// Ajax version of the previous.
$items['islandora_webform/retrieve_form/%node/ajax'] = array(
'delivery callback' => 'ajax_deliver',
)+ $items['islandora_webform/retrieve_form/%node/nojs'];
// Callback for displaying a webform with previously submitted data.
$items['islandora_webform/retrieve_form/%webform_menu/nojs/submission/%webform_menu_submission/edit'] = array(
'page callback' => '_iw_inline_webform',
'load arguments' => array(2),
'page arguments' => array(2,3,5),
'access callback' => 'webform_submission_access',
'access arguments' => array(2, 5, 'edit'),
'file' => 'inline_webform.inc',
'file path' => drupal_get_path('module', 'islandora_webform') . '/includes',
);
// Ajax version of the previous.
$items['islandora_webform/retrieve_form/%webform_menu/ajax/submission/%webform_menu_submission/edit'] = array(
'delivery callback' => 'ajax_deliver',
)+ $items['islandora_webform/retrieve_form/%webform_menu/nojs/submission/%webform_menu_submission/edit'];
return $items;
}
/**
* Implements hook_permission().
*/
function islandora_webform_permission() {
return array(
'manage islandora webform' => array(
'title' => t('Manage Islandora Webforms'),
'description' => t('Manage islandora settings on webforms.'),
),
'islandora webform link objects' => array(
'title' => t('Link islandora objects to webforms'),
'description' => t('Permission to link individual islandora objects to webforms'),
),
);
}
/**
* Implements hook_theme().
*/
function islandora_webform_theme() {
return array(
'islandora_webform_links' => array(
'file' => 'theme.inc',
'path' => drupal_get_path('module', 'islandora_webform') . '/theme',
'variables' => array(
'webform_data' => NULL,
'islandora_object' => NULL,
),
),
);
}
/**
* Implements hook_node_load().
*
* Add islandora object to $node->webform where
*/
function islandora_webform_node_load($nodes, $types) {
$args = drupal_get_query_parameters();
if (!empty($args['pid'])) {
$object = islandora_object_load($args['pid']);
if($object) {
foreach($nodes as &$node) {
if (!empty($node->webform)) {
$node->webform['islandora_object'] = $object;
}
}
}
else {
drupal_set_message(t('Islandora object :pid not found!', array(':pid' => $args['pid'])), 'warning', FALSE);
}
}
}
/**
* Implements hook_form_alter().
*/
function islandora_webform_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['#node']->type) && !empty($form['#node']->webform) && !empty($form['submitted']['islandora_object_pid'])) {
if (!empty($form['#node']->webform['islandora_object'])) {
$object = $form['#node']->webform['islandora_object'];
$form['submitted']['#prefix'] = '<h2 class="object-label">In regards to: <em>' . $object->label . '</em></h2>';
$form['submitted']['islandora_object_pid']['#default_value'] = $object->id;
if (!user_access('manage islandora webform')) {
$form['submitted']['islandora_object_pid']['#type'] = 'hidden';
$form['submitted']['islandora_object_pid']['#title_display'] = 'invisible';
}
if($form['#node']->webform['redirect_url'] == '<none>' && empty($form_state['rebuild'])) {
// Re-submitting webforms wants to redirect to the submittal view page.
// We want islandora object webforms to redirect back to the object page.
if(drupal_valid_path($form['#node']->webform['redirect_url'])) {
$redirect_url = $form['#node']->webform['redirect_url'];
}
else {
$destination = drupal_get_destination();
// See https://api.drupal.org/api/drupal/includes%21form.inc/function/drupal_redirect_form/7.x
$destination = drupal_parse_url($destination['destination']);
$redirect_url = array(array_shift($destination));
$redirect_url[] = $destination;
}
$form_state['webform_redirect'] = $redirect_url;
$form['#submit'][] = 'iw_webform_resubmit_redirect';
}
}
}
}
/**
* Direct webform resubmission back to the object page.
*
* @param $form
* @param $form_state
*/
function iw_webform_resubmit_redirect($form, &$form_state) {
if (!empty($form_state['webform_completed'])) {
unset($_GET['destination']);
$form_state['redirect'] = $form_state['webform_redirect'];
}
}
/**
* Implements hook_module_implements_alter().
*
* Let's make sure our form alters are called at the end of all.
* So we can be sure webform_ajax already has done it's own alters.
*/
function islandora_webform_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter') {
$group = $implementations['islandora_webform'];
unset($implementations['islandora_webform']);
$implementations['islandora_webform'] = $group;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* FORM_ID = webform_configure_form.
* Adds a custom message if the form is configured to be "islandora"
*/
function islandora_webform_form_webform_configure_form_alter(&$form, &$form_state) {
module_load_include('inc', 'islandora_webform', 'includes/utilities');
$node = $form_state['build_info']['args'][0];
if ((isset($form['webform_ajax_fieldset'])) && count(iw_node_islandora_webform($node))>0) {
$form['webform_ajax_fieldset']['#title']= t('Inline Islandora Webform');
$form['webform_ajax_fieldset']['webform_ajax']['#title'] = t('Inline AJAX mode');
$form['webform_ajax_fieldset']['webform_ajax']['#description'] = t('When set, all page changes (from pagebreak component), and Islandora webform submissions will be achieved inline using AJAX.');
}
$workflow_state_options = array(
'draft' => t('In Draft (core webform submission state)'),
'completed' => t('Completed (core webform submission state)'),
'ingested' => t('Ingested (islandora webform submission state)'),
'not-ingested' => t('Not ingested (islandora webform submission state)'),
);
// Per object limits.
$form['submission']['total_submit_limit']['total_submit_interval']['#options'][ISLANDORA_WEBFORM_SUBMIT_PER_OBJECT_LIMIT_ID] = t('per Islandora object');
$form['submission']['submit_limit']['submit_interval']['#options'][ISLANDORA_WEBFORM_SUBMIT_PER_OBJECT_LIMIT_ID] = t('per Islandora object');
// Add in webform_workflow states
if (module_exists('webform_workflow')) {
$available_states = webform_workflow_get_available_states($node);
if (count($available_states)) {
foreach ($available_states as $available_state) {
$workflow_state_options[$available_state->wsid] = $available_state->label;
}
}
}
$form['submission']['total_submit_limit']['total_workflow_states'] = array(
'#title' => t('Workflow states'),
'#type' => 'checkboxes',
'#description' => t('Prohibit new submissions while existing submissions <em>from any user</em> are present with any of the selected workflow states.'),
'#options' => $workflow_state_options,
);
if (!empty($node->webform['total_ws_limits'])) {
$form['submission']['total_submit_limit']['total_workflow_states']['#default_value'] = unserialize($node->webform['total_ws_limits']);
}
$form['submission']['submit_limit']['user_workflow_states'] = array(
'#title' => t('Workflow states'),
'#type' => 'checkboxes',
'#description' => t('Prohibit new submissions while existing submissions <em>from this user</em> are present with any of the selected workflow states.'),
'#options' => $workflow_state_options,
);
if (!empty($node->webform['user_ws_limits'])) {
$form['submission']['submit_limit']['user_workflow_states']['#default_value'] = unserialize($node->webform['user_ws_limits']);
}
$draft_access = array (
'#type' => 'checkbox',
'#title' => 'Allow draft editing/saving when new submission is denied.',
'#default_value' => !empty($node->webform['draft_access']) ? $node->webform['draft_access'] : 0,
'#description' => 'Allow users who are not permitted to create a new submission to edit and save a previously allowed submission that was saved as a draft.',
);
module_load_include('inc', 'islandora_webform', 'includes/utilities');
$form['advanced'] = _iw_array_insert_after('allow_draft', $form['advanced'], 'draft_access', $draft_access);
$form['#submit'][] = 'islandora_webform_webform_config_submit';
}
function islandora_webform_webform_config_submit(&$form, &$form_state) {
if(module_exists('webform_workflow')) {
$total_ws_limits = !empty($form_state['input']['total_workflow_states']) ? serialize(array_filter($form_state['input']['total_workflow_states'])) : NULL;
$user_ws_limits = !empty($form_state['input']['user_workflow_states']) ? serialize(array_filter($form_state['input']['user_workflow_states'])) : NULL;
$draft_access = !empty($form_state['input']['draft_access']) ? $form_state['input']['draft_access'] : 0;
$node = node_load($form['#node']->nid);
$node->webform['total_ws_limits'] = $total_ws_limits;
$node->webform['user_ws_limits'] = $user_ws_limits;
$node->webform['draft_access'] = $draft_access;
node_save($node);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* FORM_ID = webform_client_form.
* Removes pre_render functionality added by webform. Sorry, we need the element id for ajax
*/
function islandora_webform_form_webform_client_form_alter(&$form, &$form_state, $form_id) {
$form['#submit'][] = 'islandora_webform_client_form_submit';
$webform = $form['#node']->webform;
if (module_exists('webform_ajax') && $webform['webform_ajax'] != WEBFORM_AJAX_NO_AJAX) {
foreach (array('previous', 'next', 'submit', 'draft') as $button) {
if (isset($form['actions'][$button])) {
unset($form['actions'][$button]['#pre_render']);
}
}
}
}
/**
* Add submit handler to set a separate cookie for the pid
*
* @param $form
* @param $form_state
*/
function islandora_webform_client_form_submit(&$form, &$form_state) {
if(!empty($form_state['input']['submitted']['islandora_object_pid'])) {
$tracking_mode = webform_variable_get('webform_tracking_mode');
if ($tracking_mode === 'cookie' || $tracking_mode === 'strict') {
$cookie_name = 'webform-' . $form['#node']->nid . '-' . $form_state['input']['submitted']['islandora_object_pid'];
$time = REQUEST_TIME;
$params = session_get_cookie_params();
setcookie($cookie_name . '[' . $time . ']', $time, $time + $form['#node']->webform['submit_interval'] + 86400, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
}
}
}
/**
* Implements hook_theme_registry_alter().
*/
function islandora_webform_theme_registry_alter(&$theme_registry) {
// Defined path to the current module.
$module_path = drupal_get_path('module', 'islandora_webform');
// Find all .tpl.php files in this module's folder recursively.
$template_file_objects = drupal_find_theme_templates($theme_registry, '.tpl.php', $module_path);
// Iterate through all found template file objects.
foreach ($template_file_objects as $key => $template_file_object) {
// If the template has not already been overridden by a theme.
if (!isset($theme_registry[$key]['theme path']) || !preg_match('#/themes/#', $theme_registry[$key]['theme path'])) {
// Alter the theme path and template elements.
$theme_registry[$key]['theme path'] = $module_path;
$theme_registry[$key] = array_merge($theme_registry[$key], $template_file_object);
$theme_registry[$key]['type'] = 'module';
}
}
}
/**
* Implements hook_node_access().
*
* Do not allow users to view an islandora webform without the object's PID
* being predefined.
* Exception if user has 'manage islandora webform' permissions.
*/
function islandora_webform_node_access($node, $op, $account) {
if ($op == 'view' && isset($node->type) && $node->type == 'webform' && isset($node->webform['components']) && !user_access('manage islandora webform', $account)) {
$is_islandora_webform = FALSE;
foreach ($node->webform['components'] as $component) {
if ($component['form_key'] == 'islandora_object_pid') {
$is_islandora_webform = TRUE;
break;
}
}
if ($is_islandora_webform) {
// Check to make sure we're not viewing submissions, or editing, or whatever.
// For some reason, menu_get_item is crashing in this context. So we do it by
// brute force.
$path = explode('/', $_GET['q']);
if (count($path) == 2 || end($path) == 'view') {
$access = NODE_ACCESS_IGNORE;
$args = drupal_get_query_parameters();
if (!isset($args['pid']) && empty($node->webform['islandora_object'])) {
drupal_set_message(t('No fedora object PID was defined'), 'error');
$access = NODE_ACCESS_DENY;
}
}
}
}
$access = isset($access) ? $access : NODE_ACCESS_IGNORE;
return $access;
}
/**
* Implements hook_islandora_view_object_alter().
*
* Adds webform links to islandora object pages
*/
function islandora_webform_islandora_view_object_alter(&$object, &$rendered) {
if (variable_get('islandora_webform_link_object_view_alter', 1)) {
module_load_include('inc', 'islandora_webform', 'includes/utilities');
$webforms = islandora_webform_object_get_webforms($object);
if (!empty($webforms)) {
$rendered += array(
'webform_links' => theme('islandora_webform_links', array(
'webform_data' => $webforms,
'islandora_object' => $object,
)
),
);
}
}
}
/**
* Implements hook_islandora_object_purged().
*
* Clean up dangling references to deleted PIDs
*/
function islandora_webform_islandora_object_purged($pid) {
// NOTE: We are going to leave webform submissions referring to this PID
// undisturbed.
// If the deleted object is a content model, find any webform configurations
// which have selected that content model as a filter and reset to none.
db_update('islandora_webform_webforms')
->fields(array('cmodel_filter' => ''))
->condition('cmodel_filter', $pid);
// If the deleted object is a collection, find any webform configurations
// which have selected that collection as a filter and reset to none.
db_update('islandora_webform_webforms')
->fields(array('collection_filter' => ''))
->condition('collection_filter', $pid);
}
/**
* Utility functions
*/
/**
* Return user access = TRUE if user access for any listed permission is true.
*
* @param array|string $permissions
* array of permission strings
*
* @return bool
* TRUE if user access granted
* FALSE if user access denied
*/
function _islandora_webform_user_access_or($permissions) {
global $user;
if ($user->uid == 1) {
return TRUE;
}
$permissions = (array) $permissions;
foreach ($permissions as $permission) {
if (user_access($permission)) {
return TRUE;
}
}
return FALSE;
}
/**
* Access callback for viewing the submissions tab for a given object.
*
* We hide the tab if there are no submissions. This makes more sense than
* showing the tab for all objects that could in theory have a submission.
*
* @param AbstractObject $object
* The fedora object
*
* @return bool
* TRUE = has access
*/
function islandora_webform_object_results_access(AbstractObject $object) {
// Check if user has permissions.
if (user_access('access all webform results')) {
// If so, then if the count of webform submissions associated with this object
// is greater than zero, then grant access.
$query = db_select('webform_submissions', 's');
$query->join('webform_component', 'c', "c.nid = s.nid and c.form_key = 'islandora_object_pid'");
$query->join('webform_submitted_data', 'd', "d.sid = s.sid and d.cid = c.cid");
$query->condition('d.data', $object->id);
if ($query->countQuery()->execute()->fetchField() > 0) {
return TRUE;
}
else {
$arg = arg(3);
if (!empty($arg) && $arg == 'submissions') {
drupal_goto('islandora/object/' . $object->id);
}
else {
return FALSE;
}
}
}
return FALSE;
}
/**
* Fetch an array of metadata objects for a given fedora object.
*
* @param string $pid
* The PID for the object
* @param string $fields
* Comma-separated list of SOLR indexed fields.
* @param null $filter
* Filter string, optional.
*
* @return array
* The
*/
function iw_get_solr_data_array($pid, $fields = '', $filter = NULL) {
if (!empty($fields)) {
$qp = new IslandoraSolrQueryProcessor();
$query = 'PID:"' . $pid . '"';
$qp->buildQuery($query);
$qp->solrParams['fl'] = 'PID, ' . $fields;
if ($filter) {
$qp->solrParams['fq'] = $filter;
}
$qp->executeQuery();
if (!empty($qp->islandoraSolrResult['response']['numFound'])) {
return $qp->islandoraSolrResult['response']['objects'];
}
}
return array();
}
/**
* Implements hook_webform_submission_render_alter().
*/
function islandora_webform_webform_submission_render_alter(&$renderable) {
if (!empty($renderable['islandora_object_pid']['#value'])) {
$renderable['islandora_object_pid']['#value'] = l($renderable['islandora_object_pid']['#value'], '/islandora/object/' . $renderable['islandora_object_pid']['#value']);
$renderable['islandora_object_pid']['#theme'] = 'webform_display_markup';
}
}
/**
* Implements hook_islandora_webform_object_submission_query_count_alter().
*/
function islandora_webform_islandora_webform_object_submission_query_count_alter(&$query, $params) {
// // Don't count submissions that have been ingested.
// if($params['interval'] == ISLANDORA_WEBFORM_SUBMIT_ATATIME_LIMIT_ID) {
// $query->leftJoin('islandora_webform_ingestions', 'iwi', 'iwi.sid = s.sid AND iwi.nid = s.nid');
// $query->isNull('iwi.sid');
// }
}
/**
* Implements hook_iw_results_object_submissions_page_element_alter().
*
* If webform workflow module is enabled, we want to do two things:
* - Provide a select list to filter the results by workflow state
* - Display the workflow states in the table.
*
* @param $element
*/
function islandora_webform_iw_results_object_submissions_page_element_alter(&$element) {
if(module_exists('webform_workflow')) {
// Add the workflow state selector form.
$element['selector'] = drupal_get_form('iw_submissions_states_form', $element['#object']);
$element['selector']['#weight'] = -1;
// Find the index key ($hkey) for the 'Operations' column in the table.
$map = array_keys($element['#submissions']);
foreach ($element['table']['#header'] as $hkey => $header) {
if (isset($header['data']) && $header['data'] == 'Operations') {
break;
}
}
// Splice in the 'State' label in the table header.
$element['table']['#header'] =
array_merge(array_slice($element['table']['#header'], 0, $hkey, true),
array($hkey => array('data' => t('State'))),
array_slice($element['table']['#header'], $hkey, count($element['table']['#header'])-$hkey, true));
// Splice in the workflow states for each row in the table.
foreach($map as $row_index => $sid) {
$workflow_state = webform_workflow_state_load_by_submission($element['#submissions'][$sid]);
$element['table']['#rows'][$row_index] =
array_merge(array_slice($element['table']['#rows'][$row_index], 0, $hkey, TRUE),
array($hkey => '<span class="webform-workflow-state-label webform-workflow-state-color-' . $workflow_state->color . '">' . $workflow_state->label . '</span>'),
array_slice($element['table']['#rows'][$row_index], $hkey, count($element['table']['#rows'][$row_index]) - $hkey, TRUE));
}
}
}
/**
* Webform workflow state filter form for object submissions page.
*
* @param $form
* @param $form_state
* @param $object
*
* @return array
* a Drupal form render array.
*/
function iw_submissions_states_form($form, &$form_state, $object) {
if (module_load_include('inc', 'islandora_webform', 'includes/utilities')) {
$webforms = islandora_webform_object_get_webforms($object);
if (!empty($webforms)) {
$map = array();
$params = drupal_get_query_parameters();
$map['wfid'] = !empty($params['wfid']) ? $params['wfid'] : NULL;
$map['wsid'] = !empty($params['wsid']) ? $params['wsid'] : NULL;
$default_value = implode('-', $map);
$options = array('' => t('All'));
foreach ($webforms as $webform) {
$node = node_load($webform->entity_id);
$available_states = webform_workflow_get_available_states($node);
foreach ($available_states as $available_state) {
if(count($webforms) > 1) {
$label = $node->title . ' - ' . $available_state->label;
}
else {
$label = $available_state->label;
}
$options[$webform->entity_id . '-' . $available_state->wsid] = $label;
}
}
$form['wws_select'] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => $default_value,
'#attributes' => array(
// We want to auto-submit.
'onChange' => 'this.form.submit();',
),
);
$form['#submit'][] = 'iw_submission_states_form_submit';
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Apply'),
// the submit button is necessary, but it can be hidden
'#attributes' => array(
'style' => array('display: none;'),
),
);
return $form;
}
}
return array();
}
/**
* Submit handler for iw_submissions_states_form().
*
* Detects if filtering by webform workflow state, and if so, loads the
* page with that filter applied.
*
* @param $form
* @param $form_state
*/
function iw_submission_states_form_submit(&$form, &$form_state) {
$destination = drupal_get_destination();
$url = drupal_parse_url($destination['destination']);
$filter = explode('-', $form_state['input']['wws_select']);
if(count($filter) > 1) {
$url['query']['wfid'] = array_shift($filter);
$url['query']['wsid'] = array_shift($filter);
}
else {
unset($url['query']['wfid']);
unset($url['query']['wsid']);
}
drupal_goto($url['path'], $url);
}
function _islandora_webform_schema_alter(&$schema) {
// Define our additional fields.
if (isset($schema['webform'])) {
// Declare columns for workflow state submission permissions. We add these
// whether or not the webform_workflow module is present, since it may be
$schema['webform']['fields']['user_ws_limits'] = array(
'description' => 'Per user webform workflow state limits as a serialized array',
'type' => 'text',
);
$schema['webform']['fields']['total_ws_limits'] = array(
'description' => 'Total webform workflow state limits as a serialized array',
'type' => 'text',
);
$schema['webform']['fields']['draft_access'] = array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
'size' => 'tiny',
'description' => 'Can a draft submission be edited and submitted when new submissions are not permitted? (0 = No, 1 = Yes)',
);
}
}
/**
* Implements hook_modules_enabled().
*/
function islandora_webform_modules_enabled($modules) {
if(in_array('webform_workflow', $modules)) {
$schema = array('webform' => array());
_islandora_webform_schema_alter($schema);
foreach ($schema as $table => $table_def) {
foreach ($table_def['fields'] as $field => $spec) {
if(!db_field_exists($table, $field)) {
db_add_field($table, $field, $spec);
}
}
}
}
}
/**
* Implements hook_modules_disabled().
*/
function islandora_webform_modules_disabled($modules) {
if(in_array('webform_workflow', $modules)) {
$schema = array('webform' => array());
_islandora_webform_schema_alter($schema);
foreach ($schema as $table => $table_def) {
foreach ($table_def['fields'] as $field => $spec) {
db_drop_field($table, $field);
}
}
}
}
/**
* Implements hook_webform_submission_access().
*
* Webform_workflow denies submission edit access when there is no workflow
* state - which is not helpful.
* Here, we grant access if the submission state is in draft, deny if not.
*/
function islandora_webform_webform_submission_access($node, $submission, $op = 'view', $account) {
if (!$account) {
global $user;
$account = $user;
}
// Only handle cases where the workflow state is empty. Otherwise, we let
// the webform workflow state permissions handle it.
if (module_exists('webform_workflow') && $op == 'edit' && empty($submission->_webform_workflow_state->wsid) && $account->uid == $submission->uid) {
return $submission->is_draft && user_access('edit own webform submissions');
}
}
/**
* Implements template_preprocess_webform_submission.
*
* The display of submission components gets all munged together into a single
* long line of html text. Here we wrap each component in a div so that they
* split into separate lines.
*/
function islandora_webform_preprocess_webform_submission(&$vars) {
foreach(element_children($vars['renderable']) as $child_key) {
$vars['renderable'][$child_key]['#prefix'] = '<div class="component">';
$vars['renderable'][$child_key]['#suffix'] = '</div>';
}
}