| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | /** |
|---|
| 5 | * Implementation of hook_menu(). |
|---|
| 6 | */ |
|---|
| 7 | function recentcomments_menu($may_cache) { |
|---|
| 8 | $items = array(); |
|---|
| 9 | |
|---|
| 10 | if ($may_cache) { |
|---|
| 11 | $items[] = array('path' => 'recentcomments', 'title' => t('Recent Comments'), |
|---|
| 12 | 'callback' => 'recentcomments_page', |
|---|
| 13 | 'access' => user_access('access comments'), |
|---|
| 14 | 'type' => MENU_SUGGESTED_ITEM); |
|---|
| 15 | |
|---|
| 16 | } |
|---|
| 17 | return $items; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | function recentcomments_page() { |
|---|
| 21 | $result = pager_query(db_rewrite_sql('SELECT c.*, n.title FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE n.status = 1 AND c.status = 0 ORDER BY c.timestamp DESC', 'c'), variable_get('default_morecomments', 10)); |
|---|
| 22 | $output = ''; |
|---|
| 23 | while ($comment = db_fetch_object($result)) { |
|---|
| 24 | $output .= theme('recentcomments', $comment, ''); |
|---|
| 25 | } |
|---|
| 26 | $output .= theme('pager', NULL, variable_get('default_morecomments', 10)); |
|---|
| 27 | return $output; //theme('page', $output); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Implementation of hook_block(). |
|---|
| 32 | * |
|---|
| 33 | * Generates a block with the most recent comments. |
|---|
| 34 | */ |
|---|
| 35 | function recentcomments_block($op = 'list', $delta = 0) { |
|---|
| 36 | if ($op == 'list') { |
|---|
| 37 | $blocks[0]['info'] = t('Recent Comments'); |
|---|
| 38 | return $blocks; |
|---|
| 39 | } |
|---|
| 40 | else if ($op == 'view' && user_access('access comments')) { |
|---|
| 41 | $block['subject'] = t('Recent Comments'); |
|---|
| 42 | $block['content'] = theme('recentcomments_block'); |
|---|
| 43 | return $block; |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Find a number of recent comments. This is done in two steps. |
|---|
| 49 | * 1. Find the n (specified by $number) nodes that have the most recent |
|---|
| 50 | * comments. This is done by querying node_comment_statistics which has |
|---|
| 51 | * an index on last_comment_timestamp, and is thus a fast query. |
|---|
| 52 | * 2. Loading the information from the comments table based on the nids found |
|---|
| 53 | * in step 1. |
|---|
| 54 | * |
|---|
| 55 | * @param $number (optional) The maximum number of comments to find. |
|---|
| 56 | * @return $comments An array of comment objects each containing a nid, |
|---|
| 57 | * subject, cid, and timstamp, or an empty array if there are no recent |
|---|
| 58 | * comments visible to the current user. |
|---|
| 59 | */ |
|---|
| 60 | function recentcomments_get_recent($number = 10) { |
|---|
| 61 | // Select the $number nodes (visible to the current user) with the most |
|---|
| 62 | // recent comments. This is efficient due to the index on |
|---|
| 63 | // last_comment_timestamp. |
|---|
| 64 | $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node_comment_statistics} n WHERE n.comment_count > 0 ORDER BY n.last_comment_timestamp DESC"), 0, $number); |
|---|
| 65 | |
|---|
| 66 | $nids = array(); |
|---|
| 67 | while ($row = db_fetch_object($result)) { |
|---|
| 68 | $nids[] = $row->nid; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | $comments = array(); |
|---|
| 72 | if (!empty($nids)) { |
|---|
| 73 | // From among the comments on the nodes selected in the first query, |
|---|
| 74 | // find the $number most recent comments. |
|---|
| 75 | $result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp, n.title, u.name FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid IN ('. implode(',', $nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC', COMMENT_PUBLISHED, 0, $number); |
|---|
| 76 | while ($comment = db_fetch_object($result)) { |
|---|
| 77 | $comments[] = $comment; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return $comments; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Returns a formatted list of recent comments to be displayed in the comment |
|---|
| 86 | * block. |
|---|
| 87 | * |
|---|
| 88 | * @ingroup themeable |
|---|
| 89 | */ |
|---|
| 90 | function theme_recentcomments_block() { |
|---|
| 91 | //$items = array(); |
|---|
| 92 | $return = ""; |
|---|
| 93 | foreach (recentcomments_get_recent() as $comment) { |
|---|
| 94 | $return .= '<div class="recentcomments" style=\'padding-top:0.5em;border-bottom:1px solid #D7D7D7;\'>'.$comment->name.': '.l($comment->subject, 'node/'. $comment->nid, NULL, NULL, 'comment-'. $comment->cid) .' in '.l($comment->title,"node/$comment->nid")." ".t('<em style=\'display:block;\'>@time ago</em>', array('@time' => format_interval(time() - $comment->timestamp))).'</div>'; |
|---|
| 95 | } |
|---|
| 96 | return $return.'<div class="more-link">'. l(t('more'), 'recentcomments', array('title' => t('Read the latest comments.'))) .'</div>'; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | function theme_recentcomments($comment, $links = 0) { |
|---|
| 100 | $output = "<div class=\"comment\">\n"; |
|---|
| 101 | $output .= '<div class="node"><h2 class="title">'. l($comment->subject, "node/$comment->nid", NULL, NULL, "comment-$comment->cid") . ' ' . theme('mark', $comment->new) ." in ".l($comment->title,"node/$comment->nid")."</h2></div>\n"; |
|---|
| 102 | $output .= '<div class="moderation">'. $comment->moderation ."</div>\n"; |
|---|
| 103 | $output .= '<div class="credit">'. t('by !a on %b', array('!a' => theme('username', $comment), '%b' => format_date($comment->timestamp))) ."</div>\n"; |
|---|
| 104 | $output .= "<div class=\"body\">$comment->comment</div>\n"; |
|---|
| 105 | $output .= "<div class=\"links\">$links</div>\n"; |
|---|
| 106 | $output .= "</div>\n"; |
|---|
| 107 | return $output; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | ?> |
|---|