book.module 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. <?php
  2. /**
  3. * @file
  4. * Allows users to create and organize related content in an outline.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function book_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#book':
  12. $output = '<h3>' . t('About') . '</h3>';
  13. $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>';
  14. $output .= '<h3>' . t('Uses') . '</h3>';
  15. $output .= '<dl>';
  16. $output .= '<dt>' . t('Adding and managing book content') . '</dt>';
  17. $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>';
  18. $output .= '<dt>' . t('Book navigation') . '</dt>';
  19. $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>';
  20. $output .= '<dt>' . t('Collaboration') . '</dt>';
  21. $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>';
  22. $output .= '<dt>' . t('Printing books') . '</dt>';
  23. $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>';
  24. $output .= '</dl>';
  25. return $output;
  26. case 'admin/content/book':
  27. 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>';
  28. case 'node/%/outline':
  29. 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>';
  30. }
  31. }
  32. /**
  33. * Implements hook_theme().
  34. */
  35. function book_theme() {
  36. return array(
  37. 'book_navigation' => array(
  38. 'variables' => array('book_link' => NULL),
  39. 'template' => 'book-navigation',
  40. ),
  41. 'book_export_html' => array(
  42. 'variables' => array('title' => NULL, 'contents' => NULL, 'depth' => NULL),
  43. 'template' => 'book-export-html',
  44. ),
  45. 'book_admin_table' => array(
  46. 'render element' => 'form',
  47. ),
  48. 'book_title_link' => array(
  49. 'variables' => array('link' => NULL),
  50. ),
  51. 'book_all_books_block' => array(
  52. 'render element' => 'book_menus',
  53. 'template' => 'book-all-books-block',
  54. ),
  55. 'book_node_export_html' => array(
  56. 'variables' => array('node' => NULL, 'children' => NULL),
  57. 'template' => 'book-node-export-html',
  58. ),
  59. );
  60. }
  61. /**
  62. * Implements hook_permission().
  63. */
  64. function book_permission() {
  65. return array(
  66. 'administer book outlines' => array(
  67. 'title' => t('Administer book outlines'),
  68. ),
  69. 'create new books' => array(
  70. 'title' => t('Create new books'),
  71. ),
  72. 'add content to books' => array(
  73. 'title' => t('Add content and child pages to books'),
  74. ),
  75. 'access printer-friendly version' => array(
  76. 'title' => t('View printer-friendly books'),
  77. 'description' => t('View a book page and all of its sub-pages as a single document for ease of printing. Can be performance heavy.'),
  78. ),
  79. );
  80. }
  81. /**
  82. * Adds relevant book links to the node's links.
  83. *
  84. * @param $node
  85. * The book page node to add links to.
  86. * @param $view_mode
  87. * The view mode of the node.
  88. */
  89. function book_node_view_link($node, $view_mode) {
  90. $links = array();
  91. if (isset($node->book['depth'])) {
  92. if ($view_mode == 'full' && node_is_page($node)) {
  93. $child_type = variable_get('book_child_type', 'book');
  94. 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) {
  95. $links['book_add_child'] = array(
  96. 'title' => t('Add child page'),
  97. 'href' => 'node/add/' . str_replace('_', '-', $child_type),
  98. 'query' => array('parent' => $node->book['mlid']),
  99. );
  100. }
  101. if (user_access('access printer-friendly version')) {
  102. $links['book_printer'] = array(
  103. 'title' => t('Printer-friendly version'),
  104. 'href' => 'book/export/html/' . $node->nid,
  105. 'attributes' => array('title' => t('Show a printer-friendly version of this book page and its sub-pages.'))
  106. );
  107. }
  108. }
  109. }
  110. if (!empty($links)) {
  111. $node->content['links']['book'] = array(
  112. '#theme' => 'links__node__book',
  113. '#links' => $links,
  114. '#attributes' => array('class' => array('links', 'inline')),
  115. );
  116. }
  117. }
  118. /**
  119. * Implements hook_menu().
  120. */
  121. function book_menu() {
  122. $items['admin/content/book'] = array(
  123. 'title' => 'Books',
  124. 'description' => "Manage your site's book outlines.",
  125. 'page callback' => 'book_admin_overview',
  126. 'access arguments' => array('administer book outlines'),
  127. 'type' => MENU_LOCAL_TASK,
  128. 'file' => 'book.admin.inc',
  129. );
  130. $items['admin/content/book/list'] = array(
  131. 'title' => 'List',
  132. 'type' => MENU_DEFAULT_LOCAL_TASK,
  133. );
  134. $items['admin/content/book/settings'] = array(
  135. 'title' => 'Settings',
  136. 'page callback' => 'drupal_get_form',
  137. 'page arguments' => array('book_admin_settings'),
  138. 'access arguments' => array('administer site configuration'),
  139. 'type' => MENU_LOCAL_TASK,
  140. 'weight' => 8,
  141. 'file' => 'book.admin.inc',
  142. );
  143. $items['admin/content/book/%node'] = array(
  144. 'title' => 'Re-order book pages and change titles',
  145. 'page callback' => 'drupal_get_form',
  146. 'page arguments' => array('book_admin_edit', 3),
  147. 'access callback' => '_book_outline_access',
  148. 'access arguments' => array(3),
  149. 'type' => MENU_CALLBACK,
  150. 'file' => 'book.admin.inc',
  151. );
  152. $items['book'] = array(
  153. 'title' => 'Books',
  154. 'page callback' => 'book_render',
  155. 'access arguments' => array('access content'),
  156. 'type' => MENU_SUGGESTED_ITEM,
  157. 'file' => 'book.pages.inc',
  158. );
  159. $items['book/export/%/%'] = array(
  160. 'page callback' => 'book_export',
  161. 'page arguments' => array(2, 3),
  162. 'access arguments' => array('access printer-friendly version'),
  163. 'type' => MENU_CALLBACK,
  164. 'file' => 'book.pages.inc',
  165. );
  166. $items['node/%node/outline'] = array(
  167. 'title' => 'Outline',
  168. 'page callback' => 'book_outline',
  169. 'page arguments' => array(1),
  170. 'access callback' => '_book_outline_access',
  171. 'access arguments' => array(1),
  172. 'type' => MENU_LOCAL_TASK,
  173. 'weight' => 2,
  174. 'file' => 'book.pages.inc',
  175. );
  176. $items['node/%node/outline/remove'] = array(
  177. 'title' => 'Remove from outline',
  178. 'page callback' => 'drupal_get_form',
  179. 'page arguments' => array('book_remove_form', 1),
  180. 'access callback' => '_book_outline_remove_access',
  181. 'access arguments' => array(1),
  182. 'file' => 'book.pages.inc',
  183. );
  184. return $items;
  185. }
  186. /**
  187. * Access callback: Determines if the outline tab is accessible.
  188. *
  189. * @param $node
  190. * The node whose outline tab is to be viewed.
  191. */
  192. function _book_outline_access($node) {
  193. return user_access('administer book outlines') && node_access('view', $node);
  194. }
  195. /**
  196. * Access callback: Determines if the user can remove nodes from the outline.
  197. *
  198. * @param $node
  199. * The node to remove from the outline.
  200. *
  201. * @see book_menu()
  202. */
  203. function _book_outline_remove_access($node) {
  204. return _book_node_is_removable($node) && _book_outline_access($node);
  205. }
  206. /**
  207. * Determines if a node can be removed from the book.
  208. *
  209. * A node can be removed from a book if it is actually in a book and it either
  210. * is not a top-level page or is a top-level page with no children.
  211. */
  212. function _book_node_is_removable($node) {
  213. return (!empty($node->book['bid']) && (($node->book['bid'] != $node->nid) || !$node->book['has_children']));
  214. }
  215. /**
  216. * Implements hook_admin_paths().
  217. */
  218. function book_admin_paths() {
  219. if (variable_get('node_admin_theme')) {
  220. $paths = array(
  221. 'node/*/outline' => TRUE,
  222. 'node/*/outline/remove' => TRUE,
  223. );
  224. return $paths;
  225. }
  226. }
  227. /**
  228. * Implements hook_entity_info_alter().
  229. */
  230. function book_entity_info_alter(&$info) {
  231. // Add the 'Print' view mode for nodes.
  232. $info['node']['view modes'] += array(
  233. 'print' => array(
  234. 'label' => t('Print'),
  235. 'custom settings' => FALSE,
  236. ),
  237. );
  238. }
  239. /**
  240. * Implements hook_block_info().
  241. */
  242. function book_block_info() {
  243. $block = array();
  244. $block['navigation']['info'] = t('Book navigation');
  245. $block['navigation']['cache'] = DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE;
  246. return $block;
  247. }
  248. /**
  249. * Implements hook_block_view().
  250. *
  251. * Displays the book table of contents in a block when the current page is a
  252. * single-node view of a book node.
  253. */
  254. function book_block_view($delta = '') {
  255. $block = array();
  256. $current_bid = 0;
  257. if ($node = menu_get_object()) {
  258. $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
  259. }
  260. if (variable_get('book_block_mode', 'all pages') == 'all pages') {
  261. $block['subject'] = t('Book navigation');
  262. $book_menus = array();
  263. $pseudo_tree = array(0 => array('below' => FALSE));
  264. foreach (book_get_books() as $book_id => $book) {
  265. if ($book['bid'] == $current_bid) {
  266. // If the current page is a node associated with a book, the menu
  267. // needs to be retrieved.
  268. $book_menus[$book_id] = menu_tree_output(menu_tree_all_data($node->book['menu_name'], $node->book));
  269. }
  270. else {
  271. // Since we know we will only display a link to the top node, there
  272. // is no reason to run an additional menu tree query for each book.
  273. $book['in_active_trail'] = FALSE;
  274. // Check whether user can access the book link.
  275. $book_node = node_load($book['nid']);
  276. $book['access'] = node_access('view', $book_node);
  277. $pseudo_tree[0]['link'] = $book;
  278. $book_menus[$book_id] = menu_tree_output($pseudo_tree);
  279. }
  280. }
  281. $book_menus['#theme'] = 'book_all_books_block';
  282. $block['content'] = $book_menus;
  283. }
  284. elseif ($current_bid) {
  285. // Only display this block when the user is browsing a book.
  286. $select = db_select('node', 'n')
  287. ->fields('n', array('title'))
  288. ->condition('n.nid', $node->book['bid'])
  289. ->addTag('node_access');
  290. $title = $select->execute()->fetchField();
  291. // Only show the block if the user has view access for the top-level node.
  292. if ($title) {
  293. $tree = menu_tree_all_data($node->book['menu_name'], $node->book);
  294. // There should only be one element at the top level.
  295. $data = array_shift($tree);
  296. $block['subject'] = theme('book_title_link', array('link' => $data['link']));
  297. $block['content'] = ($data['below']) ? menu_tree_output($data['below']) : '';
  298. }
  299. }
  300. return $block;
  301. }
  302. /**
  303. * Implements hook_block_configure().
  304. */
  305. function book_block_configure($delta = '') {
  306. $block = array();
  307. $options = array(
  308. 'all pages' => t('Show block on all pages'),
  309. 'book pages' => t('Show block only on book pages'),
  310. );
  311. $form['book_block_mode'] = array(
  312. '#type' => 'radios',
  313. '#title' => t('Book navigation block display'),
  314. '#options' => $options,
  315. '#default_value' => variable_get('book_block_mode', 'all pages'),
  316. '#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."),
  317. );
  318. return $form;
  319. }
  320. /**
  321. * Implements hook_block_save().
  322. */
  323. function book_block_save($delta = '', $edit = array()) {
  324. $block = array();
  325. variable_set('book_block_mode', $edit['book_block_mode']);
  326. }
  327. /**
  328. * Returns HTML for a link to a book title when used as a block title.
  329. *
  330. * @param $variables
  331. * An associative array containing:
  332. * - link: An array containing title, href and options for the link.
  333. *
  334. * @ingroup themeable
  335. */
  336. function theme_book_title_link($variables) {
  337. $link = $variables['link'];
  338. $link['options']['attributes']['class'] = array('book-title');
  339. return l($link['title'], $link['href'], $link['options']);
  340. }
  341. /**
  342. * Returns an array of all books.
  343. *
  344. * This list may be used for generating a list of all the books, or for building
  345. * the options for a form select.
  346. *
  347. * @return
  348. * An array of all books.
  349. */
  350. function book_get_books() {
  351. $all_books = &drupal_static(__FUNCTION__);
  352. if (!isset($all_books)) {
  353. $all_books = array();
  354. $nids = db_query("SELECT DISTINCT(bid) FROM {book}")->fetchCol();
  355. if ($nids) {
  356. $query = db_select('book', 'b', array('fetch' => PDO::FETCH_ASSOC));
  357. $query->join('node', 'n', 'b.nid = n.nid');
  358. $query->join('menu_links', 'ml', 'b.mlid = ml.mlid');
  359. $query->addField('n', 'type', 'type');
  360. $query->addField('n', 'title', 'title');
  361. $query->fields('b');
  362. $query->fields('ml');
  363. $query->condition('n.nid', $nids, 'IN');
  364. $query->condition('n.status', 1);
  365. $query->orderBy('ml.weight');
  366. $query->orderBy('ml.link_title');
  367. $query->addTag('node_access');
  368. $result2 = $query->execute();
  369. foreach ($result2 as $link) {
  370. $link['href'] = $link['link_path'];
  371. $link['options'] = unserialize($link['options']);
  372. $all_books[$link['bid']] = $link;
  373. }
  374. }
  375. }
  376. return $all_books;
  377. }
  378. /**
  379. * Implements hook_form_BASE_FORM_ID_alter() for node_form().
  380. *
  381. * Adds the book fieldset to the node form.
  382. *
  383. * @see book_pick_book_nojs_submit()
  384. */
  385. function book_form_node_form_alter(&$form, &$form_state, $form_id) {
  386. $node = $form['#node'];
  387. $access = user_access('administer book outlines');
  388. if (!$access) {
  389. if (user_access('add content to books') && ((!empty($node->book['mlid']) && !empty($node->nid)) || book_type_is_allowed($node->type))) {
  390. // Already in the book hierarchy, or this node type is allowed.
  391. $access = TRUE;
  392. }
  393. }
  394. if ($access) {
  395. _book_add_form_elements($form, $form_state, $node);
  396. // Since the "Book" dropdown can't trigger a form submission when
  397. // JavaScript is disabled, add a submit button to do that. book.css hides
  398. // this button when JavaScript is enabled.
  399. $form['book']['pick-book'] = array(
  400. '#type' => 'submit',
  401. '#value' => t('Change book (update list of parents)'),
  402. '#submit' => array('book_pick_book_nojs_submit'),
  403. '#weight' => 20,
  404. );
  405. }
  406. }
  407. /**
  408. * Form submission handler for node_form().
  409. *
  410. * This handler is run when JavaScript is disabled. It triggers the form to
  411. * rebuild so that the "Parent item" options are changed to reflect the newly
  412. * selected book. When JavaScript is enabled, the submit button that triggers
  413. * this handler is hidden, and the "Book" dropdown directly triggers the
  414. * book_form_update() Ajax callback instead.
  415. *
  416. * @see book_form_update()
  417. * @see book_form_node_form_alter()
  418. */
  419. function book_pick_book_nojs_submit($form, &$form_state) {
  420. $form_state['node']->book = $form_state['values']['book'];
  421. $form_state['rebuild'] = TRUE;
  422. }
  423. /**
  424. * Builds the parent selection form element for the node form or outline tab.
  425. *
  426. * This function is also called when generating a new set of options during the
  427. * Ajax callback, so an array is returned that can be used to replace an
  428. * existing form element.
  429. *
  430. * @param $book_link
  431. * A fully loaded menu link that is part of the book hierarchy.
  432. *
  433. * @return
  434. * A parent selection form element.
  435. */
  436. function _book_parent_select($book_link) {
  437. if (variable_get('menu_override_parent_selector', FALSE)) {
  438. return array();
  439. }
  440. // Offer a message or a drop-down to choose a different parent page.
  441. $form = array(
  442. '#type' => 'hidden',
  443. '#value' => -1,
  444. '#prefix' => '<div id="edit-book-plid-wrapper">',
  445. '#suffix' => '</div>',
  446. );
  447. if ($book_link['nid'] === $book_link['bid']) {
  448. // This is a book - at the top level.
  449. if ($book_link['original_bid'] === $book_link['bid']) {
  450. $form['#prefix'] .= '<em>' . t('This is the top-level page in this book.') . '</em>';
  451. }
  452. else {
  453. $form['#prefix'] .= '<em>' . t('This will be the top-level page in this book.') . '</em>';
  454. }
  455. }
  456. elseif (!$book_link['bid']) {
  457. $form['#prefix'] .= '<em>' . t('No book selected.') . '</em>';
  458. }
  459. else {
  460. $form = array(
  461. '#type' => 'select',
  462. '#title' => t('Parent item'),
  463. '#default_value' => $book_link['plid'],
  464. '#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)),
  465. '#options' => book_toc($book_link['bid'], $book_link['parent_depth_limit'], array($book_link['mlid'])),
  466. '#attributes' => array('class' => array('book-title-select')),
  467. '#prefix' => '<div id="edit-book-plid-wrapper">',
  468. '#suffix' => '</div>',
  469. );
  470. }
  471. return $form;
  472. }
  473. /**
  474. * Builds the common elements of the book form for the node and outline forms.
  475. *
  476. * @param $node
  477. * The node whose form is being viewed.
  478. */
  479. function _book_add_form_elements(&$form, &$form_state, $node) {
  480. // If the form is being processed during the Ajax callback of our book bid
  481. // dropdown, then $form_state will hold the value that was selected.
  482. if (isset($form_state['values']['book'])) {
  483. $node->book = $form_state['values']['book'];
  484. }
  485. $form['book'] = array(
  486. '#type' => 'fieldset',
  487. '#title' => t('Book outline'),
  488. '#weight' => 10,
  489. '#collapsible' => TRUE,
  490. '#collapsed' => TRUE,
  491. '#group' => 'additional_settings',
  492. '#attributes' => array(
  493. 'class' => array('book-outline-form'),
  494. ),
  495. '#attached' => array(
  496. 'js' => array(drupal_get_path('module', 'book') . '/book.js'),
  497. ),
  498. '#tree' => TRUE,
  499. );
  500. foreach (array('menu_name', 'mlid', 'nid', 'router_path', 'has_children', 'options', 'module', 'original_bid', 'parent_depth_limit') as $key) {
  501. $form['book'][$key] = array(
  502. '#type' => 'value',
  503. '#value' => $node->book[$key],
  504. );
  505. }
  506. $form['book']['plid'] = _book_parent_select($node->book);
  507. // @see _book_admin_table_tree(). The weight may be larger than 15.
  508. $form['book']['weight'] = array(
  509. '#type' => 'weight',
  510. '#title' => t('Weight'),
  511. '#default_value' => $node->book['weight'],
  512. '#delta' => max(15, abs($node->book['weight'])),
  513. '#weight' => 5,
  514. '#description' => t('Pages at a given level are ordered first by weight and then by title.'),
  515. );
  516. $options = array();
  517. $nid = isset($node->nid) ? $node->nid : 'new';
  518. if (isset($node->nid) && ($nid == $node->book['original_bid']) && ($node->book['parent_depth_limit'] == 0)) {
  519. // This is the top level node in a maximum depth book and thus cannot be moved.
  520. $options[$node->nid] = $node->title;
  521. }
  522. else {
  523. foreach (book_get_books() as $book) {
  524. $options[$book['nid']] = $book['title'];
  525. }
  526. }
  527. if (user_access('create new books') && ($nid == 'new' || ($nid != $node->book['original_bid']))) {
  528. // The node can become a new book, if it is not one already.
  529. $options = array($nid => '<' . t('create a new book') . '>') + $options;
  530. }
  531. if (!$node->book['mlid']) {
  532. // The node is not currently in the hierarchy.
  533. $options = array(0 => '<' . t('none') . '>') + $options;
  534. }
  535. // Add a drop-down to select the destination book.
  536. $form['book']['bid'] = array(
  537. '#type' => 'select',
  538. '#title' => t('Book'),
  539. '#default_value' => $node->book['bid'],
  540. '#options' => $options,
  541. '#access' => (bool) $options,
  542. '#description' => t('Your page will be a part of the selected book.'),
  543. '#weight' => -5,
  544. '#attributes' => array('class' => array('book-title-select')),
  545. '#ajax' => array(
  546. 'callback' => 'book_form_update',
  547. 'wrapper' => 'edit-book-plid-wrapper',
  548. 'effect' => 'fade',
  549. 'speed' => 'fast',
  550. ),
  551. );
  552. }
  553. /**
  554. * Renders a new parent page select element when the book selection changes.
  555. *
  556. * This function is called via Ajax when the selected book is changed on a node
  557. * or book outline form.
  558. *
  559. * @return
  560. * The rendered parent page select element.
  561. */
  562. function book_form_update($form, $form_state) {
  563. return $form['book']['plid'];
  564. }
  565. /**
  566. * Handles additions and updates to the book outline.
  567. *
  568. * This common helper function performs all additions and updates to the book
  569. * outline through node addition, node editing, node deletion, or the outline
  570. * tab.
  571. *
  572. * @param $node
  573. * The node that is being saved, added, deleted, or moved.
  574. *
  575. * @return
  576. * TRUE if the menu link was saved; FALSE otherwise.
  577. */
  578. function _book_update_outline($node) {
  579. if (empty($node->book['bid'])) {
  580. return FALSE;
  581. }
  582. $new = empty($node->book['mlid']);
  583. $node->book['link_path'] = 'node/' . $node->nid;
  584. $node->book['link_title'] = $node->title;
  585. $node->book['parent_mismatch'] = FALSE; // The normal case.
  586. if ($node->book['bid'] == $node->nid) {
  587. $node->book['plid'] = 0;
  588. $node->book['menu_name'] = book_menu_name($node->nid);
  589. }
  590. else {
  591. // Check in case the parent is not is this book; the book takes precedence.
  592. if (!empty($node->book['plid'])) {
  593. $parent = db_query("SELECT * FROM {book} WHERE mlid = :mlid", array(
  594. ':mlid' => $node->book['plid'],
  595. ))->fetchAssoc();
  596. }
  597. if (empty($node->book['plid']) || !$parent || $parent['bid'] != $node->book['bid']) {
  598. $node->book['plid'] = db_query("SELECT mlid FROM {book} WHERE nid = :nid", array(
  599. ':nid' => $node->book['bid'],
  600. ))->fetchField();
  601. $node->book['parent_mismatch'] = TRUE; // Likely when JS is disabled.
  602. }
  603. }
  604. if (menu_link_save($node->book)) {
  605. if ($new) {
  606. // Insert new.
  607. db_insert('book')
  608. ->fields(array(
  609. 'nid' => $node->nid,
  610. 'mlid' => $node->book['mlid'],
  611. 'bid' => $node->book['bid'],
  612. ))
  613. ->execute();
  614. // Reset the cache of stored books.
  615. drupal_static_reset('book_get_books');
  616. }
  617. else {
  618. if ($node->book['bid'] != db_query("SELECT bid FROM {book} WHERE nid = :nid", array(
  619. ':nid' => $node->nid,
  620. ))->fetchField()) {
  621. // Update the bid for this page and all children.
  622. book_update_bid($node->book);
  623. // Reset the cache of stored books.
  624. drupal_static_reset('book_get_books');
  625. }
  626. }
  627. return TRUE;
  628. }
  629. // Failed to save the menu link.
  630. return FALSE;
  631. }
  632. /**
  633. * Updates the book ID of a page and its children when it moves to a new book.
  634. *
  635. * @param $book_link
  636. * A fully loaded menu link that is part of the book hierarchy.
  637. */
  638. function book_update_bid($book_link) {
  639. $query = db_select('menu_links');
  640. $query->addField('menu_links', 'mlid');
  641. for ($i = 1; $i <= MENU_MAX_DEPTH && $book_link["p$i"]; $i++) {
  642. $query->condition("p$i", $book_link["p$i"]);
  643. }
  644. $mlids = $query->execute()->fetchCol();
  645. if ($mlids) {
  646. db_update('book')
  647. ->fields(array('bid' => $book_link['bid']))
  648. ->condition('mlid', $mlids, 'IN')
  649. ->execute();
  650. }
  651. }
  652. /**
  653. * Gets the book menu tree for a page and returns it as a linear array.
  654. *
  655. * @param $book_link
  656. * A fully loaded menu link that is part of the book hierarchy.
  657. *
  658. * @return
  659. * A linear array of menu links in the order that the links are shown in the
  660. * menu, so the previous and next pages are the elements before and after the
  661. * element corresponding to the current node. The children of the current node
  662. * (if any) will come immediately after it in the array, and links will only
  663. * be fetched as deep as one level deeper than $book_link.
  664. */
  665. function book_get_flat_menu($book_link) {
  666. $flat = &drupal_static(__FUNCTION__, array());
  667. if (!isset($flat[$book_link['mlid']])) {
  668. // Call menu_tree_all_data() to take advantage of the menu system's caching.
  669. $tree = menu_tree_all_data($book_link['menu_name'], $book_link, $book_link['depth'] + 1);
  670. $flat[$book_link['mlid']] = array();
  671. _book_flatten_menu($tree, $flat[$book_link['mlid']]);
  672. }
  673. return $flat[$book_link['mlid']];
  674. }
  675. /**
  676. * Recursively converts a tree of menu links to a flat array.
  677. *
  678. * @param $tree
  679. * A tree of menu links in an array.
  680. * @param $flat
  681. * A flat array of the menu links from $tree.
  682. *
  683. * @see book_get_flat_menu().
  684. */
  685. function _book_flatten_menu($tree, &$flat) {
  686. foreach ($tree as $data) {
  687. if (!$data['link']['hidden']) {
  688. $flat[$data['link']['mlid']] = $data['link'];
  689. if ($data['below']) {
  690. _book_flatten_menu($data['below'], $flat);
  691. }
  692. }
  693. }
  694. }
  695. /**
  696. * Fetches the menu link for the previous page of the book.
  697. *
  698. * @param $book_link
  699. * A fully loaded menu link that is part of the book hierarchy.
  700. *
  701. * @return
  702. * A fully loaded menu link for the page before the one represented in
  703. * $book_link.
  704. */
  705. function book_prev($book_link) {
  706. // If the parent is zero, we are at the start of a book.
  707. if ($book_link['plid'] == 0) {
  708. return NULL;
  709. }
  710. $flat = book_get_flat_menu($book_link);
  711. // Assigning the array to $flat resets the array pointer for use with each().
  712. $curr = NULL;
  713. do {
  714. $prev = $curr;
  715. list($key, $curr) = each($flat);
  716. } while ($key && $key != $book_link['mlid']);
  717. if ($key == $book_link['mlid']) {
  718. // The previous page in the book may be a child of the previous visible link.
  719. if ($prev['depth'] == $book_link['depth'] && $prev['has_children']) {
  720. // The subtree will have only one link at the top level - get its data.
  721. $tree = book_menu_subtree_data($prev);
  722. $data = array_shift($tree);
  723. // The link of interest is the last child - iterate to find the deepest one.
  724. while ($data['below']) {
  725. $data = end($data['below']);
  726. }
  727. return $data['link'];
  728. }
  729. else {
  730. return $prev;
  731. }
  732. }
  733. }
  734. /**
  735. * Fetches the menu link for the next page of the book.
  736. *
  737. * @param $book_link
  738. * A fully loaded menu link that is part of the book hierarchy.
  739. *
  740. * @return
  741. * A fully loaded menu link for the page after the one represented in
  742. * $book_link.
  743. */
  744. function book_next($book_link) {
  745. $flat = book_get_flat_menu($book_link);
  746. // Assigning the array to $flat resets the array pointer for use with each().
  747. do {
  748. list($key, $curr) = each($flat);
  749. }
  750. while ($key && $key != $book_link['mlid']);
  751. if ($key == $book_link['mlid']) {
  752. return current($flat);
  753. }
  754. }
  755. /**
  756. * Formats the menu links for the child pages of the current page.
  757. *
  758. * @param $book_link
  759. * A fully loaded menu link that is part of the book hierarchy.
  760. *
  761. * @return
  762. * HTML for the links to the child pages of the current page.
  763. */
  764. function book_children($book_link) {
  765. $flat = book_get_flat_menu($book_link);
  766. $children = array();
  767. if ($book_link['has_children']) {
  768. // Walk through the array until we find the current page.
  769. do {
  770. $link = array_shift($flat);
  771. }
  772. while ($link && ($link['mlid'] != $book_link['mlid']));
  773. // Continue though the array and collect the links whose parent is this page.
  774. while (($link = array_shift($flat)) && $link['plid'] == $book_link['mlid']) {
  775. $data['link'] = $link;
  776. $data['below'] = '';
  777. $children[] = $data;
  778. }
  779. }
  780. if ($children) {
  781. $elements = menu_tree_output($children);
  782. return drupal_render($elements);
  783. }
  784. return '';
  785. }
  786. /**
  787. * Generates the corresponding menu name from a book ID.
  788. *
  789. * @param $bid
  790. * The book ID for which to make a menu name.
  791. *
  792. * @return
  793. * The menu name.
  794. */
  795. function book_menu_name($bid) {
  796. return 'book-toc-' . $bid;
  797. }
  798. /**
  799. * Implements hook_node_load().
  800. */
  801. function book_node_load($nodes, $types) {
  802. $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));
  803. foreach ($result as $record) {
  804. $nodes[$record['nid']]->book = $record;
  805. $nodes[$record['nid']]->book['href'] = $record['link_path'];
  806. $nodes[$record['nid']]->book['title'] = $record['link_title'];
  807. $nodes[$record['nid']]->book['options'] = unserialize($record['options']);
  808. }
  809. }
  810. /**
  811. * Implements hook_node_view().
  812. */
  813. function book_node_view($node, $view_mode) {
  814. if ($view_mode == 'full') {
  815. if (!empty($node->book['bid']) && empty($node->in_preview)) {
  816. $node->content['book_navigation'] = array(
  817. '#markup' => theme('book_navigation', array('book_link' => $node->book)),
  818. '#weight' => 100,
  819. );
  820. }
  821. }
  822. if ($view_mode != 'rss') {
  823. book_node_view_link($node, $view_mode);
  824. }
  825. }
  826. /**
  827. * Implements hook_page_alter().
  828. *
  829. * Adds the book menu to the list of menus used to build the active trail when
  830. * viewing a book page.
  831. */
  832. function book_page_alter(&$page) {
  833. if (($node = menu_get_object()) && !empty($node->book['bid'])) {
  834. $active_menus = menu_get_active_menu_names();
  835. $active_menus[] = $node->book['menu_name'];
  836. menu_set_active_menu_names($active_menus);
  837. }
  838. }
  839. /**
  840. * Implements hook_node_presave().
  841. */
  842. function book_node_presave($node) {
  843. // Always save a revision for non-administrators.
  844. if (!empty($node->book['bid']) && !user_access('administer nodes')) {
  845. $node->revision = 1;
  846. // The database schema requires a log message for every revision.
  847. if (!isset($node->log)) {
  848. $node->log = '';
  849. }
  850. }
  851. // Make sure a new node gets a new menu link.
  852. if (empty($node->nid)) {
  853. $node->book['mlid'] = NULL;
  854. }
  855. }
  856. /**
  857. * Implements hook_node_insert().
  858. */
  859. function book_node_insert($node) {
  860. if (!empty($node->book['bid'])) {
  861. if ($node->book['bid'] == 'new') {
  862. // New nodes that are their own book.
  863. $node->book['bid'] = $node->nid;
  864. }
  865. $node->book['nid'] = $node->nid;
  866. $node->book['menu_name'] = book_menu_name($node->book['bid']);
  867. _book_update_outline($node);
  868. }
  869. }
  870. /**
  871. * Implements hook_node_update().
  872. */
  873. function book_node_update($node) {
  874. if (!empty($node->book['bid'])) {
  875. if ($node->book['bid'] == 'new') {
  876. // New nodes that are their own book.
  877. $node->book['bid'] = $node->nid;
  878. }
  879. $node->book['nid'] = $node->nid;
  880. $node->book['menu_name'] = book_menu_name($node->book['bid']);
  881. _book_update_outline($node);
  882. }
  883. }
  884. /**
  885. * Implements hook_node_delete().
  886. */
  887. function book_node_delete($node) {
  888. if (!empty($node->book['bid'])) {
  889. if ($node->nid == $node->book['bid']) {
  890. // Handle deletion of a top-level post.
  891. $result = db_query("SELECT b.nid FROM {menu_links} ml INNER JOIN {book} b on b.mlid = ml.mlid WHERE ml.plid = :plid", array(
  892. ':plid' => $node->book['mlid']
  893. ));
  894. foreach ($result as $child) {
  895. $child_node = node_load($child->nid);
  896. $child_node->book['bid'] = $child_node->nid;
  897. _book_update_outline($child_node);
  898. }
  899. }
  900. menu_link_delete($node->book['mlid']);
  901. db_delete('book')
  902. ->condition('mlid', $node->book['mlid'])
  903. ->execute();
  904. drupal_static_reset('book_get_books');
  905. }
  906. }
  907. /**
  908. * Implements hook_node_prepare().
  909. */
  910. function book_node_prepare($node) {
  911. // Prepare defaults for the add/edit form.
  912. if (empty($node->book) && (user_access('add content to books') || user_access('administer book outlines'))) {
  913. $node->book = array();
  914. if (empty($node->nid) && isset($_GET['parent']) && is_numeric($_GET['parent'])) {
  915. // Handle "Add child page" links:
  916. $parent = book_link_load($_GET['parent']);
  917. if ($parent && $parent['access']) {
  918. $node->book['bid'] = $parent['bid'];
  919. $node->book['plid'] = $parent['mlid'];
  920. $node->book['menu_name'] = $parent['menu_name'];
  921. }
  922. }
  923. // Set defaults.
  924. $node->book += _book_link_defaults(!empty($node->nid) ? $node->nid : 'new');
  925. }
  926. else {
  927. if (isset($node->book['bid']) && !isset($node->book['original_bid'])) {
  928. $node->book['original_bid'] = $node->book['bid'];
  929. }
  930. }
  931. // Find the depth limit for the parent select.
  932. if (isset($node->book['bid']) && !isset($node->book['parent_depth_limit'])) {
  933. $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book);
  934. }
  935. }
  936. /**
  937. * Finds the depth limit for items in the parent select.
  938. *
  939. * @param $book_link
  940. * A fully loaded menu link that is part of the book hierarchy.
  941. *
  942. * @return
  943. * The depth limit for items in the parent select.
  944. */
  945. function _book_parent_depth_limit($book_link) {
  946. return MENU_MAX_DEPTH - 1 - (($book_link['mlid'] && $book_link['has_children']) ? menu_link_children_relative_depth($book_link) : 0);
  947. }
  948. /**
  949. * Implements hook_form_FORM_ID_alter() for node_delete_confirm().
  950. *
  951. * Alters the confirm form for a single node deletion.
  952. *
  953. * @see node_delete_confirm()
  954. */
  955. function book_form_node_delete_confirm_alter(&$form, $form_state) {
  956. $node = node_load($form['nid']['#value']);
  957. if (isset($node->book) && $node->book['has_children']) {
  958. $form['book_warning'] = array(
  959. '#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>',
  960. '#weight' => -10,
  961. );
  962. }
  963. }
  964. /**
  965. * Returns an array with default values for a book page's menu link.
  966. *
  967. * @param $nid
  968. * The ID of the node whose menu link is being created.
  969. *
  970. * @return
  971. * The default values for the menu link.
  972. */
  973. function _book_link_defaults($nid) {
  974. 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());
  975. }
  976. /**
  977. * Processes variables for book-all-books-block.tpl.php.
  978. *
  979. * All non-renderable elements are removed so that the template has full access
  980. * to the structured data but can also simply iterate over all elements and
  981. * render them (as in the default template).
  982. *
  983. * The $variables array contains the following elements:
  984. * - book_menus
  985. *
  986. * @see book-all-books-block.tpl.php
  987. */
  988. function template_preprocess_book_all_books_block(&$variables) {
  989. // Remove all non-renderable elements.
  990. $elements = $variables['book_menus'];
  991. $variables['book_menus'] = array();
  992. foreach (element_children($elements) as $index) {
  993. $variables['book_menus'][$index] = $elements[$index];
  994. }
  995. }
  996. /**
  997. * Processes variables for book-navigation.tpl.php.
  998. *
  999. * The $variables array contains the following elements:
  1000. * - book_link
  1001. *
  1002. * @see book-navigation.tpl.php
  1003. */
  1004. function template_preprocess_book_navigation(&$variables) {
  1005. $book_link = $variables['book_link'];
  1006. // Provide extra variables for themers. Not needed by default.
  1007. $variables['book_id'] = $book_link['bid'];
  1008. $variables['book_title'] = check_plain($book_link['link_title']);
  1009. $variables['book_url'] = 'node/' . $book_link['bid'];
  1010. $variables['current_depth'] = $book_link['depth'];
  1011. $variables['tree'] = '';
  1012. if ($book_link['mlid']) {
  1013. $variables['tree'] = book_children($book_link);
  1014. if ($prev = book_prev($book_link)) {
  1015. $prev_href = url($prev['href']);
  1016. drupal_add_html_head_link(array('rel' => 'prev', 'href' => $prev_href));
  1017. $variables['prev_url'] = $prev_href;
  1018. $variables['prev_title'] = check_plain($prev['title']);
  1019. }
  1020. if ($book_link['plid'] && $parent = book_link_load($book_link['plid'])) {
  1021. $parent_href = url($parent['href']);
  1022. drupal_add_html_head_link(array('rel' => 'up', 'href' => $parent_href));
  1023. $variables['parent_url'] = $parent_href;
  1024. $variables['parent_title'] = check_plain($parent['title']);
  1025. }
  1026. if ($next = book_next($book_link)) {
  1027. $next_href = url($next['href']);
  1028. drupal_add_html_head_link(array('rel' => 'next', 'href' => $next_href));
  1029. $variables['next_url'] = $next_href;
  1030. $variables['next_title'] = check_plain($next['title']);
  1031. }
  1032. }
  1033. $variables['has_links'] = FALSE;
  1034. // Link variables to filter for values and set state of the flag variable.
  1035. $links = array('prev_url', 'prev_title', 'parent_url', 'parent_title', 'next_url', 'next_title');
  1036. foreach ($links as $link) {
  1037. if (isset($variables[$link])) {
  1038. // Flag when there is a value.
  1039. $variables['has_links'] = TRUE;
  1040. }
  1041. else {
  1042. // Set empty to prevent notices.
  1043. $variables[$link] = '';
  1044. }
  1045. }
  1046. }
  1047. /**
  1048. * Recursively processes and formats menu items for book_toc().
  1049. *
  1050. * This helper function recursively modifies the table of contents array for
  1051. * each item in the menu tree, ignoring items in the exclude array or at a depth
  1052. * greater than the limit. Truncates titles over thirty characters and appends
  1053. * an indentation string incremented by depth.
  1054. *
  1055. * @param $tree
  1056. * The data structure of the book's menu tree. Includes hidden links.
  1057. * @param $indent
  1058. * A string appended to each menu item title. Increments by '--' per depth
  1059. * level.
  1060. * @param $toc
  1061. * Reference to the table of contents array. This is modified in place, so the
  1062. * function does not have a return value.
  1063. * @param $exclude
  1064. * Optional array of menu link ID values. Any link whose menu link ID is in
  1065. * this array will be excluded (along with its children).
  1066. * @param $depth_limit
  1067. * Any link deeper than this value will be excluded (along with its children).
  1068. */
  1069. function _book_toc_recurse($tree, $indent, &$toc, $exclude, $depth_limit) {
  1070. foreach ($tree as $data) {
  1071. if ($data['link']['depth'] > $depth_limit) {
  1072. // Don't iterate through any links on this level.
  1073. break;
  1074. }
  1075. if (!in_array($data['link']['mlid'], $exclude)) {
  1076. $toc[$data['link']['mlid']] = $indent . ' ' . truncate_utf8($data['link']['title'], 30, TRUE, TRUE);
  1077. if ($data['below']) {
  1078. _book_toc_recurse($data['below'], $indent . '--', $toc, $exclude, $depth_limit);
  1079. }
  1080. }
  1081. }
  1082. }
  1083. /**
  1084. * Returns an array of book pages in table of contents order.
  1085. *
  1086. * @param $bid
  1087. * The ID of the book whose pages are to be listed.
  1088. * @param $depth_limit
  1089. * Any link deeper than this value will be excluded (along with its children).
  1090. * @param $exclude
  1091. * Optional array of menu link ID values. Any link whose menu link ID is in
  1092. * this array will be excluded (along with its children).
  1093. *
  1094. * @return
  1095. * An array of (menu link ID, title) pairs for use as options for selecting a
  1096. * book page.
  1097. */
  1098. function book_toc($bid, $depth_limit, $exclude = array()) {
  1099. $tree = menu_tree_all_data(book_menu_name($bid));
  1100. $toc = array();
  1101. _book_toc_recurse($tree, '', $toc, $exclude, $depth_limit);
  1102. return $toc;
  1103. }
  1104. /**
  1105. * Processes variables for book-export-html.tpl.php.
  1106. *
  1107. * The $variables array contains the following elements:
  1108. * - title
  1109. * - contents
  1110. * - depth
  1111. *
  1112. * @see book-export-html.tpl.php
  1113. */
  1114. function template_preprocess_book_export_html(&$variables) {
  1115. global $base_url, $language;
  1116. $variables['title'] = check_plain($variables['title']);
  1117. $variables['base_url'] = $base_url;
  1118. $variables['language'] = $language;
  1119. $variables['language_rtl'] = ($language->direction == LANGUAGE_RTL);
  1120. $variables['head'] = drupal_get_html_head();
  1121. $variables['dir'] = $language->direction ? 'rtl' : 'ltr';
  1122. }
  1123. /**
  1124. * Traverses the book tree to build printable or exportable output.
  1125. *
  1126. * During the traversal, the $visit_func() callback is applied to each node and
  1127. * is called recursively for each child of the node (in weight, title order).
  1128. *
  1129. * @param $tree
  1130. * A subtree of the book menu hierarchy, rooted at the current page.
  1131. * @param $visit_func
  1132. * A function callback to be called upon visiting a node in the tree.
  1133. *
  1134. * @return
  1135. * The output generated in visiting each node.
  1136. */
  1137. function book_export_traverse($tree, $visit_func) {
  1138. $output = '';
  1139. foreach ($tree as $data) {
  1140. // Note- access checking is already performed when building the tree.
  1141. if ($node = node_load($data['link']['nid'], FALSE)) {
  1142. $children = '';
  1143. if ($data['below']) {
  1144. $children = book_export_traverse($data['below'], $visit_func);
  1145. }
  1146. if (function_exists($visit_func)) {
  1147. $output .= call_user_func($visit_func, $node, $children);
  1148. }
  1149. else {
  1150. // Use the default function.
  1151. $output .= book_node_export($node, $children);
  1152. }
  1153. }
  1154. }
  1155. return $output;
  1156. }
  1157. /**
  1158. * Generates printer-friendly HTML for a node.
  1159. *
  1160. * @param $node
  1161. * The node that will be output.
  1162. * @param $children
  1163. * All the rendered child nodes within the current node.
  1164. *
  1165. * @return
  1166. * The HTML generated for the given node.
  1167. *
  1168. * @see book_export_traverse()
  1169. */
  1170. function book_node_export($node, $children = '') {
  1171. $build = node_view($node, 'print');
  1172. unset($build['#theme']);
  1173. // @todo Rendering should happen in the template using render().
  1174. $node->rendered = drupal_render($build);
  1175. return theme('book_node_export_html', array('node' => $node, 'children' => $children));
  1176. }
  1177. /**
  1178. * Processes variables for book-node-export-html.tpl.php.
  1179. *
  1180. * The $variables array contains the following elements:
  1181. * - node
  1182. * - children
  1183. *
  1184. * @see book-node-export-html.tpl.php
  1185. */
  1186. function template_preprocess_book_node_export_html(&$variables) {
  1187. $variables['depth'] = $variables['node']->book['depth'];
  1188. $variables['title'] = check_plain($variables['node']->title);
  1189. $variables['content'] = $variables['node']->rendered;
  1190. }
  1191. /**
  1192. * Determine if a given node type is in the list of types allowed for books.
  1193. */
  1194. function book_type_is_allowed($type) {
  1195. return in_array($type, variable_get('book_allowed_types', array('book')));
  1196. }
  1197. /**
  1198. * Implements hook_node_type_update().
  1199. *
  1200. * Updates the Book module's persistent variables if the machine-readable name
  1201. * of a node type is changed.
  1202. */
  1203. function book_node_type_update($type) {
  1204. if (!empty($type->old_type) && $type->old_type != $type->type) {
  1205. // Update the list of node types that are allowed to be added to books.
  1206. $allowed_types = variable_get('book_allowed_types', array('book'));
  1207. $key = array_search($type->old_type, $allowed_types);
  1208. if ($key !== FALSE) {
  1209. $allowed_types[$type->type] = $allowed_types[$key] ? $type->type : 0;
  1210. unset($allowed_types[$key]);
  1211. variable_set('book_allowed_types', $allowed_types);
  1212. }
  1213. // Update the setting for the "Add child page" link.
  1214. if (variable_get('book_child_type', 'book') == $type->old_type) {
  1215. variable_set('book_child_type', $type->type);
  1216. }
  1217. }
  1218. }
  1219. /**
  1220. * Gets a book menu link by its menu link ID.
  1221. *
  1222. * Like menu_link_load(), but adds additional data from the {book} table.
  1223. *
  1224. * Do not call when loading a node, since this function may call node_load().
  1225. *
  1226. * @param $mlid
  1227. * The menu link ID of the menu item.
  1228. *
  1229. * @return
  1230. * A menu link, with the link translated for rendering and data added from the
  1231. * {book} table.
  1232. */
  1233. function book_link_load($mlid) {
  1234. 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(
  1235. ':mlid' => $mlid,
  1236. ))->fetchAssoc()) {
  1237. _menu_link_translate($item);
  1238. return $item;
  1239. }
  1240. return FALSE;
  1241. }
  1242. /**
  1243. * Gets the data representing a subtree of the book hierarchy.
  1244. *
  1245. * The root of the subtree will be the link passed as a parameter, so the
  1246. * returned tree will contain this item and all its descendents in the menu
  1247. * tree.
  1248. *
  1249. * @param $link
  1250. * A fully loaded menu link.
  1251. *
  1252. * @return
  1253. * An subtree of menu links in an array, in the order they should be rendered.
  1254. */
  1255. function book_menu_subtree_data($link) {
  1256. $tree = &drupal_static(__FUNCTION__, array());
  1257. // Generate a cache ID (cid) specific for this $menu_name and $link.
  1258. $cid = 'links:' . $link['menu_name'] . ':subtree-cid:' . $link['mlid'];
  1259. if (!isset($tree[$cid])) {
  1260. $cache = cache_get($cid, 'cache_menu');
  1261. if ($cache && isset($cache->data)) {
  1262. // If the cache entry exists, it will just be the cid for the actual data.
  1263. // This avoids duplication of large amounts of data.
  1264. $cache = cache_get($cache->data, 'cache_menu');
  1265. if ($cache && isset($cache->data)) {
  1266. $data = $cache->data;
  1267. }
  1268. }
  1269. // If the subtree data was not in the cache, $data will be NULL.
  1270. if (!isset($data)) {
  1271. $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
  1272. $query->join('menu_router', 'm', 'm.path = ml.router_path');
  1273. $query->join('book', 'b', 'ml.mlid = b.mlid');
  1274. $query->fields('b');
  1275. $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'));
  1276. $query->fields('ml');
  1277. $query->condition('menu_name', $link['menu_name']);
  1278. for ($i = 1; $i <= MENU_MAX_DEPTH && $link["p$i"]; ++$i) {
  1279. $query->condition("p$i", $link["p$i"]);
  1280. }
  1281. for ($i = 1; $i <= MENU_MAX_DEPTH; ++$i) {
  1282. $query->orderBy("p$i");
  1283. }
  1284. $links = array();
  1285. foreach ($query->execute() as $item) {
  1286. $links[] = $item;
  1287. }
  1288. $data['tree'] = menu_tree_data($links, array(), $link['depth']);
  1289. $data['node_links'] = array();
  1290. menu_tree_collect_node_links($data['tree'], $data['node_links']);
  1291. // Compute the real cid for book subtree data.
  1292. $tree_cid = 'links:' . $item['menu_name'] . ':subtree-data:' . hash('sha256', serialize($data));
  1293. // Cache the data, if it is not already in the cache.
  1294. if (!cache_get($tree_cid, 'cache_menu')) {
  1295. cache_set($tree_cid, $data, 'cache_menu');
  1296. }
  1297. // Cache the cid of the (shared) data using the menu and item-specific cid.
  1298. cache_set($cid, $tree_cid, 'cache_menu');
  1299. }
  1300. // Check access for the current user to each item in the tree.
  1301. menu_tree_check_access($data['tree'], $data['node_links']);
  1302. $tree[$cid] = $data['tree'];
  1303. }
  1304. return $tree[$cid];
  1305. }