You are here

function simplenews_tokens in Simplenews 7.2

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

Implements hook_tokens().

File

./simplenews.tokens.inc, line 74
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'];
      $newsletter = isset($data['newsletter']) ? $data['newsletter'] : NULL;
      $language = isset($subscriber->language->language) ? $subscriber->language : user_preferred_language($subscriber);
      $url_arguments = array(
        'absolute' => TRUE,
        'language' => $language,
      );
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'subscribe-url':
            $hash = simplenews_generate_hash($subscriber->mail, 'add');
            $url = 'newsletter/confirm/add/' . $subscriber->snid . '/' . $newsletter->newsletter_id . '/' . REQUEST_TIME . '/' . $hash;
            $replacements[$original] = url($url, $url_arguments);
            break;
          case 'unsubscribe-url':
            if (!isset($subscriber->snid)) {

              // Temporary test subscriber.
              $replacements[$original] = t('No unsubscribe link for test subscribers.');
              continue;
            }
            $hash = simplenews_generate_hash($subscriber->mail, 'remove');
            $url = 'newsletter/confirm/remove/' . $subscriber->snid . '/' . $newsletter->newsletter_id . '/' . REQUEST_TIME . '/' . $hash;
            $replacements[$original] = url($url, $url_arguments);
            break;
          case 'combined-url':
            $hash = simplenews_generate_hash($subscriber->mail, 'combined' . serialize($subscriber->changes));
            $url = 'newsletter/confirm/combined/' . $subscriber->snid . '/' . REQUEST_TIME . '/' . $hash;
            $replacements[$original] = url($url, $url_arguments);
            break;
          case 'manage-url':
            $hash = simplenews_generate_hash($subscriber->mail, 'manage');
            $url = 'newsletter/subscriptions/' . $subscriber->snid . '/' . $newsletter->newsletter_id . '/' . REQUEST_TIME . '/' . $hash;
            $replacements[$original] = url($url, $url_arguments);
            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-newsletter':
      if (!isset($data['newsletter'])) {
        return;
      }
      $newsletter = $data['newsletter'];
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'name':
            if (isset($newsletter->name)) {
              $replacements[$original] = $sanitize ? check_plain($newsletter->name) : $newsletter->name;
            }
            else {
              $replacements[$original] = t('Unassigned newsletter');
            }
            break;
          case 'url':
            $uri = entity_uri('simplenews_newsletter', $newsletter);
            $replacements[$original] = url($uri['path'], $uri['options']);
            break;
        }
      }
      break;
  }
  return $replacements;
}