book.module 9.29 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4 5 6 7 8 9 10 11 12 13 14
function book_node($field) {
  global $user;

  $info = array("name" => "book page");

  return $info[$field];
}

function book_access($op, $node) {

  if ($op == "view") {
Dries's avatar
 
Dries committed
15
    return ($node->nid && $node->status && !$node->moderate);
Dries's avatar
 
Dries committed
16 17 18 19 20 21 22 23 24 25 26 27
  }

  if  ($op == "create") {
    return 1;
  }

  if ($op == "update") {
    return 1;
  }

}

Dries's avatar
 
Dries committed
28 29 30
function book_link($type) {
  if ($type == "page" && user_access("access content")) {
    $links[] = "<a href=\"module.php?mod=book\">". t("collaborative book") ."</a>";
Dries's avatar
 
Dries committed
31
  }
Dries's avatar
 
Dries committed
32 33

  return $links ? $links : array();
Dries's avatar
 
Dries committed
34 35
}

Dries's avatar
 
Dries committed
36 37 38
function book_load($node) {
  $book = db_fetch_object(db_query("SELECT parent, weight FROM book WHERE nid = '$node->nid'"));
  return $book;
Dries's avatar
 
Dries committed
39 40
}

Dries's avatar
 
Dries committed
41 42 43
function book_insert($node) {
  if ($node->pid && $node->status == 1) {
    db_query("UPDATE node SET status = 0 WHERE nid = '$node->pid'");
Dries's avatar
 
Dries committed
44
  }
Dries's avatar
 
Dries committed
45 46
  db_query("INSERT INTO book (nid, pid, parent, weight, log) VALUES ('$node->nid', '$node->pid', '$node->parent', '$node->weight', '$node->log')");
}
Dries's avatar
 
Dries committed
47

Dries's avatar
 
Dries committed
48 49 50
function book_update($node) {
  if ($node->pid && $node->status == 1) {
    db_query("UPDATE node SET status = 0 WHERE nid = '$node->pid'");
Dries's avatar
 
Dries committed
51
  }
Dries's avatar
 
Dries committed
52 53
  db_query("UPDATE book SET parent = '$node->parent', weight = '$node->weight', log = '$node->log' WHERE nid = '$node->nid'");
}
Dries's avatar
 
Dries committed
54

Dries's avatar
 
Dries committed
55 56 57 58 59
function book_delete($node) {
  if ($node->pid && $node->status == 1) {
    db_query("UPDATE node SET status = 1 WHERE nid = '$node->pid'");
  }
  db_query("DELETE FROM book WHERE nid = '$node->nid'");
Dries's avatar
 
Dries committed
60 61
}

Dries's avatar
 
Dries committed
62
function book_save($node) {
Dries's avatar
 
Dries committed
63 64
  if ($node->nid && user_access("administer nodes")) {
    return array("pid", "log", "parent", "weight");
Dries's avatar
 
Dries committed
65 66
  }
  else {
Dries's avatar
 
Dries committed
67
    return array("moderate" => 1, "promote" => 0, "status" => 0, "log", "parent", "weight");
Dries's avatar
 
Dries committed
68
  }
Dries's avatar
 
Dries committed
69 70
}

Dries's avatar
 
Dries committed
71
function book_location($node, $nodes = array()) {
Dries's avatar
 
Dries committed
72
  $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 WHERE n.nid = '$node->parent'"));
Dries's avatar
 
Dries committed
73 74 75 76 77 78 79
  if ($parent->title) {
    $nodes = book_location($parent, $nodes);
    array_push($nodes, $parent);
  }
  return $nodes;
}

Dries's avatar
 
Dries committed
80
function book_view($node, $main = 0) {
Dries's avatar
 
Dries committed
81
  global $theme;
Dries's avatar
 
Dries committed
82

Dries's avatar
 
Dries committed
83 84
  if ($main) {
    $theme->node($node, $main);
Dries's avatar
 
Dries committed
85
  }
Dries's avatar
 
Dries committed
86 87 88
  else {
    if ($node->nid && $node->parent) {
      $list = book_parent_query($node->parent);
Dries's avatar
 
Dries committed
89 90
      $next = db_fetch_object(db_query("SELECT n.nid, n.title FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND $list AND (b.weight > '$node->weight' OR (b.weight = '$node->weight' AND n.title > '". check_query($node->title) ."')) ORDER BY b.weight ASC,n.title ASC"));
      $prev = db_fetch_object(db_query("SELECT n.nid, n.title FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND $list AND (b.weight < '$node->weight' OR (b.weight = '$node->weight' AND n.title < '". check_query($node->title) ."')) ORDER BY b.weight DESC,n.title DESC"));
Dries's avatar
 
Dries committed
91
    }
Dries's avatar
 
Dries committed
92

Dries's avatar
 
Dries committed
93
    $output .= "<TABLE BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" WIDTH=\"100%\">\n";
Dries's avatar
 
Dries committed
94

Dries's avatar
 
Dries committed
95 96 97 98 99
    if ($node->title) {
      foreach (book_location($node) as $level) {
        $location .= "$indent <A HREF=\"node.php?id=$level->nid\">$level->title</A><BR>";
        $indent .= "-";
      }
Dries's avatar
 
Dries committed
100

Dries's avatar
 
Dries committed
101
      $output .= " <TR><TD COLSPAN=\"2\">$location</TD><TD ALIGN=\"right\"><A HREF=\"module.php?mod=node&op=edit&id=$node->nid\">update</A></TD></TR>\n";
Dries's avatar
 
Dries committed
102
      $output .= " <TR><TD COLSPAN=\"3\"><HR></TD></TR>";
Dries's avatar
 
Dries committed
103
      $output .= " <TR><TD COLSPAN=\"3\"><B><BIG>". check_output($node->title) ."</BIG></B>". ($node->body ? "<BR><SMALL><I>Last updated by ". format_name($node) ." on ". format_date($node->created) ."</I></SMALL> " : "") ."</TD></TR>\n";
Dries's avatar
 
Dries committed
104
    }
Dries's avatar
 
Dries committed
105

Dries's avatar
 
Dries committed
106 107 108
    if ($node->body) {
      $output .= " <TR><TD COLSPAN=\"3\"><BR>". check_output($node->body, 1) ."</TD></TR>";
    }
Dries's avatar
 
Dries committed
109

Dries's avatar
 
Dries committed
110 111 112
    if ($node->nid) {
      $output .= " <TR><TD COLSPAN=\"3\"><BR>". book_tree($node->nid) ."</TD></TR>";
    }
Dries's avatar
 
Dries committed
113

Dries's avatar
 
Dries committed
114 115 116 117
    $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";
Dries's avatar
 
Dries committed
118

Dries's avatar
 
Dries committed
119 120
    $theme->box(t("Handbook"), $output);
  }
Dries's avatar
 
Dries committed
121 122
}

Dries's avatar
 
Dries committed
123
function book_parent_query($parent) {
Kjartan's avatar
Kjartan committed
124
  if ($parent > 0) {
Dries's avatar
 
Dries committed
125 126 127 128 129 130 131 132 133 134
    $list = array();
    foreach (book_parent($parent) as $pid) array_push($list, "b.parent = $pid");
  }
  else {
    $list = array("b.parent = ''");
  }
  return "(". implode(" OR ", $list) .")";
}

function book_toc($parent = "", $indent = "", $toc = array()) {
Dries's avatar
 
Dries committed
135

Dries's avatar
 
Dries committed
136
  // select all child nodes:
Dries's avatar
 
Dries committed
137
  $result = db_query("SELECT n.nid, n.title FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.type = 'book' AND n.status = 1 AND ". book_parent_query($parent) ." ORDER BY b.weight");
Dries's avatar
 
Dries committed
138

Dries's avatar
 
Dries committed
139
  // add root node:
Dries's avatar
 
Dries committed
140
  if (user_access("administer nodes")) {
Dries's avatar
 
Dries committed
141
    $toc[0] = "<root>";
Dries's avatar
 
Dries committed
142 143
  }

Dries's avatar
 
Dries committed
144
  // build table of contents:
Dries's avatar
 
Dries committed
145
  while ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
146 147
    $toc[$node->nid] = "$indent $node->title";
    $toc = book_toc($node->nid, "$indent-", $toc);
Dries's avatar
 
Dries committed
148
  }
Dries's avatar
 
Dries committed
149

Dries's avatar
 
Dries committed
150 151 152
  return $toc;
}

Dries's avatar
 
Dries committed
153
function book_form($node, $help, $error) {
Dries's avatar
 
Dries committed
154
  global $user;
Dries's avatar
 
Dries committed
155

Dries's avatar
 
Dries committed
156 157 158
  $output .= form_select(t("Parent"), "parent", $node->parent, book_toc(), t("The parent subject or category the page belongs in."));
  $output .= form_textarea(t("Content"), "body", $node->body, 60, 20, t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")));
  $output .= form_textarea(t("Log message"), "log", $node->log, 60, 5, t("An explanation of the additions or updates being made to help the group understand your motivations."));
Dries's avatar
 
Dries committed
159

Dries's avatar
 
Dries committed
160
  if (user_access("administer nodes")) {
Dries's avatar
 
Dries committed
161
    $output .= form_select(t("Weight"), "weight", $node->weight, array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), t("The heavier nodes will sink and the lighter nodes will be positioned nearer the top."));
Dries's avatar
 
Dries committed
162 163
  }

Dries's avatar
 
Dries committed
164 165
  if ($node->pid > 0) {
    $output .= form_hidden("pid", $node->pid);
Dries's avatar
 
Dries committed
166 167
  }

Dries's avatar
 
Dries committed
168
  return $output;
Dries's avatar
 
Dries committed
169 170
}

Dries's avatar
 
Dries committed
171
function book_parent($nid) {
Dries's avatar
 
Dries committed
172
  if ($node = node_load(array("nid" => $nid))) {
Dries's avatar
 
Dries committed
173 174 175 176 177 178 179 180
    $list[$nid] = $nid;
  }
  if ($node->pid) {
    $list = array_merge($list, book_parent($node->pid));
  }
  return $list ? $list : array();
}

Dries's avatar
Dries committed
181
function book_tree($parent = "", $depth = 0) {
Dries's avatar
 
Dries committed
182
  global $PHP_SELF;
Dries's avatar
 
Dries committed
183

Dries's avatar
 
Dries committed
184 185
  if ($depth < 3 || strstr($PHP_SELF,"admin.php")) {
    // select all child nodes:
Dries's avatar
 
Dries committed
186
    $result = db_query("SELECT n.*, b.* FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.type = 'book' AND n.status = 1 AND ". book_parent_query($parent) ." ORDER BY b.weight, n.title");
Dries's avatar
 
Dries committed
187 188 189

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

Dries's avatar
 
Dries committed
198
function book_render() {
Dries's avatar
 
Dries committed
199
  global $theme;
Dries's avatar
Dries committed
200

Dries's avatar
 
Dries committed
201
  $result = db_query("SELECT n.*, b.* FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 ORDER BY b.weight");
Dries's avatar
 
Dries committed
202 203 204 205 206 207 208 209 210 211 212 213 214

  while ($node = db_fetch_object($result)) {
    $output .= "<DT><A HREF=\"node.php?id=$node->nid\">". check_output($node->title) ."</A></DT><DD>". check_output($node->body, 1) ."<BR><BR></DD>";
  }

  $theme->header();
  $theme->box(t("Handbook"), "<DL>$output</DL>");
  $theme->footer();
}

function book_page() {
  global $op, $id, $theme;

Dries's avatar
 
Dries committed
215
  if (user_access("access content")) {
Dries's avatar
Dries committed
216

Dries's avatar
 
Dries committed
217 218
    switch ($op) {
      case "feed":
Dries's avatar
 
Dries committed
219
        print book_export_html($id, $depth = 1);
Dries's avatar
 
Dries committed
220 221 222
        break;
      default:
        book_render();
Dries's avatar
 
Dries committed
223 224 225 226 227 228 229
    }
  }
  else {
    $theme->header();
    $theme->box(t("Access denied"), message_access());
    $theme->footer();
  }
Dries's avatar
 
Dries committed
230 231
}

Dries's avatar
 
Dries committed
232 233
function book_export_html($id = "", $depth = 1) {

Dries's avatar
 
Dries committed
234
  $result = db_query("SELECT n.*, b.* FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.type = 'book' AND n.status = 1 AND n.nid = '". check_input($id) ."'");
Dries's avatar
 
Dries committed
235 236 237 238 239 240 241 242 243 244 245

  while ($node = db_fetch_object($result)) {
    $output .= "<H$depth>". check_output($node->title) ."</H$depth>";
    if ($node->body) $output .= "<blockquote>". check_output($node->body, 1) ."</blockquote>";
  }
  $output .= book_export_html_recursive($id, $depth);

  return $output;
}

function book_export_html_recursive($parent = "", $depth = 1) {
Dries's avatar
 
Dries committed
246
  $result = db_query("SELECT n.*, b.* FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.type = 'book' AND n.status = 1 AND b.parent = '$parent' ORDER BY b.weight");
Dries's avatar
 
Dries committed
247 248 249

  while ($node = db_fetch_object($result)) {
    $output .= "<H$depth>". check_output($node->title) ."</H$depth>";
Dries's avatar
 
Dries committed
250 251 252
    if ($node->body) $output .= "<blockquote>". check_output($node->body, 1) ."</blockquote>";
    if ($node->pid) $output .= book_export_html_recursive($node->pid, $depth + 1);
    $output .= book_export_html_recursive($node->nid, $depth + 1);
Dries's avatar
 
Dries committed
253
  }
Dries's avatar
 
Dries committed
254

Dries's avatar
 
Dries committed
255 256
  return $output;
}
Dries's avatar
 
Dries committed
257

Dries's avatar
 
Dries committed
258
?>