book.pages.inc 7.2 KB

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