2001-03-24 16:36:13 +00:00
<?php
$module = array("find" => "book_find",
"page" => "book_page",
2001-03-25 10:57:01 +00:00
"user" => "book_user",
2001-03-24 16:36:13 +00:00
"admin" => "book_admin");
2001-03-25 10:57:01 +00:00
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;
}
}
2001-04-05 20:33:36 +00:00
function book_post_threshold($node, $default) {
return $default;
}
function book_dump_threshold($node, $default) {
return $default;
}
function book_timout_threshold($node, $default) {
return $default;
}
2001-04-07 15:02:28 +00:00
function book_status() {
return array(dumped, expired, queued, posted);
}
2001-04-04 12:54:10 +00:00
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;
}
2001-04-04 21:09:24 +00:00
function book_view($node, $page = 1) {
2001-03-24 16:36:13 +00:00
global $theme;
2001-04-04 12:54:10 +00:00
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";
2001-03-25 10:57:01 +00:00
if ($node->title) {
2001-04-04 12:54:10 +00:00
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";
2001-03-24 16:36:13 +00:00
}
2001-04-04 12:54:10 +00:00
2001-03-25 10:57:01 +00:00
if ($node->body) {
2001-04-04 12:54:10 +00:00
$output .= " <TR><TD COLSPAN=\"3\"><BR>". check_output($node->body, 1) ."</TD></TR>";
2001-03-24 16:36:13 +00:00
}
2001-04-04 12:54:10 +00:00
$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>" : " ") ."</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";
2001-04-04 21:09:24 +00:00
if ($page) $theme->header();
2001-04-04 12:54:10 +00:00
$theme->box(t("Handbook"), $output);
2001-04-04 21:09:24 +00:00
if ($page) $theme->footer();
2001-03-24 16:36:13 +00:00
}
function book_find($keys) {
2001-03-25 10:57:01 +00:00
global $status, $user;
2001-03-24 16:36:13 +00:00
$find = array();
2001-03-29 19:50:31 +00:00
$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");
2001-03-24 16:36:13 +00:00
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;
}
2001-04-04 12:54:10 +00:00
function book_toc($parent = 0, $indent = "", $toc = array()) {
2001-03-25 16:42:52 +00:00
global $status;
2001-03-29 19:50:31 +00:00
$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");
2001-03-25 16:42:52 +00:00
while ($node = db_fetch_object($result)) {
2001-04-04 12:54:10 +00:00
$toc[$node->nid] = "$indent $node->title";
if ($node->pid) $toc = book_toc($node->pid, "$indent-", $toc);
$toc = book_toc($node->nid, "$indent-", $toc);
2001-03-25 16:42:52 +00:00
}
return $toc;
}
2001-03-24 16:36:13 +00:00
function book_form($edit = array()) {
2001-04-01 10:52:01 +00:00
global $allowed_html, $PHP_SELF, $REQUEST_URI, $user;
2001-03-24 16:36:13 +00:00
2001-03-25 10:57:01 +00:00
$output .= "<FORM ACTION=\"$REQUEST_URI\" METHOD=\"post\">\n";
2001-03-24 16:36:13 +00:00
$output .= "<B>". t("Author") .":</B><BR>\n";
2001-03-25 10:57:01 +00:00
$output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[userid]\" VALUE=\"$edit[userid]\">\n";
2001-03-24 16:36:13 +00:00
$output .= format_username(($edit[userid] ? $edit[userid] : $user->userid)) ."<P>\n";
2001-03-25 10:57:01 +00:00
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";
2001-03-31 17:56:12 +00:00
$output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[parent]\" VALUE=\"$edit[parent]\">\n";
2001-04-04 12:54:10 +00:00
$output .= "<SMALL><I>". t("The parent subject or category the page belongs in.") ."</I></SMALL><P>\n";
2001-03-25 10:57:01 +00:00
}
else {
$output .= "<B>". t("Parent") .":</B><BR>\n";
2001-03-25 16:42:52 +00:00
foreach (book_toc() as $key=>$value) $options2 .= "<OPTION VALUE=\"$key\"". ($edit[parent] == $key ? " SELECTED" : "") .">". check_select($value) ."</OPTION>";
2001-03-25 10:57:01 +00:00
if (user_access($user, "book")) $options2 .= "<OPTION VALUE=\"0\"". ($edit[parent] == 0 ? " SELECTED" : "") ."> </OPTION>";
$output .= "<SELECT NAME=\"edit[parent]\">$options2</SELECT><BR>\n";
2001-04-04 12:54:10 +00:00
$output .= "<SMALL><I>". t("The parent subject or category the page belongs in.") ."</I></SMALL><P>\n";
2001-03-25 10:57:01 +00:00
}
2001-03-24 16:36:13 +00:00
$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";
2001-04-06 14:14:16 +00:00
$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";
$output .= "<SMALL><I>". t("An explanation of the additions or updates being made to help the group understand your motivations.") ."</I></SMALL><P>\n";
2001-03-25 10:57:01 +00:00
2001-03-24 16:36:13 +00:00
if (user_access($user, "book")) {
$output .= "<B>". t("Weight") .":</B><BR>\n";
2001-03-25 10:57:01 +00:00
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";
2001-03-24 16:36:13 +00:00
$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";
}
2001-03-25 10:57:01 +00:00
$output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[pid]\" VALUE=\"$edit[pid]\">\n";
2001-03-24 16:36:13 +00:00
$output .= "<INPUT TYPE=\"hidden\" NAME=\"edit[nid]\" VALUE=\"$edit[nid]\">\n";
2001-03-25 10:57:01 +00:00
2001-03-24 16:36:13 +00:00
$output .= "</FORM>\n";
return $output;
}
function book_save($edit) {
2001-03-31 18:38:01 +00:00
node_save(array_diff(array_merge($edit, array(nid => $edit[nid], type => "book")), array(userid => $edit[userid])));
2001-03-24 16:36:13 +00:00
}
2001-03-31 19:10:53 +00:00
function book_tree($parent = "") {
2001-03-25 10:57:01 +00:00
global $PHP_SELF, $status;
2001-03-24 16:36:13 +00:00
2001-03-31 19:10:53 +00:00
$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");
2001-03-31 18:38:01 +00:00
2001-03-31 19:10:53 +00:00
$output .= "<UL>";
2001-03-24 16:36:13 +00:00
while ($node = db_fetch_object($result)) {
2001-03-31 19:10:53 +00:00
$output .= "<LI><A HREF=\"node.php?id=$node->nid\">". check_output($node->title) ."</A>";
2001-04-02 15:54:37 +00:00
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";
2001-03-31 19:10:53 +00:00
if ($node->pid) $output .= book_tree($node->pid);
$output .= book_tree($node->nid);
2001-03-31 18:38:01 +00:00
}
2001-03-31 19:10:53 +00:00
$output .= "</UL>";
2001-03-24 16:36:13 +00:00
return $output;
}
2001-03-31 19:10:53 +00:00
function book_list() {
2001-04-02 15:54:37 +00:00
return node_overview("type = 'book'");
2001-03-26 20:22:09 +00:00
}
2001-03-24 16:36:13 +00:00
function book_admin() {
2001-04-04 21:09:24 +00:00
global $op, $id, $edit, $mod, $keys, $user;
2001-03-24 16:36:13 +00:00
2001-03-31 19:10:53 +00:00
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";
2001-03-24 16:36:13 +00:00
switch ($op) {
case "add":
print book_form();
break;
2001-03-31 19:10:53 +00:00
case "list":
print book_list();
2001-03-26 20:22:09 +00:00
break;
2001-03-24 16:36:13 +00:00
case "edit":
print book_form(node_get_array(nid, $id));
break;
case "search":
2001-04-04 21:09:24 +00:00
print search_form($keys);
print search_data($keys, $mod);
2001-03-24 16:36:13 +00:00
break;
case t("Preview"):
2001-03-25 10:57:01 +00:00
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);
2001-03-24 16:36:13 +00:00
print book_form($edit);
break;
case t("Submit"):
book_save($edit);
2001-03-31 19:10:53 +00:00
print book_tree();
2001-03-24 16:36:13 +00:00
break;
default:
2001-03-31 19:10:53 +00:00
print book_tree();
2001-03-24 16:36:13 +00:00
}
}
function book_page($id = 0) {
global $theme;
book_view(node_get_object("nid", $nid));
}
2001-04-04 21:09:24 +00:00
function book_update($id) {
global $status;
if ($node = node_get_object("nid", $id)) {
if ($node->status != $status[posted]) {
return t("You can only update accepted pages: pages that are still queued or already expired can not be updated.");
}
else if (db_result(db_query("SELECT COUNT(nid) FROM node WHERE pid = '$node->nid' AND status = '$status[queued]'"))) {
2001-04-05 20:33:36 +00:00
return t("There is already an update for this node in the queue: we can only process one update at once.");
2001-04-04 21:09:24 +00:00
}
else {
return book_form(array(nid => -1, pid => $id, title => $node->title, body => $node->body, parent => $node->parent));
}
}
}
2001-03-25 10:57:01 +00:00
function book_user() {
global $edit, $id, $op, $theme, $user;
2001-04-04 21:09:24 +00:00
$title = t("Submit a book page");
2001-03-25 10:57:01 +00:00
switch($op) {
case "update":
2001-04-04 21:09:24 +00:00
$theme->box($title, book_update($id));
2001-03-25 10:57:01 +00:00
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);
2001-04-04 21:09:24 +00:00
$theme->box($title, book_form($edit));
2001-03-25 10:57:01 +00:00
break;
case t("Submit"):
book_save($edit);
2001-04-04 21:09:24 +00:00
$theme->box($title, t("Thank you for your submission."));
2001-03-25 10:57:01 +00:00
break;
default:
2001-04-04 21:09:24 +00:00
$theme->box($title, book_form());
2001-03-25 10:57:01 +00:00
}
}
2001-03-24 16:36:13 +00:00
?>