book.module 12 KB
Newer Older
Dries's avatar
 
Dries committed
1 2 3 4
<?php

$module = array("find" => "book_find",
                "page" => "book_page",
Dries's avatar
 
Dries committed
5
                "user" => "book_user",
Dries's avatar
 
Dries committed
6 7
                "admin" => "book_admin");

Dries's avatar
 
Dries committed
8 9 10 11 12 13 14 15 16 17 18 19
class Book {
  function Book($nid, $userid, $title, $body, $parent, $weight, $timestamp) {
    $this->nid = $nid;
    $this->userid = $userid;
    $this->title = $title;
    $this->body = $body;
    $this->parent = $parent;
    $this->weight = $weight;
    $this->timestamp = $timestamp;
  }
}

Dries's avatar
 
Dries committed
20
function book_navigation($node) {
Dries's avatar
 
Dries committed
21
  if ($node->nid && $node->parent && $node->weight) {
Dries's avatar
 
Dries committed
22 23
    $next = db_fetch_object(db_query("SELECT n.nid, n.title FROM node n LEFT JOIN book b ON n.nid = b.nid AND n.lid = b.lid WHERE b.parent = '$node->parent' AND b.weight > $node->weight ORDER BY b.weight ASC"));
    $prev = db_fetch_object(db_query("SELECT n.nid, n.title FROM node n LEFT JOIN book b ON n.nid = b.nid AND n.lid = b.lid WHERE b.parent = '$node->parent' AND b.weight < $node->weight ORDER BY b.weight DESC"));
Dries's avatar
 
Dries committed
24 25 26 27 28 29 30 31 32 33 34
  }

  $output .= "<HR>";
  $output .= "<TABLE BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" WIDTH=\"100%\">\n";
  $output .= " <TR><TD ALIGN=\"left\" WIDTH=\"33%\">". ($prev ? "<A HREF=\"node.php?id=$prev->nid\">". t("previous") ."</A>" : t("previous")) ."</TD><TD ALIGN=\"center\" WIDTH=\"34%\"><A HREF=\"module.php?mod=book\">index</A></TD><TD ALIGN=\"right\" WIDTH=\"33%\">". ($next ? "<A HREF=\"node.php?id=$next->nid\">". t("next") ."</A>" : t("next")) ."</TD></TR>\n";
  $output .= " <TR><TD ALIGN=\"left\" WIDTH=\"33%\">". ($prev ? "<SMALL>". check_output($prev->title) ."</SMALL>" : "") ."</TD><TD ALIGN=\"center\" WIDTH=\"34%\">". ($node->parent ? "<A HREF=\"node.php?id=$node->parent\">". t("up") ."</A>" : t("up")) ."</TD><TD ALIGN=\"right\" WIDTH=\"33%\">". ($next ? "<SMALL>". check_output($next->title) ."</SMALL>" : "") ."</TD></TR>\n";
  $output .= "</TABLE>\n";

  return $output;
}

Dries's avatar
 
Dries committed
35 36 37 38 39 40 41 42 43
function book_location($node, $nodes = array()) {
  $parent = db_fetch_object(db_query("SELECT n.nid, n.title, b.parent FROM node n LEFT JOIN book b ON n.nid = b.nid AND n.lid = b.lid WHERE n.nid = '$node->parent'"));
  if ($parent->title) {
    $nodes = book_location($parent, $nodes);
    array_push($nodes, $parent);
  }
  return $nodes;
}

Dries's avatar
 
Dries committed
44
function theme_book($node) {
Dries's avatar
 
Dries committed
45 46
  global $theme;

Dries's avatar
 
Dries committed
47 48 49 50 51 52 53
  if ($node->nid && $node->parent) {
    $next = db_fetch_object(db_query("SELECT n.nid, n.title FROM node n LEFT JOIN book b ON n.nid = b.nid AND n.lid = b.lid WHERE b.parent = '$node->parent' AND b.weight > '$node->weight' ORDER BY b.weight ASC"));
    $prev = db_fetch_object(db_query("SELECT n.nid, n.title FROM node n LEFT JOIN book b ON n.nid = b.nid AND n.lid = b.lid WHERE b.parent = '$node->parent' AND b.weight < '$node->weight' ORDER BY b.weight DESC"));
  }

  $output .= "<TABLE BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" WIDTH=\"100%\">\n";

Dries's avatar
 
Dries committed
54
  if ($node->title) {
Dries's avatar
 
Dries committed
55 56 57 58 59 60 61 62
    foreach (book_location($node) as $level) {
      $location .= "$indent <A HREF=\"node.php?id=$level->nid\">$level->title</A><BR>";
      $indent .= "-";
    }

    $output .= " <TR><TD COLSPAN=\"2\">$location</TD><TD ALIGN=\"right\">". node_control($node) ."</TD></TR>\n";
    $output .= " <TR><TD COLSPAN=\"3\"><HR></TD</TR>";
    $output .= " <TR><TD COLSPAN=\"3\"><B><BIG>". check_output($node->title) ."</BIG></B>". ($node->body ? "<BR><SMALL><I>Last updated by ". format_username($node->userid) ." on ". format_date($node->timestamp) ."</I></SMALL> " : "") ."</TD></TR>\n";
Dries's avatar
 
Dries committed
63
  }
Dries's avatar
 
Dries committed
64

Dries's avatar
 
Dries committed
65
  if ($node->body) {
Dries's avatar
 
Dries committed
66
    $output .= " <TR><TD COLSPAN=\"3\"><BR>". check_output($node->body, 1) ."</TD></TR>";
Dries's avatar
 
Dries committed
67 68
  }

Dries's avatar
 
Dries committed
69 70 71 72 73 74 75
  $output .= " <TR><TD COLSPAN=\"3\"><BR>". book_tree($node->nid) ."</TD</TR>";
  $output .= " <TR><TD COLSPAN=\"3\"><HR></TD</TR>";
  $output .= " <TR><TD ALIGN=\"left\" WIDTH=\"33%\">". ($prev ? "<A HREF=\"node.php?id=$prev->nid\">". t("previous") ."</A>" : t("previous")) ."</TD><TD ALIGN=\"center\" WIDTH=\"34%\"><A HREF=\"module.php?mod=book\">index</A></TD><TD ALIGN=\"right\" WIDTH=\"33%\">". ($next ? "<A HREF=\"node.php?id=$next->nid\">". t("next") ."</A>" : t("next")) ."</TD></TR>\n";
  $output .= " <TR><TD ALIGN=\"left\" WIDTH=\"33%\">". ($prev ? "<SMALL>". check_output($prev->title) ."</SMALL>" : "&nbsp;") ."</TD><TD ALIGN=\"center\" WIDTH=\"34%\">". ($node->parent ? "<A HREF=\"node.php?id=$node->parent\">". t("up") ."</A>" : t("up")) ."</TD><TD ALIGN=\"right\" WIDTH=\"33%\">". ($next ? "<SMALL>". check_output($next->title) ."</SMALL>" : "&nbsp;") ."</TD></TR>\n";
  $output .= "</TABLE>\n";

  $theme->box(t("Handbook"), $output);
Dries's avatar
 
Dries committed
76
}
Dries's avatar
 
Dries committed
77

Dries's avatar
 
Dries committed
78 79
function book_view($node, $page = 1) {
  if ($page) {
Dries's avatar
 
Dries committed
80 81 82 83 84
    global $theme;
    $theme->header();
    theme_book($node);
    $theme->footer();
  }
Dries's avatar
 
Dries committed
85 86 87
  else {
    theme_book($node);
  }
Dries's avatar
 
Dries committed
88 89 90
}

function book_find($keys) {
Dries's avatar
 
Dries committed
91
  global $status, $user;
Dries's avatar
 
Dries committed
92
  $find = array();
Dries's avatar
 
Dries committed
93
  $result = db_query("SELECT n.*, u.userid FROM node n LEFT JOIN book b ON n.nid = b.nid AND n.lid = b.lid LEFT JOIN users u ON n.author = u.id WHERE n.type = 'book' AND n.status = '$status[posted]' AND (n.title LIKE '%". check_input($keys) ."%' OR b.body LIKE '%". check_input($keys) ."%') ORDER BY n.timestamp DESC LIMIT 20");
Dries's avatar
 
Dries committed
94 95 96 97 98 99 100 101 102 103 104 105
  while ($node = db_fetch_object($result)) {
    array_push($find, array("title" => check_output($node->title), "link" => (user_access($user, "book") ? "admin.php?mod=book&op=edit&id=$node->nid" : "node.php?id=$node->nid"), "user" => $node->userid, "date" => $node->timestamp));
  }
  return $find;
}

function book_search() {
  global $keys, $mod;
  print search_form($keys);
  print search_data($keys, $mod);
}

Dries's avatar
 
Dries committed
106
function book_toc($parent = 0, $indent = "", $toc = array()) {
Dries's avatar
 
Dries committed
107
  global $status;
Dries's avatar
 
Dries committed
108
  $result = db_query("SELECT n.*, b.* FROM node n LEFT JOIN book b ON n.nid = b.nid AND n.lid = b.lid WHERE n.type = 'book' AND n.status = '$status[posted]' AND b.parent = '$parent' ORDER BY b.weight");
Dries's avatar
 
Dries committed
109
  while ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
110 111 112
    $toc[$node->nid] = "$indent $node->title";
    if ($node->pid) $toc = book_toc($node->pid, "$indent-", $toc);
    $toc = book_toc($node->nid, "$indent-", $toc);
Dries's avatar
 
Dries committed
113 114 115 116
  }
  return $toc;
}

Dries's avatar
 
Dries committed
117
function book_form($edit = array()) {
Dries's avatar
 
Dries committed
118
  global $allowed_html, $PHP_SELF, $REQUEST_URI, $user;
Dries's avatar
 
Dries committed
119

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

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

Dries's avatar
 
Dries committed
126 127 128 129
  if ($edit[pid]) {
    $node = node_get_object("nid", $edit[pid]);
    $output .= "<B>". t("Parent") .":</B><BR>\n";
    $output .= "<A HREF=\"node.php?id=$node->id\">". check_output($node->title) ."</A><P>\n";
Dries's avatar
 
Dries committed
130
    $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[parent]\" VALUE=\"$edit[parent]\">\n";
Dries's avatar
 
Dries committed
131
    $output .= "<SMALL><I>". t("The parent subject or category the page belongs in.") ."</I></SMALL><P>\n";
Dries's avatar
 
Dries committed
132 133 134
  }
  else {
    $output .= "<B>". t("Parent") .":</B><BR>\n";
Dries's avatar
 
Dries committed
135
    foreach (book_toc() as $key=>$value) $options2 .= "<OPTION VALUE=\"$key\"". ($edit[parent] == $key ? " SELECTED" : "") .">". check_select($value) ."</OPTION>";
Dries's avatar
 
Dries committed
136 137
    if (user_access($user, "book")) $options2 .= "<OPTION VALUE=\"0\"". ($edit[parent] == 0 ? " SELECTED" : "") .">&nbsp;</OPTION>";
    $output .= "<SELECT NAME=\"edit[parent]\">$options2</SELECT><BR>\n";
Dries's avatar
 
Dries committed
138
    $output .= "<SMALL><I>". t("The parent subject or category the page belongs in.") ."</I></SMALL><P>\n";
Dries's avatar
 
Dries committed
139
  }
Dries's avatar
 
Dries committed
140 141 142 143 144 145 146 147

  $output .= "<B>". t("Subject") .":</B><BR>\n";
  $output .= "<INPUT TYPE=\"text\" NAME=\"edit[title]\" SIZE=\"50\" MAXLENGTH=\"128\" VALUE=\"". check_textfield($edit[title]) ."\"><P>\n";

  $output .= "<B>". t("Content") .":</B><BR>\n";
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"edit[body]\" MAXLENGTH=\"20\">". 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
148 149 150
  if ($edit[pid]) {
    $output .= "<B>". t("Log message") .":</B><BR>\n";
    $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"5\" NAME=\"edit[log]\" MAXLENGTH=\"20\">". check_textarea($edit[log]) ."</TEXTAREA><BR>\n";
Dries's avatar
 
Dries committed
151
    $output .= "<SMALL><I>". t("An explanation of the updates being made to help the group understand the changes.") ."</I></SMALL><P>\n";
Dries's avatar
 
Dries committed
152 153
  }

Dries's avatar
 
Dries committed
154 155
  if (user_access($user, "book")) {
    $output .= "<B>". t("Weight") .":</B><BR>\n";
Dries's avatar
 
Dries committed
156 157
    for ($count = 0; $count < 25; $count++) $options3 .= "<OPTION VALUE=\"$count\"". ($edit[weight] == $count ? " SELECTED" : "") .">$count</OPTION>";
    $output .= "<SELECT NAME=\"edit[weight]\">$options3</SELECT><BR>\n";
Dries's avatar
 
Dries committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    $output .= "<SMALL><I>". t("The heavier nodes will sink and the lighter nodes will be positioned nearer the top.") ."</I></SMALL><P>\n";
  }

  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 title.") ."</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
173
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[pid]\" VALUE=\"$edit[pid]\">\n";
Dries's avatar
 
Dries committed
174
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[nid]\" VALUE=\"$edit[nid]\">\n";
Dries's avatar
 
Dries committed
175

Dries's avatar
 
Dries committed
176 177 178 179 180 181
  $output .= "</FORM>\n";

  return $output;
}

function book_save($edit) {
Dries's avatar
 
Dries committed
182
  node_save(array_diff(array_merge($edit, array(nid => $edit[nid], type => "book")), array(userid => $edit[userid])));
Dries's avatar
 
Dries committed
183 184
}

Dries's avatar
 
Dries committed
185
function book_tree($parent = "") {
Dries's avatar
 
Dries committed
186
  global $PHP_SELF, $status;
Dries's avatar
 
Dries committed
187

Dries's avatar
 
Dries committed
188
  $result = db_query("SELECT n.*, b.* FROM node n LEFT JOIN book b ON n.nid = b.nid AND n.lid = b.lid WHERE n.type = 'book' AND n.status = '$status[posted]' AND b.parent = '$parent' ORDER BY b.weight");
Dries's avatar
 
Dries committed
189

Dries's avatar
 
Dries committed
190
  $output .= "<UL>";
Dries's avatar
 
Dries committed
191
  while ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
192
    $output .= "<LI><A HREF=\"node.php?id=$node->nid\">". check_output($node->title) ."</A>";
Dries's avatar
 
Dries committed
193
    if ($PHP_SELF == "/admin.php") $output .= " <SMALL>(weight: $node->weight/$node->parent, status: $node->status) (<A HREF=\"admin.php?mod=book&op=edit&id=$node->nid\">edit</A>)</SMALL>\n";
Dries's avatar
 
Dries committed
194 195
    if ($node->pid) $output .= book_tree($node->pid);
    $output .= book_tree($node->nid);
Dries's avatar
 
Dries committed
196
  }
Dries's avatar
 
Dries committed
197
  $output .= "</UL>";
Dries's avatar
 
Dries committed
198 199 200
  return $output;
}

Dries's avatar
 
Dries committed
201
function book_list() {
Dries's avatar
 
Dries committed
202
  return node_overview("type = 'book'");
Dries's avatar
 
Dries committed
203 204
}

Dries's avatar
 
Dries committed
205
function book_admin() {
Dries's avatar
 
Dries committed
206
  global $op, $id, $edit, $user;
Dries's avatar
 
Dries committed
207

Dries's avatar
 
Dries committed
208
  print "<SMALL><A HREF=\"admin.php?mod=book&op=add\">add new page</A> | <A HREF=\"admin.php?mod=book&op=search\">search book</A> | <A HREF=\"admin.php?mod=book&op=list\">list overview</A> | <A HREF=\"admin.php?mod=book\">tree overview</A></SMALL><HR>\n";
Dries's avatar
 
Dries committed
209 210 211 212 213

  switch ($op) {
    case "add":
      print book_form();
      break;
Dries's avatar
 
Dries committed
214 215
   case "list":
      print book_list();
Dries's avatar
 
Dries committed
216
      break;
Dries's avatar
 
Dries committed
217 218 219 220 221 222 223
    case "edit":
      print book_form(node_get_array(nid, $id));
      break;
    case "search":
      book_search();
      break;
    case t("Preview"):
Dries's avatar
 
Dries committed
224
      book_view(new Book(($edit[nid] ? $edit[nid] : -1), ($edit[userid] ? $edit[userid] : $user->userid), $edit[title], $edit[body], $edit[parent], $edit[weight], ($edit[timestamp] ? $edit[timestamp] : time())), 0);
Dries's avatar
 
Dries committed
225 226 227 228
      print book_form($edit);
      break;
    case t("Submit"):
      book_save($edit);
Dries's avatar
 
Dries committed
229
      print book_tree();
Dries's avatar
 
Dries committed
230 231
      break;
    default:
Dries's avatar
 
Dries committed
232
      print book_tree();
Dries's avatar
 
Dries committed
233 234 235 236 237 238 239 240
  }
}

function book_page($id = 0) {
  global $theme;
  book_view(node_get_object("nid", $nid));
}

Dries's avatar
 
Dries committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
function book_user() {
  global $edit, $id, $op, $theme, $user;

  switch($op) {
    case "update":
      $node = node_get_object("nid", $id);
      $theme->box("Update a book page", book_form(array(nid => -1, pid => $id, title => $node->title, body => $node->body, parent => $node->parent)));
      break;
    case t("Preview"):
      book_view(new Book(($edit[nid] ? $edit[nid] : -1), $user->userid, $edit[title], $edit[body], $edit[parent], $edit[weight], ($edit[timestamp] ? $edit[timestamp] : time())), 0);
      $theme->box("Submit a book page", book_form($edit));
      break;
    case t("Submit"):
      book_save($edit);
      $theme->box(t("Submit a book page"), t("Thank you for your submission."));
      break;
    default:
      $theme->box("Submit a book page", book_form());
  }
}

Dries's avatar
 
Dries committed
262
?>