/[drupal]/contributions/modules/comment_page/comment_page.module
ViewVC logotype

Contents of /contributions/modules/comment_page/comment_page.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Fri Jun 13 20:10:52 2008 UTC (17 months, 1 week ago) by rszrama
Branch: MAIN
CVS Tags: DRUPAL-5--1-1, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.1: +17 -86 lines
File MIME type: text/x-php
Fixing issues w/ node access and adding a "soft" dependency on Pathauto; updating readme to reflect the latter.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Displays comments as individual pages that can be bookmarked and indexed by
7 * search engines.
8 *
9 * Development sponsored by the Ubercart project. http://www.ubercart.org
10 */
11
12 /**
13 * Implementation of hook_menu().
14 */
15 function comment_page_menu($may_cache) {
16 if ($may_cache) {
17 $items[] = array(
18 'path' => 'admin/content/comment_page',
19 'title' => t('Comment page'),
20 'description' => t('Adjust the settings for the comment page module.'),
21 'callback' => 'drupal_get_form',
22 'callback arguments' => array('comment_page_settings'),
23 'access' => user_access('administer comments'),
24 'type' => MENU_NORMAL_ITEM,
25 );
26 }
27 else {
28 if (arg(0) == 'comment' && intval(arg(1)) > 0) {
29 drupal_add_css(drupal_get_path('module', 'comment_page') .'/comment_page.css');
30
31 $items[] = array(
32 'path' => 'comment/'. arg(1),
33 'title' => t('Viewing a comment'),
34 'description' => t('View this comment.'),
35 'callback' => 'comment_page_display',
36 'callback arguments' => array(arg(1)),
37 'access' => user_access('access comments'),
38 'type' => MENU_CALLBACK,
39 );
40 }
41 }
42
43 return $items;
44 }
45
46 /**
47 * Implementation of hook_comment().
48 */
49 function comment_page_comment($arg1, $op) {
50 switch($op) {
51 case 'insert':
52 if (variable_get('default_comment_subject_on', TRUE)) {
53 $comment = (object) $arg1;
54 if (substr($comment->comment, 0, strlen($comment->subject)) == $comment->subject) {
55 $title = variable_get('default_comment_subject_prefix', t('Re:')) .' ';
56 if ($comment->pid == 0) {
57 $title .= db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $comment->nid));
58 }
59 else {
60 $title .= db_result(db_query("SELECT subject FROM {comments} WHERE cid = %d", $comment->pid));
61 }
62 db_query("UPDATE {comments} SET subject = '%s' WHERE cid = %d", $title, $comment->cid);
63 }
64 }
65 break;
66 case 'view':
67 $arg1->page_url = comment_page_url($arg1->cid, $arg1->subject);
68 break;
69 }
70 }
71
72 /**
73 * Implementation of hook_link().
74 */
75 function comment_page_link($type, $node = NULL, $teaser = FALSE) {
76 if ($type == 'comment' && variable_get('comment_page_show_link', TRUE)) {
77 if (!(arg(0) == 'comment' && arg(1) == $node->cid)) {
78 $links['link'] = array(
79 'title' => t('link'),
80 'href' => comment_page_url($node->cid, $node->subject),
81 );
82 }
83 return $links;
84 }
85 }
86
87 /**
88 * Builds the settings form for the module.
89 */
90 function comment_page_settings() {
91 $form['display'] = array(
92 '#type' => 'fieldset',
93 '#title' => t('Display settings'),
94 '#collapsible' => TRUE,
95 '#collapsed' => TRUE,
96 );
97 $form['display']['comment_page_show_link'] = array(
98 '#type' => 'checkbox',
99 '#title' => t('Show link to page view in the comment links section.'),
100 '#default_value' => variable_get('comment_page_show_link', TRUE),
101 );
102 $form['display']['comment_page_show_thread'] = array(
103 '#type' => 'checkbox',
104 '#title' => t('Show thread display on comment pages. (Recommended)'),
105 '#default_value' => variable_get('comment_page_show_thread', TRUE),
106 );
107 $form['display']['comment_page_anon_name'] = array(
108 '#type' => 'textfield',
109 '#title' => t('Anonymous user name'),
110 '#description' => t('What to display as the author name for anonymous posters.'),
111 '#default_value' => variable_get('comment_page_anon_name', t('Anonymous')),
112 );
113 $form['display']['comment_page_thread_show_count'] = array(
114 '#type' => 'checkbox',
115 '#title' => t('Show the total reply count by the thread title.'),
116 '#default_value' => variable_get('comment_page_thread_show_count', TRUE),
117 );
118 $form['display']['comment_page_thread_show_date'] = array(
119 '#type' => 'checkbox',
120 '#title' => t('Show the extended date beneath the thread title.'),
121 '#default_value' => variable_get('comment_page_thread_show_date', TRUE),
122 );
123 $form['display']['comment_page_thread_show_names'] = array(
124 '#type' => 'checkbox',
125 '#title' => t('Show author names by post data in the thread display.'),
126 '#default_value' => variable_get('comment_page_thread_show_names', TRUE),
127 );
128
129 $form['subject'] = array(
130 '#type' => 'fieldset',
131 '#title' => t('Comment subject settings'),
132 '#collapsible' => TRUE,
133 '#collapsed' => TRUE,
134 );
135 $form['subject']['default_comment_subject_on'] = array(
136 '#type' => 'checkbox',
137 '#title' => t("Default empty comment subject to '!prefix Parent node/comment title'.",
138 array('!prefix' => variable_get('default_comment_subject_prefix', t('Re:')))),
139 '#default_value' => variable_get('default_comment_subject_on', FALSE),
140 );
141 $form['subject']['default_comment_subject_prefix'] = array(
142 '#type' => 'textfield',
143 '#title' => t('Reply prefix'),
144 '#default_value' => variable_get('default_comment_subject_prefix', t('Re:')),
145 '#size' => 16,
146 );
147
148 $form['url'] = array(
149 '#type' => 'fieldset',
150 '#title' => t('Comment URL settings'),
151 '#collapsible' => TRUE,
152 '#collapsed' => TRUE,
153 );
154 $form['url']['comment_subject_in_url'] = array(
155 '#type' => 'checkbox',
156 '#title' => t('Include the comment subject in the page URL.'),
157 '#description' => t('This setting is only taken into account if <a href="!url">Pathauto</a> is installed and enabled.', array('!url' => 'http://drupal.org/project/pathauto')),
158 '#default_value' => variable_get('comment_subject_in_url', TRUE),
159 );
160
161 return system_settings_form($form);
162 }
163
164 /**
165 * Display a comment as a page.
166 */
167 function comment_page_display($comment_id) {
168 // Load up the comment.
169 $comment = _comment_load($comment_id);
170
171 // Check to make sure the user can access the node.
172 $node = node_load($comment->nid);
173 if ($node->nid) {
174 if (!node_access('view', $node)) {
175 drupal_access_denied();
176 }
177 }
178 else {
179 drupal_not_found();
180 }
181
182 if (variable_get('default_comment_subject_on', TRUE) &&
183 substr($comment->comment, 0, strlen($comment->subject)) == $comment->subject) {
184 $title = variable_get('default_comment_subject_prefix', t('Re:')) .' ';
185 if ($comment->pid == 0) {
186 $title .= db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $comment->nid));
187 }
188 else {
189 $title .= db_result(db_query("SELECT subject FROM {comments} WHERE cid = %d", $comment->pid));
190 }
191 drupal_set_title($title);
192 }
193 else {
194 drupal_set_title($comment->subject);
195 }
196
197 // Build the output with the comment followed by the thread display.
198 $output = theme('comment_view', $comment,
199 module_invoke_all('link', 'comment', $comment, 0));
200
201 if (variable_get('comment_page_show_thread', TRUE)) {
202 $output .= comment_page_thread($comment);
203 }
204
205 return $output;
206 }
207
208 /**
209 * Returns the URL for a comment page.
210 */
211 function comment_page_url($cid, $subject = NULL) {
212 if (empty($subject) || !variable_get('comment_subject_in_url', TRUE) || !function_exists('pathauto_cleanstring')) {
213 return 'comment/'. $cid;
214 }
215 else {
216 $prefix = variable_get('default_comment_subject_prefix', t('Re:'));
217 if (strlen($subject) > strlen($prefix) && strpos($subject, $prefix, strlen($prefix)) == strlen($prefix) + 1) {
218 while (strpos($subject, $prefix, strlen($prefix)) == strlen($prefix) + 1) {
219 $subject = substr($subject, strlen($prefix) + 1);
220 }
221 }
222 return 'comment/'. $cid .'/'. pathauto_cleanstring($subject);
223 }
224 }
225
226 /**
227 * Return the threaded list for display on a comment page.
228 */
229 function comment_page_thread($comment, $show_node = TRUE) {
230 drupal_add_css(drupal_get_path('module', 'comment_page') .'/comment_page.css');
231
232 $result = db_query("SELECT n.nid, n.title, n.uid, n.created, u.name FROM "
233 ."{node} AS n LEFT JOIN {users} AS u ON n.uid = u.uid "
234 ."WHERE n.nid = %d", $comment->nid);
235 $node = db_fetch_object($result);
236
237 $result = db_query("SELECT cid, pid, uid, subject, thread, name, timestamp FROM "
238 ."{comments} WHERE nid = %d ORDER BY thread DESC", $comment->nid);
239 $node->replies = db_num_rows($result);
240
241 $output = '<div class="comment-thread">';
242
243 if ($show_node) {
244 // Add the title of the node the comments are attached to.
245 $dst = 'node/'. $node->nid;
246 if (module_exists('path')) {
247 $result2 = db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $dst);
248 if ($row = db_fetch_object($result2)) {
249 $dst = $row->dst;
250 }
251 }
252 $output .= '<span class="thread-title">'. l($node->title, $dst) .'</span> ';
253
254 // Add the name of the node author if enabled.
255 if (variable_get('comment_page_thread_show_names', TRUE)) {
256 if ($node->uid) {
257 $name = l($node->name, 'user/'. $node->uid);
258 }
259 else {
260 $name = variable_get('comment_page_anon_name', t('Anonymous'));
261 }
262 $output .= '<span class="thread-author">'
263 . t('By: !name', array('!name' => $name)) .'</span> ';
264 }
265
266 // Add the reply count if enabled.
267 if (variable_get('comment_page_thread_show_count', TRUE)) {
268 $output .= '<span class="thread-count">'
269 . t('(%count <em>replies</em>)', array('%count' => $node->replies))
270 .'</span> ';
271 }
272
273 // Add the date beneath the thread title.
274 if (variable_get('comment_page_thread_show_date', TRUE)) {
275 $output .= '<span class="thread-date">'. format_date($node->created)
276 .'</span>';
277 }
278 }
279
280 // Fill the thread with the comments!
281 $nest = 0;
282 while ($row = db_fetch_object($result)) {
283 $last_nest = $nest;
284 $nest = strlen($row->thread);
285
286 if ($nest > $last_nest) {
287 $output .= '<ul>';
288 }
289 elseif ($nest < $last_nest) {
290 for ($i = $last_nest; $i > $nest; $i -= 3, $last_nest -= 3) {
291 $output .= '</ul>';
292 }
293 }
294 else {
295 $output .= '</li>';
296 }
297
298 if ($row->cid == $comment->cid) {
299 $class = 'current';
300 $subject = $row->subject;
301 }
302 else {
303 $class = 'normal';
304 $subject = l($row->subject, comment_page_url($row->cid, $row->subject));
305 }
306
307 $output .= '<li>';
308
309 // Add the comment's subject.
310 $output .= '<span class="comment-title '. $class .'">'. $subject .'</span> ';
311
312 // Add the name of the comment author if enabled.
313 if (variable_get('comment_page_thread_show_names', TRUE)) {
314 if ($node->uid) {
315 $name = l($row->name, 'user/'. $row->uid);
316 }
317 else {
318 $name = $row->name;
319 }
320 $output .= '<span class="thread-author">'
321 . t('By: !name', array('!name' => $name)) .'</span> ';
322 }
323
324 // Add the short version of the date beside the comment title.
325 if (variable_get('comment_page_thread_show_date', TRUE)) {
326 $output .= '<span class="comment-date">('
327 . format_date($row->timestamp, 'small') .')</span>';
328 }
329 }
330
331 $output .= '</div>';
332
333 return $output;
334 }
335

  ViewVC Help
Powered by ViewVC 1.1.2