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

4 5 6
/**
 * Implementation of hook_node_name().
 */
Dries's avatar
 
Dries committed
7
function book_node_name($node) {
8
  return t('book page');
Dries's avatar
 
Dries committed
9 10
}

11 12 13
/**
 * Implementation of hook_perm().
 */
Dries's avatar
 
Dries committed
14
function book_perm() {
15
  return array('maintain books');
Dries's avatar
 
Dries committed
16 17
}

18 19 20
/**
 * Implementation of hook_access().
 */
Dries's avatar
 
Dries committed
21
function book_access($op, $node) {
Dries's avatar
 
Dries committed
22
  global $user;
Dries's avatar
 
Dries committed
23

24
  if ($op == 'view') {
Dries's avatar
 
Dries committed
25 26 27 28 29 30 31 32
    /*
    ** Everyone can access all published book pages whether these pages
    ** are still waiting for approval or not.  We might not always want
    ** to display pages that are waiting for approval, but we take care
    ** of that problem in the book_view() function.
    */

    return $node->status;
Dries's avatar
 
Dries committed
33 34
  }

35
  if ($op == 'create') {
Dries's avatar
 
Dries committed
36 37 38 39 40
    /*
    ** Only registered users can create book pages.  Given the nature
    ** of the book module this is considered to be a good/safe idea.
    */

41
    return user_access('maintain books');
Dries's avatar
 
Dries committed
42 43
  }

44
  if ($op == 'update') {
Dries's avatar
 
Dries committed
45
    /*
Dries's avatar
 
Dries committed
46 47
    ** Only registered users can update book pages.  Given the nature
    ** of the book module this is considered to be a good/safe idea.
Dries's avatar
 
Dries committed
48
    ** One can only update a book page if there are no suggested updates
Dries's avatar
 
Dries committed
49 50 51 52
    ** of that page waiting for approval, when it is not a PHP-page and
    ** as long as the "create new revision"-bit is set.  That is, only
    ** updates that don't overwrite the current or pending information
    ** are allowed.
Dries's avatar
 
Dries committed
53
    */
Dries's avatar
 
Dries committed
54

55
    return user_access('maintain books') && !$node->moderate && !$node->format && $node->revision;
Dries's avatar
 
Dries committed
56
  }
Dries's avatar
 
Dries committed
57 58
}

Dries's avatar
 
Dries committed
59 60 61
/**
 * Implementation of hook_link().
 */
Dries's avatar
 
Dries committed
62
function book_link($type, $node = 0, $main = 0) {
Dries's avatar
 
Dries committed
63 64 65

  $links = array();

Dries's avatar
 
Dries committed
66 67
  if ($type == 'page' && user_access('access content')) {
    $links[] = l(t('books'), 'book', array('title' => t('Read and contribute to the collaborative books.')));
Dries's avatar
 
Dries committed
68 69
  }

Dries's avatar
 
Dries committed
70 71
  if ($type == 'node' && $node->type == 'book') {
    if (book_access('update', $node)) {
Dries's avatar
 
Dries committed
72
      $links[] = l(t('edit this page'), "node/$node->nid/edit", array('title' => t('Suggest an update for this book page.')));
Dries's avatar
 
Dries committed
73
    }
Dries's avatar
 
Dries committed
74
    if (!$main) {
Dries's avatar
 
Dries committed
75
      $links[] = l(t('printer-friendly version'), "book/print/$node->nid", array('title' => t('Show a printer-friendly version of this book page and its sub-pages.')));
Dries's avatar
 
Dries committed
76
    }
Dries's avatar
 
Dries committed
77 78
  }

Dries's avatar
 
Dries committed
79
  return $links;
Dries's avatar
 
Dries committed
80 81
}

Dries's avatar
 
Dries committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/**
 * Implementation of hook_menu().
 */
function book_menu() {
  $items = array();

  $items[] = array('path' => 'node/add/book', 'title' => t('book page'),
    'access' => user_access('maintain books'));
  $items[] = array('path' => 'admin/node/book', 'title' => t('books'),
    'callback' => 'book_admin',
    'access' => user_access('administer nodes'),
    'weight' => 4);
  $items[] = array('path' => 'admin/node/book/orphan', 'title' => t('orphan pages'),
    'callback' => 'book_admin_orphan',
    'access' => user_access('administer nodes'),
    'weight' => 8);
  $result = db_query('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 ORDER BY b.weight, n.title');
  while ($book = db_fetch_object($result)) {
    $items[] = array('path' => 'admin/node/book/'. $book->nid, 'title' => t('"%title" book', array('%title' => $book->title)));
  }
  $items[] = array('path' => 'book', 'title' => t('books'),
    'callback' => 'book_render',
    'access' => user_access('access content'),
    'type' => MENU_SUGGESTED_ITEM);
  $items[] = array('path' => 'book/view', 'title' => t('view'),
    'callback' => 'book_view_page',
    'access' => user_access('access content'),
    'type' => MENU_CALLBACK);
  $items[] = array('path' => 'book/print', 'title' => t('printer-friendly version'),
    'callback' => 'book_print',
    'access' => user_access('access content'),
    'type' => MENU_CALLBACK);

  return $items;
}

118 119 120 121 122 123
/**
 * Implementation of hook_block().
 *
 * Displays the book table of contents in a block when a node is being
 * viewed using a "book/view" path.
 */
Dries's avatar
 
Dries committed
124 125 126 127
function book_block($op = 'list', $delta = 0) {

  // Only display this block when the user is browsing a book:
  if (arg(0) == 'book' && arg(1) == 'view' && $nid = arg(2)) {
128
    $page = db_fetch_object(db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d', $nid));
Dries's avatar
 
Dries committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

    $path = book_location($page);
    $path[] = $page;

    foreach ($path as $key => $node) {
      $expand[] = $node->nid;
    }

    $title = $path[0]->title;
    $table = book_tree($expand[0], 5, $expand);
  }

  if ($op == 'list') {
    $block[0]['info'] = t('Book navigation');
  }
  else {
    $block['subject'] = $title;
    $block['content'] = $table;
  }

   return $block;
}

152 153 154
/**
 * Implementation of hook_load().
 */
Dries's avatar
 
Dries committed
155
function book_load($node) {
Dries's avatar
 
Dries committed
156
  global $user;
Dries's avatar
 
Dries committed
157

158
  $book = db_fetch_object(db_query('SELECT format, parent, weight, log FROM {book} WHERE nid = %d', $node->nid));
Dries's avatar
 
Dries committed
159

160
  if (arg(1) == 'edit' && !user_access('administer nodes')) {
Dries's avatar
 
Dries committed
161 162
    /*
    ** If a user is about to update a book page, we overload some
Dries's avatar
 
Dries committed
163
    ** fields to reflect the changes.
Dries's avatar
 
Dries committed
164 165
    */

Dries's avatar
 
Dries committed
166 167 168 169 170 171
    if ($user->uid) {
      $book->uid = $user->uid;
      $book->name = $user->name;
    }
    else {
      $book->uid = 0;
172
      $book->name = '';
Dries's avatar
 
Dries committed
173
    }
Dries's avatar
 
Dries committed
174
  }
Dries's avatar
 
Dries committed
175

Dries's avatar
 
Dries committed
176 177 178
  /*
  ** We set the revision field to indicate that we have to create
  ** a new revision when updating this book page.  We enable this
179
  ** always such that the "edit this page" links appear.
Dries's avatar
 
Dries committed
180
  */
Dries's avatar
 
Dries committed
181

Dries's avatar
 
Dries committed
182
  $book->revision = 1;
Dries's avatar
 
Dries committed
183

Dries's avatar
 
Dries committed
184
  return $book;
Dries's avatar
 
Dries committed
185 186
}

187 188 189
/**
 * Implementation of hook_insert().
 */
Dries's avatar
 
Dries committed
190
function book_insert($node) {
Dries's avatar
 
Dries committed
191
  db_query("INSERT INTO {book} (nid, format, parent, weight, log) VALUES (%d, %d, %d, %d, '%s')", $node->nid, $node->format, $node->parent, $node->weight, $node->log);
Dries's avatar
 
Dries committed
192
}
Dries's avatar
 
Dries committed
193

194 195 196
/**
 * Implementation of hook_update().
 */
Dries's avatar
 
Dries committed
197
function book_update($node) {
Dries's avatar
 
Dries committed
198
  db_query("UPDATE {book} SET format = %d, parent = %d, weight = %d, log = '%s' WHERE nid = %d", $node->format, $node->parent, $node->weight, $node->log, $node->nid);
Dries's avatar
 
Dries committed
199
}
Dries's avatar
 
Dries committed
200

201 202 203
/**
 * Implementation of hook_delete().
 */
204
function book_delete(&$node) {
205
  db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
Dries's avatar
 
Dries committed
206 207
}

208 209 210
/**
 * Implementation of hook_validate().
 */
211
function book_validate(&$node) {
212 213
  if ($node->format && user_access('create php content')) {
    // Do not filter PHP code. Do not auto-extract a teaser.
214 215 216 217 218
    $node->teaser = $node->body;
  }
  else {
    $node->format = 0;
  }
Dries's avatar
 
Dries committed
219

220 221
  // Set default values for non-administrators.
  if (!user_access('administer nodes')) {
222 223 224 225
    $node->format = 0;
    $node->weight = 0;
    $node->revision = 1;
  }
Dries's avatar
 
Dries committed
226 227
}

228 229 230
/**
 * Implementation of hook_form().
 */
Dries's avatar
 
Dries committed
231
function book_form(&$node, &$error) {
Dries's avatar
 
Dries committed
232
  global $user;
Dries's avatar
 
Dries committed
233

234 235
  $op = $_POST['op'];
  $output = form_select(t('Parent'), 'parent', $node->parent, book_toc(), t('The parent subject or category the page belongs in.'));
Dries's avatar
 
Dries committed
236

237 238
  if (function_exists('taxonomy_node_form')) {
    $output .= implode('', taxonomy_node_form('book', $node));
239 240
  }

241 242
  $output .= form_textarea(t('Body'), 'body', $node->body, 60, 20, filter_tips_short());
  $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
243

244 245 246 247
  if (user_access('administer nodes')) {
    $output .= form_weight(t('Weight'), 'weight', $node->weight, 15, t('The heavier pages will sink and the lighter pages will be positioned nearer the top.'));
    if (user_access('create php content')) {
      $output .= form_radios('Type', 'format', $node->format, array(0 => 'HTML / text', 1 => 'PHP'));
Dries's avatar
 
Dries committed
248
    }
Dries's avatar
 
Dries committed
249 250 251 252 253 254 255
  }
  else {
    /*
    ** If a regular user updates a book page, we create a new revision
    ** authored by that user:
    */

256
    $output .= form_hidden('revision', 1);
Dries's avatar
 
Dries committed
257 258 259 260 261
  }

  return $output;
}

262 263 264
/**
 * Implementation of hook_node_link().
 */
Dries's avatar
 
Dries committed
265
function book_node_link($node = 0) {
Dries's avatar
 
Dries committed
266 267
  global $user;

268 269
  $op = $_POST['op'];
  $edit = $_POST['edit'];
Dries's avatar
 
Dries committed
270

271
  if ($node->type != 'book') {
Dries's avatar
 
Dries committed
272

273 274
    if ($edit['nid']) {
      $node = node_load(array('nid' => $edit['nid']));
Dries's avatar
 
Dries committed
275 276
    }

277 278 279
    if ($op == t('Add to book outline')) {
      db_query('INSERT INTO {book} (nid, parent, weight) VALUES (%d, %d, %d)', $node->nid, $edit['parent'], $edit['weight']);
      drupal_set_message(t('added the node to the book.'));
Dries's avatar
 
Dries committed
280 281
    }

282 283 284
    if ($op == t('Update book outline')) {
      db_query('UPDATE {book} SET parent = %d, weight = %d WHERE nid = %d', $edit['parent'], $edit['weight'], $node->nid);
      drupal_set_message(t('updated the book outline.'));
Dries's avatar
 
Dries committed
285 286
    }

287 288 289
    if ($op == t('Remove from book outline')) {
      db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
      drupal_set_message(t('removed the node form the book.'));
Dries's avatar
 
Dries committed
290 291
    }

292
    $output .= '<h3>'. t('Edit book outline for node "%booktitle"', array('%booktitle' => "<em>$node->title</em>")) .'</h3>';
Dries's avatar
 
Dries committed
293

294 295
    if ($edit['nid']) {
      $page = db_fetch_object(db_query('SELECT * FROM {book} WHERE nid = %d', $node->nid));
Dries's avatar
 
Dries committed
296

297 298
      $output .= form_select(t('Parent'), 'parent', $page->parent, book_toc(), t('The parent subject or category the page belongs in.'));
      $output .= form_weight(t('Weight'), 'weight', $node->weight, 15, t('The heavier pages will sink and the lighter pages will be positioned nearer the top.'));
Dries's avatar
 
Dries committed
299 300

      if ($page->nid) {
301 302
        $output .= form_submit(t('Update book outline'));
        $output .= form_submit(t('Remove from book outline'));
Dries's avatar
 
Dries committed
303 304
      }
      else {
305
        $output .= form_submit(t('Add to book outline'));
Dries's avatar
 
Dries committed
306 307 308 309
      }

    }
    else {
310
      $output .= form_submit(t('Edit book outline'));
Dries's avatar
 
Dries committed
311 312
    }

313
    $output .= form_hidden('nid', $node->nid);
Dries's avatar
 
Dries committed
314

315
    return form($output, 'post', url('admin/node/book'));
Dries's avatar
 
Dries committed
316 317 318
  }
}

319 320 321
/**
 * Return the the most recent revision that matches the specified conditions.
 */
Dries's avatar
 
Dries committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
function book_revision_load($page, $conditions = array()) {

  $revisions = array_reverse(node_revision_list($page));

  foreach ($revisions as $revision) {

    /*
    ** Extract the specified revision:
    */

    $node = node_revision_load($page, $revision);

    /*
    ** Check to see if the conditions are met:
    */

    $status = 1;

    foreach ($conditions as $key => $value) {
      if ($node->$key != $value) {
        $status = 0;
      }
    }

    if ($status) {
      return $node;
    }
  }
}

352 353 354
/**
 * Return the path (call stack) to a certain book page.
 */
Dries's avatar
 
Dries committed
355
function book_location($node, $nodes = array()) {
Dries's avatar
 
Dries committed
356 357
  // TODO: eliminate the recursion

358
  $parent = db_fetch_object(db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d', $node->parent));
Dries's avatar
 
Dries committed
359 360 361 362 363 364 365
  if ($parent->title) {
    $nodes = book_location($parent, $nodes);
    array_push($nodes, $parent);
  }
  return $nodes;
}

Dries's avatar
 
Dries committed
366
function book_location_down($node, $nodes = array()) {
Dries's avatar
 
Dries committed
367 368
  // TODO: eliminate the recursion

369
  $last_direct_child = db_fetch_object(db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d ORDER BY b.weight DESC, n.title DESC', $node->nid));
Dries's avatar
 
Dries committed
370 371 372 373 374 375 376
  if ($last_direct_child) {
    array_push($nodes, $last_direct_child);
    $nodes = book_location_down($last_direct_child, $nodes);
  }
  return $nodes;
}

377 378 379
/**
 * Fetch the node object of the previous page of the book.
 */
Dries's avatar
 
Dries committed
380
function book_prev($node) {
Dries's avatar
 
Dries committed
381 382 383 384 385
  // if the parent is zero, we are at the start of a book so there is no previous
  if ($node->parent == 0) {
    return NULL;
  }

Dries's avatar
 
Dries committed
386
  // previous on the same level
Dries's avatar
 
Dries committed
387
  $direct_above = db_fetch_object(db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d AND n.status = 1 AND (n.moderate = 0 OR n.revisions != '') AND (b.weight < %d OR (b.weight = %d AND n.title < '%s')) ORDER BY b.weight DESC, n.title DESC", $node->parent, $node->weight, $node->weight, $node->title));
Dries's avatar
 
Dries committed
388 389 390
  if ($direct_above) {
    // get last leaf of $above
    $path = book_location_down($direct_above);
Dries's avatar
 
Dries committed
391 392

    return $path ? (count($path) > 0 ? array_pop($path) : NULL) : $direct_above;
Dries's avatar
 
Dries committed
393 394 395
  }
  else {
    // direct parent
Dries's avatar
 
Dries committed
396
    $prev = db_fetch_object(db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d AND n.status = 1 AND (n.moderate = 0 OR n.revisions != '')", $node->parent));
Dries's avatar
 
Dries committed
397 398 399 400
    return $prev;
  }
}

401 402 403
/**
 * Fetch the node object of the next page of the book.
 */
Dries's avatar
 
Dries committed
404 405
function book_next($node) {
  // get first direct child
Dries's avatar
 
Dries committed
406
  $child = db_fetch_object(db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d AND n.status = 1 AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight ASC, n.title ASC", $node->nid));
Dries's avatar
 
Dries committed
407 408 409 410
  if ($child) {
    return $child;
  }

Dries's avatar
 
Dries committed
411
  // no direct child: get next for this level or any parent in this book
412 413
  array_push($path = book_location($node), $node); // path to top-level node including this one

Dries's avatar
 
Dries committed
414
  while (($leaf = array_pop($path)) && count($path)) {
Dries's avatar
 
Dries committed
415
    $next = db_fetch_object(db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d AND n.status = 1 AND (n.moderate = 0 OR n.revisions != '') AND (b.weight > %d OR (b.weight = %d AND n.title > '%s')) ORDER BY b.weight ASC, n.title ASC", $leaf->parent, $leaf->weight, $leaf->weight, $leaf->title));
Dries's avatar
 
Dries committed
416 417 418 419 420 421
    if ($next) {
      return $next;
    }
  }
}

422 423 424
/**
 * Implementation of hook_content().
 */
Dries's avatar
 
Dries committed
425
function book_content($node, $teaser = FALSE) {
426
  $op = $_POST['op'];
Dries's avatar
 
Dries committed
427

Dries's avatar
 
Dries committed
428 429
  /*
  ** Always display the most recently approved revision of a node
Dries's avatar
 
Dries committed
430 431
  ** (if any) unless we have to display this page in the context of
  ** the moderation queue.
Dries's avatar
 
Dries committed
432 433
  */

434 435
  if ($op != t('Preview') && $node->moderate && arg(0) != 'queue') {
    $revision = book_revision_load($node, array('moderate' => 0, 'status' => 1));
Dries's avatar
 
Dries committed
436 437 438 439

    if ($revision) {
      $node = $revision;
    }
Dries's avatar
 
Dries committed
440 441
  }

Dries's avatar
 
Dries committed
442 443 444 445 446
  /*
  ** Extract the page body.  If body is dynamic (using PHP code), the body
  ** will be generated.
  */

Dries's avatar
 
Dries committed
447 448
  if ($node->format == 1) {
    // Make sure only authorized users can preview PHP pages.
449
    if ($op == t('Preview') && !user_access('create php content')) {
Dries's avatar
 
Dries committed
450 451 452 453 454 455 456 457 458
      return;
    }

    ob_start();
    eval($node->body);
    $node->teaser = $node->body = ob_get_contents();
    ob_end_clean();
  }
  else {
Dries's avatar
 
Dries committed
459
    $node = node_prepare($node, $teaser);
Dries's avatar
 
Dries committed
460
  }
Dries's avatar
 
Dries committed
461

Dries's avatar
 
Dries committed
462 463 464
  return $node;
}

465 466 467 468 469 470
/**
 * Implementation of hook_view().
 *
 * If not displayed on the main page, we render the node as a page in the
 * book with extra links to the previous and next pages.
 */
Dries's avatar
 
Dries committed
471 472
function book_view($node, $teaser = FALSE, $page = FALSE) {
  $node = book_content($node, $teaser);
Dries's avatar
 
Dries committed
473

474
  $output = '';
Dries's avatar
 
Dries committed
475

Dries's avatar
 
Dries committed
476 477
  if ($teaser) {
    $output .= theme('node', $node, $teaser, $page);
Dries's avatar
 
Dries committed
478
  }
Dries's avatar
 
Dries committed
479
  else {
Dries's avatar
 
Dries committed
480
    if ($node->moderate) {
481
      $node->body = $node->body . '<div class="log"><div class="title">'. t('Log') .":</div>$node->log</div>";
Dries's avatar
 
Dries committed
482
    }
Dries's avatar
 
Dries committed
483
    // Add the navigation and the breadcrumb if we view a page
Dries's avatar
 
Dries committed
484
    if ($page) {
Dries's avatar
 
Dries committed
485
      $node = book_navigation($node);
Dries's avatar
 
Dries committed
486
      // Print the breadcrumb
Dries's avatar
 
Dries committed
487
      menu_set_location($node->breadcrumb);
Dries's avatar
 
Dries committed
488
    }
Dries's avatar
 
Dries committed
489
    // Print the node
Dries's avatar
 
Dries committed
490
    $output .= theme('node', $node, $teaser, $page);
Dries's avatar
 
Dries committed
491
  }
Dries's avatar
 
Dries committed
492 493

  return $output;
Dries's avatar
 
Dries committed
494 495
}

496 497 498
/**
 * Present a view of a node as a book page.
 */
Dries's avatar
 
Dries committed
499
function book_show($node, $cid) {
500
  $output = '';
Dries's avatar
 
Dries committed
501

502
  if (node_access('view', $node)) {
Dries's avatar
 
Dries committed
503

504
    if ($node->type == 'book') {
Dries's avatar
 
Dries committed
505
      $output .= book_view($node, 0, 1);
Dries's avatar
 
Dries committed
506 507
    }
    else {
Dries's avatar
 
Dries committed
508

509 510
      if (node_hook($node, 'content')) {
        $node = node_invoke($node, 'content');
Dries's avatar
 
Dries committed
511

Dries's avatar
 
Dries committed
512 513 514
        /*
        ** Add the book navigation if the node is in the book.
        */
Dries's avatar
 
Dries committed
515

516
        $book = db_fetch_object(db_query('SELECT * FROM {book} WHERE nid = %d', $node->nid));
Dries's avatar
 
Dries committed
517

Dries's avatar
 
Dries committed
518 519 520 521 522 523
        if ($book) {
          foreach ($book as $key => $value) {
            $node->$key = $value;
          }
          $node = book_navigation($node);
        }
Dries's avatar
 
Dries committed
524

Dries's avatar
 
Dries committed
525 526 527
        /*
        ** make $node->type a book. This is for the links.
        */
Dries's avatar
 
Dries committed
528

529
        $node->type = 'book';
Dries's avatar
 
Dries committed
530

Dries's avatar
 
Dries committed
531 532 533
        /*
        ** View the node
        */
Dries's avatar
 
Dries committed
534
        menu_set_location($node->breadcrumb);
535
        $output .= theme('node', $node, 0, 1);
Dries's avatar
 
Dries committed
536 537 538 539 540
      }
      else {

        /*
        ** We can't get the content of the node and just view the node.
541
        ** We don't add breadcrumbs or links.
Dries's avatar
 
Dries committed
542
        */
Dries's avatar
 
Dries committed
543
        $output .= node_view($node, 0, 1);
Dries's avatar
 
Dries committed
544 545
      }
    }
546
    if (function_exists('comment_render') && $node->comment) {
Dries's avatar
 
Dries committed
547
      $output .= comment_render($node, $cid);
Dries's avatar
 
Dries committed
548 549
    }

Dries's avatar
 
Dries committed
550
    /*
Dries's avatar
 
Dries committed
551
    ** Update the history table, stating that this user viewed this node.
Dries's avatar
 
Dries committed
552
    */
Dries's avatar
 
Dries committed
553 554
    node_tag_new($node->nid);
  }
Dries's avatar
 
Dries committed
555 556 557 558
  else {
    $output .= message_access();
  }
  return $output;
Dries's avatar
 
Dries committed
559
}
Dries's avatar
 
Dries committed
560

561 562 563 564
/**
 * Prepares both the custom breadcrumb trail and the forward/backward
 * navigation for a node presented as a book page.
 */
Dries's avatar
 
Dries committed
565
function book_navigation($node) {
Dries's avatar
 
Dries committed
566
  $path = book_location($node);
Dries's avatar
 
Dries committed
567

Dries's avatar
 
Dries committed
568 569 570 571
  /*
  ** Construct the breadcrumb:
  */

Dries's avatar
 
Dries committed
572
  $node->breadcrumb = array(); // Overwrite the trail with a book trail.
Dries's avatar
 
Dries committed
573
  foreach ($path as $level) {
Dries's avatar
 
Dries committed
574
    $node->breadcrumb[] = array('path' => 'book/view/'. $level->nid, 'title' =>  $level->title);
Dries's avatar
 
Dries committed
575
  }
Dries's avatar
 
Dries committed
576
  $node->breadcrumb[] = array('path' => 'book/view/'. $node->nid);
Dries's avatar
 
Dries committed
577

Dries's avatar
 
Dries committed
578
  if ($node->nid) {
579
    $output .= '<div class="book">';
Dries's avatar
 
Dries committed
580 581

    if ($tree = book_tree($node->nid)) {
582
      $output .= '<div class="tree">'. book_tree($node->nid) .'</div>';
Dries's avatar
 
Dries committed
583
    }
Dries's avatar
 
Dries committed
584

Dries's avatar
 
Dries committed
585
    if ($prev = book_prev($node)) {
586 587 588
      $links .= '<div class="prev">';
      $links .= l(t('previous'), "book/view/$prev->nid", array('title' => t('View the previous page.')));
      $links .= '</div>';
Dries's avatar
 
Dries committed
589 590 591
      $titles .= "<div class=\"prev\">$prev->title</div>";
    }
    else {
592
      $links .= '<div class="prev">&nbsp;</div>'; // make an empty div to fill the space
Dries's avatar
 
Dries committed
593 594
    }
    if ($next = book_next($node)) {
595 596 597
      $links .= '<div class="next">';
      $links .= l(t('next'), "book/view/$next->nid", array('title' => t('View the next page.')));
      $links .= '</div>';
Dries's avatar
 
Dries committed
598 599 600
      $titles .= "<div class=\"next\">$next->title</div>";
    }
    else {
601
      $links .= '<div class="next">&nbsp;</div>'; // make an empty div to fill the space
Dries's avatar
 
Dries committed
602 603
    }
    if ($node->parent) {
604 605 606
      $links .= '<div class="up">';
      $links .= l(t('up'), "book/view/$node->parent", array('title' => t("View this page's parent section.")));
      $links .= '</div>';
Dries's avatar
 
Dries committed
607
    }
Dries's avatar
 
Dries committed
608

609
    $output .= '<div class="nav">';
Dries's avatar
 
Dries committed
610 611
    $output .= " <div class=\"links\">$links</div>";
    $output .= " <div class=\"titles\">$titles</div>";
612 613
    $output .= '</div>';
    $output .= '</div>';
Dries's avatar
 
Dries committed
614
  }
Dries's avatar
 
Dries committed
615

Dries's avatar
 
Dries committed
616
  $node->body = $node->body.$output;
Dries's avatar
 
Dries committed
617

Dries's avatar
 
Dries committed
618
  return $node;
Dries's avatar
 
Dries committed
619
}
Dries's avatar
 
Dries committed
620

Dries's avatar
 
Dries committed
621 622 623 624 625 626 627 628 629 630 631 632
function book_toc_recurse($nid, $indent, $toc, $children) {

  if ($children[$nid]) {
    foreach ($children[$nid] as $foo => $node) {
      $toc[$node->nid] = "$indent $node->title";
      $toc = book_toc_recurse($node->nid, "$indent--", $toc, $children);
    }
  }

  return $toc;
}

633 634
function book_toc($parent = 0, $indent = '', $toc = array()) {
  $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 ORDER BY b.weight, n.title');
Dries's avatar
 
Dries committed
635

Dries's avatar
 
Dries committed
636
  while ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
637 638 639 640
    if (!$children[$node->parent]) {
      $children[$node->parent] = array();
    }
    array_push($children[$node->parent], $node);
Dries's avatar
 
Dries committed
641
  }
Dries's avatar
 
Dries committed
642 643

  /*
644 645
  ** If the user is an administrator, add the top-level book page;
  ** only administrators can start new books.
Dries's avatar
 
Dries committed
646
  */
Dries's avatar
 
Dries committed
647

648 649
  if (user_access('administer nodes')) {
    $toc[0] = '<'. t('top-level') .'>';
Dries's avatar
 
Dries committed
650 651
  }

Dries's avatar
 
Dries committed
652
  $toc = book_toc_recurse($parent, $indent, $toc, $children);
Dries's avatar
 
Dries committed
653

Dries's avatar
 
Dries committed
654 655 656
  return $toc;
}

Dries's avatar
 
Dries committed
657
function book_tree_recurse($nid, $depth, $children, $unfold = array()) {
Dries's avatar
 
Dries committed
658
  if ($depth > 0) {
Dries's avatar
 
Dries committed
659 660
    if ($children[$nid]) {
      foreach ($children[$nid] as $foo => $node) {
Dries's avatar
 
Dries committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
        if (in_array($node->nid, $unfold)) {
          if ($tree = book_tree_recurse($node->nid, $depth - 1, $children, $unfold)) {
            $output .= '<li class="expanded">';
            $output .= l($node->title, "book/view/$node->nid");
            $output .= "<ul>$tree</ul>";
            $output .= '</li>';
          }
          else {
            $output .= '<li class="leaf">'. l($node->title, "book/view/$node->nid") .'</li>';
          }
        }
        else {
          if ($tree = book_tree_recurse($node->nid, 1, $children)) {
            $output .= '<li class="collapsed">'. l($node->title, "book/view/$node->nid") .'</li>';
          }
          else {
            $output .= '<li class="leaf">'. l($node->title, "book/view/$node->nid") .'</li>';
          }
Dries's avatar
 
Dries committed
679
        }
Dries's avatar
 
Dries committed
680 681
      }
    }
Dries's avatar
 
Dries committed
682 683 684 685 686
  }

  return $output;
}

Dries's avatar
 
Dries committed
687
function book_tree($parent = 0, $depth = 3, $unfold = array()) {
688
  $result = db_query('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND n.moderate = 0 ORDER BY b.weight, n.title');
Dries's avatar
 
Dries committed
689

Dries's avatar
 
Dries committed
690 691 692 693
  while ($node = db_fetch_object($result)) {
    $list = $children[$node->parent] ? $children[$node->parent] : array();
    array_push($list, $node);
    $children[$node->parent] = $list;
Dries's avatar
 
Dries committed
694
  }
Dries's avatar
 
Dries committed
695

Dries's avatar
 
Dries committed
696 697 698
  if ($tree = book_tree_recurse($parent, $depth, $children, $unfold)) {
    return "<ul>$tree</ul>";
  }
Dries's avatar
 
Dries committed
699 700
}

701
/**
Dries's avatar
Dries committed
702
 * Menu callback; prints a listing of all books.
703
 */
Dries's avatar
 
Dries committed
704
function book_render() {
Dries's avatar
 
Dries committed
705

706
  $result = db_query('SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = 0 AND n.status = 1 AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title');
Dries's avatar
 
Dries committed
707

Dries's avatar
 
Dries committed
708 709
  while ($page = db_fetch_object($result)) {
    // load the node:
710
    $node = node_load(array('nid' => $page->nid));
Dries's avatar
 
Dries committed
711

Dries's avatar
 
Dries committed
712
    if ($node) {
Dries's avatar
 
Dries committed
713 714
      // take the most recent approved revision, extract the page and check output:
      $node = book_content($node);
Dries's avatar
 
Dries committed
715
      // output the content:
716 717
      $output .= '<div class="book">';
      $output .= '<div class="title">'. l($node->title, "book/view/$node->nid") .'</div>';
718
      $output .= '<div class="body">'. $node->teaser .'</div>';
719
      $output .= '</div>';
Dries's avatar
 
Dries committed
720
    }
Dries's avatar
 
Dries committed
721 722
  }

723 724
  drupal_set_title(t('Books'));
  print theme('page', $output);
Dries's avatar
 
Dries committed
725 726
}

727
/**
Dries's avatar
Dries committed
728
 * Menu callback; prints a node as a book page, complete with navigation.
729 730 731 732 733
 */
function book_view_page($nid = 0, $cid = 0) {
  $node = node_load(array('nid' => $nid));
  $output = book_show($node, $cid);
  print theme('page', $output, $node->title);
Dries's avatar
 
Dries committed
734 735
}

736
/**
Dries's avatar
Dries committed
737
 * Menu callback; generates printer-friendly book page will all descendants.
738 739
 */
function book_print($nid = 0, $depth = 1) {
Dries's avatar
 
Dries committed
740
  global $base_url;
741
  $result = db_query('SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND n.nid = %d AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title', $nid);
Dries's avatar
 
Dries committed
742

Dries's avatar
 
Dries committed
743 744
  while ($page = db_fetch_object($result)) {
    // load the node:
745
    $node = node_load(array('nid' => $page->nid));
Dries's avatar
 
Dries committed
746

Dries's avatar
 
Dries committed
747 748
    if ($node) {
      // output the content:
749 750
      if (node_hook($node, 'content')) {
        $node = node_invoke($node, 'content');
Dries's avatar
 
Dries committed
751
      }
Dries's avatar
 
Dries committed
752
      $output .= "<h1 id=\"$node->nid\" name=\"$node->nid\" class=\"book-h$depth\">$node->title</h1>";
Dries's avatar
 
Dries committed
753

Dries's avatar
 
Dries committed
754
      if ($node->body) {
Dries's avatar
 
Dries committed
755
        $output .= $node->body;
Dries's avatar
 
Dries committed
756
      }
Dries's avatar
 
Dries committed
757
    }
Dries's avatar
 
Dries committed
758
  }
Dries's avatar
 
Dries committed
759

760
  $output .= book_print_recurse($nid, $depth);
Dries's avatar
 
Dries committed
761

Dries's avatar
 
Dries committed
762 763 764
  $html = "<html><head><title>$node->title</title>";
  $html .= "<base href=\"$base_url/\" />";
  $html .= "<style type=\"text/css\">\n@import url(misc/print.css);\n</style>";
765
  $html .= '</head><body>'. $output .'</body></html>';
Dries's avatar
 
Dries committed
766

767
  print $html;
Dries's avatar
 
Dries committed
768 769
}

770
function book_print_recurse($parent = '', $depth = 1) {
Dries's avatar
 
Dries committed
771
  $result = db_query("SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions IS NOT NULL) ORDER BY b.weight, n.title");
Dries's avatar
 
Dries committed
772

Dries's avatar
 
Dries committed
773 774
  while ($page = db_fetch_object($result)) {
    // load the node:
775
    $node = node_load(array('nid' => $page->nid));
Dries's avatar
 
Dries committed
776 777 778

    // take the most recent approved revision:
    if ($node->moderate) {
779
      $node = book_revision_load($node, array('moderate' => 0, 'status' => 1));
Dries's avatar
 
Dries committed
780 781
    }

Dries's avatar
 
Dries committed
782 783
    if ($node) {
      // output the content:
784 785
      if (node_hook($node, 'content')) {
        $node = node_invoke($node, 'content');
Dries's avatar
 
Dries committed
786
      }
Dries's avatar
 
Dries committed
787
      $output .= "<h1 id=\"$node->nid\" name=\"$node->nid\" class=\"book-h$depth\">$node->title</h1>";
Dries's avatar
 
Dries committed
788

Dries's avatar
 
Dries committed
789
      if ($node->body) {
790
        $output .= '<ul>'. $node->body .'</ul>';
Dries's avatar
 
Dries committed
791
      }
Dries's avatar
 
Dries committed
792

793
      $output .= book_print_recurse($node->nid, $depth + 1);
Dries's avatar
 
Dries committed
794
    }
Dries's avatar
 
Dries committed
795
  }
Dries's avatar
 
Dries committed
796

Dries's avatar
 
Dries committed
797 798
  return $output;
}
Dries's avatar
 
Dries committed
799

Dries's avatar
 
Dries committed
800
function book_admin_view_line($node, $depth = 0) {
Dries's avatar
 
Dries committed
801
  return array("<div style=\"padding-left: ". (25 * $depth) ."px;\">". form_textfield(NULL, "$node->nid][title", $node->title, 64, 255) ."</div>", form_weight(NULL, "$node->nid][weight", $node->weight), l(t("view node"), "node/$node->nid"), l(t("edit node"), "node/$node->nid/edit"), l(t("delete node"), "admin/node/delete/$node->nid"));
Dries's avatar
 
Dries committed
802 803 804
}

function book_admin_view_book($nid, $depth = 1) {
Dries's avatar
 
Dries committed
805
  $result = db_query("SELECT n.nid FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.parent = %d ORDER BY b.weight, n.title", $nid);
Dries's avatar
 
Dries committed
806 807 808

  while ($node = db_fetch_object($result)) {
    $node = node_load(array("nid" => $node->nid));
Dries's avatar
 
Dries committed
809 810
    $rows[] = book_admin_view_line($node, $depth);
    $rows = array_merge($rows, book_admin_view_book($node->nid, $depth + 1));
Dries's avatar
 
Dries committed
811 812
  }

Dries's avatar
 
Dries committed
813
  return $rows;
Dries's avatar
 
Dries committed
814 815
}

816 817 818
/**
 * Display an administrative view of the hierarchy of a book.
 */
Dries's avatar
 
Dries committed
819
function book_admin_view($nid, $depth = 0) {
Dries's avatar
 
Dries committed
820 821
  if ($nid) {
    $node = node_load(array("nid" => $nid));
Dries's avatar
 
Dries committed
822

Dries's avatar
 
Dries committed
823
    $output .= "<h3>$node->title</h3>";
Dries's avatar
 
Dries committed
824

Dries's avatar
 
Dries committed
825 826 827
    $header = array(t("title"), t("weight"), array("data" => t("operations"), "colspan" => 3));
    $rows[] = book_admin_view_line($node);
    $rows = array_merge($rows, book_admin_view_book($nid));
Dries's avatar
 
Dries committed
828

Dries's avatar
 
Dries committed
829
    $output .= theme("table", $header, $rows);
Dries's avatar
 
Dries committed
830
    $output .= form_submit(t("Save book pages"));
Dries's avatar
 
Dries committed
831

Dries's avatar
 
Dries committed
832 833
    return form($output);
  }
Dries's avatar
 
Dries committed
834 835 836
}

function book_admin_save($nid, $edit = array()) {
Dries's avatar
 
Dries committed
837 838
  if ($nid) {
    $book = node_load(array("nid" => $nid));
Dries's avatar
 
Dries committed
839

Dries's avatar
 
Dries committed
840 841 842 843
    foreach ($edit as $nid => $value) {
      /*
      ** Check to see whether the title needs updating:
      */
Dries's avatar
 
Dries committed
844

Dries's avatar
 
Dries committed
845
      $title = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $nid));
Dries's avatar
 
Dries committed
846
      if ($title != $value["title"]) {
Dries's avatar
 
Dries committed
847
        db_query("UPDATE {node} SET title = '%s' WHERE nid = %d", $value["title"], $nid);
Dries's avatar
 
Dries committed
848
      }
Dries's avatar
 
Dries committed
849

Dries's avatar
 
Dries committed
850 851 852
      /*
      ** Check to see whether the weight needs updating:
      */
Dries's avatar
 
Dries committed
853

Dries's avatar
 
Dries committed
854
      $weight = db_result(db_query("SELECT weight FROM {book} WHERE nid = %d", $nid));
Dries's avatar
 
Dries committed
855
      if ($weight != $value["weight"]) {
Dries's avatar
 
Dries committed
856
        db_query("UPDATE {book} SET weight = %d WHERE nid = %d", $value["weight"], $nid);
Dries's avatar
 
Dries committed
857
      }
Dries's avatar
 
Dries committed
858 859
    }

860 861
    $message = t('updated book "%title"', array('%title' => $book->title));
    watchdog('special', $message);
Dries's avatar
 
Dries committed
862

Dries's avatar
 
Dries committed
863 864
    return $message;
  }
Dries's avatar
 
Dries committed
865 866
}

867
/**
Dries's avatar
Dries committed
868
 * Menu callback; displays a listing of all orphaned book pages.
869
 */
Dries's avatar
 
Dries committed
870
function book_admin_orphan() {
Dries's avatar
 
Dries committed
871
  $result = db_query("SELECT n.nid, n.title, n.status, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid");
Dries's avatar
 
Dries committed
872 873 874 875 876

  while ($page = db_fetch_object($result)) {
    $pages[$page->nid] = $page;
  }

Dries's avatar
 
Dries committed
877
  if ($pages) {
Dries's avatar
 
Dries committed
878 879
    $output .= "<h3>Orphan pages</h3>";
    $header = array(t("title"), t("weight"), array("data" => t("operations"), "colspan" => 3));
Dries's avatar
 
Dries committed
880 881
    foreach ($pages as $nid => $node) {
      if ($node->parent && empty($pages[$node->parent])) {
Dries's avatar
 
Dries committed
882 883
        $rows[] = book_admin_view_line($node, $depth);
        $rows = array_merge($rows, book_admin_view_book($node->nid, $depth + 1));
Dries's avatar
 
Dries committed
884
      }
Dries's avatar
 
Dries committed
885
    }
Dries's avatar
 
Dries committed
886
    $output .= theme("table", $header, $rows);
Dries's avatar
 
Dries committed
887 888
  }

Dries's avatar
 
Dries committed
889
  print theme("page", $output);
Dries's avatar
 
Dries committed
890 891
}

Dries's avatar