/[drupal]/contributions/modules/simplenews/simplenews.module
ViewVC logotype

Diff of /contributions/modules/simplenews/simplenews.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.214, Sun Aug 30 12:49:50 2009 UTC revision 1.215, Wed Sep 30 14:10:20 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: simplenews.module,v 1.213 2009/08/09 10:46:16 sutharsan Exp $  // $Id: simplenews.module,v 1.214 2009/08/30 12:49:50 sutharsan Exp $
3    
4  /**  /**
5   * @defgroup simplenews   * @defgroup simplenews
# Line 330  function simplenews_nodeapi(&$node, $op, Line 330  function simplenews_nodeapi(&$node, $op,
330        if (!isset($node->simplenews_mail)) {        if (!isset($node->simplenews_mail)) {
331          global $language;          global $language;
332          $context['node'] = $node;          $context['node'] = $node;
333          $variables = simplenews_mail_tokens($user, $context, $language);  
334          if (isset($node->body)) {          if (isset($node->body)) {
335            $node->body = strtr($node->body, $variables);            $node->body = token_replace($node->body, 'simplenews', array('node' => $node));
336          }          }
337          if (isset($node->teaser)) {          if (isset($node->teaser)) {
338            $node->teaser = strtr($node->teaser, $variables);            $node->teaser = token_replace($node->teaser, 'simplenews', array('node' => $node));
339          }          }
340        }        }
341        break;        break;
# Line 508  function simplenews_form_alter(&$form, $ Line 508  function simplenews_form_alter(&$form, $
508        // Available variables are based on user_mail_tokens().        // Available variables are based on user_mail_tokens().
509        // But uses only those which can be used with uid = 0 since simplenews also sends to anonymous users.        // But uses only those which can be used with uid = 0 since simplenews also sends to anonymous users.
510        if (isset($form['body_field'])) {        if (isset($form['body_field'])) {
511          $form['body_field']['body']['#description'] = t("This will be the body of your newsletter. Available variables are: !site (the name of your website), !uri (a link to your homepage), !uri_brief (homepage link without the http://), !date (today's date), !login_uri (link to login page), !confirm_subscribe_url (subscription confirmation link), !confirm_unsubscribe_url (unsubscription link), !newsletter_url (link to this newsletter issue), !newsletter_name (name of this newsletter series).");          $form['body_field']['body']['#description'] = t("This will be the body of your newsletter. See 'Replacement patterns' for available variables.)");
512        }        }
513          $form['simplenews_subscription']['subscription_mail']['token_help'] = array(
514            '#title' => t('Replacement patterns'),
515            '#type' => 'fieldset',
516            '#collapsible' => TRUE,
517            '#collapsed' => TRUE,
518            '#weight' => -20,
519            '#description' => t('These tokens can be used in all text fields and will be replaced on-screen and in the email. Note that simplenews-receiver tokens are not suitable for on-screen use.')
520          );
521          $form['simplenews_subscription']['subscription_mail']['token_help']['help'] = array(
522            '#value' => theme('token_help', 'simplenews'),
523          );
524    
525        if (isset($form['#node']->simplenews)) {        if (isset($form['#node']->simplenews)) {
526          $simplenews_values = $form['#node']->simplenews;          $simplenews_values = $form['#node']->simplenews;
# Line 1133  function simplenews_get_subscription($ac Line 1144  function simplenews_get_subscription($ac
1144    // Load subscription data based on available account informatioin    // Load subscription data based on available account informatioin
1145    // NOTE that the order of checking for snid, mail and uid is critical. mail must be checked *before* uid. See simplenews_subscribe_user()    // NOTE that the order of checking for snid, mail and uid is critical. mail must be checked *before* uid. See simplenews_subscribe_user()
1146    if (isset($account->snid)) {    if (isset($account->snid)) {
1147      $subscription = db_fetch_object(db_query("SELECT s.* FROM {simplenews_subscriptions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE s.snid = %d", $account->snid));      $subscription = db_fetch_object(db_query("SELECT s.*, u.name FROM {simplenews_subscriptions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE s.snid = %d", $account->snid));
1148    }    }
1149    elseif (isset($account->mail)) {    elseif (isset($account->mail)) {
1150      $subscription = db_fetch_object(db_query("SELECT s.* FROM {simplenews_subscriptions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE LOWER(s.mail) = LOWER('%s')", $account->mail));      $subscription = db_fetch_object(db_query("SELECT s.*, u.name FROM {simplenews_subscriptions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE LOWER(s.mail) = LOWER('%s')", $account->mail));
1151    }    }
1152    elseif (isset($account->uid) && $account->uid > 0) {    elseif (isset($account->uid) && $account->uid > 0) {
1153      $subscription = db_fetch_object(db_query("SELECT s.* FROM {simplenews_subscriptions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE s.uid = %d", $account->uid));      $subscription = db_fetch_object(db_query("SELECT s.*, u.name FROM {simplenews_subscriptions} s LEFT JOIN {users} u ON u.uid = s.uid WHERE s.uid = %d", $account->uid));
1154    }    }
1155    
1156    if (!empty($subscription)) {    if (!empty($subscription)) {
# Line 1152  function simplenews_get_subscription($ac Line 1163  function simplenews_get_subscription($ac
1163        }        }
1164        $subscription->newsletter_subscription[$newsletter_subscription->tid] = $newsletter_subscription;        $subscription->newsletter_subscription[$newsletter_subscription->tid] = $newsletter_subscription;
1165      }      }
     $subscription->name = '';  
1166      $subscription->language = user_preferred_language($subscription);      $subscription->language = user_preferred_language($subscription);
1167    }    }
1168    else {    else {
# Line 1598  function simplenews_mail($key, &$message Line 1608  function simplenews_mail($key, &$message
1608          $footer             = $messages[$nid][$langcode]['footer'];          $footer             = $messages[$nid][$langcode]['footer'];
1609        }        }
1610    
1611        // Build message body.        // Build message body, replace tokens.
       // Replace tokens with user specific data and  
1612        // Convert to plain text if required.        // Convert to plain text if required.
1613        $variables =  simplenews_mail_tokens($context['account'], $context, is_object($context['account']->language) ? $context['account']->language : language_default());        $message['body']['body'] = token_replace($body, 'simplenews', $context);
       $body = strtr($body, $variables);  
1614        if ($context['node']->simplenews['s_format'] == 'plain') {        if ($context['node']->simplenews['s_format'] == 'plain') {
1615          $body = simplenews_html_to_text($body, variable_get('simplenews_hyperlinks_'. $context['node']->simplenews['tid'], 1));          $message['body']['body'] = simplenews_html_to_text($message['body']['body'], variable_get('simplenews_hyperlinks_'. $context['node']->simplenews['tid'], 1));
1616        }        }
       $message['body']['body'] = $body;  
1617    
1618        // Build message footer.        // Build message footer, replace tokens.
1619        // Replace tokens with user specific data        $message['body']['footer'] = token_replace($footer, 'simplenews', $context);
       $message['body']['footer'] = strtr($footer, $variables);  
1620    
1621        // Add user specific header data.        // Add user specific header data.
1622        $message['headers']['List-Unsubscribe'] = strtr('<!confirm_unsubscribe_url>', $variables);        $message ['headers']['List-Unsubscribe'] = token_replace('[simplenews-unsubscribe-url]', 'simplenews', $context);
1623    
1624        break;        break;
1625      case 'subscribe':      case 'subscribe':
1626        // Use formatted from address "name" <mail_address>        // Use formatted from address "name" <mail_address>
1627        $message['headers']['From'] = $params['from']['formatted'];        $message['headers']['From'] = $params['from']['formatted'];
1628    
1629        $variables =  simplenews_mail_tokens($context['account'], $context, is_object($context['account']->language) ? $context['account']->language : language_default());        $message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $langcode);
1630        $message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $context['account']->language, $variables);        $message['subject'] = token_replace($message['subject'], 'simplenews_subscription', $context);
1631    
1632        if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) {        if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) {
1633          $message['body'] = _simplenews_subscription_confirmation_text('subscribe_subscribed', $context['account']->language, $variables);          $message['body'] = _simplenews_subscription_confirmation_text('subscribe_subscribed', $langcode);
1634            $message['body'] = token_replace($message['body'], 'simplenews_subscription', $context);
1635        }        }
1636        else {        else {
1637          $message['body'] = _simplenews_subscription_confirmation_text('subscribe_unsubscribed', $context['account']->language, $variables);          $message['body'] = _simplenews_subscription_confirmation_text('subscribe_unsubscribed', $langcode);
1638            $message['body'] = token_replace($message['body'], 'simplenews_subscription', $context);
1639        }        }
1640        break;        break;
1641      case 'unsubscribe':      case 'unsubscribe':
1642        // Use formatted from address "name" <mail_address>        // Use formatted from address "name" <mail_address>
1643        $message['headers']['From'] = $params['from']['formatted'];        $message['headers']['From'] = $params['from']['formatted'];
1644    
1645        $variables =  simplenews_mail_tokens($context['account'], $context, is_object($context['account']->language) ? $context['account']->language : language_default());        $message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $langcode);
1646        $message['subject'] = _simplenews_subscription_confirmation_text('subscribe_subject', $context['account']->language, $variables);        $message['subject'] = token_replace($message['subject'], 'simplenews_subscription', $context);
1647    
1648        if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) {        if (simplenews_user_is_subscribed($context['account']->mail, $context['newsletter']->tid)) {
1649          $message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_subscribed', $context['account']->language, $variables);          $message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_subscribed', $langcode);
1650            $message['body'] = token_replace($message['body'], 'simplenews_subscription', $context);
1651        }        }
1652        else {        else {
1653          $message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_unsubscribed', $context['account']->language, $variables);          $message['body'] = _simplenews_subscription_confirmation_text('unsubscribe_unsubscribed', $langcode);
1654            $message['body'] = token_replace($message['body'], 'simplenews_subscription', $context);
1655        }        }
1656        break;        break;
1657    }    }
# Line 2119  function _simplenews_html_replace() { Line 2131  function _simplenews_html_replace() {
2131  }  }
2132    
2133  /**  /**
2134   * Build array of mail tokens.   * Implementation of hook_token_list().
  *  
  * Depending on the context in which this function is called, some tokens  
  * may return an empty value. e.g. !newsletter_url is empty when called  
  * while building a confirmation email.  
  *  
  * @param $subscription  
  *  Subscription or user object  
  * @param $context  
  *  [newsletter] newsletter term object  
  *  [node] node object  
  * @param $language  
  *  Language object  
  *  
  * @return array of tokens and token values.  
2135   */   */
2136  function simplenews_mail_tokens($subscription, $context, $language) {  function simplenews_token_list($type = 'all') {
2137    // Build hash for (un)subscription confirmation link for subscribed user.    $tokens = array();
   $hash = '';  
   if (isset($subscription->snid) && isset($context['newsletter']->tid)) {  
     $hash = _simplenews_generate_hash($subscription->mail, $subscription->snid, $context['newsletter']->tid);  
   }  
2138    
2139    // Get newsletter name if not in newsletter object.    switch ($type) {
2140    $name = isset($context['newsletter']->name) ? $context['newsletter']->name : '';      case 'simplenews_subscription':
2141    if (!$name) {        $tokens['simplenews']['simplenews-subscribe-url']     = t('URL of the subscription confirmation page');
2142      if (isset($context['node']->simplenews['tid'])) {        $tokens['simplenews']['simplenews-unsubscribe-url']   = t('URL of the unsubscription page');
2143        if ($term = taxonomy_get_term($context['node']->simplenews['tid'])) {        $tokens['simplenews']['simplenews-receiver-mail']     = t('Email address of the newsletter receiver');
2144          // Translate newsletter name if required.        $tokens['simplenews']['simplenews-newsletters-name']  = t('The name of the newsletter series');
2145          $name = $term->name;        $tokens['simplenews']['simplenews-newsletters-url']   = t('URL of the taxonomy page listing the issues of this newsletter series');
2146          if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_LOCALIZE) {        break;
2147            $name = tt('taxonomy:term:'. $context['node']->simplenews['tid'] .':name', $name, $language->language);      case 'all':
2148        case 'simplenews':
2149          $tokens['simplenews']['simplenews-subscribe-url']     = t('URL of the subscription confirmation page.');
2150          $tokens['simplenews']['simplenews-unsubscribe-url']   = t('URL of the unsubscription page');
2151          $tokens['simplenews']['simplenews-receiver-name']     = t('Username of the newsletter receiver or anonymous user name.');
2152          $tokens['simplenews']['simplenews-receiver-mail']     = t('Email address of the newsletter receiver');
2153          $tokens['simplenews']['simplenews-newsletter-url']    = t('URL of this newsletter issue');
2154          $tokens['simplenews']['simplenews-newsletters-name']  = t('The name of the newsletter series');
2155          $tokens['simplenews']['simplenews-newsletters-url']   = t('URL of the taxonomy page listing the issues of this newsletter series');
2156          break;
2157      }
2158      return $tokens;
2159    }
2160    
2161    /**
2162     * Implementation of hook_token_value().
2163     */
2164    function simplenews_token_values($type, $object = NULL, $options = array()) {
2165      switch ($type) {
2166        case 'all':
2167        case 'simplenews':
2168          $account = $object['account'];
2169          $node = $object['node'];
2170    
2171          // Build tokens for 'simplenews' only.
2172          $values['simplenews-receiver-name']       = !empty($account->name) ? check_plain($account->name) : check_plain(variable_get('anonymous', 'Anonymous'));
2173          $values['simplenews-newsletter-url'] = url('node/'. $node->nid, array('absolute' => TRUE, 'language' => $language));
2174    
2175          // Intentionally fall through (no break).
2176    
2177        case 'simplenews_subscription':
2178          $account = $object['account'];
2179          $newsletter = $object['newsletter'];
2180          $language = isset($account->language->language) ? $account->language->language : language_default();
2181    
2182          // Build hash for subscription/unsubscription URL.
2183          $hash = '';
2184          if (isset($account->snid) && isset($newsletter->tid)) {
2185            $hash = _simplenews_generate_hash($account->mail, $account->snid, $newsletter->tid);
2186          }
2187    
2188          // When simplenews_token_values() is called from simplenews_nodeapi()
2189          // $newsletter->name contains no value. Newsletter name is
2190          // reconstructed from $node->simplenews['tid'].
2191          $newsletter_name = isset($newsletter->name) ? $newsletter->name : '';
2192          if (!$newsletter_name) {
2193            if (isset($node->simplenews['tid'])) {
2194              if ($term = taxonomy_get_term($node->simplenews['tid'])) {
2195                $newsletter_name = $term->name;
2196                // Translate newsletter name if required.
2197                if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary(variable_get('simplenews_vid', '')) == I18N_TAXONOMY_LOCALIZE) {
2198                  $newsletter_name = tt('taxonomy:term:'. $newsletter->tid .':name', $newsletter_name, $language);
2199                }
2200              }
2201          }          }
2202        }        }
     }  
   }  
2203    
2204    // Get tokens from user_mail_tokens() and add simplenews variables.        // Build tokens for both 'simplenews_subscription' and 'simplenews'.
2205    if($subscription->uid) {        $values['simplenews-subscribe-url']       = url('newsletter/confirm/add/'. $hash, array('absolute' => TRUE, 'language' => $language));
2206      $vars = user_mail_tokens($subscription, $language);        $values['simplenews-unsubscribe-url']     = url('newsletter/confirm/remove/'. $hash, array('absolute' => TRUE, 'language' => $language));
2207          $values['simplenews-receiver-mail']       = $account->mail;
2208          $values['simplenews-newsletters-name']    = $newsletter_name ? check_plain($newsletter_name) : '';
2209          $values['simplenews-newsletters-url']     = url('taxonomy/term/'. $newsletter->tid);
2210          break;
2211    }    }
2212    else {    return $values;
     $vars = array();  
     $vars['!site'] = variable_get('site_name', 'Drupal');  
     $vars['!mailto'] = $subscription->mail;  
   }  
   // Replace existing uri and uri_brief to cope with multilingual sites.  
   $vars['!uri'] = url('', array('absolute' => TRUE, 'language' => $language));  
   $vars['!uri_brief'] = preg_replace('!^https?://!', '', $vars['!uri']);  
   $vars['!confirm_subscribe_url'] = url('newsletter/confirm/add/'. $hash, array('absolute' => TRUE, 'language' => $language));  
   $vars['!confirm_unsubscribe_url'] = url('newsletter/confirm/remove/'. $hash, array('absolute' => TRUE, 'language' => $language));  
   $vars['!newsletter_url'] = isset($context['node']->nid) ? url('node/'. $context['node']->nid, array('absolute' => TRUE, 'language' => $language)) : '';  
   $vars['!newsletter_name'] = $name;  
   
   return $vars;  
2213  }  }
2214    
2215  /**  /**
# Line 2287  function _simplenews_format_options() { Line 2323  function _simplenews_format_options() {
2323   *   *
2324   * @param string $key text identification key   * @param string $key text identification key
2325   * @param object $language language object   * @param object $language language object
  * @param array $variables array of variables as used by t()  
2326   */   */
2327  function _simplenews_subscription_confirmation_text($key, $language = NULL, $variables = array()) {  function _simplenews_subscription_confirmation_text($key, $language = NULL) {
2328    $langcode = isset($language) ? $language->language : NULL;    $langcode = isset($language) ? $language->language : NULL;
2329    
2330    if ($admin_setting = variable_get('simplenews_confirm_'. $key, FALSE)) {    if ($admin_setting = variable_get('simplenews_confirm_'. $key, FALSE)) {
2331      // An admin setting overrides the default string.      // An admin setting overrides the default string.
2332      return t($admin_setting, $variables, $langcode);      return $admin_setting;
2333    }    }
2334    else {    else {
2335      // No override, return default string.      // No override, return default string.
2336      switch ($key) {      switch ($key) {
2337        case 'subscribe_unsubscribed':        case 'subscribe_unsubscribed':
2338          return t("We have received a request for subscription of !mailto to the !newsletter_name on !site website at !uri. To confirm this subscription please use the link below.\n\n!confirm_subscribe_url", $variables, $langcode);          return t("We have received a request from the email [simplenews-receiver-mail] for subscription to the [simplenews-newsletters-name] newsletter on [site-name] website at [site-url]. To confirm this subscription please use the link below.\n\n[simplenews-subscribe-url]", $langcode);
2339        case 'subscribe_subscribed':        case 'subscribe_subscribed':
2340          return t("We have received a request for subscription of !mailto to the !newsletter_name on !site website at !uri. However, this email is already subscribed to this newsletter. If you intended to unsubscribe please visit our site: !uri", $variables, $langcode);          return t("We have received a request from the email [simplenews-receiver-mail] for subscription to the [simplenews-newsletters-name] newsletter on [site-name] website at [site-url]. However, this email is already subscribed to this newsletter. If you intended to unsubscribe please visit our site: [site-url]", $langcode);
2341        case 'unsubscribe_subscribed':        case 'unsubscribe_subscribed':
2342          return t("We have received a request to unsubscribe !mailto from the !newsletter_name on !site website at !uri. To confirm this unsubscription please use the link below.\n\n!confirm_unsubscribe_url", $variables, $langcode);          return t("We have received a request from the email [simplenews-receiver-mail] to unsubscribe you from the [simplenews-newsletters-name] on [site-name] website at [site-url]. To confirm this unsubscription please use the  below.\n\n[simplenews-unsubscribe-url]", $langcode);
2343        case 'unsubscribe_unsubscribed':        case 'unsubscribe_unsubscribed':
2344          return t("We have received a request to unsubscribe !mailto from the !newsletter_name on !site website at !uri. However, this email is not subscribed to this newsletter. If you intended to subscribe please visit our site: !uri", $variables, $langcode);          return t("We have received a from the email [simplenews-receiver-mail] request to unsubscribe you from the [simplenews-newsletters-name] on [site-name] website at [site-url]. However, this email is not subscribed to this newsletter. If you intended to subscribe please visit our site: [site-url]", $langcode);
2345        case 'subscribe_subject':        case 'subscribe_subject':
2346          return t("Confirmation for !newsletter_name from !site", $variables, $langcode);          return t("Confirmation for [simplenews-newsletters-name] from [site-name]", $langcode);
2347      }      }
2348    }    }
2349  }  }

Legend:
Removed from v.1.214  
changed lines
  Added in v.1.215

  ViewVC Help
Powered by ViewVC 1.1.2