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

Dries's avatar
 
Dries committed
3
class Story {
Dries's avatar
 
Dries committed
4
  function Story($story) {
Dries's avatar
 
Dries committed
5
    $this = new Node($story);
Dries's avatar
 
Dries committed
6 7
    $this->abstract = $story[abstract];
    $this->body = $story[body];
Dries's avatar
 
Dries committed
8 9 10
  }
}

Dries's avatar
 
Dries committed
11 12 13 14
function story_status() {
  return array(dumped, queued, posted);
}

Dries's avatar
 
Dries committed
15
function story_find($keys) {
Dries's avatar
 
Dries committed
16
  global $status, $user;
Dries's avatar
 
Dries committed
17
  $find = array();
Dries's avatar
 
Dries committed
18
  $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
19
  while ($story = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
20
    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
21 22 23 24
  }
  return $find;
}

Dries's avatar
 
Dries committed
25 26
function story_help() {
 ?>
Dries's avatar
 
Dries committed
27 28 29
  <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
30
 <?php
Dries's avatar
 
Dries committed
31 32
}

Dries's avatar
 
Dries committed
33 34 35
function story_view($node, $main = 0) {
  global $theme;
  $theme->story($node, $main);
Dries's avatar
 
Dries committed
36
}
Dries's avatar
 
Dries committed
37

Dries's avatar
 
Dries committed
38
function story_form($edit = array()) {
Dries's avatar
 
Dries committed
39
  global $allowed_html, $REQUEST_URI, $user;
Dries's avatar
 
Dries committed
40

Dries's avatar
 
Dries committed
41 42 43 44 45 46
  $form .= form_item(t("Your name"), format_username(($edit[userid] ? $edit[userid] : $user->userid)));
  $form .= form_hidden("userid", $edit[userid]);
  $form .= form_textfield(t("Subject"), "title", $edit[title], 50, 64);
  $form .= structure_form("story", $edit);
  $form .= form_textarea(t("Abstract"), "abstract", $edit[abstract], 50, 10, t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html));
  $form .= form_textarea(t("Body"), "body", $edit[body], 50, 15, t("Allowed HTML tags") .": ". htmlspecialchars($allowed_html));
Dries's avatar
 
Dries committed
47

Dries's avatar
 
Dries committed
48 49 50
  // hidden fields:
  $form .= form_hidden("timestamp", $edit[timestamp]);
  $form .= form_hidden("nid", $edit[nid]);
Dries's avatar
 
Dries committed
51

Dries's avatar
 
Dries committed
52
  if (!$edit) {
Dries's avatar
 
Dries committed
53
    $form .= form_submit(t("Preview"));
Dries's avatar
 
Dries committed
54
  }
Dries's avatar
 
Dries committed
55
  else if (!$edit[title]) {
Dries's avatar
 
Dries committed
56
    $output .= "<FONT COLOR=\"red\">". t("Warning: you did not supply a subject.") ."</FONT><P>\n";
Dries's avatar
 
Dries committed
57
    $form .= form_submit(t("Preview"));
Dries's avatar
 
Dries committed
58
  }
Dries's avatar
 
Dries committed
59
  else if (!$edit[abstract]) {
Dries's avatar
 
Dries committed
60
    $output .= "<FONT COLOR=\"red\">". t("Warning: you did not supply an abstract.") ."</FONT><P>\n";
Dries's avatar
 
Dries committed
61
    $form .= form_submit(t("Preview"));
Dries's avatar
 
Dries committed
62 63
  }
  else {
Dries's avatar
 
Dries committed
64 65
    $form .= form_submit(t("Preview"));
    $form .= form_submit(t("Submit"));
Dries's avatar
 
Dries committed
66
  }
Dries's avatar
 
Dries committed
67

Dries's avatar
 
Dries committed
68
  return form($REQUEST_URI, $form);
Dries's avatar
 
Dries committed
69 70
}

Dries's avatar
 
Dries committed
71
function story_save($edit) {
Dries's avatar
 
Dries committed
72
  node_save(array_diff(array_merge($edit, array(nid => $edit[nid], type => "story")), array(userid => $edit[userid])));
Dries's avatar
 
Dries committed
73 74
}

Dries's avatar
 
Dries committed
75 76 77 78 79
function story_block() {
/*
  //
  // disabled for now
  //
Dries's avatar
 
Dries committed
80
  global $status;
Dries's avatar
 
Dries committed
81
  $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
82 83 84
  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
85

Dries's avatar
 
Dries committed
86 87 88
  $blocks[0][subject] = "Top 10:<BR>all stories";
  $blocks[0][content] = $content;
  $blocks[0][info] = "Top 10: all stories";
Dries's avatar
 
Dries committed
89

Dries's avatar
 
Dries committed
90
  unset($content);
Dries's avatar
 
Dries committed
91

Dries's avatar
 
Dries committed
92
  $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
93 94 95
  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
96

Dries's avatar
 
Dries committed
97 98 99
  $blocks[1][subject] = "Top 10:<BR>recent stories";
  $blocks[1][content] = $content;
  $blocks[1][info] = "Top 10: recent stories";
Dries's avatar
 
Dries committed
100

Dries's avatar
 
Dries committed
101 102
  return $blocks;
*/
Dries's avatar
 
Dries committed
103 104
}

Dries's avatar
 
Dries committed
105 106
function story_query($type = "") {
  global $status;
Dries's avatar
 
Dries committed
107
  $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
108 109 110 111 112
  return ($queries[$type] ? $queries[$type] : $queries);
}

function story_overview($query = array()) {
  return node_overview($query);
Dries's avatar
 
Dries committed
113 114 115
}

function story_admin() {
Dries's avatar
 
Dries committed
116
  global $id, $edit, $mod, $keys, $op, $theme, $type, $user;
Dries's avatar
 
Dries committed
117 118

  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
119

Dries's avatar
 
Dries committed
120
  $type = ($type ? $type : 0);
Dries's avatar
 
Dries committed
121

Dries's avatar
 
Dries committed
122
  switch ($op) {
Dries's avatar
 
Dries committed
123
    case "add":
Dries's avatar
 
Dries committed
124 125 126 127
      print story_form();
      break;
    case "delete":
      print story_delete($id);
Dries's avatar
 
Dries committed
128
      print story_overview(story_query($type));
Dries's avatar
 
Dries committed
129
      break;
Dries's avatar
 
Dries committed
130
    case "edit":
Dries's avatar
 
Dries committed
131
      print story_form(node_get_array("nid", check_input($id)));
Dries's avatar
 
Dries committed
132
      break;
Dries's avatar
 
Dries committed
133 134
    case "help":
      story_help();
Dries's avatar
 
Dries committed
135
      break;
Dries's avatar
 
Dries committed
136
    case "listing":
Dries's avatar
 
Dries committed
137
      print node_listing(story_query());
Dries's avatar
 
Dries committed
138
      break;
Dries's avatar
 
Dries committed
139
    case "search":
Dries's avatar
 
Dries committed
140 141
      print search_form($keys);
      print search_data($keys, $mod);
Dries's avatar
 
Dries committed
142
      break;
Dries's avatar
 
Dries committed
143
    case t("Preview"):
Dries's avatar
 
Dries committed
144
      story_view(new Story($edit));
Dries's avatar
 
Dries committed
145
      print story_form($edit);
Dries's avatar
 
Dries committed
146 147
      break;
    case t("Submit"):
Dries's avatar
 
Dries committed
148
      story_save($edit);
Dries's avatar
 
Dries committed
149
      // fall through:
Dries's avatar
 
Dries committed
150
    default:
Dries's avatar
 
Dries committed
151
      print story_overview(story_query($type));
Dries's avatar
 
Dries committed
152 153 154 155 156
  }
}


function story_user() {
Dries's avatar
 
Dries committed
157
  global $edit, $op, $theme, $user;
Dries's avatar
 
Dries committed
158 159 160

  switch($op) {
    case t("Preview"):
Dries's avatar
 
Dries committed
161
      story_view(new Story($edit));
Dries's avatar
 
Dries committed
162
      $theme->box(t("Submit"), story_form($edit));
Dries's avatar
 
Dries committed
163
      break;
Dries's avatar
 
Dries committed
164
    case t("Submit"):
Dries's avatar
 
Dries committed
165
      story_save($edit);
Dries's avatar
 
Dries committed
166
      $theme->box(t("Submit"), t("Thank you for your submission."));
Dries's avatar
 
Dries committed
167 168
      break;
    default:
Dries's avatar
 
Dries committed
169
      $theme->box(t("Submit"), story_form());
Dries's avatar
 
Dries committed
170 171 172
  }
}

Dries's avatar
 
Dries committed
173
?>