diff --git notifications_content/notifications_content.module notifications_content/notifications_content.module index 69e1865..ae450c5 100644 --- notifications_content/notifications_content.module +++ notifications_content/notifications_content.module @@ -810,7 +810,8 @@ function notifications_content_token_values($type, $object = NULL, $options = ar break; case 'comment': if ($comment = (object)$object) { - $values['comment-url'] = url('node/'. $comment->nid, array('fragment' =>'comment-'. $comment->cid, 'absolute' => TRUE)); + $page = notifications_comment_page($comment->nid); + $values['comment-url'] = url('node/'. $comment->nid, array('fragment' =>'comment-'. $comment->cid, 'absolute' => TRUE, 'query' => $page)); $values['comment-reply-url'] = url('comment/reply/'. $comment->nid .'/'. $comment->cid, array('absolute' => TRUE)); return $values; } @@ -855,3 +856,65 @@ function notifications_content_comment_allow($account, $comment) { } return $access[$account->uid][$comment->cid]; } + +/** + * Determines the page the comment it on + * + * @see comment_new_page_count + * + * @param $nid + * the nid that the page query is to be determined for + * + * @return string + * "page=X" if the page number is greater than zero; empty string otherwise. + */ +function notifications_comment_page($nid) { + static $types = array(); + if (!isset($types[$nid])) { + $types[$nid] = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $nid)); + } + // we get the comment settings + // @see _comment_get_display_setting + $type = $types[$nid]; + $pagenum = NULL; + $new_replies = 1; + $num_comments = db_result(db_query("SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d", $nid)) + 1; + if ($type) { + $comments_per_page = variable_get('comment_default_per_page_' . $type, 50); + $mode = variable_get('comment_default_mode_' . $type, COMMENT_MODE_THREADED_EXPANDED); + $order = variable_get('comment_default_order_' . $type, COMMENT_ORDER_NEWEST_FIRST); + $flat = in_array($mode, array(COMMENT_MODE_FLAT_COLLAPSED, COMMENT_MODE_FLAT_EXPANDED)); + if ($num_comments <= $comments_per_page || ($flat && $order == COMMENT_ORDER_NEWEST_FIRST)) { + // Only one page of comments or flat forum and newest first. + // First new comment will always be on first page. + $pageno = 0; + } + else { + if ($flat) { + // Flat comments and oldest first. + $count = $num_comments - $new_replies; + } + else { + // Threaded comments. See the documentation for comment_render(). + if ($order == COMMENT_ORDER_NEWEST_FIRST) { + // Newest first: find the last thread with new comment + $result = db_query('(SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY thread DESC LIMIT 1', $nid, $new_replies); + $thread = db_result($result); + $result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND thread > '". $thread ."'", $nid); + } + else { + // Oldest first: find the first thread with new comment + $result = db_query('(SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1)) LIMIT 1', $nid, $new_replies); + $thread = substr(db_result($result), 0, -1); + $result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < '". $thread ."'", $nid); + } + $count = db_result($result_count); + } + $pageno = $count / $comments_per_page; + } + if ($pageno >= 1) { + $pagenum = "page=". intval($pageno); + } + } + return $pagenum; +} \ No newline at end of file