You are here

function subscriptions_mailvars in Subscriptions 5

4 calls to subscriptions_mailvars()
subscriptions_comment in ./subscriptions.module
Implementation of hook_comment().
subscriptions_heldcomments in ./subscriptions.module
handling for held comments
subscriptions_heldnodes in ./subscriptions.module
handling for held nodes
subscriptions_nodeapi in ./subscriptions.module
Implementation of hook_nodeapi().

File

./subscriptions.module, line 522

Code

function subscriptions_mailvars($sid, $ssid, $uid, $stype, $strsent) {
  global $user, $locale;
  $initial_user = $user;
  $initial_locale = $locale;
  if (function_exists('locale')) {
    $languages = locale_supported_languages();
    $languages = $languages['name'];
  }

  // if comment insertion, get vars
  if ($stype == 'node') {
    $result = db_query('SELECT title FROM {node} WHERE nid = %d', $sid);
    $subject = db_result($result);
    $result = db_query('SELECT u.uid, u.name, u.mail, u.language FROM {users} u INNER JOIN {subscriptions} s ON u.uid = s.uid WHERE u.status= 1 AND s.sid = %d AND s.stype = \'node\'', $sid);
    $strtype = 'thread';
    $nid = $sid;
    $cid = $ssid;
    $page = subscriptions_comment_page($cid, $nid);
    if ($page) {
      $page = "page={$page}";
    }
  }

  // if content type, get vars
  if ($stype == 'type') {
    $typestr = 'type' . $sid;
    $result = db_query('SELECT u.mail, u.name, u.uid, u.language FROM {users} u INNER JOIN {subscriptions} s ON u.uid = s.uid WHERE u.status= 1 AND s.stype =\'' . $typestr . '\'');
    $strtype = 'content type';
    $nid = $ssid;
  }

  // if node insert, test if node has a taxonomy else skip
  if ($stype == 'taxa' && !is_null($sid)) {
    $result = db_query('SELECT name FROM {term_data} WHERE tid = %d', $sid);
    $subject = db_result($result);
    $result = db_query('SELECT u.mail, u.name, u.uid, u.language FROM {users} u INNER JOIN {subscriptions} s ON u.uid = s.uid WHERE u.status= 1 AND s.sid = %d AND stype = \'taxa\'', $sid);
    $strtype = 'category';
    $nid = $ssid;
  }

  // if blog insert, get vars
  if ($stype == 'blog') {
    $result = db_query('SELECT name FROM {users} WHERE uid = %d', $uid);
    $subject = t('new blog for ') . db_result($result);
    $result = db_query('SELECT u.uid, u.name, u.mail, u.language FROM {users} u INNER JOIN {subscriptions} s ON u.uid = s.uid WHERE u.status= 1 AND s.sid = %d AND s.stype = \'blog\'', $sid);
    $strtype = 'blog';
    $nid = $ssid;
  }
  if (empty($nid)) {
    watchdog('subscriptions', "Unable to process: {$stype}, {$sid}, {$ssid}, {$uid}", WATCHDOG_WARNING);
    return;
  }
  $nobj = node_load($nid);

  // loop through subscribers and call mail function
  while ($subscriptions = db_fetch_object($result)) {
    $subscription_user = user_load(array(
      'uid' => $subscriptions->uid,
    ));

    // determine if posters should be notified of their own posts
    if (variable_get('subscriptions_sendself', 0)) {
      $selftest = TRUE;
    }
    else {
      $selftest = $subscription_user->uid != $uid;
    }

    // determine if target reciever has access to the node
    $user = $subscription_user;
    $nodeaccess = node_access('view', $nobj);
    $user = $initial_user;

    // set teaser variable
    if ($subscription_user->subscriptions_teaser) {
      $teaser = is_null($cid) ? $nobj->teaser : db_result(db_query('SELECT comment FROM {comments} WHERE cid = ' . $cid));
    }
    else {
      $teaser = '';
    }
    if ($selftest && $nodeaccess && !is_null($sid) && strpos($strsent, '!' . $subscriptions->uid . '!') === FALSE) {

      // add this user to "previously notified" string
      $strsent .= $subscriptions->uid . '!';

      // translate the message using the reciever's language
      if (function_exists('locale') && $languages[$subscriptions->language]) {
        $locale = $subscriptions->language;
      }

      // @ TODO update these to use more generic variables
      $from = variable_get('site_mail', ini_get('sendmail_from'));
      $body = theme('subscriptions_mail_item_body', $subscription_user, $from, $strtype, $nobj, $cid, $page, $teaser);
      $headers = theme('subscriptions_mail_item_headers', $subscription_user, $from, $strtype, $nobj, $cid, $page);
      $mail_subject = theme('subscriptions_mail_item_subject', $subscription_user, $from, $strtype, $nobj, $cid, $page, $subject);

      // revert to original locale
      $locale = $initial_locale;
      subscriptions_sendmail($subscriptions->name, $subscriptions->mail, $mail_subject, $body, $from, $headers);
    }
  }
  return $strsent;
}