book.pages.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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', array('items' => $book_list));
  15. }
  16. /**
  17. * Menu callback; Generates representations of a book page and its children.
  18. *
  19. * The function delegates the generation of output to helper functions. The
  20. * function name is derived by prepending 'book_export_' to the given output
  21. * type. So, e.g., a type of 'html' results in a call to the function
  22. * book_export_html().
  23. *
  24. * @param $type
  25. * A string encoding the type of output requested. The following types are
  26. * currently supported in book module:
  27. * - html: Printer-friendly HTML.
  28. * Other types may be supported in contributed modules.
  29. * @param $nid
  30. * An integer representing the node id (nid) of the node to export
  31. *
  32. * @return
  33. * A string representing the node and its children in the book hierarchy in a
  34. * format determined by the $type parameter.
  35. */
  36. function book_export($type, $nid) {
  37. // Check that the node exists and that the current user has access to it.
  38. $node = node_load($nid);
  39. if (!$node) {
  40. return MENU_NOT_FOUND;
  41. }
  42. if (!node_access('view', $node)) {
  43. return MENU_ACCESS_DENIED;
  44. }
  45. $type = drupal_strtolower($type);
  46. $export_function = 'book_export_' . $type;
  47. if (function_exists($export_function)) {
  48. print call_user_func($export_function, $nid);
  49. }
  50. else {
  51. drupal_set_message(t('Unknown export format.'));
  52. drupal_not_found();
  53. }
  54. }
  55. /**
  56. * Generates HTML for export when invoked by book_export().
  57. *
  58. * The given node is embedded to its absolute depth in a top level section. For
  59. * example, a child node with depth 2 in the hierarchy is contained in
  60. * (otherwise empty) <div> elements corresponding to depth 0 and depth 1.
  61. * This is intended to support WYSIWYG output - e.g., level 3 sections always
  62. * look like level 3 sections, no matter their depth relative to the node
  63. * selected to be exported as printer-friendly HTML.
  64. *
  65. * @param $nid
  66. * An integer representing the node id (nid) of the node to export.
  67. *
  68. * @return
  69. * A string containing HTML representing the node and its children in
  70. * the book hierarchy.
  71. */
  72. function book_export_html($nid) {
  73. if (user_access('access printer-friendly version')) {
  74. $export_data = array();
  75. $node = node_load($nid);
  76. if (isset($node->book)) {
  77. $tree = book_menu_subtree_data($node->book);
  78. $contents = book_export_traverse($tree, 'book_node_export');
  79. return theme('book_export_html', array('title' => $node->title, 'contents' => $contents, 'depth' => $node->book['depth']));
  80. }
  81. else {
  82. drupal_not_found();
  83. }
  84. }
  85. else {
  86. drupal_access_denied();
  87. }
  88. }
  89. /**
  90. * Menu callback: Shows the outline form for a single node.
  91. *
  92. * @param $node
  93. * The book node for which to show the outline.
  94. */
  95. function book_outline($node) {
  96. drupal_set_title($node->title);
  97. return drupal_get_form('book_outline_form', $node);
  98. }
  99. /**
  100. * Form constructor for the book outline form.
  101. *
  102. * Allows handling of all book outline operations via the outline tab.
  103. *
  104. * @param $node
  105. * The book node for which to show the outline.
  106. *
  107. * @see book_outline_form_submit()
  108. * @see book_remove_button_submit()
  109. * @ingroup forms
  110. */
  111. function book_outline_form($form, &$form_state, $node) {
  112. if (!isset($node->book)) {
  113. // The node is not part of any book yet - set default options.
  114. $node->book = _book_link_defaults($node->nid);
  115. }
  116. else {
  117. $node->book['original_bid'] = $node->book['bid'];
  118. }
  119. // Find the depth limit for the parent select.
  120. if (!isset($node->book['parent_depth_limit'])) {
  121. $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book);
  122. }
  123. $form['#node'] = $node;
  124. $form['#id'] = 'book-outline';
  125. _book_add_form_elements($form, $form_state, $node);
  126. $form['book']['#collapsible'] = FALSE;
  127. $form['update'] = array(
  128. '#type' => 'submit',
  129. '#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'),
  130. '#weight' => 15,
  131. );
  132. $form['remove'] = array(
  133. '#type' => 'submit',
  134. '#value' => t('Remove from book outline'),
  135. '#access' => _book_node_is_removable($node),
  136. '#weight' => 20,
  137. '#submit' => array('book_remove_button_submit'),
  138. );
  139. return $form;
  140. }
  141. /**
  142. * Form submission handler for book_outline_form().
  143. *
  144. * Redirects to removal confirmation form.
  145. *
  146. * @see book_outline_form_submit()
  147. */
  148. function book_remove_button_submit($form, &$form_state) {
  149. $form_state['redirect'] = 'node/' . $form['#node']->nid . '/outline/remove';
  150. }
  151. /**
  152. * Form submission handler for book_outline_form().
  153. *
  154. * @see book_remove_button_submit()
  155. */
  156. function book_outline_form_submit($form, &$form_state) {
  157. $node = $form['#node'];
  158. $form_state['redirect'] = "node/" . $node->nid;
  159. $book_link = $form_state['values']['book'];
  160. if (!$book_link['bid']) {
  161. drupal_set_message(t('No changes were made'));
  162. return;
  163. }
  164. $book_link['menu_name'] = book_menu_name($book_link['bid']);
  165. $node->book = $book_link;
  166. if (_book_update_outline($node)) {
  167. if ($node->book['parent_mismatch']) {
  168. // This will usually only happen when JS is disabled.
  169. drupal_set_message(t('The post has been added to the selected book. You may now position it relative to other pages.'));
  170. $form_state['redirect'] = "node/" . $node->nid . "/outline";
  171. }
  172. else {
  173. drupal_set_message(t('The book outline has been updated.'));
  174. }
  175. }
  176. else {
  177. drupal_set_message(t('There was an error adding the post to the book.'), 'error');
  178. }
  179. }
  180. /**
  181. * Form constructor to confirm removal of a node from a book.
  182. *
  183. * @param $node
  184. * The node to delete.
  185. *
  186. * @see book_remove_form_submit()
  187. * @ingroup forms
  188. */
  189. function book_remove_form($form, &$form_state, $node) {
  190. $form['#node'] = $node;
  191. $title = array('%title' => $node->title);
  192. if ($node->book['has_children']) {
  193. $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);
  194. }
  195. else {
  196. $description = t('%title may be added to hierarchy again using the Outline tab.', $title);
  197. }
  198. return confirm_form($form, t('Are you sure you want to remove %title from the book hierarchy?', $title), 'node/' . $node->nid, $description, t('Remove'));
  199. }
  200. /**
  201. * Form submission handler for book_remove_form().
  202. */
  203. function book_remove_form_submit($form, &$form_state) {
  204. $node = $form['#node'];
  205. if (_book_node_is_removable($node)) {
  206. menu_link_delete($node->book['mlid']);
  207. db_delete('book')
  208. ->condition('nid', $node->nid)
  209. ->execute();
  210. drupal_set_message(t('The post has been removed from the book.'));
  211. }
  212. $form_state['redirect'] = 'node/' . $node->nid;
  213. }