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

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

Dries Buytaert's avatar
   
Dries Buytaert committed
11
class Story {
Dries Buytaert's avatar
   
Dries Buytaert committed
12
13
  function Story($story) {
    global $user;
Dries Buytaert's avatar
   
Dries Buytaert committed
14
15
16
17
18
    $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();
Dries Buytaert's avatar
   
Dries Buytaert committed
19
20
    $this->cid = $story[cid];
    $this->tid = $story[tid];
Dries Buytaert's avatar
   
Dries Buytaert committed
21
22
23
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
24
25
26
27
function story_status() {
  return array(dumped, queued, posted);
}

Dries Buytaert's avatar
   
Dries Buytaert committed
28
function story_find($keys) {
Dries Buytaert's avatar
   
Dries Buytaert committed
29
  global $status, $user;
Dries Buytaert's avatar
   
Dries Buytaert committed
30
  $find = array();
Dries Buytaert's avatar
   
Dries Buytaert committed
31
  $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 Buytaert's avatar
   
Dries Buytaert committed
32
  while ($story = db_fetch_object($result)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
33
    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 Buytaert's avatar
   
Dries Buytaert committed
34
35
36
37
  }
  return $find;
}

Dries Buytaert's avatar
   
Dries Buytaert committed
38
39
function story_help() {
 ?>
Dries Buytaert's avatar
   
Dries Buytaert committed
40
41
42
  <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 Buytaert's avatar
   
Dries Buytaert committed
43
 <?php
Dries Buytaert's avatar
   
Dries Buytaert committed
44
45
}

Dries Buytaert's avatar
   
Dries Buytaert committed
46
47
48
49
function story_type() {
  return array("story", t("story"));
}

Dries Buytaert's avatar
   
Dries Buytaert committed
50
51
52
function story_view($node, $main = 0) {
  global $theme;
  $theme->story($node, $main);
Dries Buytaert's avatar
   
Dries Buytaert committed
53
}
Dries Buytaert's avatar
   
Dries Buytaert committed
54

Dries Buytaert's avatar
   
Dries Buytaert committed
55
function story_form($edit = array()) {
Dries Buytaert's avatar
   
Dries Buytaert committed
56
  global $allowed_html, $REQUEST_URI, $user;
Dries Buytaert's avatar
   
Dries Buytaert committed
57

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

Dries Buytaert's avatar
Dries Buytaert committed
60
  $output .= "<B>". t("Your name") .":</B><BR>\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
61
62
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[userid]\" VALUE=\"$edit[userid]\">\n";
  $output .= format_username(($edit[userid] ? $edit[userid] : $user->userid)) ."<P>";
Dries Buytaert's avatar
   
Dries Buytaert committed
63

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

Dries Buytaert's avatar
   
Dries Buytaert committed
67
  $output .= structure_form("story", $edit);
Dries Buytaert's avatar
   
Dries Buytaert committed
68

Dries Buytaert's avatar
   
Dries Buytaert committed
69
  $output .= "<B>". t("Abstract") .":</B><BR>\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
70
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"edit[abstract]\">". check_textarea($edit[abstract]) ."</TEXTAREA><BR>\n";
Dries Buytaert's avatar
   
Dries Buytaert committed
71
72
73
  $output .= "<SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html) .".</I></SMALL><P>\n";

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

Dries Buytaert's avatar
Dries Buytaert committed
77
  if (user_access($user, "story")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
78
79
    $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[timestamp]\" VALUE=\"$edit[timestamp]\">\n";
    $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[nid]\" VALUE=\"$edit[nid]\">\n";
Dries Buytaert's avatar
Dries Buytaert committed
80
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
81

82
  $duplicate = db_result(db_query("SELECT COUNT(nid) FROM node WHERE title = '". check_input($title) ."' AND type = 'story'"));
Dries Buytaert's avatar
   
Dries Buytaert committed
83

Dries Buytaert's avatar
   
Dries Buytaert committed
84
  if (!$edit) {
Dries Buytaert's avatar
   
Dries Buytaert committed
85
86
    $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview") ."\">\n";
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
87
  else if (!$edit[title]) {
Dries Buytaert's avatar
   
Dries Buytaert committed
88
89
90
    $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 Buytaert's avatar
   
Dries Buytaert committed
91
  else if (!$edit[abstract]) {
Dries Buytaert's avatar
   
Dries Buytaert committed
92
93
94
    $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 Buytaert's avatar
   
Dries Buytaert committed
95
  else if (!$edit[nid] && $duplicate) {
Dries Buytaert's avatar
   
Dries Buytaert committed
96
97
98
99
100
101
102
    $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 Buytaert's avatar
   
Dries Buytaert committed
103
104
  $output .= "</FORM>\n";

Dries Buytaert's avatar
   
Dries Buytaert committed
105
  return $output;
Dries Buytaert's avatar
   
Dries Buytaert committed
106
107
}

Dries Buytaert's avatar
   
Dries Buytaert committed
108
function story_save($edit) {
Dries Buytaert's avatar
   
Dries Buytaert committed
109
  node_save(array_diff(array_merge($edit, array(nid => $edit[nid], type => "story")), array(userid => $edit[userid])));
Dries Buytaert's avatar
   
Dries Buytaert committed
110
111
}

Dries Buytaert's avatar
   
Dries Buytaert committed
112
113
114
115
116
function story_block() {
/*
  //
  // disabled for now
  //
Dries Buytaert's avatar
   
Dries Buytaert committed
117
  global $status;
Dries Buytaert's avatar
   
Dries Buytaert committed
118
  $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 Buytaert's avatar
   
Dries Buytaert committed
119
120
121
  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 Buytaert's avatar
   
Dries Buytaert committed
122

Dries Buytaert's avatar
   
Dries Buytaert committed
123
124
125
  $blocks[0][subject] = "Top 10:<BR>all stories";
  $blocks[0][content] = $content;
  $blocks[0][info] = "Top 10: all stories";
Dries Buytaert's avatar
   
Dries Buytaert committed
126

Dries Buytaert's avatar
   
Dries Buytaert committed
127
  unset($content);
Dries Buytaert's avatar
   
Dries Buytaert committed
128

Dries Buytaert's avatar
   
Dries Buytaert committed
129
  $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 Buytaert's avatar
   
Dries Buytaert committed
130
131
132
  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 Buytaert's avatar
   
Dries Buytaert committed
133

Dries Buytaert's avatar
   
Dries Buytaert committed
134
135
136
  $blocks[1][subject] = "Top 10:<BR>recent stories";
  $blocks[1][content] = $content;
  $blocks[1][info] = "Top 10: recent stories";
Dries Buytaert's avatar
 
Dries Buytaert committed
137

Dries Buytaert's avatar
   
Dries Buytaert committed
138
139
  return $blocks;
*/
Dries Buytaert's avatar
 
Dries Buytaert committed
140
141
}

Dries Buytaert's avatar
   
Dries Buytaert committed
142
143
function story_query($type = "") {
  global $status;
Dries Buytaert's avatar
   
Dries Buytaert committed
144
  $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"), array("stories without category (integrity)", "WHERE n.type = 'story' AND n.cid = '0' ORDER BY n.timestamp DESC"), array("stories without topic (integrity)", "WHERE n.type = 'story' AND n.tid = '0' ORDER BY n.timestamp DESC"));
Dries Buytaert's avatar
   
Dries Buytaert committed
145
146
147
148
149
  return ($queries[$type] ? $queries[$type] : $queries);
}

function story_overview($query = array()) {
  return node_overview($query);
Dries Buytaert's avatar
 
Dries Buytaert committed
150
151
152
}

function story_admin() {
Dries Buytaert's avatar
   
Dries Buytaert committed
153
  global $id, $edit, $mod, $keys, $op, $theme, $type, $user;
Dries Buytaert's avatar
   
Dries Buytaert committed
154
155

  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 Buytaert's avatar
   
Dries Buytaert committed
156

Dries Buytaert's avatar
   
Dries Buytaert committed
157
  $type = ($type ? $type : 0);
Dries Buytaert's avatar
   
Dries Buytaert committed
158

Dries Buytaert's avatar
 
Dries Buytaert committed
159
  switch ($op) {
Dries Buytaert's avatar
   
Dries Buytaert committed
160
    case "add":
Dries Buytaert's avatar
   
Dries Buytaert committed
161
162
163
164
      print story_form();
      break;
    case "delete":
      print story_delete($id);
Dries Buytaert's avatar
   
Dries Buytaert committed
165
      print story_overview(story_query($type));
Dries Buytaert's avatar
   
Dries Buytaert committed
166
      break;
Dries Buytaert's avatar
 
Dries Buytaert committed
167
    case "edit":
Dries Buytaert's avatar
   
Dries Buytaert committed
168
      print story_form(node_get_array("nid", check_input($id)));
Dries Buytaert's avatar
 
Dries Buytaert committed
169
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
170
171
    case "help":
      story_help();
Dries Buytaert's avatar
 
Dries Buytaert committed
172
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
173
    case "listing":
Dries Buytaert's avatar
   
Dries Buytaert committed
174
      print node_listing(story_query());
Dries Buytaert's avatar
   
Dries Buytaert committed
175
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
176
    case "search":
Dries Buytaert's avatar
   
Dries Buytaert committed
177
178
      print search_form($keys);
      print search_data($keys, $mod);
Dries Buytaert's avatar
   
Dries Buytaert committed
179
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
180
    case t("Preview"):
Dries Buytaert's avatar
   
Dries Buytaert committed
181
      story_view(new Story($edit));
Dries Buytaert's avatar
   
Dries Buytaert committed
182
      print story_form($edit);
Dries Buytaert's avatar
   
Dries Buytaert committed
183
184
      break;
    case t("Submit"):
Dries Buytaert's avatar
   
Dries Buytaert committed
185
      story_save($edit);
Dries Buytaert's avatar
   
Dries Buytaert committed
186
      // fall through:
Dries Buytaert's avatar
   
Dries Buytaert committed
187
    default:
Dries Buytaert's avatar
   
Dries Buytaert committed
188
      print story_overview(story_query($type));
Dries Buytaert's avatar
   
Dries Buytaert committed
189
190
191
192
193
  }
}


function story_user() {
Dries Buytaert's avatar
   
Dries Buytaert committed
194
  global $edit, $op, $theme, $user;
Dries Buytaert's avatar
   
Dries Buytaert committed
195
196
197

  switch($op) {
    case t("Preview"):
Dries Buytaert's avatar
   
Dries Buytaert committed
198
      story_view(new Story($edit));
Dries Buytaert's avatar
   
Dries Buytaert committed
199
      $theme->box(t("Submit"), story_form($edit));
Dries Buytaert's avatar
   
Dries Buytaert committed
200
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
201
    case t("Submit"):
Dries Buytaert's avatar
   
Dries Buytaert committed
202
      story_save($edit);
Dries Buytaert's avatar
   
Dries Buytaert committed
203
      $theme->box(t("Submit"), t("Thank you for your submission."));
Dries Buytaert's avatar
 
Dries Buytaert committed
204
205
      break;
    default:
Dries Buytaert's avatar
   
Dries Buytaert committed
206
      $theme->box(t("Submit"), story_form());
Dries Buytaert's avatar
 
Dries Buytaert committed
207
208
209
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
210
?>