book.module 9.04 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
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
7
  }
Dries's avatar
 
Dries committed
8 9

  return $links ? $links : array();
Dries's avatar
 
Dries committed
10 11
}

Dries's avatar
 
Dries committed
12 13 14
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
15 16
}

Dries's avatar
 
Dries committed
17 18 19
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
20
  }
Dries's avatar
 
Dries committed
21 22
  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
23

Dries's avatar
 
Dries committed
24 25 26
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
27
  }
Dries's avatar
 
Dries committed
28 29
  db_query("UPDATE book SET parent = '$node->parent', weight = '$node->weight', log = '$node->log' WHERE nid = '$node->nid'");
}
Dries's avatar
 
Dries committed
30

Dries's avatar
 
Dries committed
31 32 33 34 35
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
36 37
}

Dries's avatar
 
Dries committed
38 39 40 41 42 43 44 45 46 47 48 49 50
function book_save($node) {
  if ($node->nid) {
    if (user_access("administer nodes")) {
      return array("moderate" => 1, "promote" => 0, "status" => 0, "log", "parent", "weight");
    }
    else {
      // TODO: temporay disabled book page updates by regular users
      return 0;
    }
  }
  else {
    return array("pid", "log", "parent", "weight");
  }
Dries's avatar
 
Dries committed
51 52
}

Dries's avatar
 
Dries committed
53
function book_location($node, $nodes = array()) {
Dries's avatar
 
Dries committed
54
  $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
55 56 57 58 59 60 61
  if ($parent->title) {
    $nodes = book_location($parent, $nodes);
    array_push($nodes, $parent);
  }
  return $nodes;
}

Dries's avatar
 
Dries committed
62
function book_view($node, $main = 0) {
Dries's avatar
 
Dries committed
63
  global $theme;
Dries's avatar
 
Dries committed
64

Dries's avatar
 
Dries committed
65 66
  if ($main) {
    $theme->node($node, $main);
Dries's avatar
 
Dries committed
67
  }
Dries's avatar
 
Dries committed
68 69 70
  else {
    if ($node->nid && $node->parent) {
      $list = book_parent_query($node->parent);
Dries's avatar
 
Dries committed
71 72
      $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
73
    }
Dries's avatar
 
Dries committed
74

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

Dries's avatar
 
Dries committed
77 78 79 80 81
    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
82

Dries's avatar
 
Dries committed
83 84
      $output .= " <TR><TD COLSPAN=\"3\">$location</TD></TR>";
// TODO: <TD ALIGN=\"right\">". node_control($node) ."</TD></TR>\n";
Dries's avatar
 
Dries committed
85
      $output .= " <TR><TD COLSPAN=\"3\"><HR></TD></TR>";
Dries's avatar
 
Dries committed
86
      $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
87
    }
Dries's avatar
 
Dries committed
88

Dries's avatar
 
Dries committed
89 90 91
    if ($node->body) {
      $output .= " <TR><TD COLSPAN=\"3\"><BR>". check_output($node->body, 1) ."</TD></TR>";
    }
Dries's avatar
 
Dries committed
92

Dries's avatar
 
Dries committed
93 94 95
    if ($node->nid) {
      $output .= " <TR><TD COLSPAN=\"3\"><BR>". book_tree($node->nid) ."</TD></TR>";
    }
Dries's avatar
 
Dries committed
96

Dries's avatar
 
Dries committed
97 98 99 100
    $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
101

Dries's avatar
 
Dries committed
102 103
    $theme->box(t("Handbook"), $output);
  }
Dries's avatar
 
Dries committed
104 105
}

Dries's avatar
 
Dries committed
106
function book_parent_query($parent) {
Kjartan's avatar
Kjartan committed
107
  if ($parent > 0) {
Dries's avatar
 
Dries committed
108 109 110 111 112 113 114 115 116 117
    $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
118

Dries's avatar
 
Dries committed
119
  // select all child nodes:
Dries's avatar
 
Dries committed
120
  $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
121

Dries's avatar
 
Dries committed
122
  // add root node:
Dries's avatar
 
Dries committed
123
  if (user_access("administer nodes")) {
Dries's avatar
 
Dries committed
124
    $toc[0] = "<root>";
Dries's avatar
 
Dries committed
125 126
  }

Dries's avatar
 
Dries committed
127
  // build table of contents:
Dries's avatar
 
Dries committed
128
  while ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
129 130
    $toc[$node->nid] = "$indent $node->title";
    $toc = book_toc($node->nid, "$indent-", $toc);
Dries's avatar
 
Dries committed
131
  }
Dries's avatar
 
Dries committed
132

Dries's avatar
 
Dries committed
133 134 135
  return $toc;
}

Dries's avatar
 
Dries committed
136
function book_form($node) {
Dries's avatar
 
Dries committed
137
  global $user;
Dries's avatar
 
Dries committed
138

Dries's avatar
 
Dries committed
139 140 141
  $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
142

Dries's avatar
 
Dries committed
143
  if (user_access("administer nodes")) {
Dries's avatar
 
Dries committed
144
    $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
145 146
  }

Dries's avatar
 
Dries committed
147 148
  if ($node->pid > 0) {
    $output .= form_hidden("pid", $node->pid);
Dries's avatar
 
Dries committed
149 150
  }

Dries's avatar
 
Dries committed
151
  return $output;
Dries's avatar
 
Dries committed
152 153
}

Dries's avatar
 
Dries committed
154
function book_parent($nid) {
Dries's avatar
 
Dries committed
155
  if ($node = node_load(array("nid" => $nid))) {
Dries's avatar
 
Dries committed
156 157 158 159 160 161 162 163
    $list[$nid] = $nid;
  }
  if ($node->pid) {
    $list = array_merge($list, book_parent($node->pid));
  }
  return $list ? $list : array();
}

Dries's avatar
Dries committed
164
function book_tree($parent = "", $depth = 0) {
Dries's avatar
 
Dries committed
165
  global $PHP_SELF;
Dries's avatar
 
Dries committed
166

Dries's avatar
 
Dries committed
167 168
  if ($depth < 3 || strstr($PHP_SELF,"admin.php")) {
    // select all child nodes:
Dries's avatar
 
Dries committed
169
    $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
170 171 172

    // render output:
    while ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
173
      $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
174 175 176 177 178 179 180
      $output .= book_tree($node->nid, $depth + 1);
    }
    $output = "<UL>$output</UL>";
  }
  return $output;
}

Dries's avatar
 
Dries committed
181
function book_render() {
Dries's avatar
 
Dries committed
182
  global $theme;
Dries's avatar
Dries committed
183

Dries's avatar
 
Dries committed
184
  $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
185 186 187 188 189 190 191 192 193 194 195 196 197

  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
198
  if (user_access("access content")) {
Dries's avatar
Dries committed
199

Dries's avatar
 
Dries committed
200 201
    switch ($op) {
      case "feed":
Dries's avatar
 
Dries committed
202
        print book_export_html($id, $depth = 1);
Dries's avatar
 
Dries committed
203 204 205
        break;
      default:
        book_render();
Dries's avatar
 
Dries committed
206 207 208 209 210 211 212
    }
  }
  else {
    $theme->header();
    $theme->box(t("Access denied"), message_access());
    $theme->footer();
  }
Dries's avatar
 
Dries committed
213 214
}

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

Dries's avatar
 
Dries committed
217
  $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
218 219 220 221 222 223 224 225 226 227 228

  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
229
  $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
230 231 232

  while ($node = db_fetch_object($result)) {
    $output .= "<H$depth>". check_output($node->title) ."</H$depth>";
Dries's avatar
 
Dries committed
233 234 235
    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
236
  }
Dries's avatar
 
Dries committed
237

Dries's avatar
 
Dries committed
238 239
  return $output;
}
Dries's avatar
 
Dries committed
240

Dries's avatar
 
Dries committed
241
?>