comment.module 3.99 KB
Newer Older
Dries's avatar
 
Dries committed
1 2
<?

Dries's avatar
 
Dries committed
3 4 5 6
$module = array("find" => "comment_find",
                "admin" => "comment_admin");

function comment_find($keys) {
Dries's avatar
 
Dries committed
7
  global $user;
Dries's avatar
 
Dries committed
8
  $find = array();
Dries's avatar
 
Dries committed
9
  $result = db_query("SELECT c.*, u.userid FROM comments c LEFT JOIN users u ON c.author = u.id WHERE c.subject LIKE '%$keys%' OR c.comment LIKE '%$keys%' ORDER BY c.timestamp DESC LIMIT 20");
Dries's avatar
 
Dries committed
10
  while ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
11
    array_push($find, array("subject" => check_output($comment->subject), "link" => (user_access($user, "comment") ? "admin.php?mod=comment&op=edit&id=$comment->cid" : "story.php?id=$comment->lid&cid=$comment->cid"), "user" => $story->userid, "date" => $comment->timestamp));
Dries's avatar
 
Dries committed
12 13 14
  }
  return $find;
}
Dries's avatar
 
Dries committed
15

Dries's avatar
 
Dries committed
16 17
function comment_search() {
  global $keys, $mod;
Dries's avatar
 
Dries committed
18 19
  print search_form($keys);
  print search_data($keys, $mod);
Dries's avatar
 
Dries committed
20 21
}

Dries's avatar
 
Dries committed
22
function comment_edit($id) {
Dries's avatar
 
Dries committed
23
  $result = db_query("SELECT c.*, u.userid FROM comments c LEFT JOIN users u ON c.author = u.id WHERE c.cid = '$id'");
Dries's avatar
 
Dries committed
24 25 26 27 28

  $comment = db_fetch_object($result);

  $output .= "<FORM ACTION=\"admin.php?mod=comment&op=save&id=$id\" METHOD=\"post\">\n";

29 30
  $output .= "<B>Author:</B><BR>\n";
  $output .= format_username($comment->userid) ."<P>\n";
Dries's avatar
 
Dries committed
31

32 33
  $output .= "<B>Subject:</B><BR>\n";
  $output .= "<INPUT TYPE=\"text\" NAME=\"subject\" SIZE=\"50\" VALUE=\"". check_textfield($comment->subject) ."\"><P>\n";
Dries's avatar
 
Dries committed
34 35

  $output .= "<B>Comment:</B><BR>\n";
36
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"comment\">". check_textarea($comment->comment) ."</TEXTAREA><P>\n";
Dries's avatar
 
Dries committed
37

38
  $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Save comment\">\n";
Dries's avatar
 
Dries committed
39
  $output .= "</FORM>\n";
Dries's avatar
 
Dries committed
40

Dries's avatar
 
Dries committed
41 42 43 44
  print $output;
}

function comment_save($id, $subject, $comment) {
Dries's avatar
 
Dries committed
45
  db_query("UPDATE comments SET subject = '$subject', comment = '$comment' WHERE cid = '$id'");
46
  watchdog("message", "comment: modified '$subject'");
Dries's avatar
 
Dries committed
47 48 49 50 51 52 53 54
}

function comment_display($order = "date") {
  // Initialize variables:
  $fields = array("author" => "author", "date" => "timestamp DESC", "subject" => "subject");

  // Perform SQL query:
  $result = db_query("SELECT c.*, u.userid FROM comments c LEFT JOIN users u ON u.id = c.author ORDER BY c.$fields[$order] LIMIT 50");
Dries's avatar
 
Dries committed
55

Dries's avatar
 
Dries committed
56
  // Display comments:
Dries's avatar
 
Dries committed
57
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
 
Dries committed
58
  $output .= " <TR>\n";
Dries's avatar
 
Dries committed
59
  $output .= "  <TH ALIGN=\"right\" COLSPAN=\"3\">\n";
Dries's avatar
 
Dries committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  $output .= "   <FORM ACTION=\"admin.php?mod=comment\" METHOD=\"post\">\n";
  $output .= "    <SELECT NAME=\"order\">\n";
  foreach ($fields as $key=>$value) {
    $output .= "     <OPTION VALUE=\"$key\"". ($key == $order ? " SELECTED" : "") .">Sort by $key</OPTION>\n";
  }
  $output .= "    </SELECT>\n";
  $output .= "    <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Update\">\n";
  $output .= "   </FORM>\n";
  $output .= "  </TH>\n";
  $output .= " </TR>\n";

  $output .= " <TR>\n";
  $output .= "  <TH>subject</TH>\n";
  $output .= "  <TH>author</TH>\n";
  $output .= "  <TH>operations</TH>\n";
  $output .= " </TR>\n";

  while ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
78
    $output .= " <TR><TD>". ($comment->link == "story" ? "<A HREF=\"story.php?id=$comment->lid&cid=$comment->cid&pid=$comment->pid#$comment->cid\">". check_output($comment->subject) ."</A>" : check_output($comment->subject)) ."</TD><TD>". format_username($comment->userid) ."</TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=comment&op=edit&id=$comment->cid\">edit</A></TD></TR>\n";
Dries's avatar
 
Dries committed
79 80 81
  }

  $output .= "</TABLE>\n";
Dries's avatar
 
Dries committed
82

Dries's avatar
 
Dries committed
83 84 85 86 87 88
  print $output;
}

function comment_admin() {
  global $op, $id, $subject, $comment, $order;

Dries's avatar
 
Dries committed
89 90
  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
91 92 93 94
  switch ($op) {
    case "edit":
      comment_edit($id);
      break;
Dries's avatar
 
Dries committed
95 96 97
    case "search":
      comment_search();
      break;
Dries's avatar
 
Dries committed
98
    case "Save comment":
Dries's avatar
 
Dries committed
99
      comment_save(check_input($id), check_input($subject), check_input($comment));
100
      comment_display();
Dries's avatar
 
Dries committed
101 102
      break;
    case "Update":
Dries's avatar
 
Dries committed
103
      comment_display(check_input($order));
Dries's avatar
 
Dries committed
104 105 106 107 108 109 110
      break;
    default:
      comment_display();
  }
}

?>