story.module 14.8 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
Dries's avatar
 
Dries committed
2

Dries's avatar
 
Dries committed
3 4
$module = array("cron" => "story_cron",
                "help" => "story_help",
Dries's avatar
 
Dries committed
5
                "find" => "story_find",
Dries's avatar
 
Dries committed
6 7
                "user" => "story_user",
                "queue" => "story_queue",
Dries's avatar
 
Dries committed
8 9
                "admin" => "story_admin",
                "block" => "story_block");
Dries's avatar
 
Dries committed
10

Dries's avatar
 
Dries committed
11 12
include_once "includes/section.inc";

Dries's avatar
 
Dries committed
13 14 15 16 17 18 19 20 21 22
class Story {
  function Story($userid, $title, $abstract, $body, $section, $timestamp) {    $this->userid = $userid;
    $this->title = $title;
    $this->abstract = $abstract;
    $this->body = $body;
    $this->section = $section;
    $this->timestamp = $timestamp;
  }
}

Dries's avatar
 
Dries committed
23
function story_cron() {
Dries's avatar
 
Dries committed
24 25
  global $status;
  $result = db_query("SELECT * FROM nodes WHERE status = '$status[scheduled]' AND timestamp <= ". time() ."");
Dries's avatar
 
Dries committed
26
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
27
    db_query("UPDATE nodes SET status = '$status[queued]', timestamp = '". time() ."' WHERE nid = '$story->nid' AND type = 'story'");
Dries's avatar
 
Dries committed
28 29 30
  }
}

Dries's avatar
 
Dries committed
31
function story_find($keys) {
Dries's avatar
 
Dries committed
32
  global $status, $user;
Dries's avatar
 
Dries committed
33
  $find = array();
Dries's avatar
 
Dries committed
34
  $result = db_query("SELECT n.*, s.* FROM story s LEFT JOIN nodes n ON s.node = n.nid WHERE n.status = '$status[posted]' AND (n.title LIKE '%". check_input($keys) ."%' OR s.abstract LIKE '%". check_input($keys) ."%' OR s.body LIKE '%$keys%') LIMIT 20");
Dries's avatar
 
Dries committed
35
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
36
    array_push($find, array("title" => check_output($story->title), "link" => (user_access($user, "story") ? "admin.php?mod=story&op=edit&id=$story->nid" : "node.php?id=$story->nid"), "user" => $story->userid, "date" => $story->timestamp));
Dries's avatar
 
Dries committed
37 38 39 40
  }
  return $find;
}

Dries's avatar
 
Dries committed
41 42
function story_search() {
  global $keys, $mod;
Dries's avatar
 
Dries committed
43 44
  print search_form($keys);
  print search_data($keys, $mod);
Dries's avatar
 
Dries committed
45 46
}

Dries's avatar
 
Dries committed
47 48
function story_help() {
 ?>
Dries's avatar
 
Dries committed
49 50 51 52
  <P>Scheduled stories: stories that are scheduled to be automatically published at a given date and time.  Useful when you have to leave the site alone for a while or when you want to regulate the flow of new content.</P>
  <P>Queued stories: user-contributed stories are automatically whisked away to a submission queue for moderators (i.e. registered user) to frown at.  Moderators vote whether or not a story should be posted to the front page for discussion.</P>
  <P>Posted stories: published stories accessible to all visitors.</P>
  <P>Dumped stories: rejected stories that are no longer available to visitors.</P>
Dries's avatar
 
Dries committed
53
 <?php
Dries's avatar
 
Dries committed
54 55
}

Dries's avatar
 
Dries committed
56
function story_view($node, $page = 1) {
Dries's avatar
 
Dries committed
57 58
  global $id, $cid, $op, $pid, $subject, $comment, $theme, $mode, $order, $threshold;

Dries's avatar
 
Dries committed
59
  if ($page == 1) {
Dries's avatar
 
Dries committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    switch($op) {
      case t("Preview comment"):
        $theme->header();
        comment_preview(check_input($pid), check_input($id), ($subject ? check_output($subject) : ""), ($comment ? check_output($comment) : ""));
        $theme->footer();
        break;
      case t("Post comment"):
        comment_post(check_input($pid), check_input($id), check_input($subject), check_input($comment));
        $theme->header();
        $theme->story($node, "[ <A HREF=\"node.php?op=reply&id=$node->nid&pid=0\">". t("reply to this story") ."</A> ]");
        comment_render($id, $cid);
        $theme->footer();
        break;
      case "reply":
        $theme->header();
        comment_reply(check_input($pid), check_input($id));
        $theme->footer();
        break;
      case t("Update settings"):
        comment_settings(check_input($mode), check_input($order), check_input($threshold));
        $theme->header();
        $theme->story($node, "[ <A HREF=\"node.php?op=reply&id=$node->nid&pid=0\">". t("reply to this story") ."</A> ]");
        comment_render($id, $cid);
        $theme->footer();
        break;
      case t("Moderate comments"):
        comment_moderate($moderate);
        $theme->header();
        $theme->story($node, "[ <A HREF=\"node.php?op=reply&id=$node->nid&pid=0\">". t("reply to this story") ."</A> ]");
        comment_render($id, $cid);
        $theme->footer();
        break;
      case "reply":
        $theme->header();
        comment_reply(check_input($pid), check_input($id));
        $theme->footer();
        break;
      default:
        $theme->header();
        $theme->story($node, "[ <A HREF=\"node.php?op=reply&id=$node->nid&pid=0\">". t("reply to this story") ."</A> ]");
        comment_render($id, $cid);
        $theme->footer();
    }
Dries's avatar
 
Dries committed
103
  }
Dries's avatar
 
Dries committed
104 105
  else {
    $theme->story($node, "[ <A HREF=\"node.php?op=reply&id=$node->nid&pid=0\">". t("reply to this story") ."</A> ]");
Dries's avatar
 
Dries committed
106
  }
Dries's avatar
 
Dries committed
107
}
Dries's avatar
 
Dries committed
108

Dries's avatar
 
Dries committed
109
function story_form($edit = array()) {
Dries's avatar
 
Dries committed
110
  global $allowed_html, $REQUEST_URI, $status, $theme, $user;
Dries's avatar
 
Dries committed
111

Dries's avatar
 
Dries committed
112
  $output .= "<FORM ACTION=\"$REQUEST_URI\" METHOD=\"post\">\n";
Dries's avatar
 
Dries committed
113

Dries's avatar
 
Dries committed
114
  $output .= "<B>". t("Your name") .":</B><BR>\n";
Dries's avatar
 
Dries committed
115
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[userid]\" VALUE=\"$edit[userid]\">\n";
Dries's avatar
 
Dries committed
116
  $output .= format_username(($edit[userid] ? $edit[userid] : $user->userid)) ."<P>";
Dries's avatar
 
Dries committed
117

Dries's avatar
 
Dries committed
118 119
  $output .= "<B>". t("Subject") .":</B><BR>\n";
  $output .= "<INPUT TYPE=\"text\" NAME=\"edit[title]\" SIZE=\"50\" MAXLENGTH=\"60\" VALUE=\"". check_textfield($edit[title]) ."\"><P>\n";
Dries's avatar
 
Dries committed
120

Dries's avatar
 
Dries committed
121 122
  $output .= "<B>". t("Section") .":</B><BR>\n";
  foreach ($sections = section_get() as $value) $options .= "  <OPTION VALUE=\"". check_select($value) ."\"". ($edit[section] == $value ? " SELECTED" : "") .">". check_output($value) ."</OPTION>\n";
Dries's avatar
 
Dries committed
123 124
  $output .= "<SELECT NAME=\"edit[section]\">$options</SELECT><P>\n";

Dries's avatar
 
Dries committed
125 126 127 128 129 130 131 132 133 134
  $output .= "<B>". t("Abstract") .":</B><BR>\n";
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"edit[abstract]\">". check_textarea($edit[abstract]) ."</TEXTAREA><BR>\n";
  $output .= "<SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html) .".</I></SMALL><P>\n";

  $output .= "<B>". t("Body") .":</B><BR>\n";
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"edit[body]\">". check_textarea($edit[body]) ."</TEXTAREA><BR>\n";
  $output .= "<SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html) .".</I></SMALL><P>\n";

  if (user_access($user, "story")) {
    $output .= "<B>". t("Status") .":</B><BR>\n";
Dries's avatar
 
Dries committed
135 136 137 138
    $output .= "<INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"$status[scheduled]\"". ($edit[status] == 3 ? " CHECKED" : "") ."> scheduled for <INPUT TYPE=\"text\" NAME=\"edit[timestamp]\" SIZE=\"30\" VALUE=\"". date("j F Y G:i", ($edit[timetsamp] ? $edit[timestamp] : time())) ."\"><BR>\n";
    $output .= "<INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"$status[posted]\"". ($edit[status] == 2 ? " CHECKED" : "") ."> posted<BR>\n";
    $output .= "<INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"$status[queued]\"". ($edit[status] == 1 ? " CHECKED" : "") ."> queued<BR>\n";
    $output .= "<INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"$status[dumped]\"". ($edit[status] == 0 ? " CHECKED" : "") ."> dumped<BR>\n";
Dries's avatar
 
Dries committed
139 140
    $output .= "<SMALL><I>The textfield for scheduled stories expects a string containing an English date format of when you want to have your story automatically published.  Example input: '". date("j F Y G:i") ."', '". date("m/d/y H:i") ."', '". date("F j, Y H:i") ."', ...</I></SMALL><P>\n";
  }
Dries's avatar
 
Dries committed
141

Dries's avatar
 
Dries committed
142
  $duplicate = db_result(db_query("SELECT COUNT(nid) FROM nodes WHERE title = '$title'"));
Dries's avatar
 
Dries committed
143

Dries's avatar
 
Dries committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  if (!$edit) {
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview") ."\">\n";
  }
  else if (!$edit[title]) {
    $output .= "<FONT COLOR=\"red\">". t("Warning: you did not supply a subject.") ."</FONT><P>\n";
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview") ."\">\n";
  }
  else if (!$edit[section]) {
    $output .= "<FONT COLOR=\"red\">". t("Warning: you did not supply a section.") ."</FONT><P>\n";
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview") ."\">\n";
  }
  else if (!$edit[abstract]) {
    $output .= "<FONT COLOR=\"red\">". t("Warning: you did not supply an abstract.") ."</FONT><P>\n";
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview") ."\">\n";
  }
  else if (!$edit[nid] && $duplicate) {
    $output .= "<FONT COLOR=\"red\">". t("Warning: there is already a story with that subject.") ."</FONT><P>\n";
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview") ."\">\n";
  }
  else {
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview") ."\">\n";
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Submit") ."\">\n";
  }
Dries's avatar
 
Dries committed
167

Dries's avatar
 
Dries committed
168
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[nid]\" VALUE=\"$edit[nid]\">\n";
Dries's avatar
 
Dries committed
169 170
  $output .= "</FORM>\n";

Dries's avatar
 
Dries committed
171
  return $output;
Dries's avatar
 
Dries committed
172 173
}

Dries's avatar
 
Dries committed
174
function story_save($edit) {
Dries's avatar
 
Dries committed
175
  global $status;
Dries's avatar
 
Dries committed
176
  $node = ($edit[nid] ? node_get_object("nid", $edit[nid]) : node_get_object("title", $edit[title]));
Dries's avatar
 
Dries committed
177 178
  $edit[timestamp] = ($edit[status] == $status[scheduled] && strtotime($edit[timestamp]) > time()) ? strtotime($edit[timestamp]) : ($node->timestamp ? $node->timestamp : time());
  node_save(array_diff(array_merge($edit, array(nid => $node->nid, type => "story")), array(userid => $edit[userid])));
Dries's avatar
 
Dries committed
179 180
}

Dries's avatar
 
Dries committed
181 182 183
function story_delete($id) {
  node_del_object("nid", $id);
}
Dries's avatar
 
Dries committed
184

Dries's avatar
 
Dries committed
185 186 187 188 189
function story_block() {
/*
  //
  // disabled for now
  //
Dries's avatar
 
Dries committed
190 191
  global $status;
  $result = db_query("SELECT s.id, COUNT(c.cid) AS comments, s.title FROM story s LEFT JOIN comments c ON s.id = c.lid WHERE s.status = '$status[posted]' AND c.link = 'story' GROUP BY s.id ORDER BY comments DESC LIMIT 10");
Dries's avatar
 
Dries committed
192 193 194
  while ($story = db_fetch_object($result)) {
    $content .= "<LI><A HREF=\"node.php?id=$story->id\">". check_output($story->title) ."</A><BR><SMALL>(". format_plural($story->comments, "comment", "comments") .")</SMALL></LI>\n";
  }
Dries's avatar
 
Dries committed
195

Dries's avatar
 
Dries committed
196 197 198
  $blocks[0]["subject"] = "Top 10:<BR>all stories";
  $blocks[0]["content"] = $content;
  $blocks[0]["info"] = "Top 10: all stories";
Dries's avatar
 
Dries committed
199

Dries's avatar
 
Dries committed
200
  unset($content);
Dries's avatar
 
Dries committed
201

Dries's avatar
 
Dries committed
202
  $result = db_query("SELECT s.id, COUNT(c.cid) AS comments, s.title FROM story s LEFT JOIN comments c ON s.id = c.lid WHERE s.status = '$status[posted]' AND c.link = 'story' AND ". time() ." - s.timestamp < 2419200 GROUP BY s.id ORDER BY comments DESC LIMIT 10");
Dries's avatar
 
Dries committed
203 204 205
  while ($story = db_fetch_object($result)) {
    $content .= "<LI><A HREF=\"node.php?id=$story->id\">". check_output($story->title) ."</A><BR><SMALL>(". format_plural($story->comments, "comment", "comments") .")</SMALL></LI>\n";
  }
Dries's avatar
 
Dries committed
206

Dries's avatar
 
Dries committed
207 208 209
  $blocks[1]["subject"] = "Top 10:<BR>recent stories";
  $blocks[1]["content"] = $content;
  $blocks[1]["info"] = "Top 10: recent stories";
Dries's avatar
 
Dries committed
210

Dries's avatar
 
Dries committed
211 212
  return $blocks;
*/
Dries's avatar
 
Dries committed
213 214
}

Dries's avatar
 
Dries committed
215
function story_overview() {
Dries's avatar
 
Dries committed
216 217
  global $status;

Dries's avatar
 
Dries committed
218
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
 
Dries committed
219

Dries's avatar
 
Dries committed
220 221
  // Queued stories:
  $result = db_query("SELECT n.*, u.userid FROM nodes n LEFT JOIN users u ON n.author = u.id WHERE n.type = 'story' AND n.status = '$status[queued]' ORDER BY timestamp DESC");
Dries's avatar
 
Dries committed
222
  $output .= " <TR><TH COLSPAN=\"6\">queued stories</TH></TR>\n";
Dries's avatar
 
Dries committed
223
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
224
    $output .= " <TR><TD><A HREF=\"node.php?id=$story->nid\">". check_output($story->title) ."</A></TD><TD>". format_username($story->userid) ."</TD><TD>votes: $story->votes, score: $story->score</TD><TD><A HREF=\"node.php?id=$story->nid\">view</A></TD><TD><A HREF=\"admin.php?mod=story&op=edit&id=$story->nid\">edit</A></TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=delete&id=$story->nid\">delete</A></TD></TR>\n";
Dries's avatar
 
Dries committed
225 226
  }

Dries's avatar
 
Dries committed
227
  // Scheduled stories:
Dries's avatar
 
Dries committed
228
  $result = db_query("SELECT n.*, u.userid FROM nodes n LEFT JOIN users u ON n.author = u.id WHERE n.type = 'story' AND n.status = '$status[scheduled]' ORDER BY timestamp DESC");
Dries's avatar
 
Dries committed
229
  $output .= " <TR><TH COLSPAN=\"6\">scheduled stories</TH></TR>\n";
Dries's avatar
 
Dries committed
230
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
231
    $output .= " <TR><TD><A HREF=\"node.php?id=$story->nid\">". check_output($story->title) ."</A></TD><TD>". format_username($story->userid) ."</TD><TD>". date("D, m/d/Y H:i", $story->timestamp) ."<BR><SMALL>(". format_interval($story->timestamp - time()) ." left)</SMALL></TD><TD><A HREF=\"node.php?id=$story->nid\">view</A></TD><TD><A HREF=\"admin.php?mod=story&op=edit&id=$story->nid\">edit</A></TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=delete&id=$story->nid\">delete</A></TD></TR>\n";
Dries's avatar
 
Dries committed
232 233
  }

Dries's avatar
 
Dries committed
234
  // Dumped stories:
Dries's avatar
 
Dries committed
235
  $result = db_query("SELECT n.*, u.userid FROM nodes n LEFT JOIN users u ON n.author = u.id WHERE n.type = 'story' AND n.status = '$status[dumped]' ORDER BY timestamp DESC LIMIT 5");
Dries's avatar
 
Dries committed
236
  $output .= " <TR><TH COLSPAN=\"6\">dumped stories</TH></TR>\n";
Dries's avatar
 
Dries committed
237
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
238
    $output .= " <TR><TD><A HREF=\"node.php?id=$story->nid\">". check_output($story->title) ."</A></TD><TD>". format_username($story->userid) ."</TD><TD>". date("D, m/d/Y H:i", $story->timestamp) ."</TD><TD><A HREF=\"node.php?id=$story->nid\">view</A></TD><TD><A HREF=\"admin.php?mod=story&op=edit&id=$story->nid\">edit</A></TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=delete&id=$story->nid\">delete</A></TD></TR>\n";
Dries's avatar
 
Dries committed
239 240 241
  }

  // Posted stories:
Dries's avatar
 
Dries committed
242
  $result = db_query("SELECT n.*, u.userid FROM nodes n LEFT JOIN users u ON n.author = u.id WHERE n.type = 'story' AND n.status = '$status[posted]' ORDER BY timestamp DESC LIMIT 15");
Dries's avatar
 
Dries committed
243
  $output .= " <TR><TH COLSPAN=\"6\">posted stories</TH></TR>\n";
Dries's avatar
 
Dries committed
244
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
245
    $output .= " <TR><TD><A HREF=\"node.php?id=$story->nid\">". check_output($story->title) ."</A></TD><TD>". format_username($story->userid) ."</TD><TD>". date("D, m/d/Y H:i", $story->timestamp) ."</TD><TD><A HREF=\"node.php?id=$story->nid\">view</A></TD><TD><A HREF=\"admin.php?mod=story&op=edit&id=$story->nid\">edit</A></TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=delete&id=$story->nid\">delete</A></TD></TR>\n";
Dries's avatar
 
Dries committed
246
  }
Dries's avatar
 
Dries committed
247

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

Dries's avatar
 
Dries committed
250 251 252 253
  print $output;
}

function story_admin() {
Dries's avatar
 
Dries committed
254
  global $id, $edit, $op, $theme, $user;
Dries's avatar
 
Dries committed
255

Dries's avatar
 
Dries committed
256
  print "<SMALL><A HREF=\"admin.php?mod=story&op=add\">add new story</A> | <A HREF=\"admin.php?mod=story\">overview</A> | <A HREF=\"admin.php?mod=story&op=search\">search story</A> | <A HREF=\"admin.php?mod=story&op=help\">help</A></SMALL><HR>\n";
Dries's avatar
 
Dries committed
257

Dries's avatar
 
Dries committed
258
  switch ($op) {
Dries's avatar
 
Dries committed
259
    case "add":
Dries's avatar
 
Dries committed
260 261 262 263 264
      print story_form();
      break;
    case "delete":
      print story_delete($id);
      story_overview();
Dries's avatar
 
Dries committed
265
      break;
Dries's avatar
 
Dries committed
266
    case "edit":
Dries's avatar
 
Dries committed
267
      print story_form(node_get_array("nid", check_input($id)));
Dries's avatar
 
Dries committed
268
      break;
Dries's avatar
 
Dries committed
269 270
    case "help":
      story_help();
Dries's avatar
 
Dries committed
271
      break;
Dries's avatar
 
Dries committed
272
    case "search":
Dries's avatar
 
Dries committed
273 274
      story_search();
      break;
Dries's avatar
 
Dries committed
275
    case t("Preview"):
Dries's avatar
 
Dries committed
276
      story_view(new Story(($edit[userid] ? $edit[userid] : $user->userid), $edit[title], $edit[abstract], $edit[body], $edit[section], ($edit[timestamp] ? $edit[timestamp] : time())), 0);
Dries's avatar
 
Dries committed
277 278 279
      print story_form($edit);
      break;
    case t("Submit"):
Dries's avatar
 
Dries committed
280
      story_save($edit);
Dries's avatar
 
Dries committed
281 282 283 284 285 286 287 288 289
      story_overview();
      break;
    default:
      story_overview();
  }
}


function story_user() {
Dries's avatar
 
Dries committed
290
  global $edit, $op, $theme, $user;
Dries's avatar
 
Dries committed
291 292 293

  switch($op) {
    case t("Preview"):
Dries's avatar
 
Dries committed
294
      story_view(new Story($user->userid, $edit[title], $edit[abstract], $edit[body], $edit[section], ($edit[timestamp] ? $edit[timestamp] : time())), 0);
Dries's avatar
 
Dries committed
295
      $theme->box("Submit a story", story_form($edit));
Dries's avatar
 
Dries committed
296
      break;
Dries's avatar
 
Dries committed
297
    case t("Submit"):
Dries's avatar
 
Dries committed
298
      story_save($edit);
Dries's avatar
 
Dries committed
299
      $theme->box(t("Submit a story"), t("Thank you for your submission."));
Dries's avatar
 
Dries committed
300 301
      break;
    default:
Dries's avatar
 
Dries committed
302
      $theme->box("Submit a story", story_form());
Dries's avatar
 
Dries committed
303 304 305
  }
}

Dries's avatar
 
Dries committed
306
?>