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

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

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

Dries's avatar
 
Dries committed
12 13 14 15 16 17 18 19 20 21
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
22
function story_post_threshold($node, $default) {
Dries's avatar
 
Dries committed
23
  return section_post_threshold($node->section, $default);
Dries's avatar
 
Dries committed
24 25 26
}

function story_dump_threshold($node, $default) {
Dries's avatar
 
Dries committed
27
  return section_dump_threshold($node->section, $default);
Dries's avatar
 
Dries committed
28 29 30
}

function story_timout_threshold($node, $default) {
Dries's avatar
 
Dries committed
31
  return section_timout_threshold($node->section, $default);
Dries's avatar
 
Dries committed
32 33
}

Dries's avatar
 
Dries committed
34
function story_find($keys) {
Dries's avatar
 
Dries committed
35
  global $status, $user;
Dries's avatar
 
Dries committed
36
  $find = array();
Dries's avatar
 
Dries committed
37
  $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");
Dries's avatar
 
Dries committed
38
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
39
    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
40 41 42 43
  }
  return $find;
}

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

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

Dries's avatar
 
Dries committed
55
  if ($page == 1) {
Dries's avatar
 
Dries committed
56 57 58
    switch($op) {
      case t("Preview comment"):
        $theme->header();
Dries's avatar
 
Dries committed
59
        comment_preview(check_input($pid), check_input($id), $subject, $comment);
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
        $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
99
  }
Dries's avatar
 
Dries committed
100 101
  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
102
  }
Dries's avatar
 
Dries committed
103
}
Dries's avatar
 
Dries committed
104

Dries's avatar
 
Dries committed
105
function story_form($edit = array()) {
Dries's avatar
 
Dries committed
106
  global $allowed_html, $REQUEST_URI, $user;
Dries's avatar
 
Dries committed
107

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

Dries's avatar
Dries committed
110 111 112
  $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>";
Dries's avatar
 
Dries committed
113

Dries's avatar
Dries committed
114 115
  $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
116

Dries's avatar
 
Dries committed
117 118
  $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
119 120
  $output .= "<SELECT NAME=\"edit[section]\">$options</SELECT><P>\n";

Dries's avatar
 
Dries committed
121 122 123 124 125 126 127 128
  $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";

Dries's avatar
Dries committed
129 130 131 132
  if (user_access($user, "story")) {
    $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[timestamp]\" VALUE=\"$edit[timestamp]\">\n";
    $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[nid]\" VALUE=\"$edit[nid]\">\n";
  }
Dries's avatar
 
Dries committed
133

Dries's avatar
 
Dries committed
134
  $duplicate = db_result(db_query("SELECT COUNT(nid) FROM node WHERE title = '$title'"));
Dries's avatar
 
Dries committed
135

Dries's avatar
 
Dries committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  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
159 160
  $output .= "</FORM>\n";

Dries's avatar
 
Dries committed
161
  return $output;
Dries's avatar
 
Dries committed
162 163
}

Dries's avatar
 
Dries committed
164
function story_save($edit) {
Dries's avatar
 
Dries committed
165
  node_save(array_diff(array_merge($edit, array(nid => $edit[nid], type => "story")), array(userid => $edit[userid])));
Dries's avatar
 
Dries committed
166 167
}

Dries's avatar
 
Dries committed
168 169 170 171 172
function story_block() {
/*
  //
  // disabled for now
  //
Dries's avatar
 
Dries committed
173
  global $status;
Dries's avatar
 
Dries committed
174
  $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");
Dries's avatar
 
Dries committed
175 176 177
  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
178

Dries's avatar
 
Dries committed
179 180 181
  $blocks[0][subject] = "Top 10:<BR>all stories";
  $blocks[0][content] = $content;
  $blocks[0][info] = "Top 10: all stories";
Dries's avatar
 
Dries committed
182

Dries's avatar
 
Dries committed
183
  unset($content);
Dries's avatar
 
Dries committed
184

Dries's avatar
 
Dries committed
185
  $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");
Dries's avatar
 
Dries committed
186 187 188
  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
189

Dries's avatar
 
Dries committed
190 191 192
  $blocks[1][subject] = "Top 10:<BR>recent stories";
  $blocks[1][content] = $content;
  $blocks[1][info] = "Top 10: recent stories";
Dries's avatar
 
Dries committed
193

Dries's avatar
 
Dries committed
194 195
  return $blocks;
*/
Dries's avatar
 
Dries committed
196 197
}

Dries's avatar
 
Dries committed
198
function story_overview() {
Dries's avatar
 
Dries committed
199
  return node_overview("type = 'story'");
Dries's avatar
 
Dries committed
200 201 202
}

function story_admin() {
Dries's avatar
 
Dries committed
203
  global $id, $edit, $mod, $keys, $op, $theme, $user;
Dries's avatar
 
Dries committed
204

Dries's avatar
 
Dries committed
205
  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
206

Dries's avatar
 
Dries committed
207
  switch ($op) {
Dries's avatar
 
Dries committed
208
    case "add":
Dries's avatar
 
Dries committed
209 210 211 212
      print story_form();
      break;
    case "delete":
      print story_delete($id);
Dries's avatar
 
Dries committed
213
      print story_overview();
Dries's avatar
 
Dries committed
214
      break;
Dries's avatar
 
Dries committed
215
    case "edit":
Dries's avatar
 
Dries committed
216
      print story_form(node_get_array("nid", check_input($id)));
Dries's avatar
 
Dries committed
217
      break;
Dries's avatar
 
Dries committed
218 219
    case "help":
      story_help();
Dries's avatar
 
Dries committed
220
      break;
Dries's avatar
 
Dries committed
221
    case "search":
Dries's avatar
 
Dries committed
222 223
      print search_form($keys);
      print search_data($keys, $mod);
Dries's avatar
 
Dries committed
224
      break;
Dries's avatar
 
Dries committed
225
    case t("Preview"):
Dries's avatar
 
Dries committed
226
      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
227 228 229
      print story_form($edit);
      break;
    case t("Submit"):
Dries's avatar
 
Dries committed
230
      story_save($edit);
Dries's avatar
 
Dries committed
231
      print story_overview();
Dries's avatar
 
Dries committed
232 233
      break;
    default:
Dries's avatar
 
Dries committed
234
      print story_overview();
Dries's avatar
 
Dries committed
235 236 237 238 239
  }
}


function story_user() {
Dries's avatar
 
Dries committed
240
  global $edit, $op, $theme, $user;
Dries's avatar
 
Dries committed
241 242 243

  switch($op) {
    case t("Preview"):
Dries's avatar
 
Dries committed
244
      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
245
      $theme->box("Submit a story", story_form($edit));
Dries's avatar
 
Dries committed
246
      break;
Dries's avatar
 
Dries committed
247
    case t("Submit"):
Dries's avatar
 
Dries committed
248
      story_save($edit);
Dries's avatar
 
Dries committed
249
      $theme->box(t("Submit a story"), t("Thank you for your submission."));
Dries's avatar
 
Dries committed
250 251
      break;
    default:
Dries's avatar
 
Dries committed
252
      $theme->box("Submit a story", story_form());
Dries's avatar
 
Dries committed
253 254 255
  }
}

Dries's avatar
 
Dries committed
256
?>