comment.module 26.4 KB
Newer Older
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
   
Dries committed
4
$GLOBALS["cmodes"] = array(1 => "Flat list - collapsed", 2 => "Flat list - expanded", 3 => "Threaded list - collapsed", 4 => "Threaded list - expanded");
Dries's avatar
   
Dries committed
5
$GLOBALS["corder"] = array(1 => "Date - newest first", 2 => "Date - oldest first");
Dries's avatar
   
Dries committed
6

Dries's avatar
   
Dries committed
7
8
9
function comment_help() {
  $output .= "<p>The comment module enables users to submit posts that are directly associated with a piece of content.  These associated posts are called <i>comments</i>.  Comments may be <i>threaded</i>, which means that Drupal keeps track of multiple subconversations around a piece of content.  Threading helps to keep the comment conversation more organized.  Users are presented with several ways to view the comment conversation, and if desired, users may easily choose a <i>flat</i> presentation of comments instead of threaded.  Further, users may choose to order their comments view by <i>newest first</i> or by <i>oldest first</i>.  Finally, users may view a folded list or an expanded list of comments.  Folded limits the comment display to <i>subject</i> only.  Drupal remembers the comment view preference of each user whenever he changes a view setting.</p>";
  $output .= "<p>Since a busy site generates lots of comments, Drupal takes care to present a personalized view of comments for each user.  The home page lists displays the number of read and unread comments for a given post for the current user.  Also, the tracker module (when installed) displays all recent comments on the site.  Finally, comments which the user has not yet read are highlighted with a red star (this graphic may depend on the current theme).</p>";
Kjartan's avatar
Kjartan committed
10
11
  $output .= "<p>Comments behave like other user submissions in Drupal.  Specifically, ". la("filters", array("mod" => "system", "type" => "filter")) ." like smileys and HTML work fine if the administrator has enabled them.  Also, throttles are usually enabled to prevent a single user from spamming the web site with too many comments in a short period of time.</p>";
  $output .= "<p>Administrators may control which persons are allowed to submit and administer comments.  These controls appear in the ". la("user permissions", array("mod" => "user", "op" => "permission")) ." administration page.  Additionally, administrators may edit or search through comments on the ". la("comments admininistration page", array("mod" => "comment")) .", as well as set the default display view for new users.</p>";
Dries's avatar
   
Dries committed
12
13
14
15

  return $output;
}

Dries's avatar
   
Dries committed
16
function comment_settings($mode, $order, $threshold) {
Dries's avatar
   
Dries committed
17
18
  global $user;

Dries's avatar
   
Dries committed
19
20
  if ($user->uid) {
    $user = user_save($user, array("mode" => $mode, "sort" => $order, "threshold" => $threshold));
Dries's avatar
   
Dries committed
21
22
23
  }
}

Dries's avatar
   
Dries committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function comment_num_all($nid) {
  $comment = db_fetch_object(db_query("SELECT COUNT(c.nid) AS number FROM node n LEFT JOIN comments c ON n.nid = c.nid WHERE n.nid = '$nid' GROUP BY n.nid"));
  return $comment->number ? $comment->number : 0;
}

function comment_num_new($nid) {
  global $user;

  if ($user->uid) {

    /*
    ** Retrieve the timestamp at which the current user last viewed
    ** the specified node and use this timestamp to find the number
    ** of new comments.
    */

Dries's avatar
   
Dries committed
40
41
    $history = db_fetch_object(db_query("SELECT timestamp FROM history WHERE uid = '$user->uid' AND nid = '%s'", $nid));
    $comment = db_fetch_object(db_query("SELECT COUNT(c.nid) AS number FROM node n LEFT JOIN comments c ON n.nid = c.nid WHERE n.nid = '%s' AND timestamp > '". ($history->timestamp ? $history->timestamp : 0) ."' GROUP BY n.nid", $nid));
Dries's avatar
   
Dries committed
42
43
44
45
46
47
48
49
50
51
52
53
54

    return $comment->number ? $comment->number : 0;
  }
  else {
    return 0;
  }

}

function comment_tag_new($nid) {
  global $user;

  if ($user->uid) {
Dries's avatar
   
Dries committed
55
56
    $nid = check_query($nid);

Dries's avatar
   
Dries committed
57
58
59
60
61
62
63
64
65
66
    $result = db_query("SELECT timestamp FROM history WHERE uid = '$user->uid' AND nid = '$nid'");
    if (db_fetch_object($result)) {
      db_query("UPDATE history SET timestamp = '". time() ."' WHERE uid = '$user->uid' AND nid = '$nid'");
    }
    else {
      db_query("INSERT INTO history (uid, nid, timestamp) VALUES ('$user->uid', '$nid', '". time() ."')");
    }
  }
}

Dries's avatar
   
Dries committed
67
68
69
function comment_is_new($comment) {
  global $user;
  static $date;
Dries's avatar
   
Dries committed
70

Dries's avatar
   
Dries committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  if (!$date) {
    if ($user->uid) {
      $history = db_fetch_object(db_query("SELECT timestamp FROM history WHERE uid = '$user->uid' AND nid = '$comment->nid'"));
      $date = $history->timestamp ? $history->timestamp : 0;
    }
    else {
      $date = time();
    }
  }

  if ($comment->timestamp > $date) {
    return 1;
  }
  else {
    return 0;
  }
}

Dries's avatar
   
Dries committed
89
function comment_access($op, $comment) {
Dries's avatar
   
Dries committed
90
91
  global $user;

Dries's avatar
   
Dries committed
92
93
94
95
96
97
98
99
100
101
102
  if ($op == "edit") {

    /*
    ** Authenticated users can edit their comments as long they have
    ** not been replied to.  This, in order to avoid people changing
    ** or revising their statements based on the replies their posts
    ** got. Furthermore, users can't reply to their own comments and
    ** are encouraged to extend their original comment.
    */

    return $user->uid && $user->uid == $comment->uid && comment_num_replies($comment->cid) == 0;
Dries's avatar
   
Dries committed
103
  }
Dries's avatar
   
Dries committed
104

Dries's avatar
   
Dries committed
105
106
107
108
109
110
111
112
113
114
115
}

function comment_form($edit) {
  global $user;

  $form .= "<a name=\"comment\"></a>\n";

  // name field:
  $form .= form_item(t("Your name"), format_name($user));

  // subject field:
Dries's avatar
   
Dries committed
116
  $form .= form_textfield(t("Subject"), "subject", $edit["subject"], 50, 64);
Dries's avatar
   
Dries committed
117
118

  // comment field:
Dries's avatar
   
Dries committed
119
  $form .= form_textarea(t("Comment"), "comment", $edit["comment"] ? $edit["comment"] : $user->signature, 70, 10, t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")));
Dries's avatar
   
Dries committed
120
121

  // preview button:
Dries's avatar
   
Dries committed
122
  $form .= form_hidden("cid", $edit["cid"]);
Dries's avatar
   
Dries committed
123
  $form .= form_hidden("pid", $edit["pid"]);
Dries's avatar
   
Dries committed
124
  $form .= form_hidden("nid", $edit["nid"]);
Dries's avatar
   
Dries committed
125

Dries's avatar
   
Dries committed
126
  if (!$edit["comment"]) {
Dries's avatar
   
Dries committed
127
128
129
130
131
132
133
134
135
136
    $form .= form_submit(t("Preview comment"));
  }
  else {
    $form .= form_submit(t("Preview comment"));
    $form .= form_submit(t("Post comment"));
  }

  return form($form);
}

Dries's avatar
   
Dries committed
137
138
139
140
141
142
143
144
145
146
147
function comment_edit($cid) {
  global $user;

  $comment = db_fetch_object(db_query("SELECT c.*, u.name, u.uid FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.cid = '$cid'"));

  if (comment_access("edit", $comment)) {
    comment_preview(object2array($comment));
  }
}

function comment_reply($pid, $nid) {
Dries's avatar
   
Dries committed
148
149
150
151
152
153
  global $theme, $node;

  // we must provide a taxonomy context for user_access()
  $context->nid = $nid;
  if (user_access("access comments", $context)) {
    if ($pid) {
Dries's avatar
   
Dries committed
154
      $comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.cid = '%s'", $pid));
Dries's avatar
   
Dries committed
155
156
157
158
159
160
      comment_view($comment, t("reply to this comment"));
    }
    else {
      node_view(node_load(array("nid" => $nid)));
      $pid = 0;
    }
Dries's avatar
   
Dries committed
161

Dries's avatar
   
Dries committed
162
163
    if (node_comment_mode($nid) == 1) {
      $theme->box(t("Reply"), t("This discussion is closed: you can't post new comments."));
Kjartan's avatar
Kjartan committed
164
165
    }
    else if (user_access("post comments", $context)) {
Dries's avatar
   
Dries committed
166
167
168
169
170
      $theme->box(t("Reply"), comment_form(array("pid" => $pid, "nid" => $nid)));
    }
    else {
      $theme->box(t("Reply"), t("You are not authorized to post comments."));
    }
Kjartan's avatar
Kjartan committed
171
172
  }
  else {
Dries's avatar
   
Dries committed
173
    $theme->box(t("Reply"), t("You are not authorized to view comments."));
Dries's avatar
   
Dries committed
174
175
176
177
178
179
  }
}

function comment_preview($edit) {
  global $theme, $user;

Dries's avatar
   
Dries committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  foreach ($edit as $key => $value) {
    $comment->$key = filter($value);
  }

  /*
  ** Attach the user information:
  */

  $comment->uid = $user->uid;
  $comment->name = $user->name;

  /*
  ** Attach the time:
  */

  $comment->timestamp = time();

  /*
  ** Preview the comment:
  */

  comment_view($comment, t("reply to this comment"));
Dries's avatar
   
Dries committed
202
203

  $theme->box(t("Reply"), comment_form($edit));
Kjartan's avatar
Kjartan committed
204
205

  if ($edit["pid"]) {
Kjartan's avatar
Kjartan committed
206
    $comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.cid = '%s'", $edit["pid"]));
Kjartan's avatar
Kjartan committed
207
208
209
210
211
212
    comment_view($comment, t("reply to this comment"));
  }
  else {
    node_view(node_load(array("nid" => $edit["nid"])));
    $edit["pid"] = 0;
  }
Dries's avatar
   
Dries committed
213
214
215
216
217
}

function comment_post($edit) {
  global $theme, $user;

Dries's avatar
   
Dries committed
218
219
  $context->nid = $edit["nid"];
  if (user_access("post comments", $context) && node_comment_mode($edit["nid"]) == 2) {
Dries's avatar
   
Dries committed
220

Dries's avatar
   
Dries committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    /*
    ** Validate the comment's subject.  If not specified, extract
    ** one from the comment's body.
    */

    $edit["subject"] = strip_tags(($edit["subject"] ? $edit["subject"] : substr($edit["comment"], 0, 29)));

    /*
    ** Validate the comment's body.
    */

    $edit["comment"] = filter($edit["comment"]);

    /*
    ** Check for duplicate comments.  Note that we have to use the
    ** validated/filtered data to perform such check.
    */

Dries's avatar
   
Dries committed
239
    $duplicate = db_result(db_query("SELECT COUNT(cid) FROM comments WHERE pid = '%s' AND nid = '%s' AND subject = '%s' AND comment = '%s'", $edit["pid"], $edit["nid"], $edit["subject"], $edit["comment"]), 0);
Dries's avatar
   
Dries committed
240
241

    if ($duplicate != 0) {
Dries's avatar
   
Dries committed
242
      watchdog("warning", "comment: duplicate '". $edit["subject"] ."'");
Dries's avatar
   
Dries committed
243
244
245
    }
    else {

Dries's avatar
   
Dries committed
246
      if ($edit["cid"]) {
Dries's avatar
   
Dries committed
247

Dries's avatar
   
Dries committed
248
249
250
251
252
253
        /*
        ** Update the comment in the database.  Note that the update
        ** query will fail if the comment isn't owned by the current
        ** user.
        */

Dries's avatar
   
Dries committed
254
        db_query("UPDATE comments SET subject = '%s', comment = '%s' WHERE cid = '%s' AND uid = '$user->uid'", $edit["subject"], $edit["comment"], $edit["cid"]);
Dries's avatar
   
Dries committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

        /*
        ** Add entry to the watchdog log:
        */

        watchdog("special", "comment: updated '". $edit["subject"] ."'");
      }
      else {
        /*
        ** Check the user's comment submission rate.  If exceeded,
        ** throttle() will bail out.
        */

        throttle("post comment", variable_get("max_comment_rate", 60));

        /*
        ** Add the comment to database:
        */

Dries's avatar
   
Dries committed
274
        db_query("INSERT INTO comments (nid, pid, uid, subject, comment, hostname, timestamp) VALUES ('%s', '%s', '$user->uid', '%s', '%s', '%s', '%s')", $edit["nid"], $edit["pid"], $edit["subject"], $edit["comment"], getenv("REMOTE_ADDR"), time());
Dries's avatar
   
Dries committed
275
276
277
278

        /*
        ** Add entry to the watchdog log:
        */
Dries's avatar
   
Dries committed
279

Dries's avatar
   
Dries committed
280
281
        watchdog("special", "comment: added '". $edit["subject"] ."'");
      }
Dries's avatar
   
Dries committed
282
283
284
285

      /*
      ** Clear the cache:
      */
Dries's avatar
   
Dries committed
286
287

      cache_clear();
Dries's avatar
   
Dries committed
288

Dries's avatar
   
Dries committed
289
290
    }
  }
Dries's avatar
   
Dries committed
291
292
293
294
295

  /*
  ** Redirect the user the node he commented on:
  */

Dries's avatar
   
Dries committed
296
  drupal_goto(drupal_url(array("id" => $edit["nid"], "node")));
Dries's avatar
   
Dries committed
297

Dries's avatar
   
Dries committed
298
299
}

Dries's avatar
   
Dries committed
300
function comment_num_replies($id) {
Dries's avatar
   
Dries committed
301
302
303
304
305
306
307
308
309

  $result = db_query("SELECT COUNT(cid) FROM comments WHERE pid = '$id'");
  return ($result) ? db_result($result, 0) : 0;

}

function comment_moderation($comment) {
  global $user;

Dries's avatar
   
Dries committed
310
311
312
  // XXX: disabled for now
  return "";

Dries's avatar
   
Dries committed
313
  $values = array("--", "1", "2", "3", "4", "5");
Dries's avatar
   
Dries committed
314
315
316
317
318
319
320

  $moderate = db_fetch_object(db_query("SELECT * FROM moderate WHERE cid = '$comment->cid' AND uid = '$user->uid'"));

  foreach ($values as $key => $value) {
    $options .= " <option value=\"$key\"". ($moderate->score == $key ? " selected=\"selected\"" : "") .">$value</option>\n";
  }

Dries's avatar
   
Dries committed
321
  $output .= "<select name=\"moderate[comment][$comment->cid]\">$options</select><br />". ($comment->score ? $comment->score : "--") ." / $comment->votes";
Dries's avatar
   
Dries committed
322
323
324
325
326

  return $output;
}

function comment_threshold($threshold) {
Dries's avatar
   
Dries committed
327
328
329
  // XXX: disabled for now
  return "";

Dries's avatar
   
Dries committed
330
  for ($i = 0; $i < 6; $i++) $options .= " <option value=\"$i\"". ($threshold == $i ? " selected=\"selected\"" : "") .">". t("Visibility") ." - $i</option>";
Dries's avatar
   
Dries committed
331
332
333
334
335
336
  return "<select name=\"threshold\">$options</select>\n";
}

function comment_mode($mode) {
  global $cmodes;

Dries's avatar
   
Dries committed
337
  foreach ($cmodes as $key => $value) $options .= " <option value=\"$key\"". ($mode == $key ? " selected=\"selected\"" : "") .">$value</option>\n";
Dries's avatar
   
Dries committed
338
339
340
341
342
343
  return "<select name=\"mode\">$options</select>\n";
}

function comment_order($order) {
  global $corder;

Dries's avatar
   
Dries committed
344
  foreach ($corder as $key=>$value) $options .= " <option value=\"$key\"". ($order == $key ? " selected=\"selected\"" : "") .">$value</option>\n";
Dries's avatar
   
Dries committed
345
346
347
  return "<select name=\"order\">$options</select>\n";
}

Dries's avatar
   
Dries committed
348
function comment_query($nid, $order, $pid = -1) {
Dries's avatar
   
Dries committed
349

Dries's avatar
   
Dries committed
350
  $query .= "SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.timestamp, u.uid, u.name FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.nid = '$nid'";
Dries's avatar
   
Dries committed
351
352
353
354
355

  if ($pid >= 0) {
    $query .= " AND pid = '$pid'";
  }

Dries's avatar
   
Dries committed
356
  $query .= " GROUP BY c.cid, c.pid, c.nid, c.subject, c.comment, c.timestamp, u.uid, u.name";
Dries's avatar
   
Dries committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378

  if ($order == 1) {
    $query .= " ORDER BY c.timestamp DESC";
  }
  else if ($order == 2) {
    $query .= " ORDER BY c.timestamp";
  }

  return db_query($query);

}

function comment_visible($comment, $threshold = 0) {
  if ($comment->votes == 0 || $comment->score >= $threshold) {
    return 1;
  }
  else {
    return 0;
  }
}

function comment_links($comment, $return = 1) {
Dries's avatar
   
Dries committed
379
  global $user, $theme;
Dries's avatar
   
Dries committed
380

Dries's avatar
   
Dries committed
381
382
  $links = array();

Dries's avatar
   
Dries committed
383
  if ($return) {
Kjartan's avatar
Kjartan committed
384
    $links[] = l("<span style=\"color: $theme->type;\">". t("return") ."</span>", array("id" => $comment->nid ."#". $comment->cid));
Dries's avatar
   
Dries committed
385
  }
Dries's avatar
   
Dries committed
386
387

  if (user_access("administer comments")) {
Dries's avatar
   
Dries committed
388
    $links[] = la("<span style=\"color: $theme->type;\">". t("administer") ."</span>", array("mod" => "comment", "op" => "edit", "id" => $comment->cid));
Dries's avatar
   
Dries committed
389
390
  }

Dries's avatar
   
Dries committed
391
392
393
394
395
  // here we should check if this node has read-only comments, but we already check on submit
  // and this way we save a query. it's just a cosmetic issue. otherwise just uncomment next
  // line and related bracket some lines below

  //if (node_comment_mode($comment->nid)) {
Dries's avatar
   
Dries committed
396
  if (user_access("post comments")) {
Dries's avatar
   
Dries committed
397
    if (comment_access("edit", $comment)) {
Dries's avatar
   
Dries committed
398
      $links[] = lm("<span style=\"color: $theme->type\">". t("edit your comment") ."</span>", array("mod" => "comment", "op" => "edit", "id" => $comment->cid), t("Make changes to your comment."));
Dries's avatar
   
Dries committed
399
400
    }
    else {
Dries's avatar
   
Dries committed
401
      $links[] = lm("<span style=\"color: $theme->type;\">". t("reply to this comment") ."</span>", array("mod" => "comment", "op" => "reply", "id" => $comment->nid, "pid" => $comment->cid), t("Reply to this comment."));
Dries's avatar
   
Dries committed
402
    }
Dries's avatar
   
Dries committed
403
  }
Dries's avatar
   
Dries committed
404
  //}
Dries's avatar
   
Dries committed
405

Dries's avatar
   
Dries committed
406

Dries's avatar
   
Dries committed
407
  return $theme->links($links);
Dries's avatar
   
Dries committed
408
409
410
}

function comment_view($comment, $folded = 0) {
Dries's avatar
   
Dries committed
411
412
413
  global $theme, $id;

  if (comment_is_new($comment)) {
Dries's avatar
   
Dries committed
414
    $comment->subject = "$comment->subject <span style=\"color: red;\">*</span>";
Dries's avatar
   
Dries committed
415
  }
Dries's avatar
   
Dries committed
416
417
418
419
420

  if ($folded) {
    $theme->comment($comment, $folded);
  }
  else {
Kjartan's avatar
Kjartan committed
421
    print l(check_output($comment->subject), array("id" => $comment->nid, "cid" => $comment->cid ."#". $comment->cid)) ." by ". format_name($comment) ."</small><p />";
Dries's avatar
   
Dries committed
422
423
424
  }
}

Dries's avatar
   
Dries committed
425
function comment_thread_min($comments, $threshold, $pid = 0) {
Dries's avatar
   
Dries committed
426
427
  global $user;

Dries's avatar
   
Dries committed
428
429
430
431
432
433
434
  foreach ($comments as $comment) {
    if ($comment->pid == $pid) {
      print "<ul>";
      print comment_view($comment);
      comment_thread_min($comments, $threshold, $comment->cid);
      print "</ul>";
    }
Dries's avatar
   
Dries committed
435
436
437
  }
}

Dries's avatar
   
Dries committed
438
function comment_thread_max($comments, $threshold, $pid = 0, $level = 0) {
Dries's avatar
   
Dries committed
439
440
441
442
443
444
445
446
447
448
449
  global $user;

  /*
  ** We had quite a few browser specific issues: expanded comments below
  ** the top level got truncated on the right hand side.  A range of
  ** solutions have been proposed and tried but either the right margins of
  ** the comments didn't line up well, or the heavily nested tables made
  ** for slow rendering and cluttered HTML.  This is the best work-around
  ** in terms of speed and size.
  */

Dries's avatar
   
Dries committed
450
451
  foreach ($comments as $comment) {
    if ($comment->pid == $pid) {
Steven Wittens's avatar
Steven Wittens committed
452
453
454
      if ($level) {
        print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td width=\"". ($level * 25) ."\">&nbsp;</td><td>\n";
      }
Dries's avatar
   
Dries committed
455
      comment_view($comment, comment_links($comment, 0));
Steven Wittens's avatar
Steven Wittens committed
456
457
458
      if ($level) {
        print "</td></tr></table>\n";
      }
Dries's avatar
   
Dries committed
459

Dries's avatar
   
Dries committed
460
461
      comment_thread_max($comments, $threshold, $comment->cid, $level + 1);
    }
Dries's avatar
   
Dries committed
462
463
464
465
  }

}

Dries's avatar
   
Dries committed
466
function comment_render($nid, $cid) {
Dries's avatar
   
Dries committed
467
  global $user, $theme, $mode, $order, $threshold;
Dries's avatar
   
Dries committed
468
469
470
471
472
473
474

  if (user_access("access comments")) {

    /*
    ** Pre-process variables:
    */

Dries's avatar
   
Dries committed
475
476
    if (empty($nid)) {
      $nid = 0;
Dries's avatar
   
Dries committed
477
478
479
480
481
482
483
    }

    if (empty($cid)) {
      $cid = 0;
    }

    if (empty($mode)) {
Dries's avatar
   
Dries committed
484
      $mode = $user->uid ? $user->mode : variable_get("default_comment_mode", 4);
Dries's avatar
   
Dries committed
485
486
487
    }

    if (empty($order)) {
Dries's avatar
   
Dries committed
488
      $order = $user->uid ? $user->sort : variable_get("default_comment_order", 1);
Dries's avatar
   
Dries committed
489
490
491
    }

    if (empty($threshold)) {
Dries's avatar
   
Dries committed
492
493
      // $threshold = $user->uid ? $user->threshold : variable_get("default_comment_threshold", 3);
      $threshold = 0;
Dries's avatar
   
Dries committed
494
495
496
    }

    print "<a name=\"comment\"></a>\n";
Kjartan's avatar
Kjartan committed
497
498
    print "<form method=\"post\" action=\"". drupal_url(array("mod" => "comment"), "module") ."\">\n";
    print form_hidden("nid", $nid);
Dries's avatar
   
Dries committed
499
500
501
502
503
504
505
506

    /*
    ** Render control panel:
    */

    $theme->box(t("Control panel"), $theme->comment_controls($threshold, $mode, $order));

    if ($cid > 0) {
Dries's avatar
   
Dries committed
507
      $result = db_query("SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.timestamp, u.uid, u.name FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.cid = '$cid' GROUP BY c.cid, c.pid, c.nid, c.subject, c.comment, c.timestamp, u.uid, u.name");
Dries's avatar
   
Dries committed
508
509
510
511
512
513
      if ($comment = db_fetch_object($result)) {
        comment_view($comment, comment_links($comment));
      }
    }
    else {
      if ($mode == 1) {
Dries's avatar
   
Dries committed
514
        $result = comment_query($nid, $order);
Dries's avatar
   
Dries committed
515
516
517
518
        print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
        print " <tr><th>Subject</th><th>Author</th><th>Date</th><th>Score</th></tr>\n";
        while ($comment = db_fetch_object($result)) {
          if (comment_visible($comment, $threshold)) {
Kjartan's avatar
Kjartan committed
519
            print " <tr><td>". l(check_output($comment->subject), array("id" => $comment->nid, "cid" => $comment->cid ."#". $comment->cid)) ."</td><td>". format_name($comment) ."</td><td>". format_date($comment->timestamp, "small") ."</td><td>$comment->score</td></tr>\n";
Dries's avatar
   
Dries committed
520
521
522
523
524
          }
        }
        print "</table>\n";
      }
      else if ($mode == 2) {
Dries's avatar
   
Dries committed
525
        $result = comment_query($nid, $order);
Dries's avatar
   
Dries committed
526
527
528
529
530
        while ($comment = db_fetch_object($result)) {
          comment_view($comment, (comment_visible($comment, $threshold) ? comment_links($comment, 0) : 0));
        }
      }
      else if ($mode == 3) {
Dries's avatar
   
Dries committed
531
        $result = comment_query($nid, $order);
Dries's avatar
   
Dries committed
532
        while ($comment = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
533
534
535
536
          $comments[] = $comment;
        }

        if ($comments) {
Dries's avatar
   
Dries committed
537
          comment_thread_min($comments, $threshold);
Dries's avatar
   
Dries committed
538
539
540
        }
      }
      else {
Dries's avatar
   
Dries committed
541
        $result = comment_query($nid, $order);
Dries's avatar
   
Dries committed
542
        while ($comment = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
543
544
545
546
          $comments[] = $comment;
        }

        if ($comments) {
Dries's avatar
   
Dries committed
547
          comment_thread_max($comments, $threshold);
Dries's avatar
   
Dries committed
548
549
550
551
552
553
        }
      }
    }

    print "</form>";

Dries's avatar
   
Dries committed
554
555
556
557
558
    /*
    ** Tag the node's comments as being read:
    */

    comment_tag_new($nid);
Dries's avatar
   
Dries committed
559
560
561
  }
}

Dries's avatar
   
Dries committed
562
function comment_search($keys) {
Dries's avatar
   
Dries committed
563
  global $PHP_SELF;
Kjartan's avatar
Kjartan committed
564
565
566
567
568
569
570
571
572
573
574
575
576

  // Return the results of performing a search using the indexed search
  // for this particular type of node.
  //
  // Pass an array to the "do_search" function which dictates what it
  // will search through, and what it will search for
  //
  // "keys"'s value is the keywords entered by the user
  //
  // "type"'s value is used to identify the node type in the search
  // index.
  //
  // "select"'s value is used to relate the data from the specific nodes
Dries's avatar
   
Dries committed
577
  // table to the data that the search_index table has in it, and the the
Kjartan's avatar
Kjartan committed
578
579
  // do_search functino will rank it.
  //
Dries's avatar
   
Dries committed
580
  // The select must always provide the following fields - lno, title,
Kjartan's avatar
Kjartan committed
581
582
583
584
585
  // created, uid, name, count
  //
  // The select statement may optionally provide "nid", which is a secondary
  // identifier which is currently used byt the comment module.
  //
Kjartan's avatar
Kjartan committed
586
  $find = do_search(array("keys" => $keys, "type" => "comment", "select" => "select s.lno as lno, c.nid as nid, c.subject as title, c.timestamp as created, u.uid as uid, u.name as name, s.count as count FROM search_index s, comments c LEFT JOIN users u ON c.uid = u.uid WHERE s.lno = c.cid AND s.type = 'comment' AND s.word like '%'"));
Dries's avatar
   
Dries committed
587

Dries's avatar
   
Dries committed
588
589
  return $find;
}
Dries's avatar
 
Dries committed
590

Dries's avatar
   
Dries committed
591
function comment_perm() {
Dries's avatar
   
Dries committed
592
593
594
  return array("access comments", "post comments", "administer comments");
}

Dries's avatar
   
Dries committed
595
function comment_link($type, $node = 0, $main = 0) {
Dries's avatar
   
Dries committed
596

Dries's avatar
   
Dries committed
597
  if ($type == "admin" && user_access("administer comments")) {
Dries's avatar
   
Dries committed
598
    $links[] = la(t("comments"), array("mod" => "comment"));
Dries's avatar
   
Dries committed
599
600
  }

Dries's avatar
   
Dries committed
601
  if ($type == "node" && $node->comment) {
Dries's avatar
   
Dries committed
602
603
604
605
606
607
608
609

    if ($main) {

      /*
      ** Main page: display the number of comments that have been posted.
      */

      if (user_access("access comments")) {
Dries's avatar
   
Dries committed
610
611
612
        $all = comment_num_all($node->nid);
        $new = comment_num_new($node->nid);

Kjartan's avatar
Kjartan committed
613
        $links[] = l(format_plural($all, "comment", "comments") . ($new ? ", $new ". t("new") : ""), array("id" => $node->nid ."#comment"), t("View this posting and all of its comments."));
Dries's avatar
   
Dries committed
614
615
616
617
618
      }
    }
    else {
      /*
      ** Node page: add a "post comment" link if the user is allowed to
Dries's avatar
   
Dries committed
619
      ** post comments and if this node is not read-only
Dries's avatar
   
Dries committed
620
621
622
      */

      if (user_access("post comments")) {
Dries's avatar
   
Dries committed
623
        if ($node->comment == 2) {
Kjartan's avatar
Kjartan committed
624
625
626
          $links[] = lm(t("add new comment"), array("mod" => "comment", "op" => "reply", "id" => $node->nid ."#comment"), t("Share your thoughts and opinions related to this posting."));
        }
        else {
Dries's avatar
   
Dries committed
627
628
          $links[] = t("This discussion is closed: you can't post new comments.");
        }
Dries's avatar
   
Dries committed
629
630
631
632
      }
    }
  }

Dries's avatar
   
Dries committed
633
  return $links ? $links : array();
Dries's avatar
   
Dries committed
634
635
}

Dries's avatar
   
Dries committed
636
637
function comment_node_link($node) {

Dries's avatar
Dries committed
638
  if (user_access("administer comments") && comment_num_all($node->nid)) {
Dries's avatar
   
Dries committed
639

Dries's avatar
   
Dries committed
640
641
642
    /*
    ** Edit comments:
    */
Dries's avatar
   
Dries committed
643

Dries's avatar
   
Dries committed
644
    $result = db_query("SELECT c.cid, c.subject, u.uid, u.name FROM comments c LEFT JOIN users u ON u.uid = c.uid WHERE nid = '$node->nid' ORDER BY c.timestamp");
Dries's avatar
   
Dries committed
645

Dries's avatar
   
Dries committed
646
647
648
649
650
    $output .= "<h3>". t("Edit comments") ."</h3>";
    $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
    $output .= " <tr><th>title</th><th>author</th><th colspan=\"3\">operations</th></tr>";

    while ($comment = db_fetch_object($result)) {
Kjartan's avatar
Kjartan committed
651
      $output .= "<tr><td>". l($comment->subject, array("id" => $node->nid, "cid" => $comment->cid ."#". $comment->cid)) ."</td><td>". format_name($comment) ."</td><td>". l(t("view comment"), array("id" => $node->nid, "cid" => $comment->cid ."#". $comment->cid)) ."</td><td>". la(t("edit comment"), array("mod" => "comment", "op" => "edit", "id" => $comment->cid)) ."</td><td>". la(t("delete comment"), array("mod" => "comment", "op" => "delete", "id" => $comment->cid)) ."</td></tr>";
Dries's avatar
   
Dries committed
652
653
654
655
656
657
    }

    $output .= "</table>";

    return $output;
  }
Dries's avatar
   
Dries committed
658
659
}

Dries's avatar
   
Dries committed
660
661

function comment_save($id, $edit) {
Dries's avatar
   
Dries committed
662
  db_query("UPDATE comments SET subject = '%s', comment = '%s' WHERE cid = '$id'", filter($edit["subject"]), filter($edit["comment"]));
Dries's avatar
   
Dries committed
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
  watchdog("special", "comment: modified '". $edit["subject"] ."'");
}

function comment_page() {
  global $theme, $op, $edit, $id, $pid, $cid;

  switch ($op) {
    case "edit":
      $theme->header();
      comment_edit(check_query($id));
      $theme->footer();
      break;
    case "reply":
      $theme->header();
      comment_reply(check_query($pid), check_query($id));
      $theme->footer();
      break;
    case t("Preview comment"):
      $theme->header();
      comment_preview($edit);
      $theme->footer();
      break;
    case t("Post comment"):
      comment_post($edit);
      break;
    case t("Update settings"):
Kjartan's avatar
Kjartan committed
689
      global $mode, $order, $threshold;
Dries's avatar
   
Dries committed
690
      comment_settings(check_query($mode), check_query($order), check_query($threshold));
Kjartan's avatar
Kjartan committed
691
      drupal_goto(drupal_url(array("id" => $edit["nid"]), "node"));
Dries's avatar
   
Dries committed
692
693
694
695
696
697
      break;
    default:
  }
}

function comment_admin_edit($id) {
Dries's avatar
   
Dries committed
698

Dries's avatar
   
Dries committed
699
  $result = db_query("SELECT c.*, u.name, u.uid FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.cid = '$id'");
Dries's avatar
 
Dries committed
700
701
  $comment = db_fetch_object($result);

Dries's avatar
   
Dries committed
702
  $form .= form_item(t("Author"), format_name($comment));
Dries's avatar
   
Dries committed
703
704
  $form .= form_textfield(t("Subject"), "subject", $comment->subject, 70, 128);
  $form .= form_textarea(t("Comment"), "comment", $comment->comment, 70, 15);
Dries's avatar
   
Dries committed
705
  $form .= form_hidden("cid", $id);
Dries's avatar
   
Dries committed
706
  $form .= form_submit(t("Submit"));
Dries's avatar
   
Dries committed
707
  $form .= form_submit(t("Delete"));
Dries's avatar
 
Dries committed
708

Dries's avatar
   
Dries committed
709
  return form($form);
Dries's avatar
 
Dries committed
710
711
}

Dries's avatar
   
Dries committed
712
function comment_admin_overview() {
Dries's avatar
   
Dries committed
713
  $result = db_query("SELECT c.*, u.name, u.uid FROM comments c LEFT JOIN users u ON u.uid = c.uid ORDER BY timestamp DESC LIMIT 50");
Dries's avatar
   
Dries committed
714

Dries's avatar
   
Dries committed
715
716
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
  $output .= " <tr><th>subject</th><th>author</th><th>date</th><th colspan=\"2\">operations</th></tr>\n";
Dries's avatar
 
Dries committed
717
  while ($comment = db_fetch_object($result)) {
Kjartan's avatar
Kjartan committed
718
    $output .= " <tr><td>". l(check_output($comment->subject), array("id" => $comment->nid, "cid" => $comment->cid, "pid" => $comment->pid ."#". $comment->cid)) ."</td><td>". format_name($comment) ."</td><td>". format_date($comment->timestamp, "small") ."</td><td>". la(t("edit comment"), array("mod" => comment, "op" => edit, "id" => $comment->cid)) ."</td><td>". la(t("delete comment"), array("mod" => "comment", "op" => "delete", "id" => $comment->cid)) ."</td></tr>\n";
Dries's avatar
 
Dries committed
719
  }
Dries's avatar
   
Dries committed
720
  $output .= "</table>\n";
Dries's avatar
   
Dries committed
721

Dries's avatar
   
Dries committed
722
  return $output;
Dries's avatar
 
Dries committed
723
724
}

Dries's avatar
   
Dries committed
725
726
727
function comment_delete($edit) {

  if ($edit["confirm"]) {
Dries's avatar
   
Dries committed
728
    db_query("DELETE FROM comments WHERE cid = '%s'", $edit["cid"]);
Dries's avatar
   
Dries committed
729
730
731
732
733
734
735
736
737
738
739
    watchdog("special", "comment: deleted comment #". $edit["cid"]);
  }
  else {
    $output .= form_item(t("Confirm deletion"), "");
    $output .= form_hidden("cid", $edit["cid"]);
    $output .= form_hidden("confirm", 1);
    $output .= form_submit(t("Delete"));
    $output = form($output);
  }

  return $output;
Dries's avatar
   
Dries committed
740
741
}

Dries's avatar
 
Dries committed
742
function comment_admin() {
Dries's avatar
   
Dries committed
743
  global $op, $id, $edit, $mod, $keys, $order;
Dries's avatar
 
Dries committed
744

Dries's avatar
   
Dries committed
745
  if (user_access("administer comments")) {
Dries's avatar
   
Dries committed
746

Kjartan's avatar
Kjartan committed
747
    print "<small>". la(t("overview"), array("mod" => "comment")) ." | ". la(t("search comment"), array("mod" => "comment", "op" => "search")) ."</small><hr />\n";
Dries's avatar
   
Dries committed
748
749
750

    switch ($op) {
      case "edit":
Dries's avatar
   
Dries committed
751
        print comment_admin_edit($id);
Dries's avatar
   
Dries committed
752
753
        break;
      case "search":
Dries's avatar
   
Dries committed
754
        print search_type("comment", drupal_url(array("mod" => "comment", "op" => "search"), "admin"));
Dries's avatar
   
Dries committed
755
        break;
Dries's avatar
   
Dries committed
756
      case "delete":
Dries's avatar
   
Dries committed
757
758
759
760
        print comment_delete(array("cid" => $id));
        break;
      case t("Delete"):
        print comment_delete($edit);
Dries's avatar
   
Dries committed
761
        break;
Dries's avatar
   
Dries committed
762
      case t("Submit"):
Dries's avatar
   
Dries committed
763
        print status(comment_save(check_query($id), $edit));
Dries's avatar
   
Dries committed
764
        print comment_admin_overview();
Dries's avatar
   
Dries committed
765
766
        break;
      default:
Dries's avatar
   
Dries committed
767
        print comment_admin_overview();
Dries's avatar
   
Dries committed
768
769
770
771
    }
  }
  else {
    print message_access();
Dries's avatar
 
Dries committed
772
773
  }
}
Dries's avatar
   
Dries committed
774

Kjartan's avatar
Kjartan committed
775
776
777
778
779
function comment_update_index() {

  // Return an array of values to dictate how to update the search index
  // for this particular type of node.
  //
Dries's avatar
   
Dries committed
780
  // "last_update"'s value is used with variable_set to set the
Kjartan's avatar
Kjartan committed
781
782
783
784
785
786
  // last time this node type (comment) had an index update run.
  //
  // "node_type"'s value is used to identify the node type in the search
  // index (commentt in this case).
  //
  // "select"'s value is used to select the node id and text fields from
Dries's avatar
   
Dries committed
787
  // the table we are indexing. In this case, we also check against the
Kjartan's avatar
Kjartan committed
788
  // last run date for the comments update.
Kjartan's avatar
Kjartan committed
789
  return array("last_update" => "comment_cron_last", "node_type" => "comment", "select" => "SELECT c.cid as lno, c.subject as text1, c.comment as text2 FROM comments c WHERE timestamp > ". variable_get("comment_cron_last", 1));
Kjartan's avatar
Kjartan committed
790
791
}

Dries's avatar
   
Dries committed
792
?>