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

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

Dries's avatar
   
Dries committed
7
function comment_settings($mode, $order, $threshold) {
Dries's avatar
   
Dries committed
8
9
  global $user;

Dries's avatar
   
Dries committed
10
11
  if ($user->uid) {
    $user = user_save($user, array("mode" => $mode, "sort" => $order, "threshold" => $threshold));
Dries's avatar
   
Dries committed
12
13
14
  }
}

Dries's avatar
   
Dries committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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.
    */

    $history = db_fetch_object(db_query("SELECT timestamp FROM history WHERE uid = '$user->uid' AND nid = '$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' AND timestamp > '". ($history->timestamp ? $history->timestamp : 0) ."' GROUP BY n.nid"));

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

}

function comment_tag_new($nid) {
  global $user;

  if ($user->uid) {
    $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
56
function comment_access($op, $comment) {
Dries's avatar
   
Dries committed
57
58
  global $user;

Dries's avatar
   
Dries committed
59
60
61
62
63
64
65
66
67
68
69
  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
70
  }
Dries's avatar
   
Dries committed
71

Dries's avatar
   
Dries committed
72
73
74
75
76
77
78
79
80
81
82
}

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
83
  $form .= form_textfield(t("Subject"), "subject", $edit["subject"], 50, 64);
Dries's avatar
   
Dries committed
84
85

  // comment field:
Dries's avatar
   
Dries committed
86
  $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
87
88

  // preview button:
Dries's avatar
   
Dries committed
89
  $form .= form_hidden("cid", $edit["cid"]);
Dries's avatar
   
Dries committed
90
  $form .= form_hidden("pid", $edit["pid"]);
Dries's avatar
   
Dries committed
91
  $form .= form_hidden("nid", $edit["nid"]);
Dries's avatar
   
Dries committed
92

Dries's avatar
   
Dries committed
93
  if (!$edit["comment"]) {
Dries's avatar
   
Dries committed
94
95
96
97
98
99
100
101
102
103
    $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
104
105
106
107
108
109
110
111
112
113
114
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
115
116
117
  global $theme;

  if ($pid) {
Dries's avatar
   
Dries committed
118
119
    $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 = '$pid'"));
    comment_view($comment, t("reply to this comment"));
Dries's avatar
   
Dries committed
120
121
  }
  else {
Dries's avatar
   
Dries committed
122
    node_view(node_load(array("nid" => $nid)));
Dries's avatar
   
Dries committed
123
124
125
126
    $pid = 0;
  }

  if (user_access("post comments")) {
Dries's avatar
   
Dries committed
127
    $theme->box(t("Reply"), comment_form(array("pid" => $pid, "nid" => $nid)));
Dries's avatar
   
Dries committed
128
129
130
131
132
133
134
135
136
  }
  else {
    $theme->box(t("Reply"), t("You are not authorized to post comments."));
  }
}

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

Dries's avatar
   
Dries committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  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
159
160
161
162
163
164
165
166
167

  $theme->box(t("Reply"), comment_form($edit));
}

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

  if (user_access("post comments")) {

Dries's avatar
   
Dries committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    /*
    ** 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
186
    $duplicate = db_result(db_query("SELECT COUNT(cid) FROM comments WHERE pid = '". check_query($edit["pid"]) ."' AND nid = '". check_query($edit["nid"]) ."' AND subject = '". check_query($edit["subject"]) ."' AND comment = '". check_query($edit["comment"]) ."'"), 0);
Dries's avatar
   
Dries committed
187
188

    if ($duplicate != 0) {
Dries's avatar
   
Dries committed
189
      watchdog("warning", "comment: duplicate '". $edit["subject"] ."'");
Dries's avatar
   
Dries committed
190
191
192
    }
    else {

Dries's avatar
   
Dries committed
193
      if ($edit["cid"]) {
Dries's avatar
   
Dries committed
194

Dries's avatar
   
Dries committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
        /*
        ** Update the comment in the database.  Note that the update
        ** query will fail if the comment isn't owned by the current
        ** user.
        */

        db_query("UPDATE comments SET subject = '". check_query($edit["subject"]) ."', comment = '". check_query($edit["comment"]) ."' WHERE cid = '". check_query($edit["cid"]) ."' AND uid = '$user->uid'");

        /*
        ** 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:
        */

        db_query("INSERT INTO comments (nid, pid, uid, subject, comment, hostname, timestamp) VALUES ('". check_query($edit["nid"]) ."', '". check_query($edit["pid"]) ."', '$user->uid', '". check_query($edit["subject"]) ."', '". check_query($edit["comment"]) ."', '". getenv("REMOTE_ADDR") ."', '". time() ."')");

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

Dries's avatar
   
Dries committed
227
228
        watchdog("special", "comment: added '". $edit["subject"] ."'");
      }
Dries's avatar
   
Dries committed
229
230
231
232

      /*
      ** Clear the cache:
      */
Dries's avatar
   
Dries committed
233
234

      cache_clear();
Dries's avatar
   
Dries committed
235

Dries's avatar
   
Dries committed
236
237
    }
  }
Dries's avatar
   
Dries committed
238
239
240
241
242
243
244
245

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

  $url = "node.php?id=". $edit["nid"];
  drupal_goto($url);

Dries's avatar
   
Dries committed
246
247
}

Dries's avatar
   
Dries committed
248
function comment_num_replies($id) {
Dries's avatar
   
Dries committed
249
250
251
252
253
254
255
256
257

  $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
258
259
260
  // XXX: disabled for now
  return "";

Dries's avatar
   
Dries committed
261
  $values = array("--", "1", "2", "3", "4", "5");
Dries's avatar
   
Dries committed
262
263
264
265
266
267
268
269
270
271
272
273
274

  $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";
  }

 $output .= "<select name=\"moderate[comment][$comment->cid]\">$options</select><br />". ($comment->score ? $comment->score : "--") ." / $comment->votes";

  return $output;
}

function comment_threshold($threshold) {
Dries's avatar
   
Dries committed
275
276
277
  // XXX: disabled for now
  return "";

Dries's avatar
   
Dries committed
278
279
280
281
282
283
284
  for ($i = 0; $i < 6; $i++) $options .= " <option value=\"$i\"". ($threshold == $i ? " SELECTED" : "") .">". t("Visibility") ." - $i</option>";
  return "<select name=\"threshold\">$options</select>\n";
}

function comment_mode($mode) {
  global $cmodes;

Dries's avatar
   
Dries committed
285
  foreach ($cmodes as $key => $value) $options .= " <option value=\"$key\"". ($mode == $key ? " SELECTED" : "") .">$value</option>\n";
Dries's avatar
   
Dries committed
286
287
288
289
290
291
292
293
294
295
  return "<select name=\"mode\">$options</select>\n";
}

function comment_order($order) {
  global $corder;

  foreach ($corder as $key=>$value) $options .= " <option value=\"$key\"". ($order == $key ? " SELECTED" : "") .">$value</option>\n";
  return "<select name=\"order\">$options</select>\n";
}

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

Dries's avatar
   
Dries committed
298
  $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
299
300
301
302
303

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

Dries's avatar
   
Dries committed
304
  $query .= " GROUP BY c.cid, c.pid, c.nid, c.subject, c.comment, c.timestamp, u.uid, u.name";
Dries's avatar
   
Dries committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

  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
327
  global $user, $theme;
Dries's avatar
   
Dries committed
328

Dries's avatar
   
Dries committed
329
330
  $links = array();

Dries's avatar
   
Dries committed
331
  if ($return) {
Dries's avatar
   
Dries committed
332
    $links[] = "<a href=\"node.php?id=$comment->nid#$comment->cid\"><font color=\"$theme->type\">". t("return") ."</font></a>";
Dries's avatar
   
Dries committed
333
  }
Dries's avatar
   
Dries committed
334
335
336
337
338
339

  if (user_access("administer comments")) {
    $links[] = "<a href=\"admin.php?mod=comment&op=edit&id=$comment->cid\"><font color=\"$theme->type\">". t("administer") ."</font></a>";
  }

  if (user_access("post comments")) {
Dries's avatar
   
Dries committed
340
341
342
343
344
345
    if (comment_access("edit", $comment)) {
      $links[] = "<a href=\"module.php?mod=comment&op=edit&id=$comment->cid\"><font color=\"$theme->type\">". t("edit your comment") ."</font></a>";
    }
    else {
      $links[] = "<a href=\"module.php?mod=comment&op=reply&id=$comment->nid&pid=$comment->cid\"><font color=\"$theme->type\">". t("reply to this comment") ."</font></a>";
    }
Dries's avatar
   
Dries committed
346
  }
Dries's avatar
   
Dries committed
347

Dries's avatar
   
Dries committed
348

Dries's avatar
   
Dries committed
349
  return $theme->links($links);
Dries's avatar
   
Dries committed
350
351
352
353
354
355
356
357
358
}

function comment_view($comment, $folded = 0) {
  global $theme;

  if ($folded) {
    $theme->comment($comment, $folded);
  }
  else {
Dries's avatar
   
Dries committed
359
    print "<a href=\"node.php?id=$comment->nid&cid=$comment->cid#$comment->cid\">". check_output($comment->subject) ."</a> by ". format_name($comment) ."</small><p />";
Dries's avatar
   
Dries committed
360
361
362
  }
}

Dries's avatar
   
Dries committed
363
function comment_thread_min($comments, $threshold, $pid = 0) {
Dries's avatar
   
Dries committed
364
365
  global $user;

Dries's avatar
   
Dries committed
366
367
368
369
370
371
372
  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
373
374
375
  }
}

Dries's avatar
   
Dries committed
376
function comment_thread_max($comments, $threshold, $pid = 0, $level = 0) {
Dries's avatar
   
Dries committed
377
378
379
380
381
382
383
384
385
386
387
  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
388
389
390
  foreach ($comments as $comment) {
    if ($comment->pid == $pid) {
      print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td width=\"". ($level * 25) ."\">&nbsp;</td><td>\n";
Dries's avatar
   
Dries committed
391
      comment_view($comment, comment_links($comment, 0));
Dries's avatar
   
Dries committed
392
      print "</td></tr></table>\n";
Dries's avatar
   
Dries committed
393

Dries's avatar
   
Dries committed
394
395
      comment_thread_max($comments, $threshold, $comment->cid, $level + 1);
    }
Dries's avatar
   
Dries committed
396
397
398
399
  }

}

Dries's avatar
   
Dries committed
400
function comment_render($nid, $cid) {
Dries's avatar
   
Dries committed
401
402
403
404
405
406
407
408
  global $user, $theme, $mode, $order, $threshold, $REQUEST_URI;

  if (user_access("access comments")) {

    /*
    ** Pre-process variables:
    */

Dries's avatar
   
Dries committed
409
410
    if (empty($nid)) {
      $nid = 0;
Dries's avatar
   
Dries committed
411
412
413
414
415
416
417
    }

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

    if (empty($mode)) {
Dries's avatar
   
Dries committed
418
      $mode = $user->uid ? $user->mode : variable_get("default_comment_mode", 4);
Dries's avatar
   
Dries committed
419
420
421
    }

    if (empty($order)) {
Dries's avatar
   
Dries committed
422
      $order = $user->uid ? $user->sort : variable_get("default_comment_order", 1);
Dries's avatar
   
Dries committed
423
424
425
    }

    if (empty($threshold)) {
Dries's avatar
   
Dries committed
426
427
      // $threshold = $user->uid ? $user->threshold : variable_get("default_comment_threshold", 3);
      $threshold = 0;
Dries's avatar
   
Dries committed
428
429
430
431
432
433
434
435
436
437
438
439
    }

    print "<a name=\"comment\"></a>\n";
    print "<form method=\"post\" action=\"$REQUEST_URI\">\n";

    /*
    ** Render control panel:
    */

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

    if ($cid > 0) {
Dries's avatar
   
Dries committed
440
      $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
441
442
443
444
445
446
      if ($comment = db_fetch_object($result)) {
        comment_view($comment, comment_links($comment));
      }
    }
    else {
      if ($mode == 1) {
Dries's avatar
   
Dries committed
447
        $result = comment_query($nid, $order);
Dries's avatar
   
Dries committed
448
449
450
451
        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)) {
Dries's avatar
   
Dries committed
452
            print " <tr><td><a href=\"node.php?id=$comment->nid&cid=$comment->cid#$comment->cid\">". check_output($comment->subject) ."</a></td><td>". format_name($comment) ."</td><td>". format_date($comment->timestamp, "small") ."</td><td>$comment->score</td></tr>\n";
Dries's avatar
   
Dries committed
453
454
455
456
457
          }
        }
        print "</table>\n";
      }
      else if ($mode == 2) {
Dries's avatar
   
Dries committed
458
        $result = comment_query($nid, $order);
Dries's avatar
   
Dries committed
459
460
461
462
463
        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
464
        $result = comment_query($nid, $order);
Dries's avatar
   
Dries committed
465
        while ($comment = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
466
467
468
469
470
          $comments[] = $comment;
        }

        if ($comments) {
          comment_thread_min(array_reverse($comments), $threshold);
Dries's avatar
   
Dries committed
471
472
473
        }
      }
      else {
Dries's avatar
   
Dries committed
474
        $result = comment_query($nid, $order);
Dries's avatar
   
Dries committed
475
        while ($comment = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
476
477
478
479
480
          $comments[] = $comment;
        }

        if ($comments) {
          comment_thread_max(array_reverse($comments), $threshold);
Dries's avatar
   
Dries committed
481
482
483
484
485
486
487
488
489
        }
      }
    }

    print "</form>";

  }
}

Dries's avatar
   
Dries committed
490
function comment_search($keys) {
Dries's avatar
   
Dries committed
491
  global $PHP_SELF;
Dries's avatar
   
Dries committed
492
  $result = db_query("SELECT c.*, u.name FROM comments c LEFT JOIN users u ON c.uid = u.uid WHERE c.subject LIKE '%$keys%' OR c.comment LIKE '%$keys%' ORDER BY c.timestamp DESC LIMIT 20");
Dries's avatar
   
Dries committed
493
  while ($comment = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
494
    $find[$i++] = array("title" => check_output($comment->subject), "link" => (strstr($PHP_SELF, "admin.php") ? "admin.php?mod=comment&op=edit&id=$comment->cid" : "node.php?id=$comment->nid&cid=$comment->cid"), "user" => $comment->name, "date" => $comment->timestamp);
Dries's avatar
   
Dries committed
495
496
497
  }
  return $find;
}
Dries's avatar
 
Dries committed
498

Dries's avatar
   
Dries committed
499
function comment_perm() {
Dries's avatar
   
Dries committed
500
501
502
  return array("access comments", "post comments", "administer comments");
}

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

Dries's avatar
   
Dries committed
505
  if ($type == "admin" && user_access("administer comments")) {
Dries's avatar
   
Dries committed
506
507
508
    $links[] = "<a href=\"admin.php?mod=comment\">comments</a>";
  }

Dries's avatar
   
Dries committed
509
  if ($type == "node" && $node->comment) {
Dries's avatar
   
Dries committed
510
511
512
513
514
515
516
517

    if ($main) {

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

      if (user_access("access comments")) {
Dries's avatar
   
Dries committed
518
519
520
521
        $all = comment_num_all($node->nid);
        $new = comment_num_new($node->nid);

        $links[] = "<a href=\"node.php?id=$node->nid#comment\">". format_plural($all, "comment", "comments") . ($new ? ", $new ". t("new") : "") ."</a>";
Dries's avatar
   
Dries committed
522
523
524
      }
    }
    else {
Dries's avatar
   
Dries committed
525
526
527
528
529
530
      /*
      ** Tag the node's comments as read:
      */

      comment_tag_new($node->nid);

Dries's avatar
   
Dries committed
531
532
533
534
535
536
      /*
      ** Node page: add a "post comment" link if the user is allowed to
      ** post comments.
      */

      if (user_access("post comments")) {
Dries's avatar
   
Dries committed
537
        $links[] = "<a href=\"module.php?mod=comment&op=reply&id=$node->nid#comment\">". t("add new comment") ."</a>";
Dries's avatar
   
Dries committed
538
539
540
541
      }
    }
  }

Dries's avatar
   
Dries committed
542
  return $links ? $links : array();
Dries's avatar
   
Dries committed
543
544
}

Dries's avatar
   
Dries committed
545
546
function comment_node_link($node) {

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

Dries's avatar
   
Dries committed
549
550
551
    /*
    ** Edit comments:
    */
Dries's avatar
   
Dries committed
552

Dries's avatar
   
Dries committed
553
    $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
554

Dries's avatar
   
Dries committed
555
556
557
558
559
560
561
562
563
564
565
566
    $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)) {
      $output .= "<tr><td><a href=\"node.php?id=$node->nid&cid=$comment->cid#$comment->cid\">$comment->subject</a></td><td>". format_name($comment) ."</td><td><a href=\"node.php?id=$node->nid&cid=$comment->cid#$comment->cid\">". t("view comment") ."</a></td><td><a href=\"admin.php?mod=comment&op=edit&id=$comment->cid\">". t("edit comment") ."</a></td><td><a href=\"admin.php?mod=comment&op=delete&id=$comment->cid\">". t("delete comment") ."</a></td></tr>";
    }

    $output .= "</table>";

    return $output;
  }
Dries's avatar
   
Dries committed
567
568
}

Dries's avatar
   
Dries committed
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604

function comment_save($id, $edit) {
  db_query("UPDATE comments SET subject = '". check_query(filter($edit["subject"])) ."', comment = '". check_query(filter($edit["comment"])) ."' WHERE cid = '$id'");
  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"):
      comment_settings(check_query($mode), check_query($order), check_query($threshold));
      break;
    default:
  }
}

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

Dries's avatar
   
Dries committed
606
  $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
607
608
  $comment = db_fetch_object($result);

Dries's avatar
   
Dries committed
609
  $form .= form_item(t("Author"), format_name($comment));
Dries's avatar
   
Dries committed
610
611
  $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
612
  $form .= form_hidden("cid", $id);
Dries's avatar
   
Dries committed
613
  $form .= form_submit(t("Submit"));
Dries's avatar
   
Dries committed
614
  $form .= form_submit(t("Delete"));
Dries's avatar
 
Dries committed
615

Dries's avatar
   
Dries committed
616
  return form($form);
Dries's avatar
 
Dries committed
617
618
}

Dries's avatar
   
Dries committed
619
function comment_admin_overview() {
Dries's avatar
   
Dries committed
620
  $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
621

Dries's avatar
   
Dries committed
622
623
  $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
624
  while ($comment = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
625
    $output .= " <tr><td><a href=\"node.php?id=$comment->nid&cid=$comment->cid&pid=$comment->pid#$comment->cid\">". check_output($comment->subject) ."</a></td><td>". format_name($comment) ."</td><td>". format_date($comment->timestamp, "small") ."</td><td><a href=\"admin.php?mod=comment&op=edit&id=$comment->cid\">edit comment</a></td><td><a href=\"admin.php?mod=comment&op=delete&id=$comment->cid\">delete comment</a></td></tr>\n";
Dries's avatar
 
Dries committed
626
  }
Dries's avatar
   
Dries committed
627
  $output .= "</table>\n";
Dries's avatar
   
Dries committed
628

Dries's avatar
   
Dries committed
629
  return $output;
Dries's avatar
 
Dries committed
630
631
}

Dries's avatar
   
Dries committed
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
function comment_delete($edit) {

  if ($edit["confirm"]) {
    db_query("DELETE FROM comments WHERE cid = '". check_query($edit["cid"]) ."'");
    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
647
648
}

Dries's avatar
 
Dries committed
649
function comment_admin() {
Dries's avatar
   
Dries committed
650
  global $op, $id, $edit, $mod, $keys, $order;
Dries's avatar
 
Dries committed
651

Dries's avatar
   
Dries committed
652
  if (user_access("administer comments")) {
Dries's avatar
   
Dries committed
653

Dries's avatar
   
Dries committed
654
    print "<small><a href=\"admin.php?mod=comment\">overview</a> | <a href=\"admin.php?mod=comment&op=search\">search comment</a></small><hr />\n";
Dries's avatar
   
Dries committed
655
656
657

    switch ($op) {
      case "edit":
Dries's avatar
   
Dries committed
658
        print comment_admin_edit($id);
Dries's avatar
   
Dries committed
659
660
        break;
      case "search":
Dries's avatar
   
Dries committed
661
        print search_type("comment", "admin.php?mod=comment&op=search");
Dries's avatar
   
Dries committed
662
        break;
Dries's avatar
   
Dries committed
663
      case "delete":
Dries's avatar
   
Dries committed
664
665
666
667
        print comment_delete(array("cid" => $id));
        break;
      case t("Delete"):
        print comment_delete($edit);
Dries's avatar
   
Dries committed
668
        break;
Dries's avatar
   
Dries committed
669
      case t("Submit"):
Dries's avatar
   
Dries committed
670
        print status(comment_save(check_query($id), $edit));
Dries's avatar
   
Dries committed
671
        print comment_admin_overview();
Dries's avatar
   
Dries committed
672
673
        break;
      default:
Dries's avatar
   
Dries committed
674
        print comment_admin_overview();
Dries's avatar
   
Dries committed
675
676
677
678
    }
  }
  else {
    print message_access();
Dries's avatar
 
Dries committed
679
680
  }
}
Dries's avatar
   
Dries committed
681

Dries's avatar
   
Dries committed
682
?>