book.admin.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the book module.
  5. */
  6. /**
  7. * Returns an administrative overview of all books.
  8. */
  9. function book_admin_overview() {
  10. $rows = array();
  11. $headers = array(t('Book'), t('Operations'));
  12. // Add any recognized books to the table list.
  13. foreach (book_get_books() as $book) {
  14. $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), 'admin/content/book/' . $book['nid']));
  15. }
  16. return theme('table', array('header' => $headers, 'rows' => $rows, 'empty' => t('No books available.')));
  17. }
  18. /**
  19. * Form constructor for the book settings form.
  20. *
  21. * @see book_admin_settings_validate()
  22. *
  23. * @ingroup forms
  24. */
  25. function book_admin_settings() {
  26. $types = node_type_get_names();
  27. $form['book_allowed_types'] = array(
  28. '#type' => 'checkboxes',
  29. '#title' => t('Content types allowed in book outlines'),
  30. '#default_value' => variable_get('book_allowed_types', array('book')),
  31. '#options' => $types,
  32. '#description' => t('Users with the %outline-perm permission can add all content types.', array('%outline-perm' => t('Administer book outlines'))),
  33. '#required' => TRUE,
  34. );
  35. $form['book_child_type'] = array(
  36. '#type' => 'radios',
  37. '#title' => t('Content type for child pages'),
  38. '#default_value' => variable_get('book_child_type', 'book'),
  39. '#options' => $types,
  40. '#required' => TRUE,
  41. );
  42. $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
  43. $form['#validate'][] = 'book_admin_settings_validate';
  44. return system_settings_form($form);
  45. }
  46. /**
  47. * Form validation handler for book_admin_settings().
  48. */
  49. function book_admin_settings_validate($form, &$form_state) {
  50. $child_type = $form_state['values']['book_child_type'];
  51. if (empty($form_state['values']['book_allowed_types'][$child_type])) {
  52. form_set_error('book_child_type', t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', array('%add-child' => t('Add child page'))));
  53. }
  54. }
  55. /**
  56. * Form constructor for administering a single book's hierarchy.
  57. *
  58. * @see book_admin_edit_submit()
  59. *
  60. * @param $node
  61. * The node of the top-level page in the book.
  62. *
  63. * @see book_admin_edit_validate()
  64. * @see book_admin_edit_submit()
  65. * @ingroup forms
  66. */
  67. function book_admin_edit($form, $form_state, $node) {
  68. drupal_set_title($node->title);
  69. $form['#node'] = $node;
  70. _book_admin_table($node, $form);
  71. $form['save'] = array(
  72. '#type' => 'submit',
  73. '#value' => t('Save book pages'),
  74. );
  75. return $form;
  76. }
  77. /**
  78. * Form validation handler for book_admin_edit().
  79. *
  80. * Checks that the book has not been changed while using the form.
  81. *
  82. * @see book_admin_edit_submit()
  83. */
  84. function book_admin_edit_validate($form, &$form_state) {
  85. if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
  86. form_set_error('', t('This book has been modified by another user, the changes could not be saved.'));
  87. }
  88. }
  89. /**
  90. * Form submission handler for book_admin_edit().
  91. *
  92. * This function takes care to save parent menu items before their children.
  93. * Saving menu items in the incorrect order can break the menu tree.
  94. *
  95. * @see book_admin_edit_validate()
  96. * @see menu_overview_form_submit()
  97. */
  98. function book_admin_edit_submit($form, &$form_state) {
  99. // Save elements in the same order as defined in post rather than the form.
  100. // This ensures parents are updated before their children, preventing orphans.
  101. $order = array_flip(array_keys($form_state['input']['table']));
  102. $form['table'] = array_merge($order, $form['table']);
  103. foreach (element_children($form['table']) as $key) {
  104. if ($form['table'][$key]['#item']) {
  105. $row = $form['table'][$key];
  106. $values = $form_state['values']['table'][$key];
  107. // Update menu item if moved.
  108. if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
  109. $row['#item']['plid'] = $values['plid'];
  110. $row['#item']['weight'] = $values['weight'];
  111. menu_link_save($row['#item']);
  112. }
  113. // Update the title if changed.
  114. if ($row['title']['#default_value'] != $values['title']) {
  115. $node = node_load($values['nid']);
  116. $langcode = LANGUAGE_NONE;
  117. $node->title = $values['title'];
  118. $node->book['link_title'] = $values['title'];
  119. $node->revision = 1;
  120. $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
  121. node_save($node);
  122. watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
  123. }
  124. }
  125. }
  126. drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title)));
  127. }
  128. /**
  129. * Builds the table portion of the form for the book administration page.
  130. *
  131. * @param $node
  132. * The node of the top-level page in the book.
  133. * @param $form
  134. * The form that is being modified.
  135. *
  136. * @see book_admin_edit()
  137. */
  138. function _book_admin_table($node, &$form) {
  139. $form['table'] = array(
  140. '#theme' => 'book_admin_table',
  141. '#tree' => TRUE,
  142. );
  143. $tree = book_menu_subtree_data($node->book);
  144. $tree = array_shift($tree); // Do not include the book item itself.
  145. if ($tree['below']) {
  146. $hash = drupal_hash_base64(serialize($tree['below']));
  147. // Store the hash value as a hidden form element so that we can detect
  148. // if another user changed the book hierarchy.
  149. $form['tree_hash'] = array(
  150. '#type' => 'hidden',
  151. '#default_value' => $hash,
  152. );
  153. $form['tree_current_hash'] = array(
  154. '#type' => 'value',
  155. '#value' => $hash,
  156. );
  157. _book_admin_table_tree($tree['below'], $form['table']);
  158. }
  159. }
  160. /**
  161. * Helps build the main table in the book administration page form.
  162. *
  163. * @param $tree
  164. * A subtree of the book menu hierarchy.
  165. * @param $form
  166. * The form that is being modified.
  167. *
  168. * @return
  169. * The form that is being modified.
  170. *
  171. * @see book_admin_edit()
  172. */
  173. function _book_admin_table_tree($tree, &$form) {
  174. // The delta must be big enough to give each node a distinct value.
  175. $count = count($tree);
  176. $delta = ($count < 30) ? 15 : intval($count / 2) + 1;
  177. foreach ($tree as $data) {
  178. $form['book-admin-' . $data['link']['nid']] = array(
  179. '#item' => $data['link'],
  180. 'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
  181. 'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
  182. 'href' => array('#type' => 'value', '#value' => $data['link']['href']),
  183. 'title' => array(
  184. '#type' => 'textfield',
  185. '#default_value' => $data['link']['link_title'],
  186. '#maxlength' => 255,
  187. '#size' => 40,
  188. ),
  189. 'weight' => array(
  190. '#type' => 'weight',
  191. '#default_value' => $data['link']['weight'],
  192. '#delta' => max($delta, abs($data['link']['weight'])),
  193. '#title' => t('Weight for @title', array('@title' => $data['link']['title'])),
  194. '#title_display' => 'invisible',
  195. ),
  196. 'plid' => array(
  197. '#type' => 'hidden',
  198. '#default_value' => $data['link']['plid'],
  199. ),
  200. 'mlid' => array(
  201. '#type' => 'hidden',
  202. '#default_value' => $data['link']['mlid'],
  203. ),
  204. );
  205. if ($data['below']) {
  206. _book_admin_table_tree($data['below'], $form);
  207. }
  208. }
  209. return $form;
  210. }
  211. /**
  212. * Returns HTML for a book administration form.
  213. *
  214. * @param $variables
  215. * An associative array containing:
  216. * - form: A render element representing the form.
  217. *
  218. * @see book_admin_table()
  219. * @ingroup themeable
  220. */
  221. function theme_book_admin_table($variables) {
  222. $form = $variables['form'];
  223. drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
  224. drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');
  225. $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3'));
  226. $rows = array();
  227. $destination = drupal_get_destination();
  228. $access = user_access('administer nodes');
  229. foreach (element_children($form) as $key) {
  230. $nid = $form[$key]['nid']['#value'];
  231. $href = $form[$key]['href']['#value'];
  232. // Add special classes to be used with tabledrag.js.
  233. $form[$key]['plid']['#attributes']['class'] = array('book-plid');
  234. $form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
  235. $form[$key]['weight']['#attributes']['class'] = array('book-weight');
  236. $data = array(
  237. theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']),
  238. drupal_render($form[$key]['weight']),
  239. drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
  240. l(t('view'), $href),
  241. $access ? l(t('edit'), 'node/' . $nid . '/edit', array('query' => $destination)) : '&nbsp;',
  242. $access ? l(t('delete'), 'node/' . $nid . '/delete', array('query' => $destination) ) : '&nbsp;',
  243. );
  244. $row = array('data' => $data);
  245. if (isset($form[$key]['#attributes'])) {
  246. $row = array_merge($row, $form[$key]['#attributes']);
  247. }
  248. $row['class'][] = 'draggable';
  249. $rows[] = $row;
  250. }
  251. return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'book-outline'), 'empty' => t('No book content available.')));
  252. }