workflow.tokens.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * @file
  4. * Tokens hooks for Workflow module.
  5. */
  6. /**
  7. * Implements hook_tokens().
  8. */
  9. function workflow_tokens($type, $tokens, array $data = array(), array $options = array()) {
  10. $replacements = array();
  11. $sanitize = !empty($options['sanitize']);
  12. $langcode = !empty($options['language']->language) ? $options['language']->language : LANGUAGE_NONE;
  13. $date = REQUEST_TIME;
  14. if (($type == 'node' || $type == 'workflow') && !empty($data['node']) && !empty($data['node']->nid)) {
  15. $node = $data['node'];
  16. // Get a list of all possible states for returning state names.
  17. $states = workflow_get_workflow_states_all();
  18. if ($workflow = workflow_get_workflow_type_map_by_type($node->type)) {
  19. // @TODO: If we ever move Workflow to allow M:M content types to workflows, this will need updating.
  20. if ($workflow = workflow_get_workflows_by_wid($workflow->wid)) {
  21. $wid = $workflow->wid;
  22. $last_history = workflow_get_recent_node_history($node->nid);
  23. if (isset($node->workflow) && !isset($node->workflow_stamp)) {
  24. // The node is being submitted but the form data has not been saved to the database yet,
  25. // so we set the token values from the workflow form fields.
  26. $sid = $node->workflow;
  27. $old_sid = isset($last_history->old_sid) ? $last_history->old_sid : workflow_get_creation_state_by_wid($workflow->wid);
  28. $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
  29. $uid = $node->uid;
  30. $account = user_load($node->uid);
  31. $mail = ($node->uid && isset($node->user_mail)) ? $node->user_mail : '';
  32. $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
  33. }
  34. else {
  35. if (empty($last_history)) {
  36. // If the node has no workflow history,
  37. // the node is being inserted and will soon be transitioned to the first valid state.
  38. // We find this state using the same logic as workflow_nodeapi().
  39. $choices = workflow_field_choices($node);
  40. // @TODO: Some users, like anonymous, may not be allowed
  41. // to cause transitions, so there will be no choices.
  42. $keys = array_keys($choices);
  43. $sid = array_shift($keys);
  44. $old_sid = workflow_get_creation_state_by_wid($workflow->wid);
  45. $sid = workflow_get_workflow_states_by_sid($sid)->sid;
  46. $old_sid = workflow_get_workflow_states_by_sid($old_sid)->sid;
  47. $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
  48. $uid = $node->uid;
  49. $account = user_load($node->uid);
  50. $mail = ($node->uid && isset($node->user_mail)) ? $node->user_mail : '';
  51. $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
  52. }
  53. else {
  54. // Sometimes there is no workflow set (edit?).
  55. if (!isset($node->workflow)) {
  56. $node->workflow = $last_history->sid;
  57. }
  58. // Is a transition in progress?
  59. if ($node->workflow != $last_history->sid) {
  60. $sid = $node->workflow;
  61. $old_sid = $last_history->sid;
  62. $date = $node->workflow_stamp;
  63. $uid = $node->uid;
  64. $account = user_load($node->uid);
  65. }
  66. else {
  67. // Not a transition.
  68. $sid = $last_history->sid;
  69. $old_sid = $last_history->old_sid;
  70. $date = $last_history->stamp;
  71. $uid = $last_history->uid;
  72. $account = user_load($last_history->uid);
  73. }
  74. // Default to the most recent transition data in the workflow history table.
  75. $user_name = $account->uid ? $account->name : variable_get('anonymous', 'Anonymous');
  76. $mail = $account->uid ? $account->mail : '';
  77. $comment = $last_history->comment;
  78. }
  79. }
  80. foreach ($tokens as $name => $original) {
  81. switch ($name) {
  82. case 'workflow-name':
  83. $replacements[$original] = $sanitize ? check_plain($workflow->name) : $workflow->name;
  84. break;
  85. case 'workflow-current-state-name':
  86. $replacements[$original] = $sanitize ? check_plain($states[$sid]) : $states[$sid];
  87. break;
  88. case 'workflow-old-state-name':
  89. $replacements[$original] = $sanitize ? check_plain($states[$old_sid]) : $states[$old_sid];
  90. break;
  91. case 'workflow-current-state-updating-user-name':
  92. $name = format_username($account);
  93. $replacements[$original] = $sanitize ? check_plain($name) : $name;
  94. break;
  95. case 'workflow-current-state-updating-user-link':
  96. $replacements[$original] = theme('username', array('account' => $account));
  97. break;
  98. case 'workflow-current-state-updating-user-uid':
  99. // User IDs are integers only and do not need sanitization.
  100. $replacements[$original] = $uid;
  101. break;
  102. case 'workflow-current-state-updating-user-mail':
  103. $replacements[$original] = $sanitize ? check_plain($account->mail) : $account->mail;
  104. break;
  105. case 'workflow-current-state-updating-user-mailto':
  106. $replacements[$original] = '<a href="mailto:' . check_plain($account->mail) . '">' . check_plain($user_name) . '</a>';
  107. break;
  108. case 'workflow-current-state-log-entry':
  109. $replacements[$original] = $sanitize ? check_markup($comment, filter_default_format(), $langcode) : $comment;
  110. break;
  111. case 'workflow-current-state-date-iso':
  112. $replacements[$original] = format_date($date, 'custom', 'c', NULL, $langcode);
  113. break;
  114. case 'workflow-current-state-date-tstamp':
  115. $replacements[$original] = $sanitize ? check_plain($date) : $date;
  116. break;
  117. case 'workflow-current-state-date-formatted':
  118. $replacements[$original] = format_date($date, 'medium', '', NULL, $langcode);
  119. break;
  120. default:
  121. // Add support for custom date formats. (see token.tokens.inc)
  122. // @todo Remove when http://drupal.org/node/1173706 is fixed.
  123. $date_format_types = system_get_date_types();
  124. foreach ($date_format_types as $date_type => $date_info) {
  125. if ($name == 'workflow-current-state-date-' . $date_type) {
  126. $replacements[$original] = format_date($date, $date_type, '', NULL, $langcode);
  127. }
  128. }
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. return $replacements;
  136. }
  137. /**
  138. * Implements hook_token_info().
  139. */
  140. function workflow_token_info() {
  141. $type = array(
  142. 'name' => t('Workflow'),
  143. 'description' => t('Tokens related to workflows.'),
  144. 'needs-data' => 'node',
  145. );
  146. $tokens = array();
  147. $tokens['workflow-name'] = array(
  148. 'name' => t('Workflow name'),
  149. 'description' => t('Name of workflow appied to this node'),
  150. );
  151. $tokens['workflow-current-state-name'] = array(
  152. 'name' => t('Current state name'),
  153. 'description' => t('Current state of content'),
  154. );
  155. $tokens['workflow-old-state-name'] = array(
  156. 'name' => t('Old state name'),
  157. 'description' => t('Old state of content'),
  158. );
  159. $tokens['workflow-current-state-updating-user-name'] = array(
  160. 'name' => t('Username of last state changer'),
  161. 'description' => t('Username of last state changer'),
  162. );
  163. $tokens['workflow-current-state-updating-user-link'] = array(
  164. 'name' => t('Username link of last state changer'),
  165. 'description' => t('Themed Username of last state changer'),
  166. );
  167. $tokens['workflow-current-state-updating-user-uid'] = array(
  168. 'name' => t('Uid of last state changer'),
  169. 'description' => t('uid of last state changer'),
  170. );
  171. $tokens['workflow-current-state-updating-user-mail'] = array(
  172. 'name' => t('Email of last state changer'),
  173. 'description' => t('email of last state changer'),
  174. );
  175. $tokens['workflow-current-state-updating-user-mail'] = array(
  176. 'name' => t('Email link of last state changer'),
  177. 'description' => t('email link of last state changer'),
  178. );
  179. $tokens['workflow-current-state-log-entry'] =array(
  180. 'name' => t('Last workflow comment log'),
  181. 'description' => t('Last workflow comment log'),
  182. );
  183. $tokens['workflow-current-state-date-iso'] = array(
  184. 'name' => t('Current state date (ISO)'),
  185. 'description' => t('Date of last state change (ISO)'),
  186. );
  187. $tokens['workflow-current-state-date-tstamp'] = array(
  188. 'name' => t('Current state date (timestamp)'),
  189. 'description' => t('Date of last state change (timestamp)'),
  190. );
  191. $tokens['workflow-current-state-date-formatted'] = array(
  192. 'name' => t('Current state date (formatted by site default)'),
  193. 'description' => t('Date of last state change (formatted by site default)'),
  194. );
  195. // Add support for custom date formats. (see token.tokens.inc)
  196. // @todo Remove when http://drupal.org/node/1173706 is fixed.
  197. $date_format_types = system_get_date_types();
  198. foreach ($date_format_types as $date_type => $date_info) {
  199. $tokens['workflow-current-state-date-' . $date_type] = array(
  200. 'name' => t('Current state date (using @format format)', array('@format' => $date_info['title'])),
  201. 'description' => t("A date in '@type' format. (%date)", array('@type' => $date_type, '%date' => format_date(REQUEST_TIME, $date_type))),
  202. 'module' => 'workflow',
  203. );
  204. }
  205. return array(
  206. 'types' => array('workflow' => $type),
  207. 'tokens' => array('workflow' => $tokens, 'node' => $tokens),
  208. );
  209. }