function privatemsg_tokens

7-1 privatemsg.module privatemsg_tokens($type, $tokens, array $data = array(), array $options = array())
7-2 privatemsg.module privatemsg_tokens($type, $tokens, array $data = array(), array $options = array())

Implements hook_tokens().

File

./privatemsg.module, line 2914
Allows users to send private messages to other users.

Code

function privatemsg_tokens($type, $tokens, array $data = array(), array $options = array()) {
  global $user;
  $url_options = array('absolute' => TRUE);
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }

  $recipient = $user;
  if (isset($data['privatemsg_recipient'])) {
    $recipient = $data['privatemsg_recipient'];
  }

  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'privatemsg_message' && !empty($data['privatemsg_message'])) {
    $message = $data['privatemsg_message'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'mid':
          $replacements[$original] = $message->mid;
          break;

        case 'thread-id':
          $replacements[$original] = $message->thread_id;
          break;

        case 'subject':
          // Avoid recursion.
          if (empty($options['privatemsg_recursion'])) {
            $subject = privatemsg_token_replace($message->subject, $data, $options + array('privatemsg_recursion' => 1));
          }
          else {
            $subject = $message->subject;
          }
          $replacements[$original] = $sanitize ? check_plain($subject) : $subject;
          break;

        case 'body':
          // Avoid recursion.
          if (empty($options['privatemsg_recursion'])) {
            $replacements[$original] = privatemsg_token_replace($sanitize ? check_markup($message->body, $message->format) : $message->body, $data, $options + array('privatemsg_recursion' => 1));
          }
          else {
            $replacements[$original] = $sanitize ? check_markup($message->body, $message->format) : $message->body;
          }
          break;

        case 'url':
          $uri = entity_uri('privatemsg_message', $message);
          $replacements[$original] = url($uri['path'], $url_options + $uri['options']);
          break;

          // Default values for the chained tokens handled below.
        case 'author':
          $replacements[$original] = $sanitize ? filter_xss(privatemsg_recipient_format($message->author, array('plain' => TRUE))) : privatemsg_recipient_format($message->author, array('plain' => TRUE));
          break;

        case 'recipient':
          $replacements[$original] = $sanitize ? filter_xss(privatemsg_recipient_format($recipient, array('plain' => TRUE))) : privatemsg_recipient_format($recipient, array('plain' => TRUE));
          break;

        case 'timestamp':
          $replacements[$original] = format_date($message->timestamp, 'medium', '', NULL, $language_code);
          break;
      }
    }

    if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
      $replacements += token_generate('user', $author_tokens, array('user' => $message->author), $options);
    }

    if ($recipient_tokens = token_find_with_prefix($tokens, 'recipient')) {
      $replacements += token_generate('user', $recipient_tokens, array('user' => $recipient), $options);
    }

    if ($sent_tokens = token_find_with_prefix($tokens, 'timestamp')) {
      $replacements += token_generate('date', $sent_tokens, array('date' => $message->timestamp), $options);
    }
  }

  return $replacements;
}