book.module 33.1 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 72
  if ($type == 'node' && $node->type == 'book') {
    if (book_access('update', $node)) {
      $links[] = l(t('edit this page'), "node/edit/$node->nid", 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
  if ($type == 'system') {
80
    menu('node/add/book', t('book page'), user_access('maintain books') ? MENU_FALLTHROUGH : MENU_DENIED, 0);
Dries's avatar
 
Dries committed
81

Dries's avatar
 
Dries committed
82 83 84 85
    menu('admin/node/book', t('books'), user_access('administer nodes') ? 'book_admin' : MENU_DENIED, 4);
    menu('admin/node/book/orphan', t('orphan pages'), user_access('administer nodes') ? 'book_admin_orphan' : MENU_DENIED, 8);
    menu('admin/node/book/help', t('help'), user_access('administer nodes') ? 'book_help_page' : MENU_DENIED, 9);

86
    $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');
Dries's avatar
 
Dries committed
87
    while ($book = db_fetch_object($result)) {
88
      menu("admin/node/book/$book->nid", t('"%title" book', array('%title' => $book->title)), MENU_FALLTHROUGH);
Dries's avatar
 
Dries committed
89
    }
90 91 92
    menu('book', t('books'), user_access('access content') ? 'book_render' : MENU_DENIED, 0, MENU_HIDE);
    menu('book/view', t('view'), user_access('access content') ? 'book_view_page' : MENU_DENIED, 0, MENU_HIDE, MENU_LOCKED);
    menu('book/print', t('printer-friendly version'), user_access('access content') ? 'book_print' : MENU_DENIED, 0, MENU_HIDE, MENU_LOCKED);
Dries's avatar
 
Dries committed
93 94
  }

Dries's avatar
 
Dries committed
95
  return $links;
Dries's avatar
 
Dries committed
96 97
}

98 99 100 101 102 103
/**
 * 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
104 105 106 107
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)) {
108
    $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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

    $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;
}

132 133 134
/**
 * Implementation of hook_load().
 */
Dries's avatar
 
Dries committed
135
function book_load($node) {
Dries's avatar
 
Dries committed
136
  global $user;
Dries's avatar
 
Dries committed
137

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

140
  if (arg(1) == 'edit' && !user_access('administer nodes')) {
Dries's avatar
 
Dries committed
141 142
    /*
    ** If a user is about to update a book page, we overload some
Dries's avatar
 
Dries committed
143
    ** fields to reflect the changes.
Dries's avatar
 
Dries committed
144 145
    */

Dries's avatar
 
Dries committed
146 147 148 149 150 151
    if ($user->uid) {
      $book->uid = $user->uid;
      $book->name = $user->name;
    }
    else {
      $book->uid = 0;
152
      $book->name = '';
Dries's avatar
 
Dries committed
153
    }
Dries's avatar
 
Dries committed
154
  }
Dries's avatar
 
Dries committed
155

Dries's avatar
 
Dries committed
156 157 158
  /*
  ** We set the revision field to indicate that we have to create
  ** a new revision when updating this book page.  We enable this
159
  ** always such that the "edit this page" links appear.
Dries's avatar
 
Dries committed
160
  */
Dries's avatar
 
Dries committed
161

Dries's avatar
 
Dries committed
162
  $book->revision = 1;
Dries's avatar
 
Dries committed
163

Dries's avatar
 
Dries committed
164
  return $book;
Dries's avatar
 
Dries committed
165 166
}

167 168 169
/**
 * Implementation of hook_insert().
 */
Dries's avatar
 
Dries committed
170
function book_insert($node) {
Dries's avatar
 
Dries committed
171
  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
172
}
Dries's avatar
 
Dries committed
173

174 175 176
/**
 * Implementation of hook_update().
 */
Dries's avatar
 
Dries committed
177
function book_update($node) {
Dries's avatar
 
Dries committed
178
  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
179
}
Dries's avatar
 
Dries committed
180

181 182 183
/**
 * Implementation of hook_delete().
 */
184
function book_delete(&$node) {
185
  db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
Dries's avatar
 
Dries committed
186 187
}

188 189 190
/**
 * Implementation of hook_validate().
 */
191
function book_validate(&$node) {
192 193
  if ($node->format && user_access('create php content')) {
    // Do not filter PHP code. Do not auto-extract a teaser.
194 195 196 197 198
    $node->teaser = $node->body;
  }
  else {
    $node->format = 0;
  }
Dries's avatar
 
Dries committed
199

200 201
  // Set default values for non-administrators.
  if (!user_access('administer nodes')) {
202 203 204 205
    $node->format = 0;
    $node->weight = 0;
    $node->revision = 1;
  }
Dries's avatar
 
Dries committed
206 207
}

208 209 210
/**
 * Implementation of hook_form().
 */
Dries's avatar
 
Dries committed
211
function book_form(&$node, &$error) {
Dries's avatar
 
Dries committed
212
  global $user;
Dries's avatar
 
Dries committed
213

214 215
  $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
216

217 218
  if (function_exists('taxonomy_node_form')) {
    $output .= implode('', taxonomy_node_form('book', $node));
219 220
  }

221 222
  $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
223

224 225 226 227
  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
228
    }
Dries's avatar
 
Dries committed
229 230 231 232 233 234 235
  }
  else {
    /*
    ** If a regular user updates a book page, we create a new revision
    ** authored by that user:
    */

236
    $output .= form_hidden('revision', 1);
Dries's avatar
 
Dries committed
237 238 239 240 241
  }

  return $output;
}

242 243 244
/**
 * Implementation of hook_node_link().
 */
Dries's avatar
 
Dries committed
245
function book_node_link($node = 0) {
Dries's avatar
 
Dries committed
246 247
  global $user;

248 249
  $op = $_POST['op'];
  $edit = $_POST['edit'];
Dries's avatar
 
Dries committed
250

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

253 254
    if ($edit['nid']) {
      $node = node_load(array('nid' => $edit['nid']));
Dries's avatar
 
Dries committed
255 256
    }

257 258 259
    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
260 261
    }

262 263 264
    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
265 266
    }

267 268 269
    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
270 271
    }

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

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

277 278
      $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
279 280

      if ($page->nid) {
281 282
        $output .= form_submit(t('Update book outline'));
        $output .= form_submit(t('Remove from book outline'));
Dries's avatar
 
Dries committed
283 284
      }
      else {
285
        $output .= form_submit(t('Add to book outline'));
Dries's avatar
 
Dries committed
286 287 288 289
      }

    }
    else {
290
      $output .= form_submit(t('Edit book outline'));
Dries's avatar
 
Dries committed
291 292
    }

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

295
    return form($output, 'post', url('admin/node/book'));
Dries's avatar
 
Dries committed
296 297 298
  }
}

299 300 301
/**
 * Return the the most recent revision that matches the specified conditions.
 */
Dries's avatar
 
Dries committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
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;
    }
  }
}

332 333 334
/**
 * Return the path (call stack) to a certain book page.
 */
Dries's avatar
 
Dries committed
335
function book_location($node, $nodes = array()) {
Dries's avatar
 
Dries committed
336 337
  // TODO: eliminate the recursion

338
  $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
339 340 341 342 343 344 345
  if ($parent->title) {
    $nodes = book_location($parent, $nodes);
    array_push($nodes, $parent);
  }
  return $nodes;
}

Dries's avatar
 
Dries committed
346
function book_location_down($node, $nodes = array()) {
Dries's avatar
 
Dries committed
347 348
  // TODO: eliminate the recursion

349
  $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
350 351 352 353 354 355 356
  if ($last_direct_child) {
    array_push($nodes, $last_direct_child);
    $nodes = book_location_down($last_direct_child, $nodes);
  }
  return $nodes;
}

357 358 359
/**
 * Fetch the node object of the previous page of the book.
 */
Dries's avatar
 
Dries committed
360
function book_prev($node) {
Dries's avatar
 
Dries committed
361 362 363 364 365
  // 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
366
  // previous on the same level
Dries's avatar
 
Dries committed
367
  $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
368 369 370
  if ($direct_above) {
    // get last leaf of $above
    $path = book_location_down($direct_above);
Dries's avatar
 
Dries committed
371 372

    return $path ? (count($path) > 0 ? array_pop($path) : NULL) : $direct_above;
Dries's avatar
 
Dries committed
373 374 375
  }
  else {
    // direct parent
Dries's avatar
 
Dries committed
376
    $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
377 378 379 380
    return $prev;
  }
}

381 382 383
/**
 * Fetch the node object of the next page of the book.
 */
Dries's avatar
 
Dries committed
384 385
function book_next($node) {
  // get first direct child
Dries's avatar
 
Dries committed
386
  $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
387 388 389 390
  if ($child) {
    return $child;
  }

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

Dries's avatar
 
Dries committed
394
  while (($leaf = array_pop($path)) && count($path)) {
Dries's avatar
 
Dries committed
395
    $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
396 397 398 399 400 401
    if ($next) {
      return $next;
    }
  }
}

402 403 404
/**
 * Implementation of hook_content().
 */
Dries's avatar
 
Dries committed
405
function book_content($node, $main = 0) {
406
  $op = $_POST['op'];
Dries's avatar
 
Dries committed
407

Dries's avatar
 
Dries committed
408 409
  /*
  ** Always display the most recently approved revision of a node
Dries's avatar
 
Dries committed
410 411
  ** (if any) unless we have to display this page in the context of
  ** the moderation queue.
Dries's avatar
 
Dries committed
412 413
  */

414 415
  if ($op != t('Preview') && $node->moderate && arg(0) != 'queue') {
    $revision = book_revision_load($node, array('moderate' => 0, 'status' => 1));
Dries's avatar
 
Dries committed
416 417 418 419

    if ($revision) {
      $node = $revision;
    }
Dries's avatar
 
Dries committed
420 421
  }

Dries's avatar
 
Dries committed
422 423 424 425 426
  /*
  ** Extract the page body.  If body is dynamic (using PHP code), the body
  ** will be generated.
  */

Dries's avatar
 
Dries committed
427 428
  if ($node->format == 1) {
    // Make sure only authorized users can preview PHP pages.
429
    if ($op == t('Preview') && !user_access('create php content')) {
Dries's avatar
 
Dries committed
430 431 432 433 434 435 436 437 438
      return;
    }

    ob_start();
    eval($node->body);
    $node->teaser = $node->body = ob_get_contents();
    ob_end_clean();
  }
  else {
Dries's avatar
 
Dries committed
439
    $node = node_prepare($node, $main);
Dries's avatar
 
Dries committed
440
  }
Dries's avatar
 
Dries committed
441

Dries's avatar
 
Dries committed
442 443 444
  return $node;
}

445 446 447 448 449 450
/**
 * 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
451
function book_view($node, $main = 0, $page = 0) {
Dries's avatar
 
Dries committed
452
  $node = book_content($node, $main);
Dries's avatar
 
Dries committed
453

454
  $output = '';
Dries's avatar
 
Dries committed
455

Dries's avatar
 
Dries committed
456
  if ($main) {
457
    $output .= theme('node', $node, $main, $page);
Dries's avatar
 
Dries committed
458
  }
Dries's avatar
 
Dries committed
459
  else {
Dries's avatar
 
Dries committed
460
    if ($node->moderate) {
461
      $node->body = $node->body . '<div class="log"><div class="title">'. t('Log') .":</div>$node->log</div>";
Dries's avatar
 
Dries committed
462
    }
Dries's avatar
 
Dries committed
463
    // Add the navigation and the breadcrumb if we view a page
Dries's avatar
 
Dries committed
464
    if ($page) {
Dries's avatar
 
Dries committed
465
      $node = book_navigation($node);
Dries's avatar
 
Dries committed
466
      // Print the breadcrumb
Dries's avatar
 
Dries committed
467
      drupal_set_breadcrumb($node->breadcrumb);
Dries's avatar
 
Dries committed
468
    }
Dries's avatar
 
Dries committed
469
    // Print the node
470
    $output .= theme('node', $node, $main, $page);
Dries's avatar
 
Dries committed
471
  }
Dries's avatar
 
Dries committed
472 473

  return $output;
Dries's avatar
 
Dries committed
474 475
}

476 477 478
/**
 * Present a view of a node as a book page.
 */
Dries's avatar
 
Dries committed
479
function book_show($node, $cid) {
480
  $output = '';
Dries's avatar
 
Dries committed
481

482
  if (node_access('view', $node)) {
Dries's avatar
 
Dries committed
483

484
    if ($node->type == 'book') {
Dries's avatar
 
Dries committed
485
      $output .= book_view($node, 0, 1);
Dries's avatar
 
Dries committed
486 487
    }
    else {
Dries's avatar
 
Dries committed
488

489 490
      if (node_hook($node, 'content')) {
        $node = node_invoke($node, 'content');
Dries's avatar
 
Dries committed
491

Dries's avatar
 
Dries committed
492 493 494
        /*
        ** Add the book navigation if the node is in the book.
        */
Dries's avatar
 
Dries committed
495

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

Dries's avatar
 
Dries committed
498 499 500 501 502 503
        if ($book) {
          foreach ($book as $key => $value) {
            $node->$key = $value;
          }
          $node = book_navigation($node);
        }
Dries's avatar
 
Dries committed
504

Dries's avatar
 
Dries committed
505 506 507
        /*
        ** make $node->type a book. This is for the links.
        */
Dries's avatar
 
Dries committed
508

509
        $node->type = 'book';
Dries's avatar
 
Dries committed
510

Dries's avatar
 
Dries committed
511 512 513
        /*
        ** View the node
        */
Dries's avatar
 
Dries committed
514
        drupal_set_breadcrumb($node->breadcrumb);
515
        $output .= theme('node', $node, 0, 1);
Dries's avatar
 
Dries committed
516 517 518 519 520
      }
      else {

        /*
        ** We can't get the content of the node and just view the node.
521
        ** We don't add breadcrumbs or links.
Dries's avatar
 
Dries committed
522
        */
Dries's avatar
 
Dries committed
523
        $output .= node_view($node, 0, 1);
Dries's avatar
 
Dries committed
524 525
      }
    }
526
    if (function_exists('comment_render') && $node->comment) {
Dries's avatar
 
Dries committed
527
      $output .= comment_render($node, $cid);
Dries's avatar
 
Dries committed
528 529
    }

Dries's avatar
 
Dries committed
530
    /*
Dries's avatar
 
Dries committed
531
    ** Update the history table, stating that this user viewed this node.
Dries's avatar
 
Dries committed
532
    */
Dries's avatar
 
Dries committed
533 534
    node_tag_new($node->nid);
  }
Dries's avatar
 
Dries committed
535 536 537 538
  else {
    $output .= message_access();
  }
  return $output;
Dries's avatar
 
Dries committed
539
}
Dries's avatar
 
Dries committed
540

541 542 543 544
/**
 * Prepares both the custom breadcrumb trail and the forward/backward
 * navigation for a node presented as a book page.
 */
Dries's avatar
 
Dries committed
545
function book_navigation($node) {
Dries's avatar
 
Dries committed
546
  $path = book_location($node);
Dries's avatar
 
Dries committed
547

Dries's avatar
 
Dries committed
548 549 550 551
  /*
  ** Construct the breadcrumb:
  */

552 553
  $node->breadcrumb = ''; // Overwrite the trail with a book trail.
  $node->breadcrumb[] = l(t('Home'), '');
Dries's avatar
 
Dries committed
554 555 556
  foreach ($path as $level) {
    $node->breadcrumb[] = l($level->title, "book/view/$level->nid");
  }
Dries's avatar
 
Dries committed
557

Dries's avatar
 
Dries committed
558
  if ($node->nid) {
559
    $output .= '<div class="book">';
Dries's avatar
 
Dries committed
560 561

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

Dries's avatar
 
Dries committed
565
    if ($prev = book_prev($node)) {
566 567 568
      $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
569 570 571
      $titles .= "<div class=\"prev\">$prev->title</div>";
    }
    else {
572
      $links .= '<div class="prev">&nbsp;</div>'; // make an empty div to fill the space
Dries's avatar
 
Dries committed
573 574
    }
    if ($next = book_next($node)) {
575 576 577
      $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
578 579 580
      $titles .= "<div class=\"next\">$next->title</div>";
    }
    else {
581
      $links .= '<div class="next">&nbsp;</div>'; // make an empty div to fill the space
Dries's avatar
 
Dries committed
582 583
    }
    if ($node->parent) {
584 585 586
      $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
587
    }
Dries's avatar
 
Dries committed
588

589
    $output .= '<div class="nav">';
Dries's avatar
 
Dries committed
590 591
    $output .= " <div class=\"links\">$links</div>";
    $output .= " <div class=\"titles\">$titles</div>";
592 593
    $output .= '</div>';
    $output .= '</div>';
Dries's avatar
 
Dries committed
594
  }
Dries's avatar
 
Dries committed
595

Dries's avatar
 
Dries committed
596
  $node->body = $node->body.$output;
Dries's avatar
 
Dries committed
597

Dries's avatar
 
Dries committed
598
  return $node;
Dries's avatar
 
Dries committed
599
}
Dries's avatar
 
Dries committed
600

Dries's avatar
 
Dries committed
601 602 603 604 605 606 607 608 609 610 611 612
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;
}

613 614
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
615

Dries's avatar
 
Dries committed
616
  while ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
617 618 619 620
    if (!$children[$node->parent]) {
      $children[$node->parent] = array();
    }
    array_push($children[$node->parent], $node);
Dries's avatar
 
Dries committed
621
  }
Dries's avatar
 
Dries committed
622 623

  /*
624 625
  ** If the user is an administrator, add the top-level book page;
  ** only administrators can start new books.
Dries's avatar
 
Dries committed
626
  */
Dries's avatar
 
Dries committed
627

628 629
  if (user_access('administer nodes')) {
    $toc[0] = '<'. t('top-level') .'>';
Dries's avatar
 
Dries committed
630 631
  }

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

Dries's avatar
 
Dries committed
634 635 636
  return $toc;
}

Dries's avatar
 
Dries committed
637
function book_tree_recurse($nid, $depth, $children, $unfold = array()) {
Dries's avatar
 
Dries committed
638
  if ($depth > 0) {
Dries's avatar
 
Dries committed
639 640
    if ($children[$nid]) {
      foreach ($children[$nid] as $foo => $node) {
Dries's avatar
 
Dries committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
        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
659
        }
Dries's avatar
 
Dries committed
660 661
      }
    }
Dries's avatar
 
Dries committed
662 663 664 665 666
  }

  return $output;
}

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

Dries's avatar
 
Dries committed
670 671 672 673
  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
674
  }
Dries's avatar
 
Dries committed
675

Dries's avatar
 
Dries committed
676 677 678
  if ($tree = book_tree_recurse($parent, $depth, $children, $unfold)) {
    return "<ul>$tree</ul>";
  }
Dries's avatar
 
Dries committed
679 680
}

681
/**
Dries's avatar
Dries committed
682
 * Menu callback; prints a listing of all books.
683
 */
Dries's avatar
 
Dries committed
684
function book_render() {
Dries's avatar
 
Dries committed
685

686
  $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
687

Dries's avatar
 
Dries committed
688 689
  while ($page = db_fetch_object($result)) {
    // load the node:
690
    $node = node_load(array('nid' => $page->nid));
Dries's avatar
 
Dries committed
691

Dries's avatar
 
Dries committed
692
    if ($node) {
Dries's avatar
 
Dries committed
693 694
      // take the most recent approved revision, extract the page and check output:
      $node = book_content($node);
Dries's avatar
 
Dries committed
695
      // output the content:
696 697
      $output .= '<div class="book">';
      $output .= '<div class="title">'. l($node->title, "book/view/$node->nid") .'</div>';
698
      $output .= '<div class="body">'. $node->teaser .'</div>';
699
      $output .= '</div>';
Dries's avatar
 
Dries committed
700
    }
Dries's avatar
 
Dries committed
701 702
  }

703 704
  drupal_set_title(t('Books'));
  print theme('page', $output);
Dries's avatar
 
Dries committed
705 706
}

707
/**
Dries's avatar
Dries committed
708
 * Menu callback; prints a node as a book page, complete with navigation.
709 710 711 712 713
 */
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
714 715
}

716
/**
Dries's avatar
Dries committed
717
 * Menu callback; generates printer-friendly book page will all descendants.
718 719
 */
function book_print($nid = 0, $depth = 1) {
Dries's avatar
 
Dries committed
720
  global $base_url;
721
  $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
722

Dries's avatar
 
Dries committed
723 724
  while ($page = db_fetch_object($result)) {
    // load the node:
725
    $node = node_load(array('nid' => $page->nid));
Dries's avatar
 
Dries committed
726

Dries's avatar
 
Dries committed
727 728
    if ($node) {
      // output the content:
729 730
      if (node_hook($node, 'content')) {
        $node = node_invoke($node, 'content');
Dries's avatar
 
Dries committed
731
      }
Dries's avatar
 
Dries committed
732
      $output .= "<h1 id=\"$node->nid\" name=\"$node->nid\" class=\"book-h$depth\">$node->title</h1>";
Dries's avatar
 
Dries committed
733

Dries's avatar
 
Dries committed
734
      if ($node->body) {
Dries's avatar
 
Dries committed
735
        $output .= $node->body;
Dries's avatar
 
Dries committed
736
      }
Dries's avatar
 
Dries committed
737
    }
Dries's avatar
 
Dries committed
738
  }
Dries's avatar
 
Dries committed
739

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

Dries's avatar
 
Dries committed
742 743 744
  $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>";
745
  $html .= '</head><body>'. $output .'</body></html>';
Dries's avatar
 
Dries committed
746

747
  print $html;
Dries's avatar
 
Dries committed
748 749
}

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

Dries's avatar
 
Dries committed
753 754
  while ($page = db_fetch_object($result)) {
    // load the node:
755
    $node = node_load(array('nid' => $page->nid));
Dries's avatar
 
Dries committed
756 757 758

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

Dries's avatar
 
Dries committed
762 763
    if ($node) {
      // output the content:
764 765
      if (node_hook($node, 'content')) {
        $node = node_invoke($node, 'content');
Dries's avatar
 
Dries committed
766
      }
Dries's avatar
 
Dries committed
767
      $output .= "<h1 id=\"$node->nid\" name=\"$node->nid\" class=\"book-h$depth\">$node->title</h1>";
Dries's avatar
 
Dries committed
768

Dries's avatar
 
Dries committed
769
      if ($node->body) {
770
        $output .= '<ul>'. $node->body .'</ul>';
Dries's avatar
 
Dries committed
771
      }
Dries's avatar
 
Dries committed
772

773
      $output .= book_print_recurse($node->nid, $depth + 1);
Dries's avatar
 
Dries committed
774
    }
Dries's avatar
 
Dries committed
775
  }
Dries's avatar
 
Dries committed
776

Dries's avatar
 
Dries committed
777 778
  return $output;
}
Dries's avatar
 
Dries committed
779

Dries's avatar
 
Dries committed
780
function book_admin_view_line($node, $depth = 0) {
781
  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/view/$node->nid"), l(t("edit node"), "admin/node/edit/$node->nid"), l(t("delete node"), "admin/node/delete/$node->nid"));
Dries's avatar
 
Dries committed
782 783 784
}

function book_admin_view_book($nid, $depth = 1) {
Dries's avatar
 
Dries committed
785
  $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
786 787 788

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

Dries's avatar
 
Dries committed
793
  return $rows;
Dries's avatar
 
Dries committed
794 795
}

796 797 798
/**
 * Display an administrative view of the hierarchy of a book.
 */
Dries's avatar
 
Dries committed
799
function book_admin_view($nid, $depth = 0) {
Dries's avatar
 
Dries committed
800 801
  if ($nid) {
    $node = node_load(array("nid" => $nid));
Dries's avatar
 
Dries committed
802

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

Dries's avatar
 
Dries committed
805 806 807
    $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
808

Dries's avatar
 
Dries committed
809
    $output .= theme("table", $header, $rows);
Dries's avatar
 
Dries committed
810
    $output .= form_submit(t("Save book pages"));
Dries's avatar
 
Dries committed
811

Dries's avatar
 
Dries committed
812 813
    return form($output);
  }
Dries's avatar
 
Dries committed
814 815 816
}

function book_admin_save($nid, $edit = array()) {
Dries's avatar
 
Dries committed
817 818
  if ($nid) {
    $book = node_load(array("nid" => $nid));
Dries's avatar
 
Dries committed
819

Dries's avatar
 
Dries committed
820 821 822 823
    foreach ($edit as $nid => $value) {
      /*
      ** Check to see whether the title needs updating:
      */
Dries's avatar
 
Dries committed
824

Dries's avatar
 
Dries committed
825
      $title = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $nid));
Dries's avatar
 
Dries committed
826
      if ($title != $value["title"]) {
Dries's avatar
 
Dries committed
827
        db_query("UPDATE {node} SET title = '%s' WHERE nid = %d", $value["title"], $nid);
Dries's avatar
 
Dries committed
828
      }
Dries's avatar
 
Dries committed
829

Dries's avatar
 
Dries committed
830 831 832
      /*
      ** Check to see whether the weight needs updating:
      */
Dries's avatar
 
Dries committed
833

Dries's avatar
 
Dries committed
834
      $weight = db_result(db_query("SELECT weight FROM {book} WHERE nid = %d", $nid));
Dries's avatar
 
Dries committed
835
      if ($weight != $value["weight"]) {
Dries's avatar
 
Dries committed
836
        db_query("UPDATE {book} SET weight = %d WHERE nid = %d", $value["weight"], $nid);
Dries's avatar
 
Dries committed
837
      }
Dries's avatar
 
Dries committed
838 839
    }

840 841
    $message = t('updated book "%title"', array('%title' => $book->title));
    watchdog('special', $message);
Dries's avatar
 
Dries committed
842

Dries's avatar
 
Dries committed
843 844
    return $message;
  }
Dries's avatar
 
Dries committed
845 846
}

847
/**
Dries's avatar
Dries committed
848
 * Menu callback; displays a listing of all orphaned book pages.
849
 */
Dries's avatar
 
Dries committed
850
function book_admin_orphan() {
Dries's avatar
 
Dries committed
851
  $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
852 853 854 855 856

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

Dries's avatar
 
Dries committed
857
  if ($pages) {
Dries's avatar
 
Dries committed
858 859
    $output .= "<h3>Orphan pages</h3>";
    $header = array(t("title"), t("weight"), array("data" => t("operations"), "colspan" => 3));
Dries's avatar
 
Dries committed
860 861
    foreach ($pages as $nid => $node) {
      if ($node->parent && empty($pages[$node->parent])) {
Dries's avatar
 
Dries committed
862 863
        $rows[] = book_admin_view_line($node, $depth);
        $rows = array_merge($rows, book_admin_view_book($node->nid, $depth + 1));
Dries's avatar
 
Dries committed
864
      }
Dries's avatar
 
Dries committed
865
    }
Dries's avatar
 
Dries committed
866
    $output .= theme("table", $header, $rows);
Dries's avatar
 
Dries committed
867 868
  }

Dries's avatar
 
Dries committed
869
  print theme("page", $out