Projet

Général

Profil

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

root / drupal7 / modules / book / book.module @ c7768a53

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
  reset($flat);
772
  $curr = NULL;
773
  do {
774
    $prev = $curr;
775
    $curr = current($flat);
776
    $key = key($flat);
777
    next($flat);
778
  } while ($key && $key != $book_link['mlid']);
779

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

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

    
799
/**
800
 * Fetches the menu link for the next page of the book.
801
 *
802
 * @param $book_link
803
 *   A fully loaded menu link that is part of the book hierarchy.
804
 *
805
 * @return
806
 *   A fully loaded menu link for the page after the one represented in
807
 *   $book_link.
808
 */
809
function book_next($book_link) {
810
  $flat = book_get_flat_menu($book_link);
811
  reset($flat);
812
  do {
813
    $key = key($flat);
814
    next($flat);
815
  }
816
  while ($key && $key != $book_link['mlid']);
817

    
818
  if ($key == $book_link['mlid']) {
819
    return current($flat);
820
  }
821
}
822

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

    
835
  $children = array();
836

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

    
851
  if ($children) {
852
    $elements = menu_tree_output($children);
853
    return drupal_render($elements);
854
  }
855
  return '';
856
}
857

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

    
871
/**
872
 * Implements hook_node_load().
873
 */
874
function book_node_load($nodes, $types) {
875
  $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));
876
  foreach ($result as $record) {
877
    $nodes[$record['nid']]->book = $record;
878
    $nodes[$record['nid']]->book['href'] = $record['link_path'];
879
    $nodes[$record['nid']]->book['title'] = $record['link_title'];
880
    $nodes[$record['nid']]->book['options'] = unserialize($record['options']);
881
  }
882
}
883

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

    
897
  if ($view_mode != 'rss') {
898
    book_node_view_link($node, $view_mode);
899
  }
900
}
901

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

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

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

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

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

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

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

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

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

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

    
1043
  if (isset($node->book) && $node->book['has_children']) {
1044
    $form['book_warning'] = array(
1045
      '#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>',
1046
      '#weight' => -10,
1047
    );
1048
  }
1049
}
1050

    
1051
/**
1052
 * Returns an array with default values for a book page's menu link.
1053
 *
1054
 * @param $nid
1055
 *   The ID of the node whose menu link is being created.
1056
 *
1057
 * @return
1058
 *   The default values for the menu link.
1059
 */
1060
function _book_link_defaults($nid) {
1061
  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());
1062
}
1063

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

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

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

    
1105
  if ($book_link['mlid']) {
1106
    $variables['tree'] = book_children($book_link);
1107

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

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

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

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

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

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

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

    
1204
  return $toc;
1205
}
1206

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

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

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

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

    
1251
      if ($data['below']) {
1252
        $children = book_export_traverse($data['below'], $visit_func);
1253
      }
1254

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

    
1265
  return $output;
1266
}
1267

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

    
1288
  return theme('book_node_export_html', array('node' => $node, 'children' => $children));
1289
}
1290

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

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

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

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

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

    
1345
/**
1346
 * Gets a book menu link by its menu link ID.
1347
 *
1348
 * Like menu_link_load(), but adds additional data from the {book} table.
1349
 *
1350
 * Do not call when loading a node, since this function may call node_load().
1351
 *
1352
 * @param $mlid
1353
 *   The menu link ID of the menu item.
1354
 *
1355
 * @return
1356
 *   A menu link, with the link translated for rendering and data added from the
1357
 *   {book} table. FALSE if there is an error.
1358
 */
1359
function book_link_load($mlid) {
1360
  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(
1361
      ':mlid' => $mlid,
1362
    ))->fetchAssoc()) {
1363
    _menu_link_translate($item);
1364
    return $item;
1365
  }
1366

    
1367
  return FALSE;
1368
}
1369

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

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

    
1389
  if (!isset($tree[$cid])) {
1390
    $cache = cache_get($cid, 'cache_menu');
1391

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

    
1397
      if ($cache && isset($cache->data)) {
1398
        $data = $cache->data;
1399
      }
1400
    }
1401

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

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

    
1439
  return $tree[$cid];
1440
}