diff --git a/comment_notify.inc b/comment_notify.inc index 123d788..09f8d4b 100644 --- a/comment_notify.inc +++ b/comment_notify.inc @@ -171,35 +171,6 @@ function comment_notify_update_notification($cid, $notify) { } /** - * Returns TRUE if the owner of this comment notification has already been notified. - * - * @param integer $cid - * @return boolean - */ -function comment_notify_is_notification_already_sent($cid) { - return (bool) db_select('comment_notify', 'cn') - ->fields('cn', array('cid')) - ->condition('cid', $cid) - ->condition('notified', 1) - ->execute() - ->fetchField(); -} - -/** - * Get the unique identification hash for a comment notification record. - * - * @param integer $cid - * @return string - */ -function comment_notify_get_notify_hash($cid) { - return db_select('comment_notify', 'cn') - ->fields('cn', array('notify_hash')) - ->condition('cid', $cid) - ->execute() - ->fetchField(); -} - -/** * Get the type of notification for a comment notification record. * * @param integer $cid @@ -214,46 +185,18 @@ function comment_notify_get_notification_type($cid) { } /** - * Returns the thread which a comment belongs to. - * - * @param integer $cid - * @return string - * The thread identifier. - */ -function comment_notify_get_thread($cid) { - return db_select('comment', 'c') - ->fields('c', array('thread')) - ->condition('cid', $cid) - ->execute() - ->fetchField(); -} - -/** * Get a list of mails which need to be contacted for a node. * * @param integer $nid * @return QueryStatement */ function comment_notify_get_watchers($nid) { - $query = db_select('comment', 'c'); - $query->join('comment_notify', 'cn', 'c.cid = cn.cid'); - $query->leftJoin('users', 'u', 'c.uid = u.uid'); - $query - ->condition('nid', $nid) - ->condition('notify', COMMENT_NOTIFY_DISABLED, '<>') - ->condition('c.status', COMMENT_PUBLISHED) - ->condition( - db_or() - ->condition('u.uid', 0) - ->condition('u.status', 1) - ) - ->fields('c', array('cid', 'nid', 'uid', 'name', 'thread')) - ->fields('cn', array('notify', 'notify_hash')); - $query->addField('c', 'mail', 'cmail'); - $query->addField('u', 'init', 'uinit'); - $query->addField('u', 'mail', 'umail'); - - return $query->execute(); + $cids = db_query("SELECT c.cid FROM {comment} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT JOIN {users} u ON c.uid = u.uid WHERE c.nid = :nid AND c.status = :status AND cn.notify <> :notify AND (u.uid = 0 OR u.status = 1)", array( + ':nid' => $nid, + ':status' => COMMENT_PUBLISHED, + ':notify' => COMMENT_NOTIFY_DISABLED, + ))->fetchCol(); + return comment_load_multiple($cids); } /** diff --git a/comment_notify.module b/comment_notify.module index 2df39a6..f55e9bc 100644 --- a/comment_notify.module +++ b/comment_notify.module @@ -392,6 +392,30 @@ function comment_notify_user_cancel(&$edit, $account, $method) { } /** + * Implements hook_comment_load(). + */ +function comment_notify_comment_load($comments) { + $query = db_select('comment_notify', 'cn'); + $query->join('comment', 'c', 'c.cid = cn.cid'); + $query->leftJoin('users', 'u', 'c.uid = u.uid'); + $query->condition('c.cid', array_keys($comments)); + $query->fields('cn', array('cid', 'notify', 'notify_hash', 'notified')); + $query->addField('c', 'mail', 'cmail'); + $query->addField('u', 'init', 'uinit'); + $query->addField('u', 'mail', 'umail'); + + $records = $query->execute()->fetchAllAssoc('cid'); + foreach ($records as $cid => $record) { + $comments[$cid]->notify = $record->notify; + $comments[$cid]->notify_hash = $record->notify_hash; + $comments[$cid]->notified = $record->notified; + $comments[$cid]->cmail = $record->cmail; + $comments[$cid]->uinit = $record->uinit; + $comments[$cid]->umail = $record->umail; + } +} + +/** * Private function to send the notifications. * * @param $comment @@ -412,7 +436,7 @@ function _comment_notify_mailalert($comment) { // 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 (comment_notify_is_notification_already_sent($cid)) { + if (!empty($comment->notified)) { return; } @@ -452,7 +476,7 @@ function _comment_notify_mailalert($comment) { } // For "reply to my comments" notifications, figure out what thread this is. - $thread = comment_notify_get_thread($cid); + $thread = $comment->thread; // Get the list of commenters to notify. $watchers = comment_notify_get_watchers($nid); @@ -685,13 +709,11 @@ function comment_notify_settings_validate($form, &$form_state) { * @return * A string with the internal path to the unsubscribe link, ready to be * passed to the url() function. - * - * @see comment_notify_get_notify_hash(). */ function comment_notify_get_unsubscribe_url($comment) { module_load_include('inc', 'comment_notify', 'comment_notify'); - if ($hash = comment_notify_get_notify_hash($comment->cid)) { - return 'comment_notify/disable/' . $hash; + if (!empty($comment->notify_hash)) { + return 'comment_notify/disable/' . $comment->notify_hash; } } /** @@ -704,4 +726,4 @@ function comment_notify_field_extra_fields() { 'weight' => 4, ); return $extras; -} \ No newline at end of file +}