forked from q0rban/drupal-workflow
-
Notifications
You must be signed in to change notification settings - Fork 4
/
workflow.admin.inc
1170 lines (1034 loc) · 37.9 KB
/
workflow.admin.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
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// $Id: workflow.admin.inc,v 1.7.2.3.2.6 2010/09/02 21:13:18 q0rban Exp $
/**
* @file
* Administrative pages for configuring workflows.
*/
/**
* Form builder. Create the form for adding a workflow.
*/
function workflow_add_form(&$form_state) {
$form = array();
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Workflow Label'),
'#required' => TRUE,
'#maxlength' => '254',
'#width' => 120,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Machine Name'),
'#required' => TRUE,
'#description' => t('Enter the machine name for this workflow. Must be unique and use only alphanumeric characters and underscores.'),
'#maxlength' => '254',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add Workflow'),
);
return $form;
}
/**
* Validate the workflow add form.
*
* @see workflow_add_form()
*/
function workflow_add_form_validate($form, &$form_state) {
$form_state['values']['name'] = strtolower($form_state['values']['name']);
$values = $form_state['values'];
// Make sure workflow name is not a duplicate.
if (db_result(db_query("SELECT COUNT(*) FROM {workflows} WHERE label = '%s'", $values['label']))) {
form_set_error('label', t('A workflow with the name %name already exists. Please enter another name for your new workflow.',
array('%name' => $values['label'])));
}
// Make sure machine name contains only alnum chars and underscores.
if (preg_match("/[^[:alnum:]_]/", $values['name'])) {
form_set_error('name', t("The machine name must consist of alphanumeric or underscore characters only."));
}
// Make sure workflow name is not a duplicate.
if (db_result(db_query("SELECT COUNT(*) FROM {workflows} WHERE name = '%s'", $values['name']))) {
form_set_error('name', t('A workflow with the machine name %name already exists. Please enter another name for your new workflow.',
array('%name' => $values['name'])));
}
// Make sure the workflow name isn't associated with disabled states.
if (db_result(db_query("SELECT COUNT(*) FROM {workflow_states} WHERE workflow_name = '%s'", $values['name']))) {
form_set_error('name', t('It appears this machine name has previously disabled states. Please enter another name for your new workflow.',
array('%name' => $values['name'])));
}
}
/**
* Submit handler for the workflow add form.
*
* @see workflow_add_form()
*/
function workflow_add_form_submit($form, &$form_state) {
$values = $form_state['values'];
$workflow['label'] = $values['label'];
$workflow['name'] = strtolower($values['name']);
if ($workflow = workflow_save($workflow)) {
watchdog('workflow', 'Created workflow %label', array('%label' => $workflow->label));
drupal_set_message(t('The workflow %label was created. You should now add states to your workflow.', array('%label' => $workflow->label)), 'status');
$form_state['redirect'] = 'admin/build/workflow/edit/'. $workflow->name .'/states';
}
else {
watchdog('workflow', 'There was an error creating workflow %label', array('%label' => $values['label']), WATCHDOG_WARNING);
drupal_set_message(t('There was a problem saving this workflow. Please contact your site administrator.'), 'warning');
}
workflow_invalidate_cache();
}
/**
* Form for confirmation of workflow deletion.
*
* @param $name
* The machine name of the workflow to delete.
* @return
* Confirmation form array.
*/
function workflow_delete_form(&$form_state, $name) {
$form['name'] = array(
'#type' => 'value',
'#value' => $name,
);
return confirm_form(
$form,
t('Are you sure you want to delete %title? All nodes that have a workflow state associated with this workflow will have those workflow states removed.', array('%title' => workflow_get_label($name))),
!empty($_GET['destination']) ? $_GET['destination'] : 'admin/build/workflow',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Submit handler for workflow deletion form.
*
* @see workflow_delete_form()
*/
function workflow_delete_form_submit($form, &$form_state) {
if ($form_state['values']['confirm'] == 1) {
$name = $form_state['values']['name'];
$label = workflow_get_label($name);
workflow_deletewf($name);
watchdog('workflow', 'Deleted workflow %label with all its states', array('%label' => $label));
drupal_set_message(t('The workflow %label with all its states was deleted.', array('%label' => $label)));
$form_state['redirect'] = 'admin/build/workflow';
}
}
/**
* Form for confirmation of workflow revert.
*
* @param $name
* The machine name of the workflow to revert.
* @return
* Confirmation form array.
*/
function workflow_revert_form(&$form_state, $name) {
$form['name'] = array(
'#type' => 'value',
'#value' => $name,
);
return confirm_form(
$form,
t('Are you sure you want to revert %title? Configuration will switch to the one provided by code.', array('%title' => workflow_get_label($name))),
!empty($_GET['destination']) ? $_GET['destination'] : 'admin/build/workflow',
t('This action cannot be undone.'),
t('Revert'),
t('Cancel')
);
}
/**
* Submit handler for workflow revert form.
*
* @see workflow_revert_form()
*/
function workflow_revert_form_submit($form, &$form_state) {
if ($form_state['values']['confirm'] == 1) {
$name = $form_state['values']['name'];
$label = workflow_get_label($name);
workflow_export_crud_delete($name);
watchdog('workflow', 'Reverted workflow %label with all its states', array('%label' => $label));
drupal_set_message(t('The workflow %label with all its states was reverted.', array('%label' => $label)));
$form_state['redirect'] = 'admin/build/workflow';
}
}
/**
* View workflow permissions by role
*
* @param $workflow
* The workflow object.
*/
function workflow_permissions($workflow) {
$map = array();
$roles = workflow_get_roles();
$transitions = $workflow->transitions;
$states = $workflow->states;
if (is_array($transitions)) {
foreach ($transitions as $trn) {
if (is_array($trn->roles)) {
foreach ($trn->roles as $role) {
$from = isset($states[$trn->state_name]) ? $states[$trn->state_name]->label : "";
$to = isset($states[$trn->target_state_name]) ? $states[$trn->target_state_name]->label : "";
$map[$role]['transitions'][] = array($from, WORKFLOW_ARROW, $to);
$map[$role]['name'] = ucwords($roles[$role]);
}
}
}
}
return theme('workflow_permissions', $map);
}
/**
* Theme the workflow permissions view.
*/
function theme_workflow_permissions($map) {
$header = array(t('From'), '', t('To'));
$rows = array();
foreach ($map as $role => $value) {
$rows[] = array(
'data' => array('<h3>'. t("%role may do these transitions:", array('%role' => $value['name'])) .'</h3>'),
'colspan' => 3,
);
$rows = array_merge($rows, $value['transitions']);
}
return theme('table', $header, $rows);
}
/**
* Menu callback. Edit a workflow's properties.
*
* @param $workflow
* The workflow object.
* @return
* HTML form and permissions table.
*/
function workflow_edit_form(&$form_state, $workflow) {
$form['#workflow'] = $workflow;
$form['wid'] = array(
'#type' => 'value',
'#value' => $workflow->wid,
);
$form['name'] = array(
'#type' => 'value',
'#value' => $workflow->name,
);
$form['basic'] = array(
'#type' => 'fieldset',
'#title' => t('Workflow information'),
);
$form['basic']['label'] = array(
'#type' => 'textfield',
'#default_value' => $workflow->label,
'#title' => t('Workflow Label'),
'#size' => '16',
'#maxlength' => '254',
);
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Comment for Workflow Log'),
'#tree' => TRUE,
);
$form['options']['comment_log_node'] = array(
'#type' => 'checkbox',
'#title' => t('Show a comment field in the workflow section of the editing form.'),
'#default_value' => $workflow->options['comment_log_node'],
'#description' => t("On the node editing form, a Comment form can be shown so that the person making the state change can record reasons for doing so. The comment is then included in the node's workflow history."),
);
$form['options']['comment_log_tab'] = array(
'#type' => 'checkbox',
'#title' => t('Show a comment field in the workflow section of the workflow tab form.'),
'#default_value' => $workflow->options['comment_log_tab'],
'#description' => t("On the workflow tab, a Comment form can be shown so that the person making the state change can record reasons for doing so. The comment is then included in the node's workflow history."),
);
$form['tab'] = array(
'#type' => 'fieldset',
'#title' => t('Workflow tab permissions'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['tab']['tab_roles'] = array(
'#type' => 'checkboxes',
'#options' => workflow_get_roles(),
'#default_value' => explode(',', $workflow->tab_roles),
'#description' => t('Select any roles that should have access to the workflow tab on nodes that have a workflow.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Validate the workflow editing form.
*
* @see workflow_edit_form()
*/
function workflow_edit_form_validate($form, &$form_state) {
$machine_name = $form_state['values']['name'];
// Make sure workflow name is not a duplicate.
if (!empty($form_state['values']['label'])) {
$label = check_plain(t($form_state['values']['label']));
foreach (workflow_load_all() as $name => $workflow) {
if ($name != $machine_name && check_plain(t($label)) == $workflow->label) {
form_set_error('label', t('A workflow with the label %label already exists. Please enter another label for this workflow.',
array('%label' => $label)));
break;
}
}
}
else {
// No workflow name was provided.
form_set_error('label', t('Please provide a nonblank label for this workflow.'));
}
}
/**
* Submit handler for the workflow editing form.
*
* @see workflow_edit_form()
*/
function workflow_edit_form_submit($form, &$form_state) {
$values = $form_state['values'];
$workflow = $form['#workflow'];
if (is_array($values)) {
foreach ($values as $key => $val) {
if ($key == 'wid') continue;
if (isset($workflow->$key)) {
if ($key == "tab_roles") {
$worfklow->roles = $val;
$val = implode(',', array_values($val));
}
$workflow->$key = $val;
}
}
}
// Saving object from code
if ($workflow->export_type == EXPORT_IN_CODE) {
workflow_save_transitions($workflow);
}
workflow_save($workflow);
drupal_set_message(t('The workflow was updated.'));
}
/**
* Form builder. Build the grid of transitions for defining a workflow.
*
* @param $workflow
* The workflow object.
*/
function workflow_transition_grid_form(&$form_state, $workflow) {
$form = array();
$form['#workflow'] = $workflow;
$transitions_matrix = workflow_transitions_to_matrix($workflow->transitions);
$form['transitions'] = array(
'#type' => 'fieldset',
'#title' => t('Transitions'),
'#collapsible' => TRUE,
'#tree' => TRUE,
);
$roles = workflow_get_roles();
$states = $workflow->states;
if (empty($states)) {
$form = array(
'#type' => 'markup',
'#value' => t('There are no states defined for this workflow.'),
);
return $form;
}
foreach ($states as $state) {
foreach ($states as $nested_state) {
if (workflow_is_system_state($nested_state->label)) {
// Don't allow transition TO (creation).
continue;
}
if ($nested_state->name != $state->name) {
// Need to generate checkboxes for transition from $state to $nested_state.
$from = $state;
$to = $nested_state;
foreach ($roles as $rid => $role_name) {
$default_value = 0;
if (isset($transitions_matrix[$from->name][$to->name])) {
$default_value = in_array($rid, $transitions_matrix[$from->name][$to->name]);
}
$form['transitions'][$from->name][$to->name][$rid] = array(
'#type' => 'checkbox',
'#title' => check_plain($role_name),
'#default_value' => $default_value,
);
}
}
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['permissions'] = array(
'#type' => 'fieldset',
'#title' => t('Permissions Summary'),
'#collapsible' => TRUE,
);
$form['permissions']['summary'] = array(
'#value' => workflow_permissions($workflow),
);
return $form;
}
/**
* Convert an array of transition objects to a 2-dimensional array, more appropriate for quick lookups.
*/
function workflow_transitions_to_matrix($transitions) {
$matrix = array();
if (is_array($transitions)) {
foreach ($transitions as $trn) {
$matrix[$trn->state_name][$trn->target_state_name] = $trn->roles;
}
}
return $matrix;
}
/**
* Validation handler for the transition_grid_form.
*
* @see workflow_transition_grid_form().
*/
function workflow_transition_grid_form_validate($form, &$form_state) {
$workflow = $form['#workflow'];
// Make sure 'author' is checked for (creation) -> [something].
$creation_state = _workflow_creation_state($workflow->name);
if (isset($form_state['values']['transitions'][$creation_state]) && is_array($form_state['values']['transitions'][$creation_state])) {
foreach ($form_state['values']['transitions'][$creation_state] as $to => $roles) {
if ($roles['author']) {
$author_has_permission = TRUE;
break;
}
}
}
$state_count = count($workflow->states);
if (empty($author_has_permission) && $state_count > 1) {
form_set_error('transitions', t('Please give the author permission to go from %creation to at least one state!',
array('%creation' => '(creation)')));
}
}
/**
* Submit handler for the workflow editing form.
*
* @see workflow_transition_grid_form().
*/
function workflow_transition_grid_form_submit($form, &$form_state) {
$workflow = $form['#workflow'];
if ($workflow->export_type == WORKFLOW_IN_CODE) {
//-- don't save stale transitions
unset($workflow->transitions);
workflow_save($workflow);
}
$values = $form_state['values']['transitions'];
foreach ($values as $from_state => $transitions) {
// Convert the roles array to the expected format.
foreach ($transitions as $to_state => $roles) {
$transitions[$to_state] = array_keys(array_filter($roles));
}
workflow_transitions_write($from_state, $transitions);
}
drupal_set_message(t('The workflow transitions were updated.'));
}
/**
* Theme the workflow transitions form.
*
* @see workflow_edit_form()
*/
function theme_workflow_transition_grid_form($form) {
$machine_name = $form['#workflow']->name;
$states = $form['#workflow']->states;
drupal_set_title(t('Edit Workflow %label Transitions', array('%label' => workflow_get_label($machine_name))));
$output = '';
if ($states) {
$roles = workflow_get_roles();
$header = array(array('data' => t('From / To ') . ' ' . WORKFLOW_ARROW));
$rows = array();
foreach ($states as $state) {
// Don't allow transition TO (creation).
if (!workflow_is_system_state($state->label)) {
$header[] = array('data' => $state->label);
}
$row = array(array('data' => $state->label));
foreach ($states as $nested_state) {
if (workflow_is_system_state($nested_state->label)) {
// Don't allow transition TO (creation).
continue;
}
if ($nested_state->name != $state->name) {
// Need to render checkboxes for transition from $state to $nested_state.
$from = $state;
$to = $nested_state;
$cell = '';
foreach ($roles as $rid => $role_name) {
$cell .= drupal_render($form['transitions'][$from->name][$to->name][$rid]);
}
$row[] = array('data' => $cell);
}
else {
$row[] = array('data' => '');
}
}
$rows[] = $row;
}
$form['transitions']['#children'] .= theme('table', $header, $rows);
}
else {
$output = t('There are no states defined for this workflow.');
}
// Pop the submit button in the transitions fieldset.
$form['transitions']['#children'] .= drupal_render($form['submit']);
$output .= drupal_render($form);
return $output;
}
/**
* Menu callback. Create the main workflow page, which gives an overview
* of workflows and workflow states.
*/
function workflow_overview() {
$row = array();
foreach (workflow_load_all(TRUE) as $workflow_name => $workflow) {
$links = array(
'workflow_overview_actions' => array(
'title' => t('Actions'),
'href' => "admin/build/trigger/workflow/$workflow_name"),
'workflow_overview_edit' => array(
'title' => t('Edit'),
'title_default' => t('Override'),
'href' => "admin/build/workflow/edit/$workflow_name"),
'workflow_overview_delete' => array(
'title' => t('Delete'),
// You can not delete a default workflow
'title_default' => '',
'title_overridden' => t('Revert'),
'href' => "admin/build/workflow/delete/$workflow_name",
// You can not delete a default workflow
'href_default' => "admin/build/workflow/edit/$workflow_name",
'href_overridden' => "admin/build/workflow/revert/$workflow_name"),
);
// Allow modules to insert their own workflow operations.
$links = array_merge($links, module_invoke_all('workflow_operations', 'workflow', $workflow->name));
// Remove the Actions link if Trigger is not enabled or the Workflow has
// less than 2 states.
if (!module_exists('trigger') || count($workflow->states) < 2) {
unset($links['workflow_overview_actions']);
}
$row[] = array($workflow->label,
$workflow->type,
theme('links', workflow_overview_parse_links($links, $workflow)));
}
if ($row) {
$output = theme('table', array(t('Workflow'), t('State'), t('Operations')), $row);
$output .= drupal_get_form('workflow_types_form');
}
else {
$output = '<p>' . t('No workflows have been added. Would you like to <a href="@link">add a workflow</a>?', array('@link' => url('admin/build/workflow/add'))) . '</p>';
}
return $output;
}
/**
* Pick appropriate link title and link href, depending on the state of the workflow (Normal, Default or Overridden).
*/
function workflow_overview_parse_links($links, $workflow) {
$suffix = "";
$suffix = strtolower($workflow->type);
//-- Unfortunately, we can not just deduce $suffix from $workflow->type, because "type" gets translated and it does not work in non-English languages.
if (($workflow->export_type & EXPORT_IN_CODE) && ($workflow->export_type & EXPORT_IN_DATABASE)) {
$suffix = 'overridden';
}
elseif ($workflow->export_type == EXPORT_IN_CODE) {
$suffix = 'default';
}
elseif ($workflow->export_type == EXPORT_IN_DATABASE) {
$suffix = 'normal';
}
if (is_array($links)) {
foreach ($links as &$link) {
if (isset($link["title_" . $suffix])) {
$link["title"] = $link["title_" . $suffix];
}
if (isset($link["href_" . $suffix])) {
$link["href"] = $link["href_" . $suffix];
}
}
}
return $links;
}
/**
* Page callback for the Workflow States tab.
*/
function workflow_states($workflow) {
return drupal_get_form('workflow_states_form_'. $workflow->name, $workflow);
}
/**
* Form to re-order and create states for a specific workflow.
*
* @param $workflow
* The workflow object.
*/
function workflow_states_form(&$form_state, $workflow) {
$form = array();
$form['#workflow'] = $workflow;
$form['#states'] = $states = array_filter($workflow->states, 'workflow_state_is_active');
$form['states']['#tree'] = TRUE;
foreach ($states as $state_name => $state) {
$form['states'][$state_name] = _workflow_state_edit_form($form_state, $state);
}
// Now add an empty state form.
$form['states'][] = _workflow_state_create_form($form_state, $workflow->name);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
// Since we're using custom form_ids (@see workflow_forms()) we need to
// manually specify some FAPI stuff.
$form['buttons']['submit']['#submit'] = array('workflow_states_form_submit');
$form['#validate'] = array('workflow_states_form_validate');
$form['#theme'] = array('workflow_states_form_'. $workflow->name, 'workflow_states_form');
return $form;
}
/**
* Builds the fields for a single record on the drag-and-drop overview form.
*
* This internal function should not be called outside the module, unless you're
* feeling particularly cheeky.
*
* @param $state
* The existing state object.
* @return
* Form array.
*/
function _workflow_state_edit_form(&$form_state, $state) {
$form['#state'] = $state;
$form['workflow_name'] = array(
'#type' => 'value',
'#value' => $state->workflow_name,
);
$form['sid'] = array(
'#type' => 'value',
'#value' => $state->sid,
);
$form['name'] = array(
'#type' => 'value',
'#value' => $state->name,
);
$form['system_state'] = array(
'#type' => 'value',
'#value' => workflow_is_system_state($state->label),
);
// Only allow editing of the label on non-system states.
if ($form['system_state']['#value']) {
$form['label'] = array(
'#type' => 'item',
'#value' => $state->label,
);
$form['weight'] = array(
'#type' => 'value',
'#value' => $state->weight,
);
}
else {
$form['label'] = array(
'#type' => 'textfield',
'#default_value' => empty($state->label) ? '' : $state->label,
'#size' => '16',
'#maxlength' => '254',
'#element_validate' => array('workflow_state_validate_label'),
);
$form['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => empty($state->weight) ? 0 : $state->weight,
);
}
return $form;
}
/**
* Builds the state creation form fields for the drag-and-drop overview form.
*
* This internal function should not be called outside the module, unless you're
* feeling particularly cheeky.
*
* @param $workflow_name
* The machine name of the workflow.
* @return
* Form array.
*/
function _workflow_state_create_form(&$form_state, $workflow_name) {
$form = array();
$form['workflow_name'] = array(
'#type' => 'value',
'#value' => $workflow_name,
);
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('State label'),
'#size' => '16',
'#maxlength' => '254',
'#description' => t('Enter the name for a state in your workflow. For example, if you were doing a meal workflow it may include states like <em>shop</em>, <em>prepare</em>, <em>eat</em>, and <em>clean up</em>.'),
'#element_validate' => array('workflow_state_validate_label'),
);
// Only show the machine name field if this is a new state.
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Machine name'),
'#size' => '16',
'#maxlength' => '254',
'#field_prefix' => $workflow_name . '_',
'#description' => t('Enter the machine name for this state. Must be unique and use only alphanumeric characters and underscores.'),
'#element_validate' => array('workflow_state_validate_machine_name'),
);
$form['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => 0,
'#description' => t('In listings, the heavier states will sink and the lighter states will be positioned nearer the top.'),
);
return $form;
}
/**
* Theme the drag-and-drop overview form.
*
* Arranges records in a table, and adds the css and js for draggable sorting.
*
* @see workflow_states_form()
*/
function theme_workflow_states_form($form) {
$rows = $cols = array();
foreach (element_children($form['states']) as $state_name) {
$state_form = &$form['states'][$state_name];
$cols = array();
// This is needed to prevent admin theme breaking tabledrag on forms due to its
// special handling of description field.
$cols[] = array('data' => '', 'style' => 'width:30px');
// Render the hidden 'sid' field and the title of the record into the same
// column of the row, keeping in mind that 'sid' may be empty.
$sid = empty($state_form['sid']) ? '' : drupal_render($state_form['sid']);
$cols[] = $sid . drupal_render($state_form['label']);
// If the weight field isn't disabled, add an identifying CSS class to it,
// as it's the one the tabledrag.js will be controlling.
if (!$state_form['weight']['#disabled']) {
$state_form['weight']['#attributes']['class'] = 'workflow-state-weight';
}
$cols[] = drupal_render($state_form['weight']);
// The machine name field.
$name_field = '<span class="machine-name">'. check_plain($state_form['name']['#value']) .'</span>';
$cols[] = $name_field . drupal_render($state_form['name']);
// Operations column, which is empty if this isn't an existing state.
$cols[] = empty($form['#states'][$state_name]) ? '' : _workflow_state_links($form['#states'][$state_name]);
// Add the new row to our collection of rows, and give it the 'draggable'
// class if the row isn't a system state.
$rows[] = array(
'data' => $cols,
'class' => $state_form['system_state']['#value'] ? '' : 'draggable',
);
}
// Render a list of header titles, and our array of rows, into a table. Even
// we've already rendered all of our records, we always call drupal_render()
// on the form itself after we're done, so hidden security fields and other
// elements (like buttons) will appear properly at the bottom of the form.
$id = $form['#id'] .'-table';
$header = array('', t('State Label'), t('Weight'), t('Machine Name'), t('Operations'));
$form['states']['#children'] .= theme('table', $header, $rows, array('id' => $id));
$form['states']['#children'] .= drupal_render($form['buttons']['submit']);
$output = drupal_render($form);
// Now that we've built our output, tell Drupal to add the tabledrag.js library.
// We'll pass in the ID of the table, the behavior we want it to use, and the
// class that appears on each 'weight' form element it should be controlling.
drupal_add_tabledrag($id, 'order', 'sibling', 'workflow-state-weight');
return $output;
}
/**
* Build the edit and delete links for a single record.
*
* @see workflow_states_form()
*/
function _workflow_state_links($state) {
$links = array();
if (!workflow_is_system_state($state->label)) {
$links['workflow_overview_delete_state'] = array(
'title' => t('Disable'),
'href' => "admin/build/workflow/state/disable/{$state->workflow_name}/{$state->name}",
'query' => drupal_get_destination(),
);
}
// Allow modules to insert state operations.
$links = array_merge($links, module_invoke_all('workflow_operations', 'state', $workflow->name, $state->name));
return theme('links', $links);
}
/**
* Validate the state label.
*
* @see workflow_states_form()
*/
function workflow_state_validate_label($element, &$form_state) {
$workflow_name = $form_state['values']['workflow_name'];
$wf_states = workflow_get_states($workflow_name);
$label = $element['#value'];
// Unset this workflow state, if it exists. We detect the machine name from
// #parents... it's the second to last item in the array.
$key = count($element['#parents']) - 2;
$machine_name = $element['#parents'][$key];
unset($wf_states[$machine_name]);
// Make sure label is not already in use in this workflow.
foreach ($wf_states as $wf_state) {
if ($wf_state->label == check_plain(t($label))) {
form_error($element, t('A state with the label %label already exists in this workflow. Please enter another label for your new state.', array('%label' => $label)));
break;
}
}
}
/**
* Validate the state machine name.
*
* @see workflow_states_form()
*/
function workflow_state_validate_machine_name($element, &$form_state) {
$machine_name = strtolower($element['#value']);
// Make sure machine name contains only alnum chars and underscores.
if (preg_match("/[^[:alnum:]_]/", $machine_name)) {
form_error($element, t("The machine name must consist of alphanumeric or underscore characters only."));
}
$states = $form_state['values']['states'];
//-- If there are no other states, checking for duplicate is not needed
if (!is_array($states) || sizeof($states)<1) return;
$workflow_name = $states[0]['workflow_name'];
// Ensure that this machine name is unique.
if (workflow_state_machine_name_exists($workflow_name . '_' . $machine_name)) {
form_error($element, t('A state with the machine name: %name in the workflow: %workflow already exists.',
array('%name' => $machine_name, '%workflow' => $workflow_name)));
}
}
/**
* Test to see if a state already exists with the requested machine name.
*
* @param $name
* The requested state machine name.
* @return
* Boolean whether or not the machine name is already in use.
*/
function workflow_state_machine_name_exists($name) {
static $names = array();
if (empty($names)) {
foreach (workflow_load_all() as $workflow) {
foreach ($workflow->states as $state) {
$names[] = $state->name;
}
}
}
return in_array($name, $names);
}
/**
* Validate the states form.
*
* @see workflow_states_form()
*/
function workflow_states_form_validate($form, &$form_state) {
foreach ($form_state['values']['states'] as $state_name => $state) {
// No need to validate an empty form.
if (!$state['system_state'] && (empty($state['name']) XOR empty($state['label']))) {
$form_key = 'states]['. $state_name .'][';
// Make sure the fields aren't empty.
if (empty($state['label'])) {
form_set_error($form_key .'label', t('You must specify a label for this state.'));
}
if (empty($state['name'])) {
form_set_error($form_key .'name', t('You must specify a machine name for this state.'));
}
}
}
}
/**
* Submit handler for states form.
*
* @see workflow_states_form()
*/
function workflow_states_form_submit($form, &$form_state) {
$states = $form_state['values']['states'];
$workflow = $form['#workflow'];
//-- don't save states from the code, though!
unset($workflow->states);
//-- Let's make sure workflow is already in the database!
if ($workflow->export_type == WORKFLOW_IN_CODE) {
workflow_save($workflow);
}
if (is_array($states)) {
foreach ($states as $state_name => $state) {
// Ensure the state has all necessary fields and isn't a system state before saving.
if (!empty($state['name']) && !empty($state['label']) && empty($state['system_state'])) {
$state['name'] = strtolower($state['name']);
//-- Prefix new state names with workflow-name to ensure they are unique-enough.
//-- Caution: this check is specifically "isset()" since states in code would also
//-- return TRUE on empty() but their "sid" is already set: to -1.
//-- We do not want to add prefix to states from code, only to new states!
if (!isset($state['sid'])) {
$state['name'] = $workflow->name . '_' . $state['name'];
}
elseif ($state['sid'] == -1) {
//unset -1 as sid for code-provided state so workflow_state_save does not go nuts
unset($state['sid']);
unset($states[$state_name]['sid']);
}
$state = workflow_state_save((object) $state);
if (empty($states[$state_name]['sid'])) {
watchdog('workflow', 'Created workflow state %label', array('%label' => $state->label));
drupal_set_message(t('The workflow state %label was created.', array('%label' => $state->label)));
}
}
}
}
workflow_clear_cache();
}
/**
* Form for confirmation of deleting a workflow state.
*
* @param $workflow
* The workflow object.
* @param $state_name
* The machine name of the state to delete.
* @return