You are here

function support_mail_tokens in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support.module \support_mail_tokens()

Return an array of token to value mappings for support e-mail messages.

1 call to support_mail_tokens()
support_mail in ./support.module
Implementation of hook_mail().

File

./support.module, line 1460
support.module

Code

function support_mail_tokens($account, $language, $nid, $comment, $suppress) {
  global $base_url, $user;
  static $reset = TRUE;

  // force reload node from database to get updated state/priority information,
  // but no need to reset it multiple times when sending multiple notifications
  $node = node_load($nid, NULL, $reset);
  $assigned = user_load($node->assigned);
  $reset = FALSE;
  if (isset($comment->cid)) {
    $cid = $comment->cid;

    // TODO Convert "user_load" to "user_load_multiple" if "$comment['uid']" is other than a uid.
    // To return a single user object, wrap "user_load_multiple" with "array_shift" or equivalent.
    // Example: array_shift(user_load_multiple(array(), $comment['uid']))
    $update_account = user_load($comment->uid);
  }
  else {
    $cid = 0;

    // TODO Convert "user_load" to "user_load_multiple" if "$node->uid" is other than a uid.
    // To return a single user object, wrap "user_load_multiple" with "array_shift" or equivalent.
    // Example: array_shift(user_load_multiple(array(), $node->uid))
    $update_account = user_load($node->uid);
  }
  $client = support_client_load($node->client);
  $ticket_unsubscribe_key = md5($account->uid . $node->nid);
  $all_unsubscribe_key = md5($account->uid);
  $previous_comment = db_query_range('SELECT cid FROM {comment} WHERE nid = :nid ORDER BY cid DESC', 0, 1, array(
    ':nid' => $nid,
  ))
    ->fetchField();
  if ($previous_comment) {
    $previous = db_query('SELECT * FROM {support_ticket_comment} WHERE cid = :cid', array(
      ':cid' => $previous_comment,
    ))
      ->fetchObject();
  }
  $body = '';
  $elements = node_view($node, 'full');
  if (isset($elements['body'])) {
    $body = drupal_render($elements['body']);
  }

  // TODO Please change this theme call to use an associative array for the $variables parameter.
  $tokens = array(
    '!username' => $account->name,
    '!client_name' => $client->name,
    '!client_path' => $client->path,
    '!key' => '[' . variable_get('support_key', 'tkt') . ":{$nid}]",
    '!update_username' => isset($update_account->name) ? $update_account->name : '',
    '!update_realname' => theme('username', array(
      'account' => $user,
    )),
    '!site' => variable_get('site_name', 'Drupal'),
    '!uri' => $base_url,
    '!uri_brief' => preg_replace('!^https?://!', '', $base_url),
    '!uri_login' => url('user/register', array(
      'absolute' => TRUE,
      'alias' => variable_get('support_use_aliased_urls', TRUE),
    )),
    '!mailto' => $account->mail,
    '!date' => format_date(REQUEST_TIME, 'medium', '', NULL, $language->language),
    '!ticket_subject' => check_plain($node->title),
    '!ticket_body' => $suppress ? t('The text of this ticket was manually suppressed.  You must view the ticket online to see it.') . "<br />\n" : $body . _support_mail_list_attachments($node, $comment),
    '!ticket_url' => url("node/{$nid}", array(
      'absolute' => TRUE,
      'language' => $language,
      'fragment' => "comment-{$cid}",
      'alias' => variable_get('support_use_aliased_urls', TRUE),
    )),
    '!update_url' => url("node/{$nid}", array(
      'absolute' => TRUE,
      'language' => $language,
      'fragment' => "comment-form",
      'alias' => variable_get('support_use_aliased_urls', TRUE),
    )),
    '!update' => $suppress ? t('The text of this ticket update was manually suppressed.  You must view the ticket online to see the update.') . "<br />\n" : check_markup(isset($comment->language) && isset($comment->comment_body[$comment->language][0]) ? $comment->comment_body[$comment->language][0]['value'] : '') . _support_mail_list_attachments($node, $comment),
    '!state' => (isset($previous->state) && $previous->state != $node->state ? _support_state($previous->state) . ' -> ' : '') . _support_state($node->state),
    '!priority' => (isset($previous->priority) && $previous->priority != $node->priority ? _support_priorities($previous->priority) . ' -> ' : '') . _support_priorities($node->priority),
    '!assigned_username' => !empty($assigned) ? $assigned->name : '',
    '!assigned_realname' => !empty($assigned) ? theme('username', array(
      'account' => $assigned,
    )) : '',
    '!unsubscribe_ticket' => url("support/{$nid}/unsubscribe/{$account->uid}/{$ticket_unsubscribe_key}", array(
      'absolute' => TRUE,
      'language' => $language,
      'alias' => variable_get('support_use_aliased_urls', TRUE),
    )),
    '!unsubscribe_all' => url("support/all/unsubscribe/{$account->uid}/{$all_unsubscribe_key}", array(
      'absolute' => TRUE,
      'language' => $language,
      'alias' => variable_get('support_use_aliased_urls', TRUE),
    )),
  );
  if (!empty($account->password)) {
    $tokens['!password'] = $account->password;
  }
  return $tokens;
}