workflow_notify.module 10 KB

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