You are here

function comment_notify_comment in Comment Notify 6

Same name and namespace in other branches
  1. 5.2 comment_notify.module \comment_notify_comment()
  2. 5 comment_notify.module \comment_notify_comment()

Implementation of hook_comment().

File

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

Code

function comment_notify_comment($comment, $op) {
  global $user;

  // In theory, the update or insert operations are duplicates with publish which
  // would lead to duplicate messages.  _comment_notify_mailalert() protects against that.
  switch ($op) {
    case 'validate':

      // We assume that if they are non-anonymous then they have a valid mail.
      // For anonymous users, though, we verify that they entered a mail and let comment.module validate it is real.
      if (!$user->uid && $comment['notify'] && empty($comment['mail'])) {
        form_set_error('mail', t('If you want to subscribe to comments you must supply a valid e-mail address.'));
      }
      break;
    case 'publish':

      // And send notifications - the real purpose of the module.
      _comment_notify_mailalert($comment);
      break;
    case 'update':

      // In case they have changed their status, save it in the database.
      $sql = 'UPDATE {comment_notify} SET notify = %d WHERE cid = %d';
      if ($comment['notify']) {
        db_query($sql, $comment['notify_type'], $comment['cid']);
      }
      else {
        db_query($sql, 0, $comment['cid']);
      }

      // And send notifications - the real purpose of the module.
      if ($comment['status'] == COMMENT_PUBLISHED) {
        _comment_notify_mailalert($comment);
      }
      break;
    case 'insert':

      // For new comments, we first build up a string to be used as the identifier for the alert
      $mail = empty($comment['mail']) ? $user->mail : $comment['mail'];
      $notify_hash = drupal_get_token($mail . $comment['cid']);
      if ($comment['notify']) {
        $notify = $comment['notify_type'];

        // If they don't have a preference, save one.
        $current = db_result(db_query("SELECT count(1) from {comment_notify_user_settings} WHERE uid = %d", $user->uid));
        if ($current == 0 && $user->uid) {
          db_query("INSERT INTO {comment_notify_user_settings} (uid, comment_notify) VALUES (%d, %d)", $user->uid, $comment['notify_type']);
        }
      }
      else {
        $notify = $comment['notify'];
      }

      // And then save the data.
      db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash, notified) values (%d, %d, '%s', 0)", $comment['cid'], $notify, $notify_hash);

      // And send notifications - the real purpose of the module.
      if ($comment['status'] == COMMENT_PUBLISHED) {
        _comment_notify_mailalert($comment);
      }
      break;
    case 'delete':
      db_query("DELETE FROM {comment_notify} WHERE cid = %d", $comment->cid);
      break;
  }
}