You are here

function workflow_token_values in Workflow 6

Same name and namespace in other branches
  1. 5.2 workflow.module \workflow_token_values()
  2. 5 workflow.module \workflow_token_values()
  3. 6.2 workflow.module \workflow_token_values()

Implementation of hook_token_values().

File

./workflow.module, line 1126
Support workflows made up of arbitrary states.

Code

function workflow_token_values($type, $object = NULL) {
  $values = array();
  switch ($type) {
    case 'node':
    case 'workflow':
      $node = (object) $object;
      if ($wid = workflow_get_workflow_for_type($node->type)) {
        $values['workflow-name'] = workflow_get_name($wid);
        $states = workflow_get_states($wid);
      }
      else {
        break;
      }
      $result = db_query_range("SELECT h.* FROM {workflow_node_history} h WHERE nid = %d ORDER BY stamp DESC", $node->nid, 0, 1);
      if ($row = db_fetch_object($result)) {
        $account = user_load(array(
          'uid' => $row->uid,
        ));
        $comment = $row->comment;
      }
      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($row->sid) ? $row->sid : _workflow_creation_state($wid);
        $date = time();
        $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
        $uid = $node->uid;
        $mail = $node->uid ? $node->user_mail : '';
        $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
      }
      else {
        if (!isset($node->workflow) && empty($row->sid)) {

          // If the state is not specified and the node has no workflow history,
          // the node is being inserted and will soon be transitioned to the first valid state.
          // We find this state using the same logic as workflow_nodeapi().
          $choices = workflow_field_choices($node);
          $keys = array_keys($choices);
          $sid = array_shift($keys);
          $old_sid = _workflow_creation_state($wid);
          $date = time();
          $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
          $uid = $node->uid;
          $mail = $node->uid ? $node->user_mail : '';
          $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
        }
        else {

          // Default to the most recent transition data in the workflow history table.
          $sid = $row->sid;
          $old_sid = $row->old_sid;
          $date = $row->stamp;
          $user_name = $account->uid ? $account->name : variable_get('anonymous', 'Anonymous');
          $uid = $account->uid;
          $mail = $account->uid ? $account->mail : '';
        }
      }
      $values['workflow-current-state-name'] = $states[$sid];
      $values['workflow-old-state-name'] = $states[$old_sid];
      $values['workflow-current-state-date-iso'] = date('Ymdhis', $date);
      $values['workflow-current-state-date-tstamp'] = $date;
      $values['workflow-current-state-date-formatted'] = format_date($date);
      $values['workflow-current-state-date-small'] = format_date($date, 'small');
      $values['workflow-current-state-date-medium'] = format_date($date, 'medium');
      $values['workflow-current-state-date-large'] = format_date($date, 'large');
      $values['workflow-current-state-updating-user-name'] = check_plain($user_name);
      $values['workflow-current-state-updating-user-uid'] = $uid;
      $values['workflow-current-state-updating-user-mail'] = check_plain($mail);
      $values['workflow-current-state-log-entry'] = filter_xss($comment, array(
        'a',
        'em',
        'strong',
      ));
      break;
  }
  return $values;
}