workflownode.tokens.inc 10 KB

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