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

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

Dries Buytaert's avatar
   
Dries Buytaert 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 Mannes's avatar
Kjartan Mannes 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 Buytaert's avatar
   
Dries Buytaert committed
12
13
14
15

  return $output;
}

16
function comment_system($field){
17
  $system["description"] = t("Enables user to comment on content (nodes).");
18
19
20
  return $system[$field];
}

Dries Buytaert's avatar
   
Dries Buytaert committed
21
function comment_settings($mode, $order, $threshold) {
Dries Buytaert's avatar
   
Dries Buytaert committed
22
23
  global $user;

Dries Buytaert's avatar
   
Dries Buytaert committed
24
25
  if ($user->uid) {
    $user = user_save($user, array("mode" => $mode, "sort" => $order, "threshold" => $threshold));
Dries Buytaert's avatar
   
Dries Buytaert committed
26
27
28
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 Buytaert's avatar
   
Dries Buytaert committed
45
46
    $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 Buytaert's avatar
   
Dries Buytaert committed
47
48
49
50
51
52
53
54
55
56
57
58
59

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

}

function comment_tag_new($nid) {
  global $user;

  if ($user->uid) {
Dries Buytaert's avatar
   
Dries Buytaert committed
60
61
    $nid = check_query($nid);

Dries Buytaert's avatar
   
Dries Buytaert committed
62
63
64
65
66
67
68
69
70
71
    $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 Buytaert's avatar
   
Dries Buytaert committed
72
73
74
function comment_is_new($comment) {
  global $user;
  static $date;
Dries Buytaert's avatar
   
Dries Buytaert committed
75

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

Dries Buytaert's avatar
   
Dries Buytaert committed
86
  if ($comment->timestamp > $date[$comment->nid]) {
Dries Buytaert's avatar
   
Dries Buytaert committed
87
88
89
90
91
92
93
    return 1;
  }
  else {
    return 0;
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
94
function comment_access($op, $comment) {
Dries Buytaert's avatar
   
Dries Buytaert committed
95
96
  global $user;

Dries Buytaert's avatar
   
Dries Buytaert committed
97
98
99
100
101
102
103
104
105
106
107
  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 Buytaert's avatar
   
Dries Buytaert committed
108
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
109

Dries Buytaert's avatar
   
Dries Buytaert committed
110
111
112
113
114
115
116
117
118
119
120
}

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

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

  // preview button:
Dries Buytaert's avatar
   
Dries Buytaert committed
127
  $form .= form_hidden("cid", $edit["cid"]);
Dries Buytaert's avatar
   
Dries Buytaert committed
128
  $form .= form_hidden("pid", $edit["pid"]);
Dries Buytaert's avatar
   
Dries Buytaert committed
129
  $form .= form_hidden("nid", $edit["nid"]);
Dries Buytaert's avatar
   
Dries Buytaert committed
130

Dries Buytaert's avatar
   
Dries Buytaert committed
131
  if (!$edit["comment"]) {
Dries Buytaert's avatar
   
Dries Buytaert committed
132
133
134
135
136
137
138
139
140
141
    $form .= form_submit(t("Preview comment"));
  }
  else {
    $form .= form_submit(t("Preview comment"));
    $form .= form_submit(t("Post comment"));
  }

  return form($form);
}

Dries Buytaert's avatar
   
Dries Buytaert committed
142
143
144
145
146
147
148
149
150
151
152
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) {
153
  global $theme;
Dries Buytaert's avatar
   
Dries Buytaert committed
154

155
  if (user_access("access comments")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
156
    if ($pid) {
Dries Buytaert's avatar
   
Dries Buytaert committed
157
      $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 Buytaert's avatar
   
Dries Buytaert committed
158
159
160
161
162
163
      comment_view($comment, t("reply to this comment"));
    }
    else {
      node_view(node_load(array("nid" => $nid)));
      $pid = 0;
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
164

Dries Buytaert's avatar
   
Dries Buytaert committed
165
166
    if (node_comment_mode($nid) == 1) {
      $theme->box(t("Reply"), t("This discussion is closed: you can't post new comments."));
Kjartan Mannes's avatar
Kjartan Mannes committed
167
168
    }
    else if (user_access("post comments", $context)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
169
170
171
172
173
      $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 Mannes's avatar
Kjartan Mannes committed
174
175
  }
  else {
Dries Buytaert's avatar
   
Dries Buytaert committed
176
    $theme->box(t("Reply"), t("You are not authorized to view comments."));
Dries Buytaert's avatar
   
Dries Buytaert committed
177
178
179
180
181
182
  }
}

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

Dries Buytaert's avatar
   
Dries Buytaert committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  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 Buytaert's avatar
   
Dries Buytaert committed
205
206

  $theme->box(t("Reply"), comment_form($edit));
Kjartan Mannes's avatar
Kjartan Mannes committed
207
208

  if ($edit["pid"]) {
Kjartan Mannes's avatar
Kjartan Mannes committed
209
    $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 Mannes's avatar
Kjartan Mannes committed
210
211
212
213
214
215
    comment_view($comment, t("reply to this comment"));
  }
  else {
    node_view(node_load(array("nid" => $edit["nid"])));
    $edit["pid"] = 0;
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
216
217
218
219
220
}

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

Dries Buytaert's avatar
   
Dries Buytaert committed
221
222
  $context->nid = $edit["nid"];
  if (user_access("post comments", $context) && node_comment_mode($edit["nid"]) == 2) {
Dries Buytaert's avatar
   
Dries Buytaert committed
223

Dries Buytaert's avatar
   
Dries Buytaert committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    /*
    ** 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 Buytaert's avatar
   
Dries Buytaert committed
242
    $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 Buytaert's avatar
   
Dries Buytaert committed
243
244

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

Dries Buytaert's avatar
   
Dries Buytaert committed
249
      if ($edit["cid"]) {
Dries Buytaert's avatar
   
Dries Buytaert committed
250

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

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

        /*
        ** 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 Buytaert's avatar
   
Dries Buytaert committed
277
        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 Buytaert's avatar
   
Dries Buytaert committed
278
279
280
281

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

Dries Buytaert's avatar
   
Dries Buytaert committed
283
284
        watchdog("special", "comment: added '". $edit["subject"] ."'");
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
285
286
287
288

      /*
      ** Clear the cache:
      */
Dries Buytaert's avatar
   
Dries Buytaert committed
289
290

      cache_clear();
Dries Buytaert's avatar
   
Dries Buytaert committed
291

Dries Buytaert's avatar
   
Dries Buytaert committed
292
293
    }
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
294
295
296
297
298

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

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

Dries Buytaert's avatar
   
Dries Buytaert committed
301
302
}

Dries Buytaert's avatar
   
Dries Buytaert committed
303
function comment_num_replies($id) {
Dries Buytaert's avatar
   
Dries Buytaert committed
304
305
306
307
308
309
310
311
312

  $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 Buytaert's avatar
   
Dries Buytaert committed
313
314
315
  // XXX: disabled for now
  return "";

Dries Buytaert's avatar
   
Dries Buytaert committed
316
  $values = array("--", "1", "2", "3", "4", "5");
Dries Buytaert's avatar
   
Dries Buytaert committed
317
318
319
320
321
322
323

  $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 Buytaert's avatar
   
Dries Buytaert committed
324
  $output .= "<select name=\"moderate[comment][$comment->cid]\">$options</select><br />". ($comment->score ? $comment->score : "--") ." / $comment->votes";
Dries Buytaert's avatar
   
Dries Buytaert committed
325
326
327
328
329

  return $output;
}

function comment_threshold($threshold) {
Dries Buytaert's avatar
   
Dries Buytaert committed
330
331
332
  // XXX: disabled for now
  return "";

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

function comment_mode($mode) {
  global $cmodes;

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

function comment_order($order) {
  global $corder;

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

Dries Buytaert's avatar
   
Dries Buytaert committed
351
function comment_query($nid, $order, $pid = -1) {
Dries Buytaert's avatar
   
Dries Buytaert committed
352

Dries Buytaert's avatar
   
Dries Buytaert committed
353
  $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 Buytaert's avatar
   
Dries Buytaert committed
354
355
356
357
358

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

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

  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 Buytaert's avatar
   
Dries Buytaert committed
382
  global $user, $theme;
Dries Buytaert's avatar
   
Dries Buytaert committed
383

Dries Buytaert's avatar
   
Dries Buytaert committed
384
385
  $links = array();

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

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

Dries Buytaert's avatar
   
Dries Buytaert committed
394
395
396
397
398
399
  /*
  ** 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 the next line and
  ** related bracket some lines below.
  */
Dries Buytaert's avatar
   
Dries Buytaert committed
400
401

  //if (node_comment_mode($comment->nid)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
402
  if (user_access("post comments")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
403
    if (comment_access("edit", $comment)) {
Kjartan Mannes's avatar
Kjartan Mannes committed
404
      $links[] = lm("<span style=\"color: $theme->type\">". t("edit your comment") ."</span>", array("mod" => "comment", "op" => "edit", "id" => $comment->cid), "", array("title" => t("Make changes to your comment.")));
Dries Buytaert's avatar
   
Dries Buytaert committed
405
406
    }
    else {
Kjartan Mannes's avatar
Kjartan Mannes committed
407
      $links[] = lm("<span style=\"color: $theme->type;\">". t("reply to this comment") ."</span>", array("mod" => "comment", "op" => "reply", "id" => $comment->nid, "pid" => $comment->cid), "", array("title" => t("Reply to this comment.")));
Dries Buytaert's avatar
   
Dries Buytaert committed
408
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
409
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
410
  //}
Dries Buytaert's avatar
   
Dries Buytaert committed
411
412

  return $theme->links($links);
Dries Buytaert's avatar
   
Dries Buytaert committed
413
414
415
}

function comment_view($comment, $folded = 0) {
Dries Buytaert's avatar
   
Dries Buytaert committed
416
417
418
  global $theme, $id;

  if (comment_is_new($comment)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
419
    $comment->subject = "$comment->subject <span style=\"color: red;\">*</span>";
Dries Buytaert's avatar
   
Dries Buytaert committed
420
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
421
422
423
424
425

  if ($folded) {
    $theme->comment($comment, $folded);
  }
  else {
Dries Buytaert's avatar
   
Dries Buytaert committed
426
    print l(check_output($comment->subject), array("id" => $comment->nid, "cid" => $comment->cid), "node", $comment->cid) ." ". t("by") ." ". format_name($comment) ."</small><p />";
Dries Buytaert's avatar
   
Dries Buytaert committed
427
428
429
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
430
function comment_thread_min($comments, $threshold, $pid = 0) {
Dries Buytaert's avatar
   
Dries Buytaert committed
431
432
  global $user;

Dries Buytaert's avatar
   
Dries Buytaert committed
433
434
435
436
437
438
439
  foreach ($comments as $comment) {
    if ($comment->pid == $pid) {
      print "<ul>";
      print comment_view($comment);
      comment_thread_min($comments, $threshold, $comment->cid);
      print "</ul>";
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
440
441
442
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
443
function comment_thread_max($comments, $threshold, $pid = 0, $level = 0) {
Dries Buytaert's avatar
   
Dries Buytaert committed
444
445
446
447
448
449
450
451
452
453
454
  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 Buytaert's avatar
   
Dries Buytaert committed
455
456
  foreach ($comments as $comment) {
    if ($comment->pid == $pid) {
Steven Wittens's avatar
Steven Wittens committed
457
458
459
      if ($level) {
        print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td width=\"". ($level * 25) ."\">&nbsp;</td><td>\n";
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
460
      comment_view($comment, comment_links($comment, 0));
Steven Wittens's avatar
Steven Wittens committed
461
462
463
      if ($level) {
        print "</td></tr></table>\n";
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
464

Dries Buytaert's avatar
   
Dries Buytaert committed
465
466
      comment_thread_max($comments, $threshold, $comment->cid, $level + 1);
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
467
468
469
470
  }

}

Kjartan Mannes's avatar
Kjartan Mannes committed
471
function comment_render($nid, $cid = 0) {
Dries Buytaert's avatar
   
Dries Buytaert committed
472
  global $user, $theme, $mode, $order, $threshold;
Dries Buytaert's avatar
   
Dries Buytaert committed
473
474
475
476
477
478
479

  if (user_access("access comments")) {

    /*
    ** Pre-process variables:
    */

Dries Buytaert's avatar
   
Dries Buytaert committed
480
481
    if (empty($nid)) {
      $nid = 0;
Dries Buytaert's avatar
   
Dries Buytaert committed
482
483
484
    }

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

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

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

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

    /*
    ** Render control panel:
    */

505
506
507
    if (comment_num_all($nid)) {
      $theme->box(t("Control panel"), $theme->comment_controls($threshold, $mode, $order));
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
508

Kjartan Mannes's avatar
Kjartan Mannes committed
509
    if ($cid) {
Dries Buytaert's avatar
   
Dries Buytaert committed
510
      $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 Buytaert's avatar
   
Dries Buytaert committed
511
512
513
514
      if ($comment = db_fetch_object($result)) {
        comment_view($comment, comment_links($comment));
      }
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
515
516
517
518

    if ($cid) {
      $result = comment_query($nid, $order, $cid);
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
519
    else {
Dries Buytaert's avatar
   
Dries Buytaert committed
520
521
522
523
      $result = comment_query($nid, $order);
    }

    if ($mode == 1) {
524
      if (db_num_rows($result)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
525
        print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
526
        print " <tr><th>". t("Subject") ."</th><th>". t("Author") ."</th><th>". t("Date") ."</th></tr>\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
527
528
        while ($comment = db_fetch_object($result)) {
          if (comment_visible($comment, $threshold)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
529
            print " <tr><td>". l(check_output($comment->subject), array("id" => $comment->nid, "cid" => $comment->cid), "node", $comment->cid) ."</td><td>". format_name($comment) ."</td><td>". format_date($comment->timestamp, "small") ."</td></tr>\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
530
531
532
533
          }
        }
        print "</table>\n";
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
534
535
536
537
538
539
540
541
542
    }
    else if ($mode == 2) {
      while ($comment = db_fetch_object($result)) {
        comment_view($comment, (comment_visible($comment, $threshold) ? comment_links($comment, 0) : 0));
      }
    }
    else if ($mode == 3) {
      while ($comment = db_fetch_object($result)) {
        $comments[] = $comment;
Dries Buytaert's avatar
   
Dries Buytaert committed
543
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
544

Dries Buytaert's avatar
   
Dries Buytaert committed
545
      if ($comments) {
Kjartan Mannes's avatar
Kjartan Mannes committed
546
        if ($cid) {
Dries Buytaert's avatar
   
Dries Buytaert committed
547
548
549
          comment_thread_min($comments, $threshold, $cid, 1);
        }
        else {
Dries Buytaert's avatar
   
Dries Buytaert committed
550
          comment_thread_min($comments, $threshold);
Dries Buytaert's avatar
   
Dries Buytaert committed
551
552
        }
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
553
554
555
556
557
    }
    else {
      while ($comment = db_fetch_object($result)) {
        $comments[] = $comment;
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
558

Dries Buytaert's avatar
   
Dries Buytaert committed
559
560
561
562
563
      if ($comments) {
        if ($cid) {
          comment_thread_max($comments, $threshold, $cid, 1);
        }
        else {
Kjartan Mannes's avatar
Kjartan Mannes committed
564
          comment_thread_max($comments, $threshold, 0, 0);
Dries Buytaert's avatar
   
Dries Buytaert committed
565
566
567
568
569
570
        }
      }
    }

    print "</form>";

Dries Buytaert's avatar
   
Dries Buytaert committed
571
572
573
574
575
    /*
    ** Tag the node's comments as being read:
    */

    comment_tag_new($nid);
Dries Buytaert's avatar
   
Dries Buytaert committed
576
577
578
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
579
function comment_search($keys) {
Dries Buytaert's avatar
   
Dries Buytaert committed
580
  global $PHP_SELF;
Kjartan Mannes's avatar
Kjartan Mannes committed
581

Dries Buytaert's avatar
   
Dries Buytaert committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
  /*
  ** 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
  ** table to the data that the search_index table has in it, and the the
  ** do_search functino will rank it.
  **
  ** The select must always provide the following fields - lno, title,
  ** 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 Mannes's avatar
Kjartan Mannes committed
605
  $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 Buytaert's avatar
   
Dries Buytaert committed
606

Dries Buytaert's avatar
   
Dries Buytaert committed
607
608
  return $find;
}
Dries Buytaert's avatar
 
Dries Buytaert committed
609

Dries Buytaert's avatar
   
Dries Buytaert committed
610
function comment_perm() {
Dries Buytaert's avatar
   
Dries Buytaert committed
611
612
613
  return array("access comments", "post comments", "administer comments");
}

Dries Buytaert's avatar
   
Dries Buytaert committed
614
function comment_link($type, $node = 0, $main = 0) {
Dries Buytaert's avatar
   
Dries Buytaert committed
615

Dries Buytaert's avatar
   
Dries Buytaert committed
616
  if ($type == "admin" && user_access("administer comments")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
617
    $links[] = la(t("comments"), array("mod" => "comment"));
Dries Buytaert's avatar
   
Dries Buytaert committed
618
619
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
620
  if ($type == "node" && $node->comment) {
Dries Buytaert's avatar
   
Dries Buytaert committed
621
622
623
624
625
626
627
628

    if ($main) {

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

      if (user_access("access comments")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
629
630
631
        $all = comment_num_all($node->nid);
        $new = comment_num_new($node->nid);

Kjartan Mannes's avatar
Kjartan Mannes committed
632
        $links[] = l(format_plural($all, "comment", "comments") . ($new ? ", $new ". t("new") : ""), array("id" => $node->nid), "node", "comment", array("title" => t("View this posting and all of its comments.")));
Dries Buytaert's avatar
   
Dries Buytaert committed
633
634
635
636
637
      }
    }
    else {
      /*
      ** Node page: add a "post comment" link if the user is allowed to
Dries Buytaert's avatar
   
Dries Buytaert committed
638
      ** post comments and if this node is not read-only
Dries Buytaert's avatar
   
Dries Buytaert committed
639
640
641
      */

      if (user_access("post comments")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
642
        if ($node->comment == 2) {
Kjartan Mannes's avatar
Kjartan Mannes committed
643
          $links[] = lm(t("add new comment"), array("mod" => "comment", "op" => "reply", "id" => $node->nid), "comment", array("title" => t("Share your thoughts and opinions related to this posting.")));
Kjartan Mannes's avatar
Kjartan Mannes committed
644
645
        }
        else {
Dries Buytaert's avatar
   
Dries Buytaert committed
646
647
          $links[] = t("This discussion is closed: you can't post new comments.");
        }
Dries Buytaert's avatar
   
Dries Buytaert committed
648
649
650
651
      }
    }
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
652
  return $links ? $links : array();
Dries Buytaert's avatar
   
Dries Buytaert committed
653
654
}

Dries Buytaert's avatar
   
Dries Buytaert committed
655
656
function comment_node_link($node) {

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

Dries Buytaert's avatar
   
Dries Buytaert committed
659
660
661
    /*
    ** Edit comments:
    */
Dries Buytaert's avatar
   
Dries Buytaert committed
662

Dries Buytaert's avatar
   
Dries Buytaert committed
663
    $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 Buytaert's avatar
   
Dries Buytaert committed
664

Dries Buytaert's avatar
   
Dries Buytaert committed
665
666
667
668
669
    $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 Mannes's avatar
Kjartan Mannes committed
670
      $output .= "<tr><td>". l($comment->subject, array("id" => $node->nid, "cid" => $comment->cid), "node", $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 Buytaert's avatar
   
Dries Buytaert committed
671
672
673
674
675
676
    }

    $output .= "</table>";

    return $output;
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
677
678
}

Dries Buytaert's avatar
   
Dries Buytaert committed
679
680

function comment_save($id, $edit) {
Dries Buytaert's avatar
   
Dries Buytaert committed
681
  db_query("UPDATE comments SET subject = '%s', comment = '%s' WHERE cid = '$id'", filter($edit["subject"]), filter($edit["comment"]));
Dries Buytaert's avatar
   
Dries Buytaert committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
  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"):
708
      global $mode, $order, $threshold;
Dries Buytaert's avatar
   
Dries Buytaert committed
709
      comment_settings(check_query($mode), check_query($order), check_query($threshold));
Dries Buytaert's avatar
   
Dries Buytaert committed
710
      drupal_goto(drupal_url(array("id" => $edit["nid"], "mode" => $mode, "order" => $order), "node"));
Dries Buytaert's avatar
   
Dries Buytaert committed
711
712
713
714
715
716
      break;
    default:
  }
}

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

Dries Buytaert's avatar
   
Dries Buytaert committed
718
  $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 Buytaert's avatar
 
Dries Buytaert committed
719
720
  $comment = db_fetch_object($result);

Dries Buytaert's avatar
   
Dries Buytaert committed
721
  $form .= form_item(t("Author"), format_name($comment));
Dries Buytaert's avatar
   
Dries Buytaert committed
722
723
  $form .= form_textfield(t("Subject"), "subject", $comment->subject, 70, 128);
  $form .= form_textarea(t("Comment"), "comment", $comment->comment, 70, 15);
Dries Buytaert's avatar
   
Dries Buytaert committed
724
  $form .= form_hidden("cid", $id);
Dries Buytaert's avatar
   
Dries Buytaert committed
725
  $form .= form_submit(t("Submit"));
Dries Buytaert's avatar
   
Dries Buytaert committed
726
  $form .= form_submit(t("Delete"));
Dries Buytaert's avatar
 
Dries Buytaert committed
727

Dries Buytaert's avatar
   
Dries Buytaert committed
728
  return form($form);
Dries Buytaert's avatar
 
Dries Buytaert committed
729
730
}

Dries Buytaert's avatar
   
Dries Buytaert committed
731
function comment_admin_overview() {
Dries Buytaert's avatar
   
Dries Buytaert committed
732
  $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 Buytaert's avatar
   
Dries Buytaert committed
733

Dries Buytaert's avatar
   
Dries Buytaert committed
734
735
  $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 Buytaert's avatar
 
Dries Buytaert committed
736
  while ($comment = db_fetch_object($result)) {
Kjartan Mannes's avatar
Kjartan Mannes committed
737
    $output .= " <tr><td>". l(check_output($comment->subject), array("id" => $comment->nid, "cid" => $comment->cid, "pid" => $comment->pid), "node", $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 Buytaert's avatar
 
Dries Buytaert committed
738
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
739
  $output .= "</table>\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
740

Dries Buytaert's avatar
   
Dries Buytaert committed
741
  return $output;
Dries Buytaert's avatar
 
Dries Buytaert committed
742
743
}

Dries Buytaert's avatar
   
Dries Buytaert committed
744
745
746
function comment_delete($edit) {

  if ($edit["confirm"]) {
Dries Buytaert's avatar
   
Dries Buytaert committed
747
    db_query("DELETE FROM comments WHERE cid = '%s'", $edit["cid"]);
Dries Buytaert's avatar
   
Dries Buytaert committed
748
749
750
751
752
753
754
755
756
757
758
    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 Buytaert's avatar
   
Dries Buytaert committed
759
760
}

Dries Buytaert's avatar
 
Dries Buytaert committed
761
function comment_admin() {
Dries Buytaert's avatar
   
Dries Buytaert committed
762
  global $op, $id, $edit, $mod, $keys, $order;
Dries Buytaert's avatar
 
Dries Buytaert committed
763

Dries Buytaert's avatar
   
Dries Buytaert committed
764
  if (user_access("administer comments")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
765

Kjartan Mannes's avatar
Kjartan Mannes committed
766
    print "<small>". la(t("overview"), array("mod" => "comment")) ." | ". la(t("search comment"), array("mod" => "comment", "op" => "search")) ."</small><hr />\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
767
768
769

    switch ($op) {
      case "edit":
Dries Buytaert's avatar
   
Dries Buytaert committed
770
        print comment_admin_edit($id);
Dries Buytaert's avatar
   
Dries Buytaert committed
771
772
        break;
      case "search":
Dries Buytaert's avatar
   
Dries Buytaert committed
773
        print search_type("comment", drupal_url(array("mod" => "comment", "op" => "search"), "admin"));
Dries Buytaert's avatar
   
Dries Buytaert committed
774
        break;
Dries Buytaert's avatar
   
Dries Buytaert committed
775
      case "delete":
Dries Buytaert's avatar
   
Dries Buytaert committed
776
777
778
779
        print comment_delete(array("cid" => $id));
        break;
      case t("Delete"):
        print comment_delete($edit);
Dries Buytaert's avatar
   
Dries Buytaert committed
780
        break;
Dries Buytaert's avatar
   
Dries Buytaert committed
781
      case t("Submit"):
Dries Buytaert's avatar
   
Dries Buytaert committed
782
        print status(comment_save(check_query($id), $edit));
Dries Buytaert's avatar
   
Dries Buytaert committed
783
        print comment_admin_overview();
Dries Buytaert's avatar
   
Dries Buytaert committed
784
785
        break;
      default:
Dries Buytaert's avatar
   
Dries Buytaert committed
786
        print comment_admin_overview();
Dries Buytaert's avatar
   
Dries Buytaert committed
787
788
789
790
    }
  }
  else {
    print message_access();
Dries Buytaert's avatar
 
Dries Buytaert committed
791
792
  }
}
Dries Buytaert's avatar
   
Dries Buytaert committed
793

Kjartan Mannes's avatar
Kjartan Mannes committed
794
795
function comment_update_index() {

Dries Buytaert's avatar
   
Dries Buytaert committed
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  /*
  ** Return an array of values to dictate how to update the search index
  ** for this particular type of node.
  **
  ** "last_update"'s value is used with variable_set to set the
  ** 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
  ** the table we are indexing. In this case, we also check against the
  ** last run date for the comments update.
  */

Kjartan Mannes's avatar
Kjartan Mannes committed
811
  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 Mannes's avatar
Kjartan Mannes committed
812
813
}

Dries Buytaert's avatar
   
Dries Buytaert committed
814
?>