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

Dries's avatar
 
Dries committed
4
function comment_search($keys) {
Dries's avatar
 
Dries committed
5
  global $PHP_SELF;
Dries's avatar
 
Dries committed
6
  $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
7
  while ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
8
    $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->lid&cid=$comment->cid"), "user" => $comment->name, "date" => $comment->timestamp);
Dries's avatar
 
Dries committed
9 10 11
  }
  return $find;
}
Dries's avatar
 
Dries committed
12

Dries's avatar
 
Dries committed
13
function comment_perm() {
Dries's avatar
 
Dries committed
14 15 16
  return array("access comments", "post comments", "administer comments");
}

Dries's avatar
 
Dries committed
17
function comment_link($type, $node = 0, $main = 0) {
Dries's avatar
 
Dries committed
18 19 20 21
  if ($type == "admin" and user_access("administer comments")) {
    $links[] = "<a href=\"admin.php?mod=comment\">comments</a>";
  }

Dries's avatar
 
Dries committed
22
  if ($type == "node" && $node->comment) {
Dries's avatar
 
Dries committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

    if ($main) {

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

      if (user_access("access comments")) {
        $links[] = "<a href=\"node.php?id=$node->nid#comment\">". format_plural(node_get_comments($node->nid), "comment", "comments") ."</a>";
      }
    }
    else {
      /*
      ** Node page: add a "post comment" link if the user is allowed to
      ** post comments.
      */

      if (user_access("post comments")) {
        $links[] = "<a href=\"node.php?id=$node->nid&op=comment#comment\">". t("add new comment") ."</a>";
      }
    }
  }

Dries's avatar
 
Dries committed
46
  return $links ? $links : array();
Dries's avatar
 
Dries committed
47 48
}

Dries's avatar
 
Dries committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
function comment_node_link($node) {

  /*
  ** Edit comments:
  */

  $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 lid = '$node->nid' ORDER BY c.timestamp");
  $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
68
function comment_edit($id) {
Dries's avatar
 
Dries committed
69

Dries's avatar
 
Dries committed
70
  $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
71 72
  $comment = db_fetch_object($result);

Dries's avatar
 
Dries committed
73
  $form .= form_item(t("Author"), format_name($comment));
Dries's avatar
 
Dries committed
74 75
  $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
76
  $form .= form_submit(t("Submit"));
Dries's avatar
 
Dries committed
77

Dries's avatar
 
Dries committed
78
  return form($form);
Dries's avatar
 
Dries committed
79 80
}

Dries's avatar
 
Dries committed
81
function comment_save($id, $edit) {
Dries's avatar
 
Dries committed
82
  db_query("UPDATE comments SET subject = '". check_input(filter($edit[subject])) ."', comment = '". check_input(filter($edit[comment])) ."' WHERE cid = '$id'");
Dries's avatar
Dries committed
83
  watchdog("special", "comment: modified '$edit[subject]'");
Dries's avatar
 
Dries committed
84 85
}

Dries's avatar
 
Dries committed
86
function comment_overview() {
Dries's avatar
 
Dries committed
87
  $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
88

Dries's avatar
 
Dries committed
89
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
 
Dries committed
90
  $output .= " <TR><TH>subject</TH><TH>author</TH><TH>date</TH><TH COLSPAN=\"2\">operations</TH></TR>\n";
Dries's avatar
 
Dries committed
91
  while ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
92
    $output .= " <TR><TD><A HREF=\"node.php?id=$comment->lid&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
93 94
  }
  $output .= "</TABLE>\n";
Dries's avatar
 
Dries committed
95

Dries's avatar
 
Dries committed
96
  return $output;
Dries's avatar
 
Dries committed
97 98
}

Dries's avatar
 
Dries committed
99 100
function comment_delete($id) {
  db_query("DELETE FROM comments WHERE cid = '$id'");
Dries's avatar
 
Dries committed
101
  db_query("DELETE FROM moderate WHERE cid = '$id'");
Dries's avatar
 
Dries committed
102 103 104
  watchdog("special", "comment: deleted '$id'");
}

Dries's avatar
 
Dries committed
105
function comment_admin() {
Dries's avatar
 
Dries committed
106
  global $op, $id, $edit, $mod, $keys, $order;
Dries's avatar
 
Dries committed
107

Dries's avatar
 
Dries committed
108
  if (user_access("administer comments")) {
Dries's avatar
 
Dries committed
109

Dries's avatar
 
Dries committed
110 111 112 113 114 115 116
    print "<SMALL><A HREF=\"admin.php?mod=comment\">overview</A> | <A HREF=\"admin.php?mod=comment&op=search\">search comment</A></SMALL><HR>\n";

    switch ($op) {
      case "edit":
        print comment_edit($id);
        break;
      case "search":
Dries's avatar
 
Dries committed
117
        print search_type("comment", "admin.php?mod=comment&op=search");
Dries's avatar
 
Dries committed
118
        break;
Dries's avatar
 
Dries committed
119
      case "delete":
Dries's avatar
 
Dries committed
120
        print comment_delete(check_query($id));
Dries's avatar
 
Dries committed
121 122
        print comment_overview();
        break;
Dries's avatar
 
Dries committed
123
      case t("Submit"):
Dries's avatar
 
Dries committed
124
        print status(comment_save(check_query($id), $edit));
Dries's avatar
 
Dries committed
125 126 127 128 129 130 131 132
        print comment_overview();
        break;
      default:
        print comment_overview();
    }
  }
  else {
    print message_access();
Dries's avatar
 
Dries committed
133 134
  }
}
Dries's avatar
 
Dries committed
135

Dries's avatar
 
Dries committed
136
?>