You are here

function workflow_tokens in Workflow 7

Same name and namespace in other branches
  1. 7.2 workflow.tokens.inc \workflow_tokens()

Implements hook_tokens().

File

./workflow.tokens.inc, line 10
Tokens hooks for Workflow module.

Code

function workflow_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  $langcode = !empty($options['language']->language) ? $options['language']->language : LANGUAGE_NONE;
  $date = REQUEST_TIME;
  if (($type == 'node' || $type == 'workflow') && !empty($data['node']) && !empty($data['node']->nid)) {
    $entity_type = 'node';

    // @todo: support other entity types in workflow_tokens().
    $node = $data['node'];

    // Get a list of all possible states for returning state names.
    $states = WorkflowState::getStates();
    if ($workflow = workflow_get_workflows_by_type($node->type, $entity_type)) {
      $wid = $workflow->wid;
      $last_history = workflow_get_recent_node_history($node->nid);
      if (isset($node->workflow) && !isset($node->workflow_stamp)) {

        // The node is being submitted but the form data has not been saved to the database yet,
        // so we set the token values from the workflow form fields.
        $sid = $node->workflow;
        $old_sid = isset($last_history->old_sid) ? $last_history->old_sid : $workflow
          ->getCreationSid();
        $user_name = $node->uid ? isset($node->name) ? $node->name : variable_get('anonymous', 'Anonymous') : variable_get('anonymous', 'Anonymous');
        $uid = $node->uid;
        $account = user_load($node->uid);
        $mail = $node->uid && isset($node->user_mail) ? $node->user_mail : '';
        $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
      }
      else {
        if (empty($last_history)) {

          // If the node has no workflow history,
          // the node is being inserted and will soon be transitioned to the first valid state.
          $sid = $workflow
            ->getFirstSid($entity_type, $node);
          if ($sid) {

            // @TODO: Some users, like anonymous, may not be allowed
            // to cause transitions, so there will be no choices.
            $old_sid = $workflow
              ->getCreationSid();
          }
          else {

            // What to choose?
            $sid = $node->workflow;
            $old_sid = $workflow
              ->getCreationSid();
          }
          $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
          $uid = $node->uid;
          $account = user_load($node->uid);
          $mail = $node->uid && isset($node->user_mail) ? $node->user_mail : '';
          $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
        }
        else {

          // Sometimes there is no workflow set (edit?).
          if (!isset($node->workflow)) {
            $node->workflow = $last_history->sid;
          }

          // Is a transition in progress?
          if ($node->workflow != $last_history->sid) {
            $sid = $node->workflow;
            $old_sid = $last_history->sid;
            $date = $node->workflow_stamp;
            $uid = $node->uid;
            $account = user_load($node->uid);
          }
          else {

            // Not a transition.
            $sid = $last_history->sid;
            $old_sid = $last_history->old_sid;
            $date = $last_history->stamp;
            $uid = $last_history->uid;
            $account = user_load($last_history->uid);
          }

          // Default to the most recent transition data in the workflow history table.
          $user_name = $account->uid ? $account->name : variable_get('anonymous', 'Anonymous');
          $mail = $account->uid ? $account->mail : '';
          $comment = $last_history->comment;
        }
        foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'workflow-name':
              $replacements[$original] = $sanitize ? check_plain($workflow->name) : $workflow->name;
              break;
            case 'workflow-current-state-name':
              $replacements[$original] = $sanitize ? check_plain($states[$sid]
                ->getName()) : $states[$sid]
                ->getName();
              break;
            case 'workflow-old-state-name':
              $replacements[$original] = $sanitize ? check_plain($states[$old_sid]
                ->getName()) : $states[$old_sid]
                ->getName();
              break;
            case 'workflow-current-state-updating-user-name':
              $name = format_username($account);
              $replacements[$original] = $sanitize ? check_plain($name) : $name;
              break;
            case 'workflow-current-state-updating-user-link':
              $replacements[$original] = theme('username', array(
                'account' => $account,
              ));
              break;
            case 'workflow-current-state-updating-user-uid':

              // User IDs are integers only and do not need sanitization.
              $replacements[$original] = $uid;
              break;
            case 'workflow-current-state-updating-user-mail':
              $replacements[$original] = $sanitize ? check_plain($account->mail) : $account->mail;
              break;
            case 'workflow-current-state-updating-user-mailto':
              $replacements[$original] = '<a href="mailto:' . check_plain($account->mail) . '">' . check_plain($user_name) . '</a>';
              break;
            case 'workflow-current-state-log-entry':
              $replacements[$original] = $sanitize ? check_markup($comment, filter_default_format(), $langcode) : $comment;
              break;
            case 'workflow-current-state-date-iso':
              $replacements[$original] = format_date($date, 'custom', 'c', NULL, $langcode);
              break;
            case 'workflow-current-state-date-tstamp':
              $replacements[$original] = $sanitize ? check_plain($date) : $date;
              break;
            case 'workflow-current-state-date-formatted':
              $replacements[$original] = format_date($date, 'medium', '', NULL, $langcode);
              break;
            default:

              // Add support for custom date formats. (see token.tokens.inc)
              // @todo Remove when http://drupal.org/node/1173706 is fixed.
              $date_format_types = system_get_date_types();
              foreach ($date_format_types as $date_type => $date_info) {
                if ($name == 'workflow-current-state-date-' . $date_type) {
                  $replacements[$original] = format_date($date, $date_type, '', NULL, $langcode);
                }
              }
              break;
          }
        }
      }
    }
  }
  return $replacements;
}