story.module 10.8 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
                "type" => "story_type",
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
class Story {
Dries's avatar
   
Dries committed
14
15
16
17
18
19
20
21
  function Story($story, $category = 0, $topic = 0) {
    $this->userid = $story[userid] ? $story[userid] : $user->userid;
    $this->title = $story[title];
    $this->abstract = $story[abstract];
    $this->body = $story[body];
    $this->timestamp = $story[timestamp] ? $story[timestamp] : time();
    $this->category = ($category ? $category : node_get_category($story[nid]));
    $this->topic = ($topic ? $topic : node_get_topic($story[nid]));
Dries's avatar
   
Dries committed
22
23
24
  }
}

Dries's avatar
   
Dries committed
25
function story_post_threshold($node, $default) {
Dries's avatar
   
Dries committed
26
  return section_post_threshold($node->section, $default);
Dries's avatar
   
Dries committed
27
28
29
}

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

function story_timout_threshold($node, $default) {
Dries's avatar
   
Dries committed
34
  return section_timout_threshold($node->section, $default);
Dries's avatar
   
Dries committed
35
36
}

Dries's avatar
   
Dries committed
37
38
39
40
function story_status() {
  return array(dumped, queued, posted);
}

Dries's avatar
   
Dries committed
41
function story_find($keys) {
Dries's avatar
   
Dries committed
42
  global $status, $user;
Dries's avatar
   
Dries committed
43
  $find = array();
Dries's avatar
   
Dries committed
44
  $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
45
  while ($story = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
46
    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
47
48
49
50
  }
  return $find;
}

Dries's avatar
   
Dries committed
51
52
function story_help() {
 ?>
Dries's avatar
   
Dries committed
53
54
55
  <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
56
 <?php
Dries's avatar
   
Dries committed
57
58
}

Dries's avatar
   
Dries committed
59
60
61
62
function story_type() {
  return array("story", t("story"));
}

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

Dries's avatar
   
Dries committed
66
  if ($page == 1) {
Dries's avatar
   
Dries committed
67
68
69
    switch($op) {
      case t("Preview comment"):
        $theme->header();
Dries's avatar
   
Dries committed
70
        comment_preview(check_input($pid), check_input($id), $subject, $comment);
Dries's avatar
   
Dries committed
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
        $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
110
  }
Dries's avatar
   
Dries committed
111
112
  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
113
  }
Dries's avatar
   
Dries committed
114
}
Dries's avatar
   
Dries committed
115

Dries's avatar
   
Dries committed
116
function story_form($story = array()) {
Dries's avatar
   
Dries committed
117
  global $allowed_html, $REQUEST_URI, $user;
Dries's avatar
   
Dries committed
118

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

Dries's avatar
Dries committed
121
  $output .= "<B>". t("Your name") .":</B><BR>\n";
Dries's avatar
   
Dries committed
122
123
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"story[userid]\" VALUE=\"$story[userid]\">\n";
  $output .= format_username(($story[userid] ? $story[userid] : $user->userid)) ."<P>";
Dries's avatar
   
Dries committed
124

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

Dries's avatar
   
Dries committed
128
  $output .= structure_form("story");
Dries's avatar
   
Dries committed
129

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

  $output .= "<B>". t("Body") .":</B><BR>\n";
Dries's avatar
   
Dries committed
135
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"story[body]\">". check_textarea($story[body]) ."</TEXTAREA><BR>\n";
Dries's avatar
   
Dries committed
136
137
  $output .= "<SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html) .".</I></SMALL><P>\n";

Dries's avatar
Dries committed
138
  if (user_access($user, "story")) {
Dries's avatar
   
Dries committed
139
140
    $output .= "<INPUT TYPE=\"hidden\" NAME=\"story[timestamp]\" VALUE=\"$story[timestamp]\">\n";
    $output .= "<INPUT TYPE=\"hidden\" NAME=\"story[nid]\" VALUE=\"$story[nid]\">\n";
Dries's avatar
Dries committed
141
  }
Dries's avatar
   
Dries committed
142

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

Dries's avatar
   
Dries committed
145
  if (!$story) {
Dries's avatar
   
Dries committed
146
147
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview") ."\">\n";
  }
Dries's avatar
   
Dries committed
148
  else if (!$story[title]) {
Dries's avatar
   
Dries committed
149
150
151
    $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";
  }
Dries's avatar
   
Dries committed
152
  else if (!$story[section]) {
Dries's avatar
   
Dries committed
153
154
155
    $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";
  }
Dries's avatar
   
Dries committed
156
  else if (!$story[abstract]) {
Dries's avatar
   
Dries committed
157
158
159
    $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";
  }
Dries's avatar
   
Dries committed
160
  else if (!$story[nid] && $duplicate) {
Dries's avatar
   
Dries committed
161
162
163
164
165
166
167
    $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
168
169
  $output .= "</FORM>\n";

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

Dries's avatar
   
Dries committed
173
174
175
function story_save($story, $category, $topic) {
  node_save(array_diff(array_merge($story, array(nid => $story[nid], type => "story")), array(userid => $story[userid])));
  structure_save($category, $topic);
Dries's avatar
   
Dries committed
176
177
}

Dries's avatar
   
Dries committed
178
179
180
181
182
function story_block() {
/*
  //
  // disabled for now
  //
Dries's avatar
   
Dries committed
183
  global $status;
Dries's avatar
   
Dries committed
184
  $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
185
186
187
  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
188

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

Dries's avatar
   
Dries committed
193
  unset($content);
Dries's avatar
   
Dries committed
194

Dries's avatar
   
Dries committed
195
  $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
196
197
198
  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
199

Dries's avatar
   
Dries committed
200
201
202
  $blocks[1][subject] = "Top 10:<BR>recent stories";
  $blocks[1][content] = $content;
  $blocks[1][info] = "Top 10: recent stories";
Dries's avatar
 
Dries committed
203

Dries's avatar
   
Dries committed
204
205
  return $blocks;
*/
Dries's avatar
 
Dries committed
206
207
}

Dries's avatar
   
Dries committed
208
209
function story_query($type = "") {
  global $status;
Dries's avatar
   
Dries committed
210
  $queries = array(array("recent stories", "WHERE n.type = 'story' ORDER BY n.timestamp DESC"), array("posted stories", "WHERE n.type = 'story' AND n.status = '$status[posted]' ORDER BY n.timestamp DESC"), array("queued stories", "WHERE n.type = 'story' AND n.status = '$status[queued]' ORDER BY n.timestamp DESC"), array("dumped stories", "WHERE n.type = 'story' AND n.status = '$status[dumped]' ORDER BY n.timestamp DESC"));
Dries's avatar
   
Dries committed
211
212
213
214
215
  return ($queries[$type] ? $queries[$type] : $queries);
}

function story_overview($query = array()) {
  return node_overview($query);
Dries's avatar
 
Dries committed
216
217
218
}

function story_admin() {
Dries's avatar
   
Dries committed
219
  global $id, $story, $category, $topic, $mod, $keys, $op, $theme, $type, $user;
Dries's avatar
   
Dries committed
220
221

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

Dries's avatar
   
Dries committed
223
  $type = ($type ? $type : 0);
Dries's avatar
   
Dries committed
224

Dries's avatar
 
Dries committed
225
  switch ($op) {
Dries's avatar
   
Dries committed
226
    case "add":
Dries's avatar
   
Dries committed
227
228
229
230
      print story_form();
      break;
    case "delete":
      print story_delete($id);
Dries's avatar
   
Dries committed
231
      print story_overview(story_query($type));
Dries's avatar
   
Dries committed
232
      break;
Dries's avatar
 
Dries committed
233
    case "edit":
Dries's avatar
   
Dries committed
234
      print story_form(node_get_array("nid", check_input($id)));
Dries's avatar
 
Dries committed
235
      break;
Dries's avatar
   
Dries committed
236
237
    case "help":
      story_help();
Dries's avatar
 
Dries committed
238
      break;
Dries's avatar
   
Dries committed
239
    case "listing":
Dries's avatar
   
Dries committed
240
      print node_listing(story_query());
Dries's avatar
   
Dries committed
241
      break;
Dries's avatar
   
Dries committed
242
    case "search":
Dries's avatar
   
Dries committed
243
244
      print search_form($keys);
      print search_data($keys, $mod);
Dries's avatar
   
Dries committed
245
      break;
Dries's avatar
   
Dries committed
246
    case t("Preview"):
Dries's avatar
   
Dries committed
247
248
      story_view(new Story($story, $category, $topic),0);
      print story_form($story);
Dries's avatar
   
Dries committed
249
250
      break;
    case t("Submit"):
Dries's avatar
   
Dries committed
251
      story_save($story, $category, $topic);
Dries's avatar
   
Dries committed
252
      // fall through:
Dries's avatar
   
Dries committed
253
    default:
Dries's avatar
   
Dries committed
254
      print story_overview(story_query($type));
Dries's avatar
   
Dries committed
255
256
257
258
259
  }
}


function story_user() {
Dries's avatar
   
Dries committed
260
  global $story, $category, $topic, $op, $theme, $user;
Dries's avatar
   
Dries committed
261
262
263

  switch($op) {
    case t("Preview"):
Dries's avatar
   
Dries committed
264
265
      story_view(new Story($story, $category, $topic), 0);
      $theme->box("Submit", story_form($story));
Dries's avatar
   
Dries committed
266
      break;
Dries's avatar
   
Dries committed
267
    case t("Submit"):
Dries's avatar
   
Dries committed
268
269
      story_save($story, $category, $topic);
      $theme->box(t("Submit"), t("Thank you for your submission."));
Dries's avatar
 
Dries committed
270
271
      break;
    default:
Dries's avatar
   
Dries committed
272
      $theme->box("Submit", story_form());
Dries's avatar
 
Dries committed
273
274
275
  }
}

Dries's avatar
   
Dries committed
276
?>