workflow_notify.admin.inc 7.4 KB

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