story.module 7.89 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
class Story {
Dries's avatar
 
Dries committed
12 13
  function Story($story) {
    global $user;
Dries's avatar
 
Dries 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's avatar
 
Dries committed
19 20
    $this->cid = $story[cid];
    $this->tid = $story[tid];
Dries's avatar
 
Dries committed
21 22 23
  }
}

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

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

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

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

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

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

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

Dries's avatar
Dries committed
60
  $output .= "<B>". t("Your name") .":</B><BR>\n";
Dries's avatar
 
Dries 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's avatar
 
Dries committed
63

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

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

Dries's avatar
 
Dries committed
69
  $output .= "<B>". t("Abstract") .":</B><BR>\n";
Dries's avatar
 
Dries committed
70
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"edit[abstract]\">". check_textarea($edit[abstract]) ."</TEXTAREA><BR>\n";
Dries's avatar
 
Dries 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's avatar
 
Dries committed
74
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"edit[body]\">". check_textarea($edit[body]) ."</TEXTAREA><BR>\n";
Dries's avatar
 
Dries committed
75 76
  $output .= "<SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html) .".</I></SMALL><P>\n";

Dries's avatar
Dries committed
77
  if (user_access($user, "story")) {
Dries's avatar
 
Dries 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's avatar
Dries committed
80
  }
Dries's avatar
 
Dries committed
81

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

Dries's avatar
 
Dries committed
99
  return $output;
Dries's avatar
 
Dries committed
100 101
}

Dries's avatar
 
Dries committed
102
function story_save($edit) {
Dries's avatar
 
Dries committed
103
  node_save(array_diff(array_merge($edit, array(nid => $edit[nid], type => "story")), array(userid => $edit[userid])));
Dries's avatar
 
Dries committed
104 105
}

Dries's avatar
 
Dries committed
106 107 108 109 110
function story_block() {
/*
  //
  // disabled for now
  //
Dries's avatar
 
Dries committed
111
  global $status;
Dries's avatar
 
Dries committed
112
  $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
113 114 115
  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
116

Dries's avatar
 
Dries committed
117 118 119
  $blocks[0][subject] = "Top 10:<BR>all stories";
  $blocks[0][content] = $content;
  $blocks[0][info] = "Top 10: all stories";
Dries's avatar
 
Dries committed
120

Dries's avatar
 
Dries committed
121
  unset($content);
Dries's avatar
 
Dries committed
122

Dries's avatar
 
Dries committed
123
  $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
124 125 126
  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
127

Dries's avatar
 
Dries committed
128 129 130
  $blocks[1][subject] = "Top 10:<BR>recent stories";
  $blocks[1][content] = $content;
  $blocks[1][info] = "Top 10: recent stories";
Dries's avatar
 
Dries committed
131

Dries's avatar
 
Dries committed
132 133
  return $blocks;
*/
Dries's avatar
 
Dries committed
134 135
}

Dries's avatar
 
Dries committed
136 137
function story_query($type = "") {
  global $status;
Dries's avatar
 
Dries committed
138
  $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's avatar
 
Dries committed
139 140 141 142 143
  return ($queries[$type] ? $queries[$type] : $queries);
}

function story_overview($query = array()) {
  return node_overview($query);
Dries's avatar
 
Dries committed
144 145 146
}

function story_admin() {
Dries's avatar
 
Dries committed
147
  global $id, $edit, $mod, $keys, $op, $theme, $type, $user;
Dries's avatar
 
Dries committed
148 149

  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
150

Dries's avatar
 
Dries committed
151
  $type = ($type ? $type : 0);
Dries's avatar
 
Dries committed
152

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


function story_user() {
Dries's avatar
 
Dries committed
188
  global $edit, $op, $theme, $user;
Dries's avatar
 
Dries committed
189 190 191

  switch($op) {
    case t("Preview"):
Dries's avatar
 
Dries committed
192
      story_view(new Story($edit));
Dries's avatar
 
Dries committed
193
      $theme->box(t("Submit"), story_form($edit));
Dries's avatar
 
Dries committed
194
      break;
Dries's avatar
 
Dries committed
195
    case t("Submit"):
Dries's avatar
 
Dries committed
196
      story_save($edit);
Dries's avatar
 
Dries committed
197
      $theme->box(t("Submit"), t("Thank you for your submission."));
Dries's avatar
 
Dries committed
198 199
      break;
    default:
Dries's avatar
 
Dries committed
200
      $theme->box(t("Submit"), story_form());
Dries's avatar
 
Dries committed
201 202 203
  }
}

Dries's avatar
 
Dries committed
204
?>