Projet

Général

Profil

Paste
Télécharger (46,8 ko) Statistiques
| Branche: | Révision:

root / htmltest / modules / book / book.module @ 85ad3d82

1
<?php
2

    
3
/**
4
 * @file
5
 * Allows users to create and organize related content in an outline.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function book_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/help#book':
14
      $output = '<h3>' . t('About') . '</h3>';
15
      $output .= '<p>' . t('The Book module is used for creating structured, multi-page content, such as site resource guides, manuals, and wikis. It allows you to create content that has chapters, sections, subsections, or any similarly-tiered structure. For more information, see the online handbook entry for <a href="@book">Book module</a>.', array('@book' => 'http://drupal.org/documentation/modules/book/')) . '</p>';
16
      $output .= '<h3>' . t('Uses') . '</h3>';
17
      $output .= '<dl>';
18
      $output .= '<dt>' . t('Adding and managing book content') . '</dt>';
19
      $output .= '<dd>' . t('You can assign separate permissions for <em>creating</em>, <em>editing</em>, and <em>deleting</em> book content, as well as <em>adding content to books</em>, and <em>creating new books</em>. Users with the <em>Administer book outlines</em> permission can add <em>any</em> type of content to a book by selecting the appropriate book outline while editing the content. They can also view a list of all books, and edit and rearrange section titles on the <a href="@admin-book">Book administration page</a>.', array('@admin-book' => url('admin/content/book'))) . '</dd>';
20
      $output .= '<dt>' . t('Book navigation') . '</dt>';
21
      $output .= '<dd>' . t("Book pages have a default book-specific navigation block. This navigation block contains links that lead to the previous and next pages in the book, and to the level above the current page in the book's structure. This block can be enabled on the <a href='@admin-block'>Blocks administration page</a>. For book pages to show up in the book navigation, they must be added to a book outline.", array('@admin-block' => url('admin/structure/block'))) . '</dd>';
22
      $output .= '<dt>' . t('Collaboration') . '</dt>';
23
      $output .= '<dd>' . t('Books can be created collaboratively, as they allow users with appropriate permissions to add pages into existing books, and add those pages to a custom table of contents menu.') . '</dd>';
24
      $output .= '<dt>' . t('Printing books') . '</dt>';
25
      $output .= '<dd>' . t("Users with the <em>View printer-friendly books</em> permission can select the <em>printer-friendly version</em> link visible at the bottom of a book page's content to generate a printer-friendly display of the page and all of its subsections.") . '</dd>';
26
      $output .= '</dl>';
27
      return $output;
28
    case 'admin/content/book':
29
      return '<p>' . t('The book module offers a means to organize a collection of related content pages, collectively known as a book. When viewed, this content automatically displays links to adjacent book pages, providing a simple navigation system for creating and reviewing structured content.') . '</p>';
30
    case 'node/%/outline':
31
      return '<p>' . t('The outline feature allows you to include pages in the <a href="@book">Book hierarchy</a>, as well as move them within the hierarchy or to <a href="@book-admin">reorder an entire book</a>.', array('@book' => url('book'), '@book-admin' => url('admin/content/book'))) . '</p>';
32
  }
33
}
34

    
35
/**
36
 * Implements hook_theme().
37
 */
38
function book_theme() {
39
  return array(
40
    'book_navigation' => array(
41
      'variables' => array('book_link' => NULL),
42
      'template' => 'book-navigation',
43
    ),
44
    'book_export_html' => array(
45
      'variables' => array('title' => NULL, 'contents' => NULL, 'depth' => NULL),
46
      'template' => 'book-export-html',
47
    ),
48
    'book_admin_table' => array(
49
      'render element' => 'form',
50
    ),
51
    'book_title_link' => array(
52
      'variables' => array('link' => NULL),
53
    ),
54
    'book_all_books_block' => array(
55
      'render element' => 'book_menus',
56
      'template' => 'book-all-books-block',
57
    ),
58
    'book_node_export_html' => array(
59
      'variables' => array('node' => NULL, 'children' => NULL),
60
      'template' => 'book-node-export-html',
61
    ),
62
  );
63
}
64

    
65
/**
66
 * Implements hook_permission().
67
 */
68
function book_permission() {
69
  return array(
70
    'administer book outlines' => array(
71
      'title' => t('Administer book outlines'),
72
    ),
73
    'create new books' => array(
74
      'title' => t('Create new books'),
75
    ),
76
    'add content to books' => array(
77
      'title' => t('Add content and child pages to books'),
78
    ),
79
    'access printer-friendly version' => array(
80
      'title' => t('View printer-friendly books'),
81
      'description' => t('View a book page and all of its sub-pages as a single document for ease of printing. Can be performance heavy.'),
82
    ),
83
  );
84
}
85

    
86
/**
87
 * Adds relevant book links to the node's links.
88
 *
89
 * @param $node
90
 *   The book page node to add links to.
91
 * @param $view_mode
92
 *   The view mode of the node.
93
 */
94
function book_node_view_link($node, $view_mode) {
95
  $links = array();
96

    
97
  if (isset($node->book['depth'])) {
98
    if ($view_mode == 'full' && node_is_page($node)) {
99
      $child_type = variable_get('book_child_type', 'book');
100
      if ((user_access('add content to books') || user_access('administer book outlines')) && node_access('create', $child_type) && $node->status == 1 && $node->book['depth'] < MENU_MAX_DEPTH) {
101
        $links['book_add_child'] = array(
102
          'title' => t('Add child page'),
103
          'href' => 'node/add/' . str_replace('_', '-', $child_type),
104
          'query' => array('parent' => $node->book['mlid']),
105
        );
106
      }
107

    
108
      if (user_access('access printer-friendly version')) {
109
        $links['book_printer'] = array(
110
          'title' => t('Printer-friendly version'),
111
          'href' => 'book/export/html/' . $node->nid,
112
          'attributes' => array('title' => t('Show a printer-friendly version of this book page and its sub-pages.'))
113
        );
114
      }
115
    }
116
  }
117

    
118
  if (!empty($links)) {
119
    $node->content['links']['book'] = array(
120
      '#theme' => 'links__node__book',
121
      '#links' => $links,
122
      '#attributes' => array('class' => array('links', 'inline')),
123
    );
124
  }
125
}
126

    
127
/**
128
 * Implements hook_menu().
129
 */
130
function book_menu() {
131
  $items['admin/content/book'] = array(
132
    'title' => 'Books',
133
    'description' => "Manage your site's book outlines.",
134
    'page callback' => 'book_admin_overview',
135
    'access arguments' => array('administer book outlines'),
136
    'type' => MENU_LOCAL_TASK,
137
    'file' => 'book.admin.inc',
138
  );
139
  $items['admin/content/book/list'] = array(
140
    'title' => 'List',
141
    'type' => MENU_DEFAULT_LOCAL_TASK,
142
  );
143
  $items['admin/content/book/settings'] = array(
144
    'title' => 'Settings',
145
    'page callback' => 'drupal_get_form',
146
    'page arguments' => array('book_admin_settings'),
147
    'access arguments' => array('administer site configuration'),
148
    'type' => MENU_LOCAL_TASK,
149
    'weight' => 8,
150
    'file' => 'book.admin.inc',
151
  );
152
  $items['admin/content/book/%node'] = array(
153
    'title' => 'Re-order book pages and change titles',
154
    'page callback' => 'drupal_get_form',
155
    'page arguments' => array('book_admin_edit', 3),
156
    'access callback' => '_book_outline_access',
157
    'access arguments' => array(3),
158
    'type' => MENU_CALLBACK,
159
    'file' => 'book.admin.inc',
160
  );
161
  $items['book'] = array(
162
    'title' => 'Books',
163
    'page callback' => 'book_render',
164
    'access arguments' => array('access content'),
165
    'type' => MENU_SUGGESTED_ITEM,
166
    'file' => 'book.pages.inc',
167
  );
168
  $items['book/export/%/%'] = array(
169
    'page callback' => 'book_export',
170
    'page arguments' => array(2, 3),
171
    'access arguments' => array('access printer-friendly version'),
172
    'type' => MENU_CALLBACK,
173
    'file' => 'book.pages.inc',
174
  );
175
  $items['node/%node/outline'] = array(
176
    'title' => 'Outline',
177
    'page callback' => 'book_outline',
178
    'page arguments' => array(1),
179
    'access callback' => '_book_outline_access',
180
    'access arguments' => array(1),
181
    'type' => MENU_LOCAL_TASK,
182
    'weight' => 2,
183
    'file' => 'book.pages.inc',
184
  );
185
  $items['node/%node/outline/remove'] = array(
186
    'title' => 'Remove from outline',
187
    'page callback' => 'drupal_get_form',
188
    'page arguments' => array('book_remove_form', 1),
189
    'access callback' => '_book_outline_remove_access',
190
    'access arguments' => array(1),
191
    'file' => 'book.pages.inc',
192
  );
193

    
194
  return $items;
195
}
196

    
197
/**
198
 * Access callback: Determines if the outline tab is accessible.
199
 *
200
 * @param $node
201
 *   The node whose outline tab is to be viewed.
202
 */
203
function _book_outline_access($node) {
204
  return user_access('administer book outlines') && node_access('view', $node);
205
}
206

    
207
/**
208
 * Access callback: Determines if the user can remove nodes from the outline.
209
 *
210
 * @param $node
211
 *   The node to remove from the outline.
212
 *
213
 * @see book_menu()
214
 */
215
function _book_outline_remove_access($node) {
216
  return _book_node_is_removable($node) && _book_outline_access($node);
217
}
218

    
219
/**
220
 * Determines if a node can be removed from the book.
221
 *
222
 * A node can be removed from a book if it is actually in a book and it either
223
 * is not a top-level page or is a top-level page with no children.
224
 *
225
 * @param $node
226
 *   The node to remove from the outline.
227
 */
228
function _book_node_is_removable($node) {
229
  return (!empty($node->book['bid']) && (($node->book['bid'] != $node->nid) || !$node->book['has_children']));
230
}
231

    
232
/**
233
 * Implements hook_admin_paths().
234
 */
235
function book_admin_paths() {
236
  if (variable_get('node_admin_theme')) {
237
    $paths = array(
238
      'node/*/outline' => TRUE,
239
      'node/*/outline/remove' => TRUE,
240
    );
241
    return $paths;
242
  }
243
}
244

    
245
/**
246
 * Implements hook_entity_info_alter().
247
 */
248
function book_entity_info_alter(&$info) {
249
  // Add the 'Print' view mode for nodes.
250
  $info['node']['view modes'] += array(
251
    'print' => array(
252
      'label' => t('Print'),
253
      'custom settings' => FALSE,
254
    ),
255
  );
256
}
257

    
258
/**
259
 * Implements hook_block_info().
260
 */
261
function book_block_info() {
262
  $block = array();
263
  $block['navigation']['info'] = t('Book navigation');
264
  $block['navigation']['cache'] = DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE;
265

    
266
  return $block;
267
}
268

    
269
/**
270
 * Implements hook_block_view().
271
 *
272
 * Displays the book table of contents in a block when the current page is a
273
 * single-node view of a book node.
274
 */
275
function book_block_view($delta = '') {
276
  $block = array();
277
  $current_bid = 0;
278
  if ($node = menu_get_object()) {
279
    $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
280
  }
281

    
282
  if (variable_get('book_block_mode', 'all pages') == 'all pages') {
283
    $block['subject'] = t('Book navigation');
284
    $book_menus = array();
285
    $pseudo_tree = array(0 => array('below' => FALSE));
286
    foreach (book_get_books() as $book_id => $book) {
287
      if ($book['bid'] == $current_bid) {
288
        // If the current page is a node associated with a book, the menu
289
        // needs to be retrieved.
290
        $book_menus[$book_id] = menu_tree_output(menu_tree_all_data($node->book['menu_name'], $node->book));
291
      }
292
      else {
293
        // Since we know we will only display a link to the top node, there
294
        // is no reason to run an additional menu tree query for each book.
295
        $book['in_active_trail'] = FALSE;
296
        // Check whether user can access the book link.
297
        $book_node = node_load($book['nid']);
298
        $book['access'] = node_access('view', $book_node);
299
        $pseudo_tree[0]['link'] = $book;
300
        $book_menus[$book_id] = menu_tree_output($pseudo_tree);
301
      }
302
    }
303
    $book_menus['#theme'] = 'book_all_books_block';
304
    $block['content'] = $book_menus;
305
  }
306
  elseif ($current_bid) {
307
    // Only display this block when the user is browsing a book.
308
  $select = db_select('node', 'n')
309
    ->fields('n', array('title'))
310
    ->condition('n.nid', $node->book['bid'])
311
    ->addTag('node_access');
312
    $title = $select->execute()->fetchField();
313
    // Only show the block if the user has view access for the top-level node.
314
    if ($title) {
315
      $tree = menu_tree_all_data($node->book['menu_name'], $node->book);
316
      // There should only be one element at the top level.
317
      $data = array_shift($tree);
318
      $block['subject'] = theme('book_title_link', array('link' => $data['link']));
319
      $block['content'] = ($data['below']) ? menu_tree_output($data['below']) : '';
320
    }
321
  }
322

    
323
  return $block;
324
}
325

    
326
/**
327
 * Implements hook_block_configure().
328
 */
329
function book_block_configure($delta = '') {
330
  $block = array();
331
  $options = array(
332
    'all pages' => t('Show block on all pages'),
333
    'book pages' => t('Show block only on book pages'),
334
  );
335
  $form['book_block_mode'] = array(
336
    '#type' => 'radios',
337
    '#title' => t('Book navigation block display'),
338
    '#options' => $options,
339
    '#default_value' => variable_get('book_block_mode', 'all pages'),
340
    '#description' => t("If <em>Show block on all pages</em> is selected, the block will contain the automatically generated menus for all of the site's books. If <em>Show block only on book pages</em> is selected, the block will contain only the one menu corresponding to the current page's book. In this case, if the current page is not in a book, no block will be displayed. The <em>Page specific visibility settings</em> or other visibility settings can be used in addition to selectively display this block."),
341
    );
342

    
343
  return $form;
344
}
345

    
346
/**
347
 * Implements hook_block_save().
348
 */
349
function book_block_save($delta = '', $edit = array()) {
350
  $block = array();
351
  variable_set('book_block_mode', $edit['book_block_mode']);
352
}
353

    
354
/**
355
 * Returns HTML for a link to a book title when used as a block title.
356
 *
357
 * @param $variables
358
 *   An associative array containing:
359
 *   - link: An array containing title, href and options for the link.
360
 *
361
 * @ingroup themeable
362
 */
363
function theme_book_title_link($variables) {
364
  $link = $variables['link'];
365

    
366
  $link['options']['attributes']['class'] = array('book-title');
367

    
368
  return l($link['title'], $link['href'], $link['options']);
369
}
370

    
371
/**
372
 * Returns an array of all books.
373
 *
374
 * This list may be used for generating a list of all the books, or for building
375
 * the options for a form select.
376
 *
377
 * @return
378
 *   An array of all books.
379
 */
380
function book_get_books() {
381
  $all_books = &drupal_static(__FUNCTION__);
382

    
383
  if (!isset($all_books)) {
384
    $all_books = array();
385
    $nids = db_query("SELECT DISTINCT(bid) FROM {book}")->fetchCol();
386

    
387
    if ($nids) {
388
      $query = db_select('book', 'b', array('fetch' => PDO::FETCH_ASSOC));
389
      $query->join('node', 'n', 'b.nid = n.nid');
390
      $query->join('menu_links', 'ml', 'b.mlid = ml.mlid');
391
      $query->addField('n', 'type', 'type');
392
      $query->addField('n', 'title', 'title');
393
      $query->fields('b');
394
      $query->fields('ml');
395
      $query->condition('n.nid', $nids, 'IN');
396
      $query->condition('n.status', 1);
397
      $query->orderBy('ml.weight');
398
      $query->orderBy('ml.link_title');
399
      $query->addTag('node_access');
400
      $result2 = $query->execute();
401
      foreach ($result2 as $link) {
402
        $link['href'] = $link['link_path'];
403
        $link['options'] = unserialize($link['options']);
404
        $all_books[$link['bid']] = $link;
405
      }
406
    }
407
  }
408

    
409
  return $all_books;
410
}
411

    
412
/**
413
 * Implements hook_form_BASE_FORM_ID_alter() for node_form().
414
 *
415
 * Adds the book fieldset to the node form.
416
 *
417
 * @see book_pick_book_nojs_submit()
418
 */
419
function book_form_node_form_alter(&$form, &$form_state, $form_id) {
420
  $node = $form['#node'];
421
  $access = user_access('administer book outlines');
422
  if (!$access) {
423
    if (user_access('add content to books') && ((!empty($node->book['mlid']) && !empty($node->nid)) || book_type_is_allowed($node->type))) {
424
      // Already in the book hierarchy, or this node type is allowed.
425
      $access = TRUE;
426
    }
427
  }
428

    
429
  if ($access) {
430
    _book_add_form_elements($form, $form_state, $node);
431
    // Since the "Book" dropdown can't trigger a form submission when
432
    // JavaScript is disabled, add a submit button to do that. book.css hides
433
    // this button when JavaScript is enabled.
434
    $form['book']['pick-book'] = array(
435
      '#type' => 'submit',
436
      '#value' => t('Change book (update list of parents)'),
437
      '#submit' => array('book_pick_book_nojs_submit'),
438
      '#weight' => 20,
439
    );
440
  }
441
}
442

    
443
/**
444
 * Form submission handler for node_form().
445
 *
446
 * This handler is run when JavaScript is disabled. It triggers the form to
447
 * rebuild so that the "Parent item" options are changed to reflect the newly
448
 * selected book. When JavaScript is enabled, the submit button that triggers
449
 * this handler is hidden, and the "Book" dropdown directly triggers the
450
 * book_form_update() Ajax callback instead.
451
 *
452
 * @see book_form_update()
453
 * @see book_form_node_form_alter()
454
 */
455
function book_pick_book_nojs_submit($form, &$form_state) {
456
  $form_state['node']->book = $form_state['values']['book'];
457
  $form_state['rebuild'] = TRUE;
458
}
459

    
460
/**
461
 * Builds the parent selection form element for the node form or outline tab.
462
 *
463
 * This function is also called when generating a new set of options during the
464
 * Ajax callback, so an array is returned that can be used to replace an
465
 * existing form element.
466
 *
467
 * @param $book_link
468
 *   A fully loaded menu link that is part of the book hierarchy.
469
 *
470
 * @return
471
 *   A parent selection form element.
472
 */
473
function _book_parent_select($book_link) {
474
  if (variable_get('menu_override_parent_selector', FALSE)) {
475
    return array();
476
  }
477
  // Offer a message or a drop-down to choose a different parent page.
478
  $form = array(
479
    '#type' => 'hidden',
480
    '#value' => -1,
481
    '#prefix' => '<div id="edit-book-plid-wrapper">',
482
    '#suffix' => '</div>',
483
  );
484

    
485
  if ($book_link['nid'] === $book_link['bid']) {
486
    // This is a book - at the top level.
487
    if ($book_link['original_bid'] === $book_link['bid']) {
488
      $form['#prefix'] .= '<em>' . t('This is the top-level page in this book.') . '</em>';
489
    }
490
    else {
491
      $form['#prefix'] .= '<em>' . t('This will be the top-level page in this book.') . '</em>';
492
    }
493
  }
494
  elseif (!$book_link['bid']) {
495
    $form['#prefix'] .= '<em>' . t('No book selected.') . '</em>';
496
  }
497
  else {
498
    $form = array(
499
      '#type' => 'select',
500
      '#title' => t('Parent item'),
501
      '#default_value' => $book_link['plid'],
502
      '#description' => t('The parent page in the book. The maximum depth for a book and all child pages is !maxdepth. Some pages in the selected book may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)),
503
      '#options' => book_toc($book_link['bid'], $book_link['parent_depth_limit'], array($book_link['mlid'])),
504
      '#attributes' => array('class' => array('book-title-select')),
505
      '#prefix' => '<div id="edit-book-plid-wrapper">',
506
      '#suffix' => '</div>',
507
    );
508
  }
509

    
510
  return $form;
511
}
512

    
513
/**
514
 * Builds the common elements of the book form for the node and outline forms.
515
 *
516
 * @param $node
517
 *   The node whose form is being viewed.
518
 */
519
function _book_add_form_elements(&$form, &$form_state, $node) {
520
  // If the form is being processed during the Ajax callback of our book bid
521
  // dropdown, then $form_state will hold the value that was selected.
522
  if (isset($form_state['values']['book'])) {
523
    $node->book = $form_state['values']['book'];
524
  }
525

    
526
  $form['book'] = array(
527
    '#type' => 'fieldset',
528
    '#title' => t('Book outline'),
529
    '#weight' => 10,
530
    '#collapsible' => TRUE,
531
    '#collapsed' => TRUE,
532
    '#group' => 'additional_settings',
533
    '#attributes' => array(
534
      'class' => array('book-outline-form'),
535
    ),
536
    '#attached' => array(
537
      'js' => array(drupal_get_path('module', 'book') . '/book.js'),
538
    ),
539
    '#tree' => TRUE,
540
  );
541
  foreach (array('menu_name', 'mlid', 'nid', 'router_path', 'has_children', 'options', 'module', 'original_bid', 'parent_depth_limit') as $key) {
542
    $form['book'][$key] = array(
543
      '#type' => 'value',
544
      '#value' => $node->book[$key],
545
    );
546
  }
547

    
548
  $form['book']['plid'] = _book_parent_select($node->book);
549

    
550
  // @see _book_admin_table_tree(). The weight may be larger than 15.
551
  $form['book']['weight'] = array(
552
    '#type' => 'weight',
553
    '#title' => t('Weight'),
554
    '#default_value' => $node->book['weight'],
555
    '#delta' => max(15, abs($node->book['weight'])),
556
    '#weight' => 5,
557
    '#description' => t('Pages at a given level are ordered first by weight and then by title.'),
558
  );
559
  $options = array();
560
  $nid = isset($node->nid) ? $node->nid : 'new';
561

    
562
  if (isset($node->nid) && ($nid == $node->book['original_bid']) && ($node->book['parent_depth_limit'] == 0)) {
563
    // This is the top level node in a maximum depth book and thus cannot be moved.
564
    $options[$node->nid] = $node->title;
565
  }
566
  else {
567
    foreach (book_get_books() as $book) {
568
      $options[$book['nid']] = $book['title'];
569
    }
570
  }
571

    
572
  if (user_access('create new books') && ($nid == 'new' || ($nid != $node->book['original_bid']))) {
573
    // The node can become a new book, if it is not one already.
574
    $options = array($nid => '<' . t('create a new book') . '>') + $options;
575
  }
576
  if (!$node->book['mlid']) {
577
    // The node is not currently in the hierarchy.
578
    $options = array(0 => '<' . t('none') . '>') + $options;
579
  }
580

    
581
  // Add a drop-down to select the destination book.
582
  $form['book']['bid'] = array(
583
    '#type' => 'select',
584
    '#title' => t('Book'),
585
    '#default_value' => $node->book['bid'],
586
    '#options' => $options,
587
    '#access' => (bool) $options,
588
    '#description' => t('Your page will be a part of the selected book.'),
589
    '#weight' => -5,
590
    '#attributes' => array('class' => array('book-title-select')),
591
    '#ajax' => array(
592
      'callback' => 'book_form_update',
593
      'wrapper' => 'edit-book-plid-wrapper',
594
      'effect' => 'fade',
595
      'speed' => 'fast',
596
    ),
597
  );
598
}
599

    
600
/**
601
 * Renders a new parent page select element when the book selection changes.
602
 *
603
 * This function is called via Ajax when the selected book is changed on a node
604
 * or book outline form.
605
 *
606
 * @return
607
 *   The rendered parent page select element.
608
 */
609
function book_form_update($form, $form_state) {
610
  return $form['book']['plid'];
611
}
612

    
613
/**
614
 * Handles additions and updates to the book outline.
615
 *
616
 * This common helper function performs all additions and updates to the book
617
 * outline through node addition, node editing, node deletion, or the outline
618
 * tab.
619
 *
620
 * @param $node
621
 *   The node that is being saved, added, deleted, or moved.
622
 *
623
 * @return
624
 *   TRUE if the menu link was saved; FALSE otherwise.
625
 */
626
function _book_update_outline($node) {
627
  if (empty($node->book['bid'])) {
628
    return FALSE;
629
  }
630
  $new = empty($node->book['mlid']);
631

    
632
  $node->book['link_path'] = 'node/' . $node->nid;
633
  $node->book['link_title'] = $node->title;
634
  $node->book['parent_mismatch'] = FALSE; // The normal case.
635

    
636
  if ($node->book['bid'] == $node->nid) {
637
    $node->book['plid'] = 0;
638
    $node->book['menu_name'] = book_menu_name($node->nid);
639
  }
640
  else {
641
    // Check in case the parent is not is this book; the book takes precedence.
642
    if (!empty($node->book['plid'])) {
643
      $parent = db_query("SELECT * FROM {book} WHERE mlid = :mlid", array(
644
        ':mlid' => $node->book['plid'],
645
      ))->fetchAssoc();
646
    }
647
    if (empty($node->book['plid']) || !$parent || $parent['bid'] != $node->book['bid']) {
648
      $node->book['plid'] = db_query("SELECT mlid FROM {book} WHERE nid = :nid", array(
649
        ':nid' => $node->book['bid'],
650
      ))->fetchField();
651
      $node->book['parent_mismatch'] = TRUE; // Likely when JS is disabled.
652
    }
653
  }
654

    
655
  if (menu_link_save($node->book)) {
656
    if ($new) {
657
      // Insert new.
658
      db_insert('book')
659
        ->fields(array(
660
          'nid' => $node->nid,
661
          'mlid' => $node->book['mlid'],
662
          'bid' => $node->book['bid'],
663
        ))
664
        ->execute();
665
      // Reset the cache of stored books.
666
      drupal_static_reset('book_get_books');
667
    }
668
    else {
669
      if ($node->book['bid'] != db_query("SELECT bid FROM {book} WHERE nid = :nid", array(
670
          ':nid' => $node->nid,
671
        ))->fetchField()) {
672
        // Update the bid for this page and all children.
673
        book_update_bid($node->book);
674
        // Reset the cache of stored books.
675
        drupal_static_reset('book_get_books');
676
      }
677
    }
678

    
679
    return TRUE;
680
  }
681

    
682
  // Failed to save the menu link.
683
  return FALSE;
684
}
685

    
686
/**
687
 * Updates the book ID of a page and its children when it moves to a new book.
688
 *
689
 * @param $book_link
690
 *   A fully loaded menu link that is part of the book hierarchy.
691
 */
692
function book_update_bid($book_link) {
693
  $query = db_select('menu_links');
694
  $query->addField('menu_links', 'mlid');
695
  for ($i = 1; $i <= MENU_MAX_DEPTH && $book_link["p$i"]; $i++) {
696
    $query->condition("p$i", $book_link["p$i"]);
697
  }
698
  $mlids = $query->execute()->fetchCol();
699

    
700
  if ($mlids) {
701
    db_update('book')
702
      ->fields(array('bid' => $book_link['bid']))
703
      ->condition('mlid', $mlids, 'IN')
704
      ->execute();
705
  }
706
}
707

    
708
/**
709
 * Gets the book menu tree for a page and returns it as a linear array.
710
 *
711
 * @param $book_link
712
 *   A fully loaded menu link that is part of the book hierarchy.
713
 *
714
 * @return
715
 *   A linear array of menu links in the order that the links are shown in the
716
 *   menu, so the previous and next pages are the elements before and after the
717
 *   element corresponding to the current node. The children of the current node
718
 *   (if any) will come immediately after it in the array, and links will only
719
 *   be fetched as deep as one level deeper than $book_link.
720
 */
721
function book_get_flat_menu($book_link) {
722
  $flat = &drupal_static(__FUNCTION__, array());
723

    
724
  if (!isset($flat[$book_link['mlid']])) {
725
    // Call menu_tree_all_data() to take advantage of the menu system's caching.
726
    $tree = menu_tree_all_data($book_link['menu_name'], $book_link, $book_link['depth'] + 1);
727
    $flat[$book_link['mlid']] = array();
728
    _book_flatten_menu($tree, $flat[$book_link['mlid']]);
729
  }
730

    
731
  return $flat[$book_link['mlid']];
732
}
733

    
734
/**
735
 * Recursively converts a tree of menu links to a flat array.
736
 *
737
 * @param $tree
738
 *   A tree of menu links in an array.
739
 * @param $flat
740
 *   A flat array of the menu links from $tree, passed by reference.
741
 *
742
 * @see book_get_flat_menu().
743
 */
744
function _book_flatten_menu($tree, &$flat) {
745
  foreach ($tree as $data) {
746
    if (!$data['link']['hidden']) {
747
      $flat[$data['link']['mlid']] = $data['link'];
748
      if ($data['below']) {
749
        _book_flatten_menu($data['below'], $flat);
750
      }
751
    }
752
  }
753
}
754

    
755
/**
756
 * Fetches the menu link for the previous page of the book.
757
 *
758
 * @param $book_link
759
 *   A fully loaded menu link that is part of the book hierarchy.
760
 *
761
 * @return
762
 *   A fully loaded menu link for the page before the one represented in
763
 *   $book_link.
764
 */
765
function book_prev($book_link) {
766
  // If the parent is zero, we are at the start of a book.
767
  if ($book_link['plid'] == 0) {
768
    return NULL;
769
  }
770
  $flat = book_get_flat_menu($book_link);
771
  // Assigning the array to $flat resets the array pointer for use with each().
772
  $curr = NULL;
773
  do {
774
    $prev = $curr;
775
    list($key, $curr) = each($flat);
776
  } while ($key && $key != $book_link['mlid']);
777

    
778
  if ($key == $book_link['mlid']) {
779
    // The previous page in the book may be a child of the previous visible link.
780
    if ($prev['depth'] == $book_link['depth'] && $prev['has_children']) {
781
      // The subtree will have only one link at the top level - get its data.
782
      $tree = book_menu_subtree_data($prev);
783
      $data = array_shift($tree);
784
      // The link of interest is the last child - iterate to find the deepest one.
785
      while ($data['below']) {
786
        $data = end($data['below']);
787
      }
788

    
789
      return $data['link'];
790
    }
791
    else {
792
      return $prev;
793
    }
794
  }
795
}
796

    
797
/**
798
 * Fetches the menu link for the next page of the book.
799
 *
800
 * @param $book_link
801
 *   A fully loaded menu link that is part of the book hierarchy.
802
 *
803
 * @return
804
 *   A fully loaded menu link for the page after the one represented in
805
 *   $book_link.
806
 */
807
function book_next($book_link) {
808
  $flat = book_get_flat_menu($book_link);
809
  // Assigning the array to $flat resets the array pointer for use with each().
810
  do {
811
    list($key, $curr) = each($flat);
812
  }
813
  while ($key && $key != $book_link['mlid']);
814

    
815
  if ($key == $book_link['mlid']) {
816
    return current($flat);
817
  }
818
}
819

    
820
/**
821
 * Formats the menu links for the child pages of the current page.
822
 *
823
 * @param $book_link
824
 *   A fully loaded menu link that is part of the book hierarchy.
825
 *
826
 * @return
827
 *   HTML for the links to the child pages of the current page.
828
 */
829
function book_children($book_link) {
830
  $flat = book_get_flat_menu($book_link);
831

    
832
  $children = array();
833

    
834
  if ($book_link['has_children']) {
835
    // Walk through the array until we find the current page.
836
    do {
837
      $link = array_shift($flat);
838
    }
839
    while ($link && ($link['mlid'] != $book_link['mlid']));
840
    // Continue though the array and collect the links whose parent is this page.
841
    while (($link = array_shift($flat)) && $link['plid'] == $book_link['mlid']) {
842
      $data['link'] = $link;
843
      $data['below'] = '';
844
      $children[] = $data;
845
    }
846
  }
847

    
848
  if ($children) {
849
    $elements = menu_tree_output($children);
850
    return drupal_render($elements);
851
  }
852
  return '';
853
}
854

    
855
/**
856
 * Generates the corresponding menu name from a book ID.
857
 *
858
 * @param $bid
859
 *   The book ID for which to make a menu name.
860
 *
861
 * @return
862
 *   The menu name.
863
 */
864
function book_menu_name($bid) {
865
  return 'book-toc-' . $bid;
866
}
867

    
868
/**
869
 * Implements hook_node_load().
870
 */
871
function book_node_load($nodes, $types) {
872
  $result = db_query("SELECT * FROM {book} b INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE b.nid IN (:nids)", array(':nids' =>  array_keys($nodes)), array('fetch' => PDO::FETCH_ASSOC));
873
  foreach ($result as $record) {
874
    $nodes[$record['nid']]->book = $record;
875
    $nodes[$record['nid']]->book['href'] = $record['link_path'];
876
    $nodes[$record['nid']]->book['title'] = $record['link_title'];
877
    $nodes[$record['nid']]->book['options'] = unserialize($record['options']);
878
  }
879
}
880

    
881
/**
882
 * Implements hook_node_view().
883
 */
884
function book_node_view($node, $view_mode) {
885
  if ($view_mode == 'full') {
886
    if (!empty($node->book['bid']) && empty($node->in_preview)) {
887
      $node->content['book_navigation'] = array(
888
        '#markup' => theme('book_navigation', array('book_link' => $node->book)),
889
        '#weight' => 100,
890
      );
891
    }
892
  }
893

    
894
  if ($view_mode != 'rss') {
895
    book_node_view_link($node, $view_mode);
896
  }
897
}
898

    
899
/**
900
 * Implements hook_page_alter().
901
 *
902
 * Adds the book menu to the list of menus used to build the active trail when
903
 * viewing a book page.
904
 */
905
function book_page_alter(&$page) {
906
  if (($node = menu_get_object()) && !empty($node->book['bid'])) {
907
    $active_menus = menu_get_active_menu_names();
908
    $active_menus[] = $node->book['menu_name'];
909
    menu_set_active_menu_names($active_menus);
910
  }
911
}
912

    
913
/**
914
 * Implements hook_node_presave().
915
 */
916
function book_node_presave($node) {
917
  // Always save a revision for non-administrators.
918
  if (!empty($node->book['bid']) && !user_access('administer nodes')) {
919
    $node->revision = 1;
920
    // The database schema requires a log message for every revision.
921
    if (!isset($node->log)) {
922
      $node->log = '';
923
    }
924
  }
925
  // Make sure a new node gets a new menu link.
926
  if (empty($node->nid)) {
927
    $node->book['mlid'] = NULL;
928
  }
929
}
930

    
931
/**
932
 * Implements hook_node_insert().
933
 */
934
function book_node_insert($node) {
935
  if (!empty($node->book['bid'])) {
936
    if ($node->book['bid'] == 'new') {
937
      // New nodes that are their own book.
938
      $node->book['bid'] = $node->nid;
939
    }
940
    $node->book['nid'] = $node->nid;
941
    $node->book['menu_name'] = book_menu_name($node->book['bid']);
942
    _book_update_outline($node);
943
  }
944
}
945

    
946
/**
947
 * Implements hook_node_update().
948
 */
949
function book_node_update($node) {
950
  if (!empty($node->book['bid'])) {
951
    if ($node->book['bid'] == 'new') {
952
      // New nodes that are their own book.
953
      $node->book['bid'] = $node->nid;
954
    }
955
    $node->book['nid'] = $node->nid;
956
    $node->book['menu_name'] = book_menu_name($node->book['bid']);
957
    _book_update_outline($node);
958
  }
959
}
960

    
961
/**
962
 * Implements hook_node_delete().
963
 */
964
function book_node_delete($node) {
965
  if (!empty($node->book['bid'])) {
966
    if ($node->nid == $node->book['bid']) {
967
      // Handle deletion of a top-level post.
968
      $result = db_query("SELECT b.nid FROM {menu_links} ml INNER JOIN {book} b on b.mlid = ml.mlid WHERE ml.plid = :plid", array(
969
        ':plid' => $node->book['mlid']
970
      ));
971
      foreach ($result as $child) {
972
        $child_node = node_load($child->nid);
973
        $child_node->book['bid'] = $child_node->nid;
974
        _book_update_outline($child_node);
975
      }
976
    }
977
    menu_link_delete($node->book['mlid']);
978
    db_delete('book')
979
      ->condition('mlid', $node->book['mlid'])
980
      ->execute();
981
    drupal_static_reset('book_get_books');
982
  }
983
}
984

    
985
/**
986
 * Implements hook_node_prepare().
987
 */
988
function book_node_prepare($node) {
989
  // Prepare defaults for the add/edit form.
990
  if (empty($node->book) && (user_access('add content to books') || user_access('administer book outlines'))) {
991
    $node->book = array();
992

    
993
    if (empty($node->nid) && isset($_GET['parent']) && is_numeric($_GET['parent'])) {
994
      // Handle "Add child page" links:
995
      $parent = book_link_load($_GET['parent']);
996

    
997
      if ($parent && $parent['access']) {
998
        $node->book['bid'] = $parent['bid'];
999
        $node->book['plid'] = $parent['mlid'];
1000
        $node->book['menu_name'] = $parent['menu_name'];
1001
      }
1002
    }
1003
    // Set defaults.
1004
    $node->book += _book_link_defaults(!empty($node->nid) ? $node->nid : 'new');
1005
  }
1006
  else {
1007
    if (isset($node->book['bid']) && !isset($node->book['original_bid'])) {
1008
      $node->book['original_bid'] = $node->book['bid'];
1009
    }
1010
  }
1011
  // Find the depth limit for the parent select.
1012
  if (isset($node->book['bid']) && !isset($node->book['parent_depth_limit'])) {
1013
    $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book);
1014
  }
1015
}
1016

    
1017
/**
1018
 * Finds the depth limit for items in the parent select.
1019
 *
1020
 * @param $book_link
1021
 *   A fully loaded menu link that is part of the book hierarchy.
1022
 *
1023
 * @return
1024
 *   The depth limit for items in the parent select.
1025
 */
1026
function _book_parent_depth_limit($book_link) {
1027
  return MENU_MAX_DEPTH - 1 - (($book_link['mlid'] && $book_link['has_children']) ? menu_link_children_relative_depth($book_link) : 0);
1028
}
1029

    
1030
/**
1031
 * Implements hook_form_FORM_ID_alter() for node_delete_confirm().
1032
 *
1033
 * Alters the confirm form for a single node deletion.
1034
 *
1035
 * @see node_delete_confirm()
1036
 */
1037
function book_form_node_delete_confirm_alter(&$form, $form_state) {
1038
  $node = node_load($form['nid']['#value']);
1039

    
1040
  if (isset($node->book) && $node->book['has_children']) {
1041
    $form['book_warning'] = array(
1042
      '#markup' => '<p>' . t('%title is part of a book outline, and has associated child pages. If you proceed with deletion, the child pages will be relocated automatically.', array('%title' => $node->title)) . '</p>',
1043
      '#weight' => -10,
1044
    );
1045
  }
1046
}
1047

    
1048
/**
1049
 * Returns an array with default values for a book page's menu link.
1050
 *
1051
 * @param $nid
1052
 *   The ID of the node whose menu link is being created.
1053
 *
1054
 * @return
1055
 *   The default values for the menu link.
1056
 */
1057
function _book_link_defaults($nid) {
1058
  return array('original_bid' => 0, 'menu_name' => '', 'nid' => $nid, 'bid' => 0, 'router_path' => 'node/%', 'plid' => 0, 'mlid' => 0, 'has_children' => 0, 'weight' => 0, 'module' => 'book', 'options' => array());
1059
}
1060

    
1061
/**
1062
 * Processes variables for book-all-books-block.tpl.php.
1063
 *
1064
 * All non-renderable elements are removed so that the template has full access
1065
 * to the structured data but can also simply iterate over all elements and
1066
 * render them (as in the default template).
1067
 *
1068
 * @param $variables
1069
 *   An associative array containing the following key:
1070
 *   - book_menus
1071
 *
1072
 * @see book-all-books-block.tpl.php
1073
 */
1074
function template_preprocess_book_all_books_block(&$variables) {
1075
  // Remove all non-renderable elements.
1076
  $elements = $variables['book_menus'];
1077
  $variables['book_menus'] = array();
1078
  foreach (element_children($elements) as $index) {
1079
    $variables['book_menus'][$index] = $elements[$index];
1080
  }
1081
}
1082

    
1083
/**
1084
 * Processes variables for book-navigation.tpl.php.
1085
 *
1086
 * @param $variables
1087
 *   An associative array containing the following key:
1088
 *   - book_link
1089
 *
1090
 * @see book-navigation.tpl.php
1091
 */
1092
function template_preprocess_book_navigation(&$variables) {
1093
  $book_link = $variables['book_link'];
1094

    
1095
  // Provide extra variables for themers. Not needed by default.
1096
  $variables['book_id'] = $book_link['bid'];
1097
  $variables['book_title'] = check_plain($book_link['link_title']);
1098
  $variables['book_url'] = 'node/' . $book_link['bid'];
1099
  $variables['current_depth'] = $book_link['depth'];
1100
  $variables['tree'] = '';
1101

    
1102
  if ($book_link['mlid']) {
1103
    $variables['tree'] = book_children($book_link);
1104

    
1105
    if ($prev = book_prev($book_link)) {
1106
      $prev_href = url($prev['href']);
1107
      drupal_add_html_head_link(array('rel' => 'prev', 'href' => $prev_href));
1108
      $variables['prev_url'] = $prev_href;
1109
      $variables['prev_title'] = check_plain($prev['title']);
1110
    }
1111

    
1112
    if ($book_link['plid'] && $parent = book_link_load($book_link['plid'])) {
1113
      $parent_href = url($parent['href']);
1114
      drupal_add_html_head_link(array('rel' => 'up', 'href' => $parent_href));
1115
      $variables['parent_url'] = $parent_href;
1116
      $variables['parent_title'] = check_plain($parent['title']);
1117
    }
1118

    
1119
    if ($next = book_next($book_link)) {
1120
      $next_href = url($next['href']);
1121
      drupal_add_html_head_link(array('rel' => 'next', 'href' => $next_href));
1122
      $variables['next_url'] = $next_href;
1123
      $variables['next_title'] = check_plain($next['title']);
1124
    }
1125
  }
1126

    
1127
  $variables['has_links'] = FALSE;
1128
  // Link variables to filter for values and set state of the flag variable.
1129
  $links = array('prev_url', 'prev_title', 'parent_url', 'parent_title', 'next_url', 'next_title');
1130
  foreach ($links as $link) {
1131
    if (isset($variables[$link])) {
1132
      // Flag when there is a value.
1133
      $variables['has_links'] = TRUE;
1134
    }
1135
    else {
1136
      // Set empty to prevent notices.
1137
      $variables[$link] = '';
1138
    }
1139
  }
1140
}
1141

    
1142
/**
1143
 * Recursively processes and formats menu items for book_toc().
1144
 *
1145
 * This helper function recursively modifies the table of contents array for
1146
 * each item in the menu tree, ignoring items in the exclude array or at a depth
1147
 * greater than the limit. Truncates titles over thirty characters and appends
1148
 * an indentation string incremented by depth.
1149
 *
1150
 * @param $tree
1151
 *   The data structure of the book's menu tree. Includes hidden links.
1152
 * @param $indent
1153
 *   A string appended to each menu item title. Increments by '--' per depth
1154
 *   level.
1155
 * @param $toc
1156
 *   Reference to the table of contents array. This is modified in place, so the
1157
 *   function does not have a return value.
1158
 * @param $exclude
1159
 *   (optional) An array of menu link ID values. Any link whose menu link ID is
1160
 *   in this array will be excluded (along with its children). Defaults to an
1161
 *   empty array.
1162
 * @param $depth_limit
1163
 *   Any link deeper than this value will be excluded (along with its children).
1164
 */
1165
function _book_toc_recurse($tree, $indent, &$toc, $exclude, $depth_limit) {
1166
  foreach ($tree as $data) {
1167
    if ($data['link']['depth'] > $depth_limit) {
1168
      // Don't iterate through any links on this level.
1169
      break;
1170
    }
1171

    
1172
    if (!in_array($data['link']['mlid'], $exclude)) {
1173
      $toc[$data['link']['mlid']] = $indent . ' ' . truncate_utf8($data['link']['title'], 30, TRUE, TRUE);
1174
      if ($data['below']) {
1175
        _book_toc_recurse($data['below'], $indent . '--', $toc, $exclude, $depth_limit);
1176
      }
1177
    }
1178
  }
1179
}
1180

    
1181
/**
1182
 * Returns an array of book pages in table of contents order.
1183
 *
1184
 * @param $bid
1185
 *   The ID of the book whose pages are to be listed.
1186
 * @param $depth_limit
1187
 *   Any link deeper than this value will be excluded (along with its children).
1188
 * @param $exclude
1189
 *   Optional array of menu link ID values. Any link whose menu link ID is in
1190
 *   this array will be excluded (along with its children).
1191
 *
1192
 * @return
1193
 *   An array of (menu link ID, title) pairs for use as options for selecting a
1194
 *   book page.
1195
 */
1196
function book_toc($bid, $depth_limit, $exclude = array()) {
1197
  $tree = menu_tree_all_data(book_menu_name($bid));
1198
  $toc = array();
1199
  _book_toc_recurse($tree, '', $toc, $exclude, $depth_limit);
1200

    
1201
  return $toc;
1202
}
1203

    
1204
/**
1205
 * Processes variables for book-export-html.tpl.php.
1206
 *
1207
 * @param $variables
1208
 *   An associative array containing the following keys:
1209
 *   - title
1210
 *   - contents
1211
 *   - depth
1212
 *
1213
 * @see book-export-html.tpl.php
1214
 */
1215
function template_preprocess_book_export_html(&$variables) {
1216
  global $base_url, $language;
1217

    
1218
  $variables['title'] = check_plain($variables['title']);
1219
  $variables['base_url'] = $base_url;
1220
  $variables['language'] = $language;
1221
  $variables['language_rtl'] = ($language->direction == LANGUAGE_RTL);
1222
  $variables['head'] = drupal_get_html_head();
1223
  $variables['dir'] = $language->direction ? 'rtl' : 'ltr';
1224
}
1225

    
1226
/**
1227
 * Traverses the book tree to build printable or exportable output.
1228
 *
1229
 * During the traversal, the $visit_func() callback is applied to each node and
1230
 * is called recursively for each child of the node (in weight, title order).
1231
 *
1232
 * @param $tree
1233
 *   A subtree of the book menu hierarchy, rooted at the current page.
1234
 * @param $visit_func
1235
 *   A function callback to be called upon visiting a node in the tree.
1236
 *
1237
 * @return
1238
 *   The output generated in visiting each node.
1239
 */
1240
function book_export_traverse($tree, $visit_func) {
1241
  $output = '';
1242

    
1243
  foreach ($tree as $data) {
1244
    // Note- access checking is already performed when building the tree.
1245
    if ($node = node_load($data['link']['nid'], FALSE)) {
1246
      $children = '';
1247

    
1248
      if ($data['below']) {
1249
        $children = book_export_traverse($data['below'], $visit_func);
1250
      }
1251

    
1252
      if (function_exists($visit_func)) {
1253
        $output .= call_user_func($visit_func, $node, $children);
1254
      }
1255
      else {
1256
        // Use the default function.
1257
        $output .= book_node_export($node, $children);
1258
      }
1259
    }
1260
  }
1261

    
1262
  return $output;
1263
}
1264

    
1265
/**
1266
 * Generates printer-friendly HTML for a node.
1267
 *
1268
 * @param $node
1269
 *   The node that will be output.
1270
 * @param $children
1271
 *   (optional) All the rendered child nodes within the current node. Defaults
1272
 *   to an empty string.
1273
 *
1274
 * @return
1275
 *   The HTML generated for the given node.
1276
 *
1277
 * @see book_export_traverse()
1278
 */
1279
function book_node_export($node, $children = '') {
1280
  $build = node_view($node, 'print');
1281
  unset($build['#theme']);
1282
  // @todo Rendering should happen in the template using render().
1283
  $node->rendered = drupal_render($build);
1284

    
1285
  return theme('book_node_export_html', array('node' => $node, 'children' => $children));
1286
}
1287

    
1288
/**
1289
 * Processes variables for book-node-export-html.tpl.php.
1290
 *
1291
 * @param $variables
1292
 *   An associative array containing the following keys:
1293
 *   - node
1294
 *   - children
1295
 *
1296
 * @see book-node-export-html.tpl.php
1297
 */
1298
function template_preprocess_book_node_export_html(&$variables) {
1299
  $variables['depth'] = $variables['node']->book['depth'];
1300
  $variables['title'] = check_plain($variables['node']->title);
1301
  $variables['content'] = $variables['node']->rendered;
1302
}
1303

    
1304
/**
1305
 * Determine if a given node type is in the list of types allowed for books.
1306
 *
1307
 * @param $type
1308
 *   A node type.
1309
 *
1310
 * @return
1311
 *   A Boolean TRUE if the node type can be included in books; otherwise, FALSE.
1312
 */
1313
function book_type_is_allowed($type) {
1314
  return in_array($type, variable_get('book_allowed_types', array('book')));
1315
}
1316

    
1317
/**
1318
 * Implements hook_node_type_update().
1319
 *
1320
 * Updates the Book module's persistent variables if the machine-readable name
1321
 * of a node type is changed.
1322
 */
1323
function book_node_type_update($type) {
1324
  if (!empty($type->old_type) && $type->old_type != $type->type) {
1325
    // Update the list of node types that are allowed to be added to books.
1326
    $allowed_types = variable_get('book_allowed_types', array('book'));
1327
    $key = array_search($type->old_type, $allowed_types);
1328

    
1329
    if ($key !== FALSE) {
1330
      $allowed_types[$type->type] = $allowed_types[$key] ? $type->type : 0;
1331
      unset($allowed_types[$key]);
1332
      variable_set('book_allowed_types', $allowed_types);
1333
    }
1334

    
1335
    // Update the setting for the "Add child page" link.
1336
    if (variable_get('book_child_type', 'book') == $type->old_type) {
1337
      variable_set('book_child_type', $type->type);
1338
    }
1339
  }
1340
}
1341

    
1342
/**
1343
 * Gets a book menu link by its menu link ID.
1344
 *
1345
 * Like menu_link_load(), but adds additional data from the {book} table.
1346
 *
1347
 * Do not call when loading a node, since this function may call node_load().
1348
 *
1349
 * @param $mlid
1350
 *   The menu link ID of the menu item.
1351
 *
1352
 * @return
1353
 *   A menu link, with the link translated for rendering and data added from the
1354
 *   {book} table. FALSE if there is an error.
1355
 */
1356
function book_link_load($mlid) {
1357
  if ($item = db_query("SELECT * FROM {menu_links} ml INNER JOIN {book} b ON b.mlid = ml.mlid LEFT JOIN {menu_router} m ON m.path = ml.router_path WHERE ml.mlid = :mlid", array(
1358
      ':mlid' => $mlid,
1359
    ))->fetchAssoc()) {
1360
    _menu_link_translate($item);
1361
    return $item;
1362
  }
1363

    
1364
  return FALSE;
1365
}
1366

    
1367
/**
1368
 * Gets the data representing a subtree of the book hierarchy.
1369
 *
1370
 * The root of the subtree will be the link passed as a parameter, so the
1371
 * returned tree will contain this item and all its descendents in the menu
1372
 * tree.
1373
 *
1374
 * @param $link
1375
 *   A fully loaded menu link.
1376
 *
1377
 * @return
1378
 *   A subtree of menu links in an array, in the order they should be rendered.
1379
 */
1380
function book_menu_subtree_data($link) {
1381
  $tree = &drupal_static(__FUNCTION__, array());
1382

    
1383
  // Generate a cache ID (cid) specific for this $menu_name and $link.
1384
  $cid = 'links:' . $link['menu_name'] . ':subtree-cid:' . $link['mlid'];
1385

    
1386
  if (!isset($tree[$cid])) {
1387
    $cache = cache_get($cid, 'cache_menu');
1388

    
1389
    if ($cache && isset($cache->data)) {
1390
      // If the cache entry exists, it will just be the cid for the actual data.
1391
      // This avoids duplication of large amounts of data.
1392
      $cache = cache_get($cache->data, 'cache_menu');
1393

    
1394
      if ($cache && isset($cache->data)) {
1395
        $data = $cache->data;
1396
      }
1397
    }
1398

    
1399
    // If the subtree data was not in the cache, $data will be NULL.
1400
    if (!isset($data)) {
1401
      $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
1402
      $query->join('menu_router', 'm', 'm.path = ml.router_path');
1403
      $query->join('book', 'b', 'ml.mlid = b.mlid');
1404
      $query->fields('b');
1405
      $query->fields('m', array('load_functions', 'to_arg_functions', 'access_callback', 'access_arguments', 'page_callback', 'page_arguments', 'delivery_callback', 'title', 'title_callback', 'title_arguments', 'type'));
1406
      $query->fields('ml');
1407
      $query->condition('menu_name', $link['menu_name']);
1408
      for ($i = 1; $i <= MENU_MAX_DEPTH && $link["p$i"]; ++$i) {
1409
        $query->condition("p$i", $link["p$i"]);
1410
      }
1411
      for ($i = 1; $i <= MENU_MAX_DEPTH; ++$i) {
1412
        $query->orderBy("p$i");
1413
      }
1414
      $links = array();
1415
      foreach ($query->execute() as $item) {
1416
        $links[] = $item;
1417
      }
1418
      $data['tree'] = menu_tree_data($links, array(), $link['depth']);
1419
      $data['node_links'] = array();
1420
      menu_tree_collect_node_links($data['tree'], $data['node_links']);
1421
      // Compute the real cid for book subtree data.
1422
      $tree_cid = 'links:' . $item['menu_name'] . ':subtree-data:' . hash('sha256', serialize($data));
1423
      // Cache the data, if it is not already in the cache.
1424

    
1425
      if (!cache_get($tree_cid, 'cache_menu')) {
1426
        cache_set($tree_cid, $data, 'cache_menu');
1427
      }
1428
      // Cache the cid of the (shared) data using the menu and item-specific cid.
1429
      cache_set($cid, $tree_cid, 'cache_menu');
1430
    }
1431
    // Check access for the current user to each item in the tree.
1432
    menu_tree_check_access($data['tree'], $data['node_links']);
1433
    $tree[$cid] = $data['tree'];
1434
  }
1435

    
1436
  return $tree[$cid];
1437
}