workflow_notify.module 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * @file
  4. * Notify roles for Workflow state transitions.
  5. */
  6. /**
  7. * Implements hook_permission().
  8. */
  9. function workflow_notify_permission() {
  10. return array(
  11. 'workflow notify' => array(
  12. 'title' => t('Receive workflow notifications'),
  13. 'description' => t('The user may be notified of a workflow state change.'),
  14. ),
  15. );
  16. }
  17. /**
  18. * Implements hook_help().
  19. */
  20. function workflow_notify_help($path, $arg) {
  21. switch ($path) {
  22. case 'admin/config/workflow/workflow/notify/%':
  23. return '<p>' . t('The selected roles will be notified of each state change selected.') . '</p>';
  24. }
  25. }
  26. /**
  27. * Implements hook_menu().
  28. */
  29. function workflow_notify_menu() {
  30. $items = array();
  31. $items['admin/config/workflow/workflow/notify/%workflow'] = array(
  32. 'title' => 'Notifications',
  33. 'access arguments' => array('administer workflow'),
  34. 'page callback' => 'drupal_get_form',
  35. 'page arguments' => array('workflow_notify_settings_form', 5),
  36. 'type' => MENU_CALLBACK,
  37. 'file' => 'workflow_notify.admin.inc',
  38. );
  39. return $items;
  40. }
  41. /**
  42. * Implements hook_theme().
  43. */
  44. function workflow_notify_theme() {
  45. return array(
  46. 'workflow_notify_settings_form' => array(
  47. 'render element' => 'form',
  48. 'file' => 'workflow_notify.admin.inc',
  49. ),
  50. );
  51. }
  52. /**
  53. * Implements hook_hook_info().
  54. */
  55. function workflow_notify_hook_info() {
  56. $hooks['workflow_notify'] = array(
  57. 'group' => 'workflow',
  58. );
  59. return $hooks;
  60. }
  61. /**
  62. * Implements hook_workflow_operations().
  63. * Add an action link to this module.
  64. */
  65. function workflow_notify_workflow_operations($op, $workflow = NULL) {
  66. switch ($op) {
  67. case 'workflow':
  68. $wid = $workflow->getWorkflowId();
  69. $actions = array(
  70. 'workflow_notify_settings' => array(
  71. 'title' => t('Notifications'),
  72. 'href' => "admin/config/workflow/workflow/notify/$wid",
  73. 'attributes' => array('alt' => t('Notify users about state changes.')),
  74. ),
  75. );
  76. $actions['workflow_notify_settings']['attributes']['title'] =
  77. $actions['workflow_notify_settings']['attributes']['alt'];
  78. return $actions;
  79. }
  80. }
  81. /**
  82. * Implements hook_workflow().
  83. * Calls drupal_mail via _workflow_notify() for Workflow Field;
  84. *
  85. * @param $op
  86. * The current workflow operation: 'transition permitted', 'transition pre', or 'transition post'.
  87. * @param $old_state
  88. * The state ID of the current state.
  89. * @param $new_state
  90. * The state ID of the new state.
  91. * @param $node
  92. * The node whose workflow state is changing.
  93. * @param $force
  94. * The caller indicated that the transition should be forced. (bool).
  95. * This is only available on the "pre" and "post" calls.
  96. */
  97. function workflow_notify_workflow($op, $old_state, $new_state, $entity, $force, $entity_type = '', $field_name = '', $transition = NULL, $user = NULL) {
  98. global $user;
  99. switch ($op) {
  100. // React to a transition after it's done.
  101. case 'transition post':
  102. // This is only called for Workfow Node. For Workflow Field, see workflow_entity_entity_update().
  103. _workflow_notify($new_state, $entity, $entity_type, $field_name, $transition, $user);
  104. return;
  105. }
  106. }
  107. /**
  108. * Implements hook_entity_update().
  109. * Calls drupal_mail via _workflow_notify() for Workflow Field;
  110. *
  111. * @param $entity
  112. * @param $entity_type
  113. */
  114. function workflow_notify_entity_update($entity, $entity_type) {
  115. if (isset($entity->workflow_transitions)) {
  116. $new_state = workflow_node_current_state($entity, $entity_type, $field_name);
  117. foreach ($entity->workflow_transitions as $field_name => &$transition) {
  118. $new_state = workflow_node_current_state($entity, $entity_type, $field_name);
  119. _workflow_notify($new_state, $entity, $entity_type, $field_name, $transition, $user = NULL);
  120. }
  121. }
  122. return;
  123. }
  124. /**
  125. * Implements hook_mail();
  126. * Build email messages.
  127. */
  128. function workflow_notify_mail($key, &$message, $params) {
  129. switch ($key) {
  130. case 'workflow_notify':
  131. $filter = $params['filter'];
  132. $message['send'] = TRUE;
  133. $message['subject'] = filter_xss(token_replace($params['context']['subject'], $params['data'], $params));
  134. $message['body'][] = check_markup(token_replace($params['context']['body'], $params['data'], $params), $filter);
  135. watchdog('workflow_notify',
  136. '<ul><li>Subject: @subject</li><li>Body: @body</li><li>To: @to</li><li>From: @from</li></ul>', array(
  137. '@subject' => $message['subject'],
  138. '@body' => implode('<br />', $message['body']),
  139. '@to' => $message['to'],
  140. '@from' => $message['from'],
  141. ),
  142. WATCHDOG_INFO, l(t('view'), 'node/' . $params['data']['node']->nid));
  143. return;
  144. }
  145. }
  146. /**
  147. * Calls drupal_mail() to notify users.
  148. *
  149. * @param $new_state
  150. * @param $entity
  151. * @param string $entity_type
  152. * @param string $field_name
  153. * @param null $transition
  154. * @param null $user
  155. * @throws EntityMalformedException
  156. */
  157. function _workflow_notify($new_state, $entity, $entity_type = '', $field_name = '', $transition = NULL, $user = NULL) {
  158. global $user;
  159. // See if this is a state that we notify for.
  160. $notify = variable_get('workflow_notify_roles', array());
  161. if (!isset($notify[$new_state])) {
  162. return;
  163. }
  164. // The name of the person making the change.
  165. $changer = format_username($user);
  166. $changer_mail = $user->mail;
  167. // Okay, we are notifying someone of this change.
  168. // So let's get the workflow object.
  169. list($entity_id, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
  170. /* @var $workflow Workflow */
  171. $workflow = workflow_get_workflows_by_type($entity_bundle, $entity_type);
  172. $wid = $workflow->getWorkflowId();
  173. // And all the states.
  174. $states = $workflow->getStates(TRUE);
  175. // Get the specific roles to notify.
  176. $notify = $notify[$new_state];
  177. // See if we want to notify the author too?
  178. $notify_author = in_array(-1, $notify);
  179. unset($notify[-1]);
  180. // There could be no roles set.
  181. if ($notify) {
  182. // Get all the user accounts in those roles.
  183. $query = "SELECT DISTINCT ur.uid "
  184. . "FROM {users_roles} ur "
  185. . "INNER JOIN {users} u ON u.uid = ur.uid "
  186. . "WHERE ur.rid IN (:rids) "
  187. . "AND u.status = 1 ";
  188. $users = db_query($query, array(':rids' => $notify))->fetchCol();
  189. } else {
  190. $users = array();
  191. }
  192. // Some entities (like Term) have no Author.
  193. if ($notify_author && isset($entity->uid)) {
  194. $users[] = $entity->uid;
  195. }
  196. // Load all the user entities, making sure there are no duplicates.
  197. $accounts = entity_load('user', array_unique($users, SORT_NUMERIC));
  198. // Call all modules that want to limit the list.
  199. $args = array(
  200. 'users' => $accounts,
  201. 'node' => $entity,
  202. 'entity' => $entity, // Preparing for entities, keeping backward compatibility.
  203. 'entity_type' => $entity_type,
  204. 'state' => $new_state,
  205. 'roles' => $notify,
  206. 'workflow' => $workflow,
  207. );
  208. foreach (module_implements('workflow_notify') as $module) {
  209. $function = $module . '_workflow_notify';
  210. $function('users', $args);
  211. }
  212. // Retrieve the remaining list without duplicates.
  213. $accounts = $args['users'];
  214. $addr_list = array();
  215. // Just quit if there are no users.
  216. if (empty($accounts)) {
  217. watchdog('workflow_notify', 'No recipients - email skipped.', array(),
  218. WATCHDOG_DEBUG, l(t('view'), 'node/' . $entity_id)); // @todo: other entity types.
  219. return;
  220. }
  221. foreach ($accounts as $uid => $account) {
  222. $addr_list[] = format_username($account) . '<' . $account->mail . '>';
  223. }
  224. $params = array(
  225. 'clear' => TRUE,
  226. 'sanitize' => FALSE,
  227. 'data' => array(
  228. 'node' => $entity,
  229. 'entity' => $entity, // Preparing for entities, keeping backward compatibility.
  230. 'entity_type' => $entity_type,
  231. 'user' => $user,
  232. ),
  233. 'filter' => variable_get('workflow_notify_filter_format_' . $wid, 'filtered_html'),
  234. );
  235. // Build the subject and body of the mail.
  236. // Token replacement occurs in hook_mail().
  237. // @TODO: Currently no translation occurs.
  238. $params['context']['subject'] = variable_get("workflow_notify_subject_$new_state",
  239. '[node:title] is now "[workflow:workflow-current-state-name]"');
  240. $params['context']['body'] = variable_get("workflow_notify_body_$new_state",
  241. '<a href="[node:url:absolute]">[node:title]</a> is now "@state".');
  242. switch (variable_get('workflow_notify_from_address_' . $wid, 'site')) {
  243. case 'site':
  244. $from = variable_get('site_mail', ini_get('sendmail_from'));
  245. break;
  246. case 'changer':
  247. $from = $user->mail;
  248. break;
  249. }
  250. // Send the email.
  251. drupal_mail('workflow_notify',
  252. 'workflow_notify',
  253. implode(', ', $addr_list),
  254. language_default(),
  255. $params,
  256. $from);
  257. return;
  258. }