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

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

Dries's avatar
 
Dries committed
9 10
include_once "includes/section.inc";

Dries's avatar
 
Dries committed
11 12 13
function story_cron() {
  $result = db_query("SELECT * FROM stories WHERE status = 3 AND timestamp <= ". time() ."");
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
14
    db_query("UPDATE stories SET status = '1', timestamp = '". time() ."' WHERE id = '$story->id'");
Dries's avatar
 
Dries committed
15 16 17
  }
}

Dries's avatar
 
Dries committed
18
function story_find($keys) {
Dries's avatar
 
Dries committed
19
  global $user;
Dries's avatar
 
Dries committed
20 21 22
  $find = array();
  $result = db_query("SELECT s.*, u.userid FROM stories s LEFT JOIN users u ON s.author = u.id WHERE s.status = 2 AND (s.subject LIKE '%". check_input($keys) ."%' OR s.abstract LIKE '%". check_input($keys) ."%' OR s.article LIKE '%". check_input($keys) ."%') ORDER BY s.timestamp DESC LIMIT 20");
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
23
    array_push($find, array("subject" => check_output($story->subject), "link" => (user_permission($user) ? "admin.php?mod=story&op=edit&id=$story->id" : "story.php?id=$story->id"), "user" => $story->userid, "date" => $story->timestamp));
Dries's avatar
 
Dries committed
24 25 26 27
  }
  return $find;
}

Dries's avatar
 
Dries committed
28 29
function story_search() {
  global $keys, $mod;
Dries's avatar
 
Dries committed
30 31
  print search_form($keys);
  print search_data($keys, $mod);
Dries's avatar
 
Dries committed
32 33
}

Dries's avatar
 
Dries committed
34 35
function story_help() {
 ?>
Dries's avatar
 
Dries committed
36 37 38 39
  <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
40 41 42
 <?
}

Dries's avatar
 
Dries committed
43
function story_block() {
Dries's avatar
 
Dries committed
44
  $result = db_query("SELECT s.id, COUNT(c.cid) AS comments, s.subject FROM stories 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");
Dries's avatar
 
Dries committed
45
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
46
    $content .= "<LI><A HREF=\"story.php?id=$story->id\">". check_output($story->subject) ."</A><BR><SMALL>(". format_plural($story->comments, "comment", "comments") .")</SMALL></LI>\n";
Dries's avatar
 
Dries committed
47 48 49 50 51 52
  }

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

Dries's avatar
 
Dries committed
53 54
  unset($content);

Dries's avatar
 
Dries committed
55
  $result = db_query("SELECT s.id, COUNT(c.cid) AS comments, s.subject FROM stories 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");
Dries's avatar
 
Dries committed
56
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
57
    $content .= "<LI><A HREF=\"story.php?id=$story->id\">". check_output($story->subject) ."</A><BR><SMALL>(". format_plural($story->comments, "comment", "comments") .")</SMALL></LI>\n";
Dries's avatar
 
Dries committed
58 59 60 61 62 63 64 65
  }

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

  return $blocks;
}
Dries's avatar
 
Dries committed
66

Dries's avatar
 
Dries committed
67
function story_add() {
Dries's avatar
 
Dries committed
68
  global $allowed_html;
Dries's avatar
 
Dries committed
69 70

  $output .= "<FORM ACTION=\"admin.php?mod=story\" METHOD=\"post\">\n";
Dries's avatar
 
Dries committed
71

Dries's avatar
 
Dries committed
72 73 74 75 76
  $output .= "<P>\n";
  $output .= " <B>Subject:</B><BR>\n";
  $output .= " <INPUT TYPE=\"text\" NAME=\"edit[subject]\" SIZE=\"50\" MAXLENGTH=\"60\"><BR>\n";
  $output .= "</P>\n";

Dries's avatar
 
Dries committed
77
  $output .= "<P><B>Section:</B><BR>\n";
Dries's avatar
 
Dries committed
78
  $output .= " <SELECT NAME=\"edit[section]\">\n";
Dries's avatar
 
Dries committed
79
  foreach ($sections = section_get() as $value) {
Dries's avatar
 
Dries committed
80
    $output .= "  <OPTION VALUE=\"$value\">$value</OPTION>\n";
Dries's avatar
 
Dries committed
81 82 83 84
  }
  $output .= " </SELECT>\n";
  $output .= "</P>\n";

Dries's avatar
 
Dries committed
85
  $output .= "<P>\n";
Dries's avatar
 
Dries committed
86 87 88 89 90
  $output .= " <B>Abstract:</B><BR>\n";
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"edit[abstract]\" MAXLENGTH=\"20\"></TEXTAREA><BR>\n";
  $output .= " <SMALL><I>Allowed HTML tags: ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
  $output .= "</P>\n";

Dries's avatar
 
Dries committed
91
  $output .= "<P>\n";
Dries's avatar
 
Dries committed
92 93 94 95 96 97 98 99 100 101 102 103 104
  $output .= " <B>Extended story:</B><BR>\n";
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"edit[article]\"></TEXTAREA><BR>\n";
  $output .= " <SMALL><I>Allowed HTML tags: ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= " <B>Status:</B><BR>\n";
  $output .= " <INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"3\">scheduled story for <INPUT TYPE=\"text\" NAME=\"edit[date]\" SIZE=\"30\" VALUE=\"". date("j F Y G:i") ."\"><BR>\n";
  $output .= " <INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"2\">posted story<BR>\n";
  $output .= " <INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"1\" CHECKED>queued story<BR>\n";
  $output .= " <INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"0\">dumped story<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.<BR>Example input: '". date("j F Y G:i") ."', '". date("m/d/y H:i") ."', '". date("F j, Y H:i") ."', ...</I></SMALL>\n";
  $output .= "</P>\n";
Dries's avatar
 
Dries committed
105

Dries's avatar
 
Dries committed
106 107 108 109 110 111 112 113 114 115 116 117
  $output .= "<P>\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Add story\">\n";
  $output .= "</P>\n";

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

  print $output;
}

function story_add_save($edit) {
  global $user;
  $timestamp = ($edit[status] == 3 && strtotime($edit[date]) > time()) ? strtotime($edit[date]) : time();
Dries's avatar
 
Dries committed
118
  db_query("INSERT INTO stories (author, subject, abstract, article, section, status, timestamp) VALUES ('$user->id', '". check_input($edit[subject]) ."', '". check_input($edit[abstract]) ."', '". check_input($edit[article]) ."', '". check_input($edit[section]) ."', '$edit[status]', '$timestamp')");
Dries's avatar
 
Dries committed
119 120 121
  watchdog("story", "story: added '$edit[subject]'");
}

Dries's avatar
 
Dries committed
122
function story_edit($id) {
Dries's avatar
 
Dries committed
123
  global $allowed_html;
Dries's avatar
 
Dries committed
124 125 126 127

  $result = db_query("SELECT s.*, u.userid FROM stories s LEFT JOIN users u ON s.author = u.id WHERE s.id = $id");
  $story = db_fetch_object($result);

Dries's avatar
 
Dries committed
128
  $output .= "<FORM ACTION=\"admin.php?mod=story&id=$id\" METHOD=\"post\">\n";
Dries's avatar
 
Dries committed
129 130 131 132 133 134 135 136

  $output .= "<P>\n";
  $output .= " <B>Author:</B><BR>\n";
  $output .= " ". format_username($story->userid) ."\n";
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= " <B>Subject:</B><BR>\n";
Dries's avatar
 
Dries committed
137
  $output .= " <INPUT TYPE=\"text\" NAME=\"edit[subject]\" SIZE=\"50\" VALUE=\"". check_textfield($story->subject) ."\"><BR>\n";
Dries's avatar
 
Dries committed
138 139
  $output .= "</P>\n";

Dries's avatar
 
Dries committed
140
  $output .= "<P>\n";
Dries's avatar
 
Dries committed
141 142 143 144
  $output .= " <B>Section:</B><BR>\n";
  $output .= " <SELECT NAME=\"edit[section]\">\n";
  foreach ($sections = section_get() as $value) {
    $output .= "  <OPTION VALUE=\"$value\"". ($story->section == $value ? " SELECTED" : "") .">$value</OPTION>\n";
Dries's avatar
 
Dries committed
145 146 147 148 149
  }
  $output .= "</SELECT>\n";
  $output .= "</P>\n";

  $output .= "<P>\n";
Dries's avatar
 
Dries committed
150
  $output .= " <B>Abstract:</B><BR>\n";
Dries's avatar
 
Dries committed
151
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"edit[abstract]\">". check_textarea($story->abstract) ."</TEXTAREA><BR>\n";
Dries's avatar
 
Dries committed
152
  $output .= " <SMALL><I>Allowed HTML tags: ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
Dries's avatar
 
Dries committed
153 154 155
  $output .= "</P>\n";

  $output .= "<P>\n";
Dries's avatar
 
Dries committed
156
  $output .= " <B>Editor's note/updates:</B><BR>\n";
Dries's avatar
 
Dries committed
157
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"edit[updates]\">". check_textarea($story->updates) ."</TEXTAREA><BR>\n";
Dries's avatar
 
Dries committed
158
  $output .= " <SMALL><I>Allowed HTML tags: ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
Dries's avatar
 
Dries committed
159 160 161 162
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= " <B>Extended story:</B><BR>\n";
Dries's avatar
 
Dries committed
163
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"edit[article]\">". check_textarea($story->article) ."</TEXTAREA><BR>\n";
Dries's avatar
 
Dries committed
164
  $output .= " <SMALL><I>Allowed HTML tags: ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
Dries's avatar
 
Dries committed
165 166
  $output .= "</P>\n";

Dries's avatar
 
Dries committed
167 168 169 170 171 172 173
  $output .= "<P>\n";
  $output .= " <B>Status:</B><BR>\n";
  $output .= " <INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"3\"". ($story->status == 3 ? " CHECKED" : "") .">scheduled story for <INPUT TYPE=\"text\" NAME=\"edit[date]\" SIZE=\"30\" VALUE=\"". date("j F Y G:i", $story->timestamp) ."\"><BR>\n";
  $output .= " <INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"2\"". ($story->status == 2 ? " CHECKED" : "") .">posted story<BR>\n";
  $output .= " <INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"1\"". ($story->status == 1 ? " CHECKED" : "") .">queued story<BR>\n";
  $output .= " <INPUT TYPE=\"radio\" NAME=\"edit[status]\" VALUE=\"0\"". ($story->status == 0 ? " CHECKED" : "") .">dumped story<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.<BR>Example input: '". date("j F Y G:i") ."', '". date("m/d/y H:i") ."', '". date("F j, Y H:i") ."', ...</I></SMALL>\n";
Dries's avatar
 
Dries committed
174 175 176 177 178 179
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Save story\">\n";
  $output .= "</P>\n";
  $output .= "</FORM>\n";
Dries's avatar
 
Dries committed
180

Dries's avatar
 
Dries committed
181 182 183
  print $output;
}

Dries's avatar
 
Dries committed
184
function story_edit_save($id, $edit) {
Dries's avatar
 
Dries committed
185 186
  if ($edit[status] == 3 && strtotime($edit[date]) > time()) db_query("UPDATE stories SET subject = '". check_input($edit[subject]) ."', abstract = '". check_input($edit[abstract]) ."', updates = '". check_input($edit[updates]) ."', article = '". check_input($edit[article]) ."', section = '". check_input($edit[section]) ."', status = '$edit[status]', timestamp = '". strtotime($edit[date]) ."' WHERE id = '$id'");
  else db_query("UPDATE stories SET subject = '". check_input($edit[subject]) ."', abstract = '". check_input($edit[abstract]) ."', updates = '". check_input($edit[updates]) ."', article = '". check_input($edit[article]) ."', section = '". check_input($edit[section]) ."', status = '$edit[status]' WHERE id = '$id'");
Dries's avatar
Dries committed
187
  watchdog("message", "story: modified `$edit[subject]'");
Dries's avatar
 
Dries committed
188 189
}

Dries's avatar
 
Dries committed
190
function story_display() {
Dries's avatar
 
Dries committed
191
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
 
Dries committed
192 193 194 195 196

  // Pending stories:
  $result = db_query("SELECT s.*, u.userid FROM stories s LEFT JOIN users u ON u.id = s.author WHERE s.status = 1 ORDER BY timestamp DESC");
  $output .= " <TR><TH COLSPAN=\"4\">queued stories</TH></TR>\n";
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
197
    $output .= " <TR><TD><A HREF=\"story.php?id=$story->id\">". check_output($story->subject) ."</A></TD><TD>". format_username($story->userid) ."</TD><TD>votes: $story->votes, score: $story->score</TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=edit&id=$story->id\">edit</A></TD></TR>\n";
Dries's avatar
 
Dries committed
198 199
  }

Dries's avatar
 
Dries committed
200 201 202
  // Scheduled stories:
  $result = db_query("SELECT s.*, u.userid FROM stories s LEFT JOIN users u ON u.id = s.author WHERE s.status = 3 ORDER BY timestamp");
  $output .= " <TR><TH COLSPAN=\"4\">scheduled stories</TH></TR>\n";
Dries's avatar
 
Dries committed
203
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
204
    $output .= " <TR><TD><A HREF=\"story.php?id=$story->id\">". check_output($story->subject) ."</A></TD><TD>". format_username($story->userid) ."</TD><TD>". date("D, m/d/Y H:i", $story->timestamp) ." - ". format_interval($story->timestamp - time()) ." left</TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=edit&id=$story->id\">edit</A></TD></TR>\n";
Dries's avatar
 
Dries committed
205 206
  }

Dries's avatar
 
Dries committed
207 208 209 210
  // Dumped stories:
  $result = db_query("SELECT s.*, u.userid FROM stories s LEFT JOIN users u ON u.id = s.author WHERE s.status = 0 ORDER BY timestamp DESC LIMIT 5");
  $output .= " <TR><TH COLSPAN=\"4\">dumped stories</TTH></TR>\n";
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
211
    $output .= " <TR><TD><A HREF=\"story.php?id=$story->id\">". check_output($story->subject) ."</A></TD><TD>". format_username($story->userid) ."</TD><TD>$story->section</TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=edit&id=$story->id\">edit</A></TD></TR>\n";
Dries's avatar
 
Dries committed
212 213 214 215 216 217
  }

  // Posted stories:
  $result = db_query("SELECT s.*, u.userid FROM stories s LEFT JOIN users u ON u.id = s.author WHERE s.status = 2 ORDER BY timestamp DESC LIMIT 15");
  $output .= " <TR><TH COLSPAN=\"4\">posted stories</TH></TR>\n";
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
218
    $output .= " <TR><TD><A HREF=\"story.php?id=$story->id\">". check_output($story->subject) ."</A></TD><TD>". format_username($story->userid) ."</TD><TD>$story->section</TD><TD ALIGN=\"center\"><A HREF=\"admin.php?mod=story&op=edit&id=$story->id\">edit</A></TD></TR>\n";
Dries's avatar
 
Dries committed
219
  }
Dries's avatar
 
Dries committed
220

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

Dries's avatar
 
Dries committed
223 224 225 226
  print $output;
}

function story_admin() {
Dries's avatar
 
Dries committed
227 228
  global $op, $id, $edit;

Dries's avatar
 
Dries committed
229
  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
230

Dries's avatar
 
Dries committed
231
  switch ($op) {
Dries's avatar
 
Dries committed
232 233 234
    case "add":
      story_add();
      break;
Dries's avatar
 
Dries committed
235 236 237
    case "edit":
      story_edit($id);
      break;
Dries's avatar
 
Dries committed
238 239
    case "help":
      story_help();
Dries's avatar
 
Dries committed
240
      break;
Dries's avatar
 
Dries committed
241 242 243
   case "search":
      story_search();
      break;
Dries's avatar
 
Dries committed
244 245 246 247 248 249 250
    case "Add story":
      story_add_save($edit);
      story_display();
      break;
    case "Save story":
      story_edit_save($id, $edit);
      story_display();
Dries's avatar
 
Dries committed
251 252 253 254 255 256
      break;
    default:
      story_display();
  }
}

Dries's avatar
 
Dries committed
257
?>