You are here

function simplenews_tokens in Simplenews 7

Same name and namespace in other branches
  1. 8.2 simplenews.tokens.inc \simplenews_tokens()
  2. 8 simplenews.tokens.inc \simplenews_tokens()
  3. 7.2 simplenews.tokens.inc \simplenews_tokens()
  4. 3.x simplenews.tokens.inc \simplenews_tokens()

Implements hook_tokens().

File

./simplenews.tokens.inc, line 79
Token related hook implementations.

Code

function simplenews_tokens($type, $tokens, $data = array(), $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  switch ($type) {
    case 'simplenews-subscriber':
      if (!isset($data['simplenews_subscriber'])) {
        return;
      }
      $subscriber = $data['simplenews_subscriber'];
      $category = isset($data['category']) ? $data['category'] : NULL;
      $language = isset($subscriber->language->language) ? $subscriber->language : user_preferred_language($subscriber);

      // Build hash for the URL of the (un)subscribe confirmation page.
      $hash = '';
      if (isset($subscriber->snid) && isset($category->tid)) {
        $hash = simplenews_generate_hash($subscriber->mail, $subscriber->snid, $category->tid);
      }
      elseif (isset($subscriber->snid)) {
        $combined_hash = simplenews_generate_hash($subscriber->mail, $subscriber->snid, 0);
      }
      foreach ($tokens as $name => $original) {
        switch ($name) {

          // Simple key values on the node.
          case 'subscribe-url':
            $replacements[$original] = url('newsletter/confirm/add/' . $hash, array(
              'absolute' => TRUE,
              'language' => $language,
            ));
            break;
          case 'unsubscribe-url':
            $replacements[$original] = url('newsletter/confirm/remove/' . $hash, array(
              'absolute' => TRUE,
              'language' => $language,
            ));
            break;
          case 'combined-url':
            $replacements[$original] = url('newsletter/confirm/combined/' . $combined_hash, array(
              'absolute' => TRUE,
              'language' => $language,
            ));
            break;
          case 'manage-url':
            $replacements[$original] = url('newsletter/subscriptions/' . $hash, array(
              'absolute' => TRUE,
              'language' => $language,
            ));
            break;
          case 'mail':
            $replacements[$original] = $sanitize ? check_plain($subscriber->mail) : $subscriber->mail;
            break;
        }
      }
      if (($user_tokens = token_find_with_prefix($tokens, 'user')) && !empty($subscriber->uid)) {
        $replacements += token_generate('user', $user_tokens, array(
          'user' => user_load($subscriber->uid),
        ), $options);
      }
      break;
    case 'simplenews-category':
      if (!isset($data['category'])) {
        return;
      }
      $category = $data['category'];
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'name':
            if (isset($category->name)) {
              $newsletter_name = _simplenews_newsletter_name($category, $language_code);
              $replacements[$original] = $sanitize ? check_plain($newsletter_name) : $newsletter_name;
            }
            else {
              $replacements[$original] = t('Unassigned newsletter');
            }
            break;
          case 'url':
            $uri = entity_uri('taxonomy_term', $category);
            $replacements[$original] = url($uri['path'], $uri['options']);
            break;
        }
      }
      if ($term_tokens = token_find_with_prefix($tokens, 'term')) {
        $replacements += token_generate('taxonomy_term', $term_tokens, array(
          'taxonomy_term' => $category,
        ), $options);
      }
      break;
  }
  return $replacements;
}