story.module 14.6 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
  $result = db_query("SELECT * FROM nodes WHERE status = 3 AND timestamp <= ". time() ."");
Dries's avatar
   
Dries committed
25
  while ($story = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
26
    db_query("UPDATE nodes SET status = '1', timestamp = '". time() ."' WHERE nid = '$story->nid' AND type = 'story'");
Dries's avatar
   
Dries committed
27
28
29
  }
}

Dries's avatar
   
Dries committed
30
function story_find($keys) {
Dries's avatar
   
Dries committed
31
  global $user;
Dries's avatar
   
Dries committed
32
  $find = array();
Dries's avatar
   
Dries committed
33
  $result = db_query("SELECT n.*, s.* FROM story s LEFT JOIN nodes n ON s.node = n.nid WHERE n.status = 2 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
34
  while ($story = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
35
    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
36
37
38
39
  }
  return $find;
}

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

Dries's avatar
   
Dries committed
46
47
function story_help() {
 ?>
Dries's avatar
   
Dries committed
48
49
50
51
  <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
52
 <?php
Dries's avatar
   
Dries committed
53
54
}

Dries's avatar
   
Dries committed
55
56
57
58
59
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
function story_view($node, $page = 0) {
  global $id, $cid, $op, $pid, $subject, $comment, $theme, $mode, $order, $threshold;

  if ($page == 0) {
    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
102
  }
Dries's avatar
   
Dries committed
103
104
  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
105
  }
Dries's avatar
   
Dries committed
106
}
Dries's avatar
   
Dries committed
107

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

Dries's avatar
   
Dries committed
111
  include "includes/story.inc";
Dries's avatar
 
Dries committed
112

Dries's avatar
   
Dries committed
113
  $output .= "<FORM ACTION=\"$PHP_SELF?mod=story&id=$edit[id]\" METHOD=\"post\">\n";
Dries's avatar
   
Dries committed
114

Dries's avatar
   
Dries committed
115
116
  $output .= "<B>". t("Your name") .":</B><BR>\n";
  $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
135
136
137
138
139
140
  $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";
    $output .= "<INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"3\"". ($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=\"2\"". ($edit[status] == 2 ? " CHECKED" : "") ."> posted<BR>\n";
    $output .= "<INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"1\"". ($edit[status] == 1 ? " CHECKED" : "") ."> queued<BR>\n";
    $output .= "<INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"0\"". ($edit[status] == 0 ? " CHECKED" : "") ."> dumped<BR>\n";
    $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
175
176
177
function story_save($edit) {
  $node = ($edit[nid] ? node_get_object("nid", $edit[nid]) : node_get_object("title", $edit[title]));
  $edit[timestamp] = ($edit[status] == 3 && strtotime($edit[timestamp]) > time()) ? strtotime($edit[timestamp]) : ($node->timestamp ? $node->timestamp : time());
  node_save(array_merge($edit, array(nid => ($edit[nid] ? $edit[nid] : $node->nid), type => "story")));
Dries's avatar
   
Dries committed
178
179
}

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

Dries's avatar
   
Dries committed
184
185
186
187
188
189
190
191
192
function story_block() {
/*
  //
  // disabled for now
  //
  $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 = 2 AND c.link = 'story' GROUP BY s.id ORDER BY comments DESC LIMIT 10");
  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
193

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

Dries's avatar
   
Dries committed
198
  unset($content);
Dries's avatar
   
Dries committed
199

Dries's avatar
   
Dries committed
200
201
202
203
  $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 = 2 AND c.link = 'story' AND ". time() ." - s.timestamp < 2419200 GROUP BY s.id ORDER BY comments DESC LIMIT 10");
  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
204

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

Dries's avatar
   
Dries committed
209
210
  return $blocks;
*/
Dries's avatar
 
Dries committed
211
212
}

Dries's avatar
   
Dries committed
213
function story_overview() {
Dries's avatar
   
Dries committed
214
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
   
Dries committed
215
216

  // Pending stories:
Dries's avatar
   
Dries committed
217
218
  $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 = 1 ORDER BY timestamp DESC");
  $output .= " <TR><TH COLSPAN=\"6\">queued stories</TH></TR>\n";
Dries's avatar
   
Dries committed
219
  while ($story = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
220
    $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
221
222
  }

Dries's avatar
   
Dries committed
223
  // Scheduled stories:
Dries's avatar
   
Dries committed
224
225
  $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 = 3 ORDER BY timestamp DESC");
  $output .= " <TR><TH COLSPAN=\"6\">scheduled stories</TH></TR>\n";
Dries's avatar
 
Dries committed
226
  while ($story = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
227
    $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
228
229
  }

Dries's avatar
   
Dries committed
230
  // Dumped stories:
Dries's avatar
   
Dries committed
231
232
  $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 = 0 ORDER BY timestamp DESC LIMIT 5");
  $output .= " <TR><TH COLSPAN=\"6\">dumped stories</TH></TR>\n";
Dries's avatar
   
Dries committed
233
  while ($story = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
234
    $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
235
236
237
  }

  // Posted stories:
Dries's avatar
   
Dries committed
238
239
  $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 = 2 ORDER BY timestamp DESC LIMIT 15");
  $output .= " <TR><TH COLSPAN=\"6\">posted stories</TH></TR>\n";
Dries's avatar
   
Dries committed
240
  while ($story = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
241
    $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
242
  }
Dries's avatar
   
Dries committed
243

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

Dries's avatar
 
Dries committed
246
247
248
249
  print $output;
}

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

Dries's avatar
   
Dries committed
252
  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
253

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


function story_user() {
  global $edit, $id, $op, $theme, $user;

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

Dries's avatar
   
Dries committed
302
?>