You are here

function _comment_notify_mailalert in Comment Notify 6

Same name and namespace in other branches
  1. 8 comment_notify.module \_comment_notify_mailalert()
  2. 5.2 comment_notify.module \_comment_notify_mailalert()
  3. 5 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 365
This module provides comment follow-up e-mail notification for anonymous and registered users.

Code

function _comment_notify_mailalert($comment) {
  $comment = (object) $comment;
  global $language;
  global $base_url;
  global $user;
  $nid = $comment->nid;
  $cid = $comment->cid;

  // Check to see if a notification has already been sent for this
  // comment so that edits to a comment don't trigger an additional
  // notification.
  if (db_result(db_query('SELECT cid from {comment_notify} WHERE cid = %d AND notified = %d', $cid, 1))) {
    return;
  }
  $initial_language = $language;
  if (function_exists('locale')) {
    $languages = locale_language_list();
    $languages = $languages['name'];
  }
  $node = node_load($nid);

  // Render up the node and comment.
  $node_teaser = node_view($node, TRUE, TRUE, FALSE);
  $node_body = node_view($node, FALSE, TRUE, FALSE);
  $comment_text = check_markup($comment->comment, $comment->format);

  // No mails if this is not an enabled content type.
  $enabled_types = variable_get('comment_notify_node_types', array(
    $node->type => TRUE,
  ));
  if (empty($enabled_types[$node->type])) {
    return;
  }
  if (!isset($comment->mail)) {
    $comment_account = user_load(array(
      'name' => $comment->name,
    ));
    $comment_mail = $comment_account->mail;
  }
  else {
    $comment_mail = $comment->mail;
  }
  $sent_to = array();

  // Send to a subscribed author if they are not the current commenter
  $author = user_load(array(
    'uid' => $node->uid,
  ));
  if (!empty($author->node_notify_mailalert) && $author->node_notify_mailalert == 1 && $user->uid != $author->uid && node_access('view', $node, $author)) {

    // Get the author's language.
    $language = user_preferred_language($author);
    $message['subject'] = t('!site :: new comment for your post.', array(
      '!site' => variable_get('site_name', 'drupal'),
    ));
    $message['body'] = t(variable_get('node_notify_default_mailtext', AUTHOR_MAILTEXT), array(
      '!cid' => $cid,
      '!commname' => $comment->name,
      '!commtext' => drupal_html_to_text($comment_text),
      '!commsubj' => $comment->subject,
      '!comment_url' => url('node/' . $nid, array(
        'absolute' => TRUE,
        'fragment' => 'comment-' . $cid,
      )),
      '!node_title' => $node->title,
      '!node_teaser' => drupal_html_to_text($node_teaser),
      '!mission' => variable_get('site_mission', ''),
      '!nid' => $nid,
      '!node_body' => drupal_html_to_text($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', array(
        'absolute' => TRUE,
      )),
      '!edit_uri' => url('user/' . $author->uid . '/edit', array(
        'absolute' => TRUE,
      )),
    ));
    drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message);
    $sent_to[] = strtolower($author->mail);
  }

  // For "reply to my comments" notifications, figure out what thread this is.
  $thread = db_result(db_query("SELECT thread FROM {comments} WHERE cid = %d", $cid));

  //Get the list of commenters to notify
  $result = db_query(db_rewrite_sql("SELECT c.cid, c.nid, c.uid, c.name, c.mail AS cmail, u.mail AS umail, u.init AS uinit, c.uid, c.name, cn.notify, cn.notify_hash, c.thread\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 c.nid = %d AND cn.notify > 0 AND c.status = 0 AND (u.status = 1 OR u.uid = 0)", 'c', 'cid'), $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;
    $relevant_thread = substr($thread, 0, drupal_strlen($alert->thread) - 1);
    if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->thread) != 0) {
      continue;
    }
    if ($mail != $comment_mail && !in_array(strtolower($mail), $sent_to) && ($alert->uid != $comment->uid || $alert->uid == 0)) {
      $message = array();
      if (!empty($alert->uid)) {
        $recipient_user = user_load(array(
          'uid' => $alert->uid,
        ));
        $language = user_preferred_language($recipient_user);
      }
      else {
        $language = language_default();
        $recipient_user = drupal_anonymous_user();
      }

      // Make sure they have access to this node before showing a bunch of node information.
      if (!node_access('view', $node, $recipient_user)) {
        continue;
      }
      $message['subject'] = t('!site :: new comment for your post.', array(
        '!site' => variable_get('site_name', 'drupal'),
      ));
      $message['body'] = t(variable_get('comment_notify_default_mailtext', DEFAULT_MAILTEXT), array(
        '!cid' => $cid,
        '!commname' => $comment->name,
        '!commtext' => drupal_html_to_text($comment_text),
        '!commsubj' => $comment->subject,
        '!comment_url' => url('node/' . $nid, array(
          'absolute' => TRUE,
          'fragment' => 'comment-' . $cid,
        )),
        '!node_title' => $node->title,
        '!node_teaser' => drupal_html_to_text($node_teaser),
        '!mission' => variable_get('site_mission', ''),
        '!nid' => $nid,
        '!node_body' => drupal_html_to_text($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', array(
          'absolute' => TRUE,
        )),
        '!edit_uri' => url('user/' . $alert->uid . '/edit', array(
          'absolute' => TRUE,
        )),
        '!link1' => url('comment_notify/disable/' . $alert->notify_hash, array(
          'absolute' => TRUE,
        )),
      ));
      drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
      $sent_to[] = strtolower($mail);

      // Make the mail link to user's /edit, unless it's an anonymous user.
      if ($alert->uid != 0) {
        $user_mail = l($mail, 'user/' . $alert->uid . '/edit');
      }
      else {
        $user_mail = check_plain($mail);
      }

      // Add an entry to the watchdog log.
      watchdog('comment_notify', 'Notified: !user_mail', array(
        '!user_mail' => $user_mail,
      ), WATCHDOG_NOTICE, l(t('source comment'), 'node/' . $nid, array(
        'fragment' => 'comment-' . $alert->cid,
      )));

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

  // Record that a notification was sent for this comment so that
  // notifications aren't sent again if the comment is later edited.
  db_query('UPDATE {comment_notify} SET notified = 1 WHERE cid = %d', $cid);
}