book.pages.inc

  1. 7.x drupal-7.x/modules/book/book.pages.inc
  2. 6.x drupal-6.x/modules/book/book.pages.inc

User page callbacks for the book module.

File

drupal-6.x/modules/book/book.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the book module.
  5. */
  6. /**
  7. * Menu callback; prints a listing of all books.
  8. */
  9. function book_render() {
  10. $book_list = array();
  11. foreach (book_get_books() as $book) {
  12. $book_list[] = l($book['title'], $book['href'], $book['options']);
  13. }
  14. return theme('item_list', $book_list);
  15. }
  16. /**
  17. * Menu callback; Generates various representation of a book page and its children.
  18. *
  19. * The function delegates the generation of output to helper functions.
  20. * The function name is derived by prepending 'book_export_' to the
  21. * given output type. So, e.g., a type of 'html' results in a call to
  22. * the function book_export_html().
  23. *
  24. * @param $type
  25. * A string encoding the type of output requested. The following
  26. * types are currently supported in book module:
  27. *
  28. * - html: HTML (printer friendly output)
  29. *
  30. * Other types may be supported in contributed modules.
  31. * @param $nid
  32. * An integer representing the node id (nid) of the node to export
  33. * @return
  34. * A string representing the node and its children in the book hierarchy
  35. * in a format determined by the $type parameter.
  36. */
  37. function book_export($type, $nid) {
  38. // Check that the node exists and that the current user has access to it.
  39. $node = node_load($nid);
  40. if (!$node) {
  41. return MENU_NOT_FOUND;
  42. }
  43. if (!node_access('view', $node)) {
  44. return MENU_ACCESS_DENIED;
  45. }
  46. $type = drupal_strtolower($type);
  47. $export_function = 'book_export_'. $type;
  48. if (function_exists($export_function)) {
  49. print call_user_func($export_function, $nid);
  50. }
  51. else {
  52. drupal_set_message(t('Unknown export format.'));
  53. drupal_not_found();
  54. }
  55. }
  56. /**
  57. * This function is called by book_export() to generate HTML for export.
  58. *
  59. * The given node is /embedded to its absolute depth in a top level
  60. * section/. For example, a child node with depth 2 in the hierarchy
  61. * is contained in (otherwise empty) &lt;div&gt; elements
  62. * corresponding to depth 0 and depth 1. This is intended to support
  63. * WYSIWYG output - e.g., level 3 sections always look like level 3
  64. * sections, no matter their depth relative to the node selected to be
  65. * exported as printer-friendly HTML.
  66. *
  67. * @param $nid
  68. * An integer representing the node id (nid) of the node to export.
  69. * @return
  70. * A string containing HTML representing the node and its children in
  71. * the book hierarchy.
  72. */
  73. function book_export_html($nid) {
  74. if (user_access('access printer-friendly version')) {
  75. $export_data = array();
  76. $node = node_load($nid);
  77. if (isset($node->book)) {
  78. $tree = book_menu_subtree_data($node->book);
  79. $contents = book_export_traverse($tree, 'book_node_export');
  80. return theme('book_export_html', $node->title, $contents, $node->book['depth']);
  81. }
  82. else {
  83. drupal_not_found();
  84. }
  85. }
  86. else {
  87. drupal_access_denied();
  88. }
  89. }
  90. /**
  91. * Menu callback; show the outline form for a single node.
  92. */
  93. function book_outline($node) {
  94. drupal_set_title(check_plain($node->title));
  95. return drupal_get_form('book_outline_form', $node);
  96. }
  97. /**
  98. * Build the form to handle all book outline operations via the outline tab.
  99. *
  100. * @see book_outline_form_submit()
  101. * @see book_remove_button_submit()
  102. *
  103. * @ingroup forms
  104. */
  105. function book_outline_form(&$form_state, $node) {
  106. if (!isset($node->book)) {
  107. // The node is not part of any book yet - set default options.
  108. $node->book = _book_link_defaults($node->nid);
  109. }
  110. else {
  111. $node->book['original_bid'] = $node->book['bid'];
  112. }
  113. // Find the depth limit for the parent select.
  114. if (!isset($node->book['parent_depth_limit'])) {
  115. $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book);
  116. }
  117. $form['#node'] = $node;
  118. $form['#id'] = 'book-outline';
  119. _book_add_form_elements($form, $node);
  120. $form['book']['#collapsible'] = FALSE;
  121. $form['update'] = array(
  122. '#type' => 'submit',
  123. '#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'),
  124. '#weight' => 15,
  125. );
  126. $form['remove'] = array(
  127. '#type' => 'submit',
  128. '#value' => t('Remove from book outline'),
  129. '#access' => $node->nid != $node->book['bid'] && $node->book['bid'],
  130. '#weight' => 20,
  131. '#submit' => array('book_remove_button_submit'),
  132. );
  133. return $form;
  134. }
  135. /**
  136. * Button submit function to redirect to removal confirm form.
  137. *
  138. * @see book_outline_form()
  139. */
  140. function book_remove_button_submit($form, &$form_state) {
  141. $form_state['redirect'] = 'node/'. $form['#node']->nid .'/outline/remove';
  142. }
  143. /**
  144. * Handles book outline form submissions from the outline tab.
  145. *
  146. * @see book_outline_form()
  147. */
  148. function book_outline_form_submit($form, &$form_state) {
  149. $node = $form['#node'];
  150. $form_state['redirect'] = "node/". $node->nid;
  151. $book_link = $form_state['values']['book'];
  152. if (!$book_link['bid']) {
  153. drupal_set_message(t('No changes were made'));
  154. return;
  155. }
  156. $book_link['menu_name'] = book_menu_name($book_link['bid']);
  157. $node->book = $book_link;
  158. if (_book_update_outline($node)) {
  159. if ($node->book['parent_mismatch']) {
  160. // This will usually only happen when JS is disabled.
  161. drupal_set_message(t('The post has been added to the selected book. You may now position it relative to other pages.'));
  162. $form_state['redirect'] = "node/". $node->nid ."/outline";
  163. }
  164. else {
  165. drupal_set_message(t('The book outline has been updated.'));
  166. }
  167. }
  168. else {
  169. drupal_set_message(t('There was an error adding the post to the book.'), 'error');
  170. }
  171. }
  172. /**
  173. * Menu callback; builds a form to confirm removal of a node from the book.
  174. *
  175. * @see book_remove_form_submit()
  176. *
  177. * @ingroup forms
  178. */
  179. function book_remove_form(&$form_state, $node) {
  180. $form['#node'] = $node;
  181. $title = array('%title' => $node->title);
  182. if ($node->book['has_children']) {
  183. $description = t('%title has associated child pages, which will be relocated automatically to maintain their connection to the book. To recreate the hierarchy (as it was before removing this page), %title may be added again using the Outline tab, and each of its former child pages will need to be relocated manually.', $title);
  184. }
  185. else {
  186. $description = t('%title may be added to hierarchy again using the Outline tab.', $title);
  187. }
  188. return confirm_form($form, t('Are you sure you want to remove %title from the book hierarchy?', $title), 'node/'. $node->nid, $description, t('Remove'));
  189. }
  190. /**
  191. * Confirm form submit function to remove a node from the book.
  192. *
  193. * @see book_remove_form()
  194. */
  195. function book_remove_form_submit($form, &$form_state) {
  196. $node = $form['#node'];
  197. if ($node->nid != $node->book['bid']) {
  198. // Only allowed when this is not a book (top-level page).
  199. menu_link_delete($node->book['mlid']);
  200. db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
  201. drupal_set_message(t('The post has been removed from the book.'));
  202. }
  203. $form_state['redirect'] = 'node/'. $node->nid;
  204. }
  205. /**
  206. * Renders a new parent page select element when the book selection changes.
  207. *
  208. * This function is called via AJAX when the selected book is changed on a node
  209. * or book outline form. It creates a new parent page select element, adds it
  210. * to the cached form, and then returns the rendered element so it can be
  211. * displayed on the form.
  212. *
  213. * @return
  214. * The rendered parent page select element.
  215. */
  216. function book_form_update() {
  217. $bid = $_POST['book']['bid'];
  218. if ($form = form_get_cache($_POST['form_build_id'], $form_state)) {
  219. // Validate the bid.
  220. if (isset($form['book']['bid']['#options'][$bid])) {
  221. $book_link = $form['#node']->book;
  222. $book_link['bid'] = $bid;
  223. // Get the new options and update the cache.
  224. $form['book']['plid'] = _book_parent_select($book_link);
  225. form_set_cache($_POST['form_build_id'], $form, $form_state);
  226. // Build and render the new select element, then return it in JSON format.
  227. $form_state = array();
  228. $form['#post'] = array();
  229. $form = form_builder($form['form_id']['#value'] , $form, $form_state);
  230. $output = drupal_render($form['book']['plid']);
  231. drupal_json(array('status' => TRUE, 'data' => $output));
  232. }
  233. else {
  234. drupal_json(array('status' => FALSE, 'data' => ''));
  235. }
  236. }
  237. else {
  238. drupal_json(array('status' => FALSE, 'data' => ''));
  239. }
  240. exit();
  241. }