You are here

function _comment_notify_mailalert in Comment Notify 5.2

Same name and namespace in other branches
  1. 8 comment_notify.module \_comment_notify_mailalert()
  2. 5 comment_notify.module \_comment_notify_mailalert()
  3. 6 comment_notify.module \_comment_notify_mailalert()
  4. 7 comment_notify.module \_comment_notify_mailalert()

Private function to send the notifications.

Parameters

$comment: The comment array as found in hook_comment $op = publish.

1 call to _comment_notify_mailalert()
comment_notify_comment in ./comment_notify.module
Implementation of hook_comment().

File

./comment_notify.module, line 346
This module provides comment follow-up e-mail notification for anonymous and registered users.

Code

function _comment_notify_mailalert($comment) {
  $comment = (object) $comment;
  global $locale;
  global $base_url;
  global $user;
  $initial_locale = $locale;
  if (function_exists('locale')) {
    $languages = locale_supported_languages();
    $languages = $languages['name'];
  }
  $nid = $comment->nid;
  $cid = $comment->cid;
  $node = node_load($nid);
  if (!isset($comment->mail)) {
    $comment_account = user_load(array(
      'name' => $comment->name,
    ));
    $comment_mail = $comment_account->mail;
  }
  else {
    $comment_mail = $comment->mail;
  }
  $from = variable_get('site_mail', ini_get('sendmail_from'));
  $sent_to = array();
  $subject = t('!site :: new comment for your post.', array(
    '!site' => variable_get('site_name', 'drupal'),
  ));

  // Send to a subscribed author if they are not the current commenter
  $author = user_load(array(
    'uid' => $node->uid,
  ));
  if ($author->node_notify_mailalert == 1 && $user->mail != $author->mail) {
    $message = t(variable_get('node_notify_default_mailtext', AUTHOR_MAILTEXT), array(
      '!commname' => $comment->name,
      '!commtext' => $comment->comment,
      '!commsubj' => $comment->subject,
      '!comment_url' => url('node/' . $nid, NULL, NULL, 1) . '#comment-' . $cid,
      '!node_title' => $node->title,
      '!node_teaser' => $node->teaser,
      '!mission' => variable_get('site_mission', ''),
      '!node_body' => $node->body,
      '!name' => $author->name,
      '!site' => variable_get('site_name', 'drupal'),
      '!uri' => $base_url,
      '!uri_brief' => preg_replace('!^https?://!', '', $base_url),
      '!date' => format_date(time()),
      '!login_uri' => url('user', NULL, NULL, 1),
      '!edit_uri' => url('user/' . $alert->uid . '/edit', NULL, NULL, 1),
    ));
    drupal_mail('node_notify_mail', $author->mail, $subject, $message, $from, array());
    $sent_to[] = $author->mail;
  }

  //Get the list of commenters to notify
  $result = db_query("SELECT DISTINCT c.cid, c.uid, c.name, c.nid, c.mail AS cmail, u.mail AS umail, u.init AS uinit, c.uid, c.name, cn.notify, cn.notify_hash\n    FROM {comments} c INNER JOIN {comment_notify} cn on c.cid = cn.cid LEFT OUTER JOIN {users} u ON c.uid = u.uid\n    WHERE nid = %d  AND cn.notify > 0 AND c.status = 0 AND (u.status = 1 OR u.uid = 0)", $nid);

  // TODO? the original big query had stuff making sure the mail was populated and contained .+@.+ Perhaps check for that here and set notify = 0 if that is the case for this cid
  while ($alert = db_fetch_object($result)) {
    $umail = empty($alert->umail) ? $alert->uinit : $alert->umail;
    $mail = empty($alert->cmail) ? $umail : $alert->cmail;
    if ($alert->notify == COMMENT_NOTIFY_COMMENT && $alert->cid != $comment->pid) {
      continue;
    }
    if ($mail != $comment_mail && !in_array($mail, $sent_to) && $alert->uid != $comment->uid) {
      if (function_exists('locale') && $languages[$user->language]) {
        $locale = $user->language;
      }
      $message = t(variable_get('comment_notify_default_mailtext', DEFAULT_MAILTEXT), array(
        '!commname' => $comment->name,
        '!commtext' => $comment->comment,
        '!commsubj' => $comment->subject,
        '!comment_url' => url('node/' . $nid, NULL, NULL, 1) . '#comment-' . $cid,
        '!node_title' => $node->title,
        '!node_teaser' => $node->teaser,
        '!mission' => variable_get('site_mission', ''),
        '!node_body' => $node->body,
        '!name' => $alert->name,
        '!site' => variable_get('site_name', 'drupal'),
        '!uri' => $base_url,
        '!uri_brief' => preg_replace('!^https?://!', '', $base_url),
        '!date' => format_date(time()),
        '!login_uri' => url('user', NULL, NULL, 1),
        '!edit_uri' => url('user/' . $alert->uid . '/edit', NULL, NULL, 1),
        '!link1' => url('comment_notify/disable/' . $alert->notify_hash, NULL, NULL, 1),
      ));
      drupal_mail('comment_notify_mail', $mail, $subject, $message, $from, array());
      $sent_to[] = $mail;
      if ($alert->uid != 0) {
        $watchdog_message = t('Notified: <a href="!url">@user_mail</a>', array(
          '!url' => url('user/' . $alert->uid . '/edit'),
          '@user_mail' => $mail,
        ));
      }
      else {
        $watchdog_message = t('Notified @user_mail', array(
          '@user_mail' => $mail,
        ));
      }

      // Add an entry to the watchdog log.
      watchdog('comment_notify', $watchdog_message, WATCHDOG_NOTICE, l(t('source comment'), 'node/' . $nid, NULL, NULL, 'comment-' . $alert->cid));

      // revert to previous (site default) locale
      $locale = $initial_locale;
    }
  }
}