workflow_notify.pages.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * @file
  4. * Admin UI to Notify roles for Workflow state transitions.
  5. */
  6. /**
  7. * Settings form.
  8. */
  9. function workflow_notify_settings_form($form, &$form_state, Workflow $workflow) {
  10. // If we don't have a workflow that goes with this, return to the admin pg.
  11. if (!is_object($workflow)) {
  12. drupal_set_message(t('Improper worklow ID provided.'), 'error');
  13. drupal_goto(WORKFLOW_ADMIN_UI_PATH);
  14. }
  15. // @todo: Let's get a better page title.
  16. drupal_set_title(t('@name Notifications', array('@name' => $workflow->label())));
  17. // Let's add some breadcrumbs.
  18. // workflow_admin_ui_breadcrumbs($workflow);
  19. $form['#tree'] = TRUE;
  20. $form['#wid'] = $wid = $workflow->getWorkflowId();
  21. $form['#workflow'] = $workflow;
  22. $noyes = array(t('No'), t('Yes'));
  23. // Get all the states for this workflow, except the (creation) state.
  24. $states = $workflow->getStates();
  25. $form['#states'] = $states;
  26. if (!$states) {
  27. $form['#columns'] = array();
  28. $form['error'] = array(
  29. '#type' => 'markup',
  30. '#value' => t('There are no states defined for this workflow.'),
  31. );
  32. return $form;
  33. }
  34. // A list of role names keyed by role ID, including the 'author' role.
  35. $roles = workflow_get_roles('participate in workflow');
  36. // The module_invoke_all function does not allow passing by reference.
  37. $args = array('roles' => $roles);
  38. foreach (module_implements('workflow_notify') as $module) {
  39. $function = $module . '_workflow_notify';
  40. // Call the $op='roles' hooks so the list may be modified.
  41. $function('roles', $args);
  42. }
  43. // Get the modified list.
  44. $roles = $args['roles'];
  45. // Setting for "from" address.
  46. $form['from_address'] = array(
  47. '#title' => t('Set "From" address to'),
  48. '#type' => 'radios',
  49. '#options' => array(
  50. 'site' => t('Site email address'),
  51. 'changer' => t('Current user'),
  52. ),
  53. '#default_value' => variable_get("workflow_notify_from_address_$wid", 'site'),
  54. '#attributes' => array('class' => array('container-inline')),
  55. '#description' => t('Determines whether to use the site address or the address of the person
  56. causing the state change.'),
  57. );
  58. // Get the list of input formats.
  59. $formats = array();
  60. foreach (filter_formats() as $fid => $filter) {
  61. $formats[$fid] = $filter->name;
  62. }
  63. $form['filter'] = array(
  64. '#title' => t('Filter format to use on message'),
  65. '#type' => 'radios',
  66. '#options' => $formats,
  67. '#default_value' => variable_get("workflow_notify_filter_format_$wid", 'filtered_html'),
  68. '#attributes' => array('class' => array('container-inline')),
  69. '#description' => t('The message body will be processed using this filter format
  70. in order to ensure security.'),
  71. );
  72. $form['tokens'] = array(
  73. '#theme' => 'token_tree_link',
  74. '#token_types' => array('node', 'workflow'),
  75. );
  76. // Column names for theme function.
  77. $columns = array(
  78. 'state' => t('State'),
  79. 'roles' => t('Roles to notify'),
  80. );
  81. $args = array('columns' => $columns);
  82. // The module_invoke_all function does not allow passing by reference.
  83. foreach (module_implements('workflow_notify') as $module) {
  84. $function = $module . '_workflow_notify';
  85. $function('columns', $args);
  86. }
  87. $form['#columns'] = $args['columns'];
  88. // We always want subject and body last.
  89. $form['#columns']['subject'] = t('Subject');
  90. $form['#columns']['body'] = t('Body');
  91. $notify = variable_get('workflow_notify_roles', array());
  92. foreach ($states as $sid => $state) {
  93. // Allow modules to insert state operations.
  94. $state_links = module_invoke_all('workflow_operations', 'state', $workflow, $state);
  95. $form['states'][$sid] = array(
  96. 'state' => array(
  97. '#type' => 'markup',
  98. '#markup' => filter_xss($state->state),
  99. ),
  100. 'roles' => array(
  101. '#type' => 'checkboxes',
  102. '#options' => $roles,
  103. '#default_value' => (isset($notify[$sid]) ? $notify[$sid] : array()),
  104. '#attributes' => array('class' => array('state-roles')),
  105. ),
  106. 'subject' => array(
  107. '#type' => 'textarea',
  108. '#rows' => 5,
  109. '#default_value' => variable_get('workflow_notify_subject_' . $sid,
  110. '[node:title] is now "[node:field_workflow_state:new-state:label]"'),
  111. '#attributes' => array('class' => array('subject')),
  112. ),
  113. 'body' => array(
  114. '#type' => 'textarea',
  115. '#rows' => 5,
  116. '#default_value' => variable_get('workflow_notify_body_' . $sid,
  117. '<a href="[node:url:absolute]">[node:title]</a> is now "[node:field_workflow_state:new-state:label]".'),
  118. '#attributes' => array('class' => array('body')),
  119. ),
  120. );
  121. }
  122. $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  123. return $form;
  124. }
  125. /**
  126. * Theme the settings form.
  127. */
  128. function theme_workflow_notify_settings_form($variables) {
  129. $form = $variables['form'];
  130. $output = '';
  131. $table_id = 'workflow-notify-settings';
  132. $output .= drupal_render($form['from_address']);
  133. $output .= drupal_render($form['filter']);
  134. $output .= drupal_render($form['tokens']);
  135. $table = array(
  136. 'rows' => array(),
  137. 'header' => array(), // To be filled in.
  138. 'attributes' => array('id' => $table_id, 'style' => 'width: 100%; margin-top: 1em;'),
  139. );
  140. $columns = $form['#columns'];
  141. $colspan = count($columns);
  142. foreach ($columns as $field => $title) {
  143. $table['header'][] = $title;
  144. }
  145. // Iterate over each element in our $form['states'] array
  146. foreach (element_children($form['states']) as $id) {
  147. // We are now ready to add each element of our $form data to the rows
  148. // array, so that they end up as individual table cells when rendered
  149. // in the final table.
  150. $row = array();
  151. foreach ($columns as $field => $title) {
  152. $row[] = array(
  153. 'data' => drupal_render($form['states'][$id][$field]),
  154. 'class' => array(drupal_html_class($field)),
  155. );
  156. }
  157. // Put the data row into the table.
  158. $table['rows'][] = array('data' => $row);
  159. }
  160. $output .= theme('table', $table);
  161. $output .= drupal_render($form['explain']);
  162. // And then render any remaining form elements (such as our submit button)
  163. $output .= drupal_render_children($form);
  164. return $output;
  165. }
  166. function workflow_notify_settings_form_submit($form, &$form_state) {
  167. $wid = $form['#wid'];
  168. $workflow = $form['#workflow'];
  169. $roles = variable_get('workflow_notify_roles', array());
  170. $form_state['redirect'] = WORKFLOW_ADMIN_UI_PATH;
  171. variable_set("workflow_notify_from_address_$wid" , $form_state['values']['from_address']);
  172. variable_set("workflow_notify_filter_format_$wid" , $form_state['values']['filter']);
  173. foreach ($form_state['values']['states'] as $sid => $values) {
  174. $selected = array_filter($values['roles']);
  175. // Are there any roles selected?
  176. if ($selected) {
  177. $roles[$sid] = $selected;
  178. }
  179. else {
  180. // No, so make sure this state is gone.
  181. unset($roles[$sid]);
  182. }
  183. variable_set("workflow_notify_subject_$sid", $values['subject']);
  184. variable_set("workflow_notify_body_$sid", $values['body']);
  185. }
  186. variable_set('workflow_notify_roles', $roles);
  187. drupal_set_message(t('The notification settings have been saved.'));
  188. }