summaryrefslogtreecommitdiff
path: root/modules/story.module
blob: f47a93e0b629648451e498a68cc5f51223c9c30c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php

$module = array("cron" => "story_cron",
                "help" => "story_help",
                "find" => "story_find",
                "user" => "story_user",
                "queue" => "story_queue",
                "admin" => "story_admin",
                "block" => "story_block");

include_once "includes/section.inc";

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;
  }
}

function story_cron() {
  global $status;
  $result = db_query("SELECT * FROM node WHERE status = '$status[scheduled]' AND timestamp <= ". time() ."");
  while ($story = db_fetch_object($result)) {
    db_query("UPDATE node SET status = '$status[queued]', timestamp = '". time() ."' WHERE nid = '$story->nid' AND type = 'story'");
  }
}

function story_find($keys) {
  global $status, $user;
  $find = array();
  $result = db_query("SELECT n.*, s.* FROM story s LEFT JOIN node n ON n.nid = s.nid AND n.lid = s.lid WHERE n.status = '$status[posted]' AND (n.title LIKE '%$keys%' OR s.abstract LIKE '%$keys%' OR s.body LIKE '%$keys%') LIMIT 20");
  while ($story = db_fetch_object($result)) {
    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));
  }
  return $find;
}

function story_search() {
  global $keys, $mod;
  print search_form($keys);
  print search_data($keys, $mod);
}

function story_help() {
 ?>
  <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>
 <?php
}

function story_view($node, $page = 1) {
  global $id, $cid, $op, $moderate, $pid, $subject, $comment, $theme, $mode, $order, $threshold;

  if ($page == 1) {
    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();
    }
  }
  else {
    $theme->story($node, "[ <A HREF=\"node.php?op=reply&id=$node->nid&pid=0\">". t("reply to this story") ."</A> ]");
  }
}

function story_form($edit = array()) {
  global $allowed_html, $REQUEST_URI, $status, $theme, $user;

  $output .= "<FORM ACTION=\"$REQUEST_URI\" METHOD=\"post\">\n";

  $output .= "<B>". t("Your name") .":</B><BR>\n";
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[userid]\" VALUE=\"$edit[userid]\">\n";
  $output .= format_username(($edit[userid] ? $edit[userid] : $user->userid)) ."<P>";

  $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";

  $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";
  $output .= "<SELECT NAME=\"edit[section]\">$options</SELECT><P>\n";

  $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=\"$status[scheduled]\"". ($edit[status] == $status[scheduled] ? " 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] == $status[posted] ? " CHECKED" : "") ."> posted<BR>\n";
    $output .= "<INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"$status[queued]\"". ($edit[status] == $status[queued] ? " CHECKED" : "") ."> queued<BR>\n";
    $output .= "<INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"$status[dumped]\"". ($edit[status] == $status[dumped] ? " 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";

    $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[timestamp]\" VALUE=\"$edit[timestamp]\">\n";
    $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[nid]\" VALUE=\"$edit[nid]\">\n";
  }

  $duplicate = db_result(db_query("SELECT COUNT(nid) FROM node WHERE title = '$title'"));

  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";
  }
  $output .= "</FORM>\n";

  return $output;
}

function story_save($edit) {
  global $status;
  $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 => $edit[nid], type => "story")), array(userid => $edit[userid])));
}

function story_delete($id) {
  return ($node = node_del("nid", $id) ? "story has been deleted" : "failed to delete story: change status to 'dumped' first");
}

function story_block() {
/*
  //
  // disabled for now
  //
  global $status;
  $result = db_query("SELECT s.lid, COUNT(c.cid) AS comments, s.title FROM story s LEFT JOIN comments c ON s.lid = c.lid WHERE s.status = '$status[posted]' AND c.link = 'story' GROUP BY s.lid 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";
  }

  $blocks[0][subject] = "Top 10:<BR>all stories";
  $blocks[0][content] = $content;
  $blocks[0][info] = "Top 10: all stories";

  unset($content);

  $result = db_query("SELECT s.lid, COUNT(c.cid) AS comments, s.title FROM story s LEFT JOIN comments c ON s.lid = c.lid WHERE s.status = '$status[posted]' AND c.link = 'story' AND ". time() ." - s.timestamp < 2419200 GROUP BY s.lid 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";
  }

  $blocks[1][subject] = "Top 10:<BR>recent stories";
  $blocks[1][content] = $content;
  $blocks[1][info] = "Top 10: recent stories";

  return $blocks;
*/
}

function story_overview() {
  global $status;

  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";

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

  // Scheduled stories:
  $result = db_query("SELECT n.*, u.userid FROM node n LEFT JOIN users u ON n.author = u.id WHERE n.type = 'story' AND n.status = '$status[scheduled]' ORDER BY timestamp DESC");
  $output .= " <TR><TH COLSPAN=\"6\">scheduled stories</TH></TR>\n";
  while ($node = db_fetch_object($result)) {
    $output .= " <TR><TD><A HREF=\"node.php?id=$node->nid\">". check_output($node->title) ."</A></TD><TD>". format_username($node->userid) ."</TD><TD>". format_date($node->timestamp, "small") ."<BR><SMALL>(". format_interval($node->timestamp - time()) ." left)</SMALL></TD><TD><A HREF=\"node.php?id=$node->nid\">view</A></TD><TD><A HREF=\"admin.php?mod=story&op=edit&id=$node->nid\">edit</A></TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=delete&id=$node->nid\">delete</A></TD></TR>\n";
  }

  // Dumped stories:
  $result = db_query("SELECT n.*, u.userid FROM node 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");
  $output .= " <TR><TH COLSPAN=\"6\">dumped stories</TH></TR>\n";
  while ($node = db_fetch_object($result)) {
    $output .= " <TR><TD><A HREF=\"node.php?id=$node->nid\">". check_output($node->title) ."</A></TD><TD>". format_username($node->userid) ."</TD><TD>". format_date($node->timestamp, "small") ."</TD><TD><A HREF=\"node.php?id=$node->nid\">view</A></TD><TD><A HREF=\"admin.php?mod=story&op=edit&id=$node->nid\">edit</A></TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=delete&id=$node->nid\">delete</A></TD></TR>\n";
  }

  // Posted stories:
  $result = db_query("SELECT n.*, u.userid FROM node 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");
  $output .= " <TR><TH COLSPAN=\"6\">posted stories</TH></TR>\n";
  while ($node = db_fetch_object($result)) {
    $output .= " <TR><TD><A HREF=\"node.php?id=$node->nid\">". check_output($node->title) ."</A></TD><TD>". format_username($node->userid) ."</TD><TD>". format_date($node->timestamp, "small") ."</TD><TD><A HREF=\"node.php?id=$node->nid\">view</A></TD><TD><A HREF=\"admin.php?mod=story&op=edit&id=$node->nid\">edit</A></TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=delete&id=$node->nid\">delete</A></TD></TR>\n";
  }

  $output .= "</TABLE>\n";

  print $output;
}

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

  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";

  switch ($op) {
    case "add":
      print story_form();
      break;
    case "delete":
      print story_delete($id);
      story_overview();
      break;
    case "edit":
      print story_form(node_get_array("nid", check_input($id)));
      break;
    case "help":
      story_help();
      break;
    case "search":
      story_search();
      break;
    case t("Preview"):
      story_view(new Story(($edit[userid] ? $edit[userid] : $user->userid), $edit[title], $edit[abstract], $edit[body], $edit[section], ($edit[timestamp] ? $edit[timestamp] : time())), 0);
      print story_form($edit);
      break;
    case t("Submit"):
      story_save($edit);
      story_overview();
      break;
    default:
      story_overview();
  }
}


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

  switch($op) {
    case t("Preview"):
      story_view(new Story($user->userid, $edit[title], $edit[abstract], $edit[body], $edit[section], ($edit[timestamp] ? $edit[timestamp] : time())), 0);
      $theme->box("Submit a story", story_form($edit));
      break;
    case t("Submit"):
      story_save($edit);
      $theme->box(t("Submit a story"), t("Thank you for your submission."));
      break;
    default:
      $theme->box("Submit a story", story_form());
  }
}

?>