You are here

function simplenews_token_values in Simplenews 6.2

Implementation of hook_token_value().

File

./simplenews.module, line 2049
Simplenews node handling, sent email, newsletter block and general hooks

Code

function simplenews_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  switch ($type) {
    case 'all':
    case 'simplenews':
      $account = $object['account'];
      $node = $object['node'];
      $language = isset($account->language) ? $account->language : language_default();
      $langcode = $language->language;
      $urlargs = array(
        'absolute' => TRUE,
        'language' => $language,
      );

      // Build tokens for 'simplenews' only.
      $values['simplenews-receiver-name'] = !empty($account->name) ? check_plain($account->name) : check_plain(variable_get('anonymous', 'Anonymous'));
      $values['simplenews-newsletter-url'] = url('node/' . $node->nid, $urlargs);

      // Try to switch to translation if possible.
      if ($node->tnid) {
        $translations = translation_node_get_translations($node->tnid);
        $node_translated = $translations[$langcode];
        if ($node_translated) {
          $values['simplenews-newsletter-url'] = url('node/' . $node_translated->nid, $urlargs);
        }
      }

    // Intentionally fall through (no break).
    case 'simplenews_subscription':
      $account = $object['account'];
      $newsletter = $object['newsletter'];
      $node = isset($object['node']) ? $object['node'] : '';
      $language = isset($account->language) ? $account->language : language_default();
      $urlargs = array(
        'absolute' => TRUE,
        'language' => $language,
      );

      // Build hash for (un)subscribe URL.
      $hash = '';
      if (isset($account->snid) && isset($newsletter->tid)) {
        $hash = _simplenews_generate_hash($account->mail, $account->snid, $newsletter->tid);
      }

      // When simplenews_token_values() is called from simplenews_nodeapi()
      // $newsletter->name contains no value. Newsletter name is
      // reconstructed from $node->simplenews['tid'].
      if (!isset($newsletter->name) || !isset($newsletter->tid)) {
        $tid = isset($newsletter->tid) ? $newsletter->tid : $node->simplenews['tid'];
        if ($tid && ($term = taxonomy_get_term($tid))) {
          $newsletter->name = $term->name;
          $newsletter->tid = $tid;
        }
      }
      module_load_include('inc', 'simplenews', 'includes/simplenews.mail');

      // Translate newsletter name
      $newsletter_name = _simplenews_tt_newsletter_name($newsletter, $language->language);

      // Info for simplenews-from-name and simplenews-from-mail tokens
      $from = _simplenews_set_from($node);

      // Build tokens for both 'simplenews_subscription' and 'simplenews'.
      $values['simplenews-subscribe-url'] = url('newsletter/confirm/add/' . $hash, $urlargs);
      $values['simplenews-unsubscribe-url'] = url('newsletter/confirm/remove/' . $hash, $urlargs);
      $values['simplenews-receiver-mail'] = $account->mail;
      $values['simplenews-from-name'] = $from['formatted'];
      $values['simplenews-from-mail'] = $from['address'];
      $values['simplenews-newsletters-name'] = $newsletter_name ? check_plain($newsletter_name) : '';
      $values['simplenews-newsletters-url'] = '';
      if (isset($newsletter->tid)) {
        $values['simplenews-newsletters-url'] = url('taxonomy/term/' . $newsletter->tid, $urlargs);
      }
      $values['simplenews-subscriptions-url'] = url('newsletter/subscriptions', $urlargs);
      break;
  }
  return $values;
}