diff -u comment_notify.orig/comment_notify.install comment_notify/comment_notify.install --- comment_notify.orig/comment_notify.install 2009-01-03 23:15:41.000000000 +0530 +++ comment_notify/comment_notify.install 2009-01-30 17:15:15.000000000 +0530 @@ -92,4 +92,48 @@ db_query("UPDATE {permission} SET perm = '%s' WHERE pid = %d", $permissions, $row->pid); } return $ret; -} \ No newline at end of file +} +/** + * Adding one more field in table comment_notify to store the full path to + * commment with all its parents. + */ +function comment_notify_update_6002() { + $ret = array(); + db_add_field($ret, 'comment_notify', 'parents', array('type' => 'varchar', 'length' => 255)); + $number_rows_result = db_fetch_object(db_query("SELECT COUNT(*) AS num FROM {comments} c")); + + if ($number_rows_result->num != 0) { + $level = 1; + $if_condition = "IF (c". $level .".pid <> 0, CONCAT(c". $level .".pid, ','),'')"; + $table = "c". $level; + $join = NULL; + $original_concat_clause = ""; + $concat_clause = ""; + // First find the level of comments by recursively querying. + do { + if ($level != 1) { + $previous_level = $level - 1; + $join .= " JOIN {comments} c". $level ." ON c". $previous_level .".pid=c". $level .".cid"; + } + $concat_clause = "CONCAT('0,', ". $if_condition .")"; + $sql = "SELECT ". $concat_clause ." AS parents FROM {comments} c1 ". $join; + $result = db_fetch_array(db_query($sql)); + if ($result != NULL) { + $original_sql = $sql; + $original_concat_clause = $concat_clause; + } + $level++; + $table = "c" . $level; + $if_condition = " IF(c". $level .".pid <> 0, CONCAT(c". $level .".pid, ','),''),". $if_condition; + } while ($result != NULL); + + // In the while loop it was joined because we wanted the series + // of joins to finally return null record set but when we update + // we need to generated the complete set of records for the full depth. + $sql = str_replace('JOIN', 'LEFT JOIN', $original_sql); + // Replace the 'SELECT' command of the select query with an 'UPDATE' command. + $table_array = explode('FROM', $sql); + db_query("UPDATE ". $table_array[1] ." LEFT JOIN {comment_notify} cn ON c1.cid = cn.cid SET cn.parents = ". $original_concat_clause); + } + return $ret; +} diff -u comment_notify.orig/comment_notify.module comment_notify/comment_notify.module --- comment_notify.orig/comment_notify.module 2008-12-29 23:29:09.000000000 +0530 +++ comment_notify/comment_notify.module 2009-01-30 17:21:55.000000000 +0530 @@ -190,7 +190,6 @@ case 'disable': $key = $arg; db_query("UPDATE {comment_notify} SET notify = 0 WHERE notify_hash = '%s'", $arg); - drupal_set_message(t('Your comment follow-up notification for this post was disabled. Thanks.')); $title = t('Disabled comment follow-up notification feature for this post.'); break; @@ -227,17 +226,22 @@ db_query($sql, $comment['notify'], $comment['cid']); break; case 'insert': + // Get pid from comments table for current cid, get the parents for this cid. + $parent_result = db_fetch_object(db_query("SELECT pid FROM {comments} WHERE cid = %d", $comment['cid'])); + $parent = $parent_result->pid .','; + $parents_of_parent = db_fetch_object(db_query("SELECT parents FROM {comment_notify} WHERE cid = %d", $parent_result->pid)); + $parent_list = $parents_of_parent->parents .$parent; // If they subscribe and don't have a default let them know that it's possible to set one. if (empty($user->comment_notify_mailalert) && $comment['notify']) { drupal_set_message(t('You can change the default for this field in "Comment follow-up notification settings" on your account edit page.', array('!uri' => url('user/'. $user->uid .'/edit')))); } - // 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']); // And then save the data. - db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) values (%d, %d, '%s')", $comment['cid'], $comment['notify'], $notify_hash); + db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash, parents) VALUES (%d, %d, '%s', '%s')", $comment['cid'], $comment['notify'], $notify_hash, $parent_list); break; + case 'delete': db_query("DELETE FROM {comment_notify} WHERE cid = %d", $comment->cid); break; @@ -345,70 +349,73 @@ $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 - FROM {comments} c INNER JOIN {comment_notify} cn on c.cid = cn.cid LEFT OUTER JOIN {users} u ON c.uid = u.uid - 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) { - break; + // Get the parents of a particular comment. + $parent_result = db_fetch_object(db_query("SELECT parents FROM {comment_notify} cn LEFT JOIN {comments} c ON cn.cid = c.cid LEFT JOIN {users} u ON u.uid = c.uid WHERE cn.cid = %d", $cid)); + if ($parent_result->parents != NULL) { + $parents_arr = explode(',', $parent_result->parents); + $parents .= $parents_arr[0]; + for ($i = 1; $i < count($parents_arr) - 1; $i++) { + $parents .= ','; + $parents .= $parents_arr[$i]; } - if ($mail != $comment_mail && !in_array($mail, $sent_to) && $alert->uid != $comment->uid) { - $message = array(); - if (!empty($alert->uid)) { - $recipient_user = user_load(array('uid' => $alert->uid)); - $language = user_preferred_language($recipient_user); - } - else { - $language = language_default(); + // Select all comments that are in the parent path for the + // current comment or that have comment subscription set as subscribe to node. + $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 FROM {comments} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT OUTER JOIN {users} u ON c.uid = u.uid WHERE nid = %d AND c.status = 0 AND (u.status = 1 OR u.uid = 0) AND (cn.cid IN ('%s') OR cn.notify = 1)", $nid, $parents); + + while ($alert = db_fetch_object($result)) { + $umail = empty($alert->umail) ? $alert->uinit : $alert->umail; + $mail = empty($alert->cmail) ? $umail : $alert->cmail; + if ($alert->cid != $comment->pid && $mail !=$author->mail) { + if ($mail != $comment_mail && !in_array($mail, $sent_to) && $alert->uid != $comment->uid) { + $message = array(); + if (!empty($alert->uid)) { + $recipient_user = user_load(array('uid' => $alert->uid)); + $language = user_preferred_language($recipient_user); + } + else { + $language = language_default(); + } + $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( + '!commname' => $comment->name, + '!commtext' => $comment->comment, + '!commsubj' => $comment->subject, + '!comment_url' => url('node/'. $nid, array('absolute' => TRUE, 'fragment' => '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', 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[] = $mail; + + if ($alert->uid != 0) { + $watchdog_message = 'Notified: @user_mail'; + } + else { + $watchdog_message = 'Notified @user_mail'; + } + + // Add an entry to the watchdog log. + watchdog('comment_notify', $watchdog_message, array('!url' => url('user/'. $alert->uid .'/edit'), '@user_mail' => $mail), + WATCHDOG_NOTICE, l(t('source comment'), 'node/'. $nid, array('fragment' => 'comment-'. $alert->cid))); + + // revert to previous (site default) locale + $language = $initial_language; + } } - - $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( - '!commname' => $comment->name, - '!commtext' => $comment->comment, - '!commsubj' => $comment->subject, - '!comment_url' => url('node/'. $nid, array('absolute' => TRUE, 'fragment' => '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', 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[] = $mail; - - if ($alert->uid != 0) { - $watchdog_message = 'Notified: @user_mail'; - } - else { - $watchdog_message = 'Notified @user_mail'; - } - - watchdog('comment_notify', $watchdog_message, array('!url' => url('user/'. $alert->uid .'/edit'), '@user_mail' => $mail), - WATCHDOG_NOTICE, l(t('source comment'), 'node/'. $nid, array('fragment' => 'comment-'. $alert->cid))); - - // Add an entry to the watchdog log. - - // revert to previous (site default) locale - $language = $initial_language; } } }