workflow_admin_ui.page.workflow.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @file
  4. * Provides an Admin UI page for the Workflow Properties.
  5. */
  6. /**
  7. * Menu callback. Edit a workflow's properties.
  8. *
  9. * @param Workflow $worflow
  10. * The workflow object.
  11. *
  12. * @return array
  13. * HTML form and permissions table.
  14. */
  15. function workflow_admin_ui_edit_form($form, &$form_state, $workflow = NULL, $op) {
  16. $noyes = array(t('No'), t('Yes'));
  17. $fieldset_options = array(0 => t('No fieldset'), 1 => t('Collapsible fieldset'), 2 => t('Collapsed fieldset'));
  18. $form = array();
  19. $form['workflow'] = array(
  20. '#type' => 'value',
  21. '#value' => $workflow,
  22. );
  23. $form['submit'] = array(
  24. '#type' => 'submit',
  25. '#value' => t('Save'),
  26. '#submit' => array('workflow_admin_ui_edit_form_submit'),
  27. '#validate' => array('workflow_admin_ui_edit_form_validate'),
  28. '#weight' => 15,
  29. );
  30. $form['label'] = array(
  31. '#title' => t('Label'),
  32. '#type' => 'textfield',
  33. '#default_value' => isset($workflow->label) ? $workflow->label : $workflow->name,
  34. '#description' => t('The human-readable name of this content type.'),
  35. '#required' => TRUE,
  36. '#maxlength' => '254',
  37. '#size' => 30,
  38. );
  39. $form['name'] = array(
  40. '#type' => 'machine_name',
  41. '#default_value' => $workflow->getName(),
  42. '#maxlength' => '254', // $todo D8 : '#maxlength' => 32,
  43. '#required' => TRUE,
  44. '#disabled' => !empty($workflow->locked),
  45. '#machine_name' => array(
  46. 'exists' => 'workflow_load',
  47. 'source' => array('label'),
  48. ),
  49. '#description' => t('A unique machine-readable name for this content type. It must only contain lowercase letters, numbers, and underscores.'),
  50. );
  51. // All other settings are only for workflow_node.
  52. if (!module_exists('workflownode')) {
  53. return $form; // <==== exit !
  54. }
  55. $form['basic'] = array(
  56. '#type' => 'fieldset',
  57. '#title' => t('Workflow form settings'),
  58. );
  59. $form['basic']['fieldset'] = array(
  60. '#type' => 'select',
  61. '#options' => $fieldset_options,
  62. '#title' => t('Show the form in a fieldset?'),
  63. '#default_value' => isset($workflow->options['fieldset']) ? $workflow->options['fieldset'] : 0,
  64. '#description' => t("The Widget can be wrapped in a visible fieldset. You'd
  65. do this when you use the widget on a Node Edit page."
  66. ),
  67. );
  68. $form['basic']['options'] = array(
  69. '#type' => 'select',
  70. '#title' => t('How to show the available states'),
  71. '#required' => FALSE,
  72. '#default_value' => isset($workflow->options['options']) ? $workflow->options['options'] : 'radios',
  73. // '#multiple' => TRUE / FALSE,
  74. '#options' => array(
  75. // These options are taken from options.module
  76. 'select' => 'Select list',
  77. 'radios' => 'Radio buttons',
  78. // This option does not work properly on Comment Add form.
  79. 'buttons' => 'Action buttons',
  80. ),
  81. '#description' => t("The Widget shows all available states. Decide which
  82. is the best way to show them."
  83. ),
  84. );
  85. $form['basic']['name_as_title'] = array(
  86. '#type' => 'radios',
  87. '#options' => $noyes,
  88. '#attributes' => array('class' => array('container-inline')),
  89. '#title' => t('Use the workflow name as the title of the workflow form?'),
  90. '#default_value' => isset($workflow->options['name_as_title']) ? $workflow->options['name_as_title'] : 0,
  91. '#description' => t('The workflow section of the editing form is in its own
  92. fieldset. Checking the box will add the workflow name as the title of workflow section of the editing form.'),
  93. );
  94. $form['schedule'] = array(
  95. '#type' => 'fieldset',
  96. '#title' => t('Scheduling for Workflow'),
  97. );
  98. $form['schedule']['schedule'] = array(
  99. '#type' => 'radios',
  100. '#options' => $noyes,
  101. '#attributes' => array('class' => array('container-inline')),
  102. '#title' => t('Allow scheduling of workflow transitions?'),
  103. '#default_value' => isset($workflow->options['schedule']) ? $workflow->options['schedule'] : 1,
  104. '#description' => t('Workflow transitions may be scheduled to a moment in the future.
  105. Soon after the desired moment, the transition is executed by Cron.'),
  106. );
  107. $form['schedule']['schedule_timezone'] = array(
  108. '#type' => 'radios',
  109. '#options' => $noyes,
  110. '#attributes' => array('class' => array('container-inline')),
  111. '#title' => t('Show a timezone when scheduling a transition?'),
  112. '#default_value' => isset($workflow->options['schedule_timezone']) ? $workflow->options['schedule_timezone'] : 1,
  113. );
  114. $form['comment'] = array(
  115. '#type' => 'fieldset',
  116. '#title' => t('Comment for Workflow Log'),
  117. );
  118. $form['comment']['comment_log_node'] = array(
  119. '#type' => 'select',
  120. '#required' => FALSE,
  121. '#options' => array(
  122. // Use 0/1/2 to stay compatible with previous checkbox.
  123. 0 => t('hidden'),
  124. 1 => t('optional'),
  125. 2 => t('required'),
  126. ),
  127. '#attributes' => array('class' => array('container-inline')),
  128. '#title' => t('Show a comment field in the workflow section of the editing form?'),
  129. '#default_value' => isset($workflow->options['comment_log_node']) ? $workflow->options['comment_log_node'] : 0,
  130. '#description' => t(
  131. 'On the node editing form, a Comment form can be shown so that the person
  132. making the state change can record reasons for doing so. The comment is
  133. then included in the node\'s workflow history.'
  134. ),
  135. );
  136. $form['comment']['comment_log_tab'] = array(
  137. '#type' => 'select',
  138. '#required' => FALSE,
  139. '#options' => array(
  140. // Use 0/1/2 to stay compatible with previous checkbox.
  141. 0 => t('hidden'),
  142. 1 => t('optional'),
  143. 2 => t('required'),
  144. ),
  145. '#attributes' => array('class' => array('container-inline')),
  146. '#title' => t('Show a comment field in the workflow section of the workflow tab form?'),
  147. '#default_value' => isset($workflow->options['comment_log_tab']) ? $workflow->options['comment_log_tab'] : 0,
  148. '#description' => t(
  149. 'On the workflow tab, a Comment form can be shown so that the person
  150. making the state change can record reasons for doing so. The comment
  151. is then included in the node\'s workflow history.'
  152. ),
  153. );
  154. $form['watchdog'] = array(
  155. '#type' => 'fieldset',
  156. '#title' => t('Watchdog Logging for Workflow'),
  157. );
  158. $form['watchdog']['watchdog_log'] = array(
  159. '#type' => 'radios',
  160. '#options' => $noyes,
  161. '#attributes' => array('class' => array('container-inline')),
  162. '#title' => t('Log informational watchdog messages when a transition is executed (state of a node is changed)?'),
  163. '#default_value' => isset($workflow->options['watchdog_log']) ? $workflow->options['watchdog_log'] : 0,
  164. '#description' => t('Optionally log transition state changes to watchdog.'),
  165. );
  166. $form['tab'] = array(
  167. '#type' => 'fieldset',
  168. '#title' => t('Workflow tab permissions'),
  169. '#collapsible' => TRUE,
  170. '#collapsed' => FALSE,
  171. );
  172. $form['tab']['history_tab_show'] = array(
  173. '#type' => 'checkbox',
  174. '#title' => t('Use the workflow history, and show it on a separate tab.'),
  175. '#required' => FALSE,
  176. '#default_value' => isset($workflow->options['history_tab_show']) ? $workflow->options['history_tab_show'] : 1,
  177. '#description' => t("Every state change is recorded in table
  178. {workflow_node_history}. If checked and user has proper permission, a
  179. tab 'Workflow' is shown on the entity view page, which gives access to
  180. the History of the workflow."),
  181. );
  182. $form['tab']['tab_roles'] = array(
  183. '#type' => 'checkboxes',
  184. '#options' => workflow_get_roles(),
  185. '#default_value' => array_keys($workflow->tab_roles),
  186. '#description' => t('Select any roles that should have access to the workflow tab on nodes that have a workflow.'),
  187. );
  188. return $form;
  189. }
  190. /**
  191. * Validate the workflow edit/add form.
  192. */
  193. function workflow_admin_ui_edit_form_validate($form, &$form_state) {
  194. $workflow = $form_state['values']['workflow'];
  195. $name = $form_state['values']['name'];
  196. // Make sure workflow name is not numeric.
  197. if (ctype_digit($name)) {
  198. form_set_error('name', t('Please choose a non-numeric name for your workflow.',
  199. array('%name' => $name)));
  200. }
  201. // Make sure workflow name is not a duplicate.
  202. foreach (workflow_load_multiple() as $stored_workflow) {
  203. if ($name == $stored_workflow->name && $workflow->wid != $stored_workflow->wid) {
  204. form_set_error('name', t('A workflow with the name %name already exists. Please enter another name for this workflow.',
  205. array('%name' => $name)));
  206. break;
  207. }
  208. }
  209. }
  210. /**
  211. * Submit handler for the workflow editing form.
  212. *
  213. * @see workflow_edit_form()
  214. * @todo: this is only valid for Node API, not for Field API.
  215. * Field API has 'Field settings'.
  216. */
  217. function workflow_admin_ui_edit_form_submit($form, &$form_state) {
  218. $workflow = $form_state['values']['workflow'];
  219. $insert = !empty($workflow->is_new);
  220. $workflow->name = trim($form_state['values']['name']);
  221. $workflow->label = trim($form_state['values']['label']);
  222. // For workflow_field, all is in the field settings.
  223. // All other settings are only for workflow_node.
  224. if (module_exists('workflownode')) {
  225. $workflow->tab_roles = array_filter($form_state['values']['tab_roles']);
  226. $workflow->options = array(
  227. 'name_as_title' => $form_state['values']['name_as_title'],
  228. 'fieldset' => $form_state['values']['fieldset'],
  229. 'options' => $form_state['values']['options'],
  230. 'schedule' => $form_state['values']['schedule'],
  231. 'schedule_timezone' => $form_state['values']['schedule_timezone'],
  232. 'comment_log_node' => $form_state['values']['comment_log_node'],
  233. 'comment_log_tab' => $form_state['values']['comment_log_tab'],
  234. 'watchdog_log' => $form_state['values']['watchdog_log'],
  235. 'history_tab_show' => $form_state['values']['history_tab_show'],
  236. );
  237. }
  238. $workflow->save();
  239. if ($insert) {
  240. $args = array(
  241. '%name' => $workflow->getName(),
  242. '@url' => url(WORKFLOW_ADMIN_UI_PATH . "/edit/$workflow->wid"),
  243. );
  244. watchdog('workflow', 'Created workflow %name', $args);
  245. drupal_set_message(t('The workflow %name was created. Please maintain the states and transitions.', $args), 'status');
  246. }
  247. else {
  248. drupal_set_message(t('The workflow was updated.'));
  249. }
  250. // This redirect is needed, when changing the workflow name, with name in URL.
  251. // Also for cloning a workflow.
  252. $form_state['redirect'] = WORKFLOW_ADMIN_UI_PATH . "/manage/$workflow->wid";
  253. }