workflow.tokens.inc 9.9 KB

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