book.module 33.5 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) {
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
  }

Dries's avatar
 
Dries committed
241
  $output .= form_textarea(t('Body'), 'body', $node->body, 60, 20, filter_tips_short(), NULL, TRUE);
242
  $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
  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')) {
247
      $output .= form_radios(t('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
      return;
    }

    ob_start();
    eval($node->body);
Dries's avatar
 
Dries committed
455
    $node->body = ob_get_contents();
Dries's avatar
 
Dries committed
456
    ob_end_clean();
Dries's avatar
 
Dries committed
457 458
    $node->teaser = node_teaser($node->body);
    $node->readmore = (strlen($node->teaser) < strlen($node->body));
Dries's avatar
 
Dries committed
459 460
  }
  else {
Dries's avatar
 
Dries committed
461
    $node = node_prepare($node, $teaser);
Dries's avatar
 
Dries committed
462
  }
Dries's avatar
 
Dries committed
463

Dries's avatar
 
Dries committed
464 465 466
  return $node;
}

467 468 469 470 471 472
/**
 * 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
473 474
function book_view($node, $teaser = FALSE, $page = FALSE) {
  $node = book_content($node, $teaser);
Dries's avatar
 
Dries committed
475

476
  $output = '';
Dries's avatar
 
Dries committed
477

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

  return $output;
Dries's avatar
 
Dries committed
496 497
}

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

504
  if (node_access('view', $node)) {
Dries's avatar
 
Dries committed
505

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

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

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

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

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

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

531
        $node->type = 'book';
Dries's avatar
 
Dries committed
532

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

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

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

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

Dries's avatar
 
Dries committed
570 571 572 573
  /*
  ** Construct the breadcrumb:
  */

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

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

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

Dries's avatar
 
Dries committed
587
    if ($prev = book_prev($node)) {
588 589 590
      $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
591 592 593
      $titles .= "<div class=\"prev\">$prev->title</div>";
    }
    else {
594
      $links .= '<div class="prev">&nbsp;</div>'; // make an empty div to fill the space
Dries's avatar
 
Dries committed
595 596
    }
    if ($next = book_next($node)) {
597 598 599
      $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
600 601 602
      $titles .= "<div class=\"next\">$next->title</div>";
    }
    else {
603
      $links .= '<div class="next">&nbsp;</div>'; // make an empty div to fill the space
Dries's avatar
 
Dries committed
604 605
    }
    if ($node->parent) {
606 607 608
      $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
609
    }
Dries's avatar
 
Dries committed
610

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

Dries's avatar
 
Dries committed
618
  $node->body = $node->body.$output;
Dries's avatar
 
Dries committed
619

Dries's avatar
 
Dries committed
620
  return $node;
Dries's avatar
 
Dries committed
621
}
Dries's avatar
 
Dries committed
622

Dries's avatar
 
Dries committed
623 624 625 626 627 628 629 630 631 632 633 634
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;
}

635 636
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
637

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

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

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

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

Dries's avatar
 
Dries committed
656 657 658
  return $toc;
}

Dries's avatar
 
Dries committed
659
function book_tree_recurse($nid, $depth, $children, $unfold = array()) {
Dries's avatar
 
Dries committed
660
  if ($depth > 0) {
Dries's avatar
 
Dries committed
661 662
    if ($children[$nid]) {
      foreach ($children[$nid] as $foo => $node) {
Dries's avatar
 
Dries committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
        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
681
        }
Dries's avatar
 
Dries committed
682 683
      }
    }
Dries's avatar
 
Dries committed
684 685 686 687 688
  }

  return $output;
}

Dries's avatar
 
Dries committed
689
function book_tree($parent = 0, $depth = 3, $unfold = array()) {
690
  $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
691

Dries's avatar
 
Dries committed
692 693 694 695
  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
696
  }
Dries's avatar
 
Dries committed
697

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

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

708
  $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
709

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

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

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

729
/**
Dries's avatar
Dries committed
730
 * Menu callback; prints a node as a book page, complete with navigation.
731 732 733 734 735
 */
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
736 737
}

738
/**
Dries's avatar
Dries committed
739
 * Menu callback; generates printer-friendly book page will all descendants.
740 741
 */
function book_print($nid = 0, $depth = 1) {
Dries's avatar
 
Dries committed
742
  global $base_url;
743
  $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
744

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

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

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

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

Dries's avatar
 
Dries committed
764 765 766
  $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>";
767
  $html .= '</head><body>'. $output .'</body></html>';
Dries's avatar
 
Dries committed
768

769
  print $html;
Dries's avatar
 
Dries committed
770 771
}

772
function book_print_recurse($parent = '', $depth = 1) {
Dries's avatar
 
Dries committed
773
  $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
774

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

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

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

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

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

Dries's avatar
 
Dries committed
799 800
  return $output;
}
Dries's avatar
 
Dries committed
801

Dries's avatar
 
Dries committed
802
function book_admin_view_line($node, $depth = 0) {
Dries's avatar
 
Dries committed
803
  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
804 805 806
}

function book_admin_view_book($nid, $depth = 1) {
Dries's avatar
 
Dries committed
807
  $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
808 809 810

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

Dries's avatar
 
Dries committed
815
  return $rows;
Dries's avatar
 
Dries committed
816 817
}

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

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

Dries's avatar
 
Dries committed
827 828 829
    $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
830

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

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

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

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

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

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

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

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

Dries's avatar
 
Dries committed
865 866
    return $message;
  }
Dries's avatar
 
Dries committed
867 868
}

869
/**
Dries's avatar
Dries committed
870
 * Menu callback; displays a listing of all orphaned book pages.
871
 */
Dries's avatar
 
Dries committed
872
function book_admin_orphan() {
Dries's avatar
 
Dries committed
873
  $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
874 875 876 877 878

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

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