book.admin.inc

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

Admin page callbacks for the book module.

File

drupal-6.x/modules/book/book.admin.inc
View source
  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. foreach (book_get_books() as $book) {
  12. $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), "admin/content/book/". $book['nid']));
  13. }
  14. $headers = array(t('Book'), t('Operations'));
  15. return theme('table', $headers, $rows);
  16. }
  17. /**
  18. * Builds and returns the book settings form.
  19. *
  20. * @see book_admin_settings_validate()
  21. *
  22. * @ingroup forms
  23. */
  24. function book_admin_settings() {
  25. $types = node_get_types('names');
  26. $form['book_allowed_types'] = array(
  27. '#type' => 'checkboxes',
  28. '#title' => t('Allowed book outline types'),
  29. '#default_value' => variable_get('book_allowed_types', array('book')),
  30. '#options' => $types,
  31. '#description' => t('Select content types which users with the %add-perm permission will be allowed to add to the book hierarchy. Users with the %outline-perm permission can add all content types.', array('%add-perm' => t('add content to books'), '%outline-perm' => t('administer book outlines'))),
  32. '#required' => TRUE,
  33. );
  34. $form['book_child_type'] = array(
  35. '#type' => 'radios',
  36. '#title' => t('Default child page type'),
  37. '#default_value' => variable_get('book_child_type', 'book'),
  38. '#options' => $types,
  39. '#description' => 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'))),
  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. * Validate the book settings form.
  48. *
  49. * @see book_admin_settings()
  50. */
  51. function book_admin_settings_validate($form, &$form_state) {
  52. $child_type = $form_state['values']['book_child_type'];
  53. if (empty($form_state['values']['book_allowed_types'][$child_type])) {
  54. 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'))));
  55. }
  56. }
  57. /**
  58. * Build the form to administrate the hierarchy of a single book.
  59. *
  60. * @see book_admin_edit_submit()
  61. *
  62. * @ingroup forms.
  63. */
  64. function book_admin_edit($form_state, $node) {
  65. drupal_set_title(check_plain($node->title));
  66. $form = array();
  67. $form['#node'] = $node;
  68. _book_admin_table($node, $form);
  69. $form['save'] = array(
  70. '#type' => 'submit',
  71. '#value' => t('Save book pages'),
  72. );
  73. return $form;
  74. }
  75. /**
  76. * Check that the book has not been changed while using the form.
  77. *
  78. * @see book_admin_edit()
  79. */
  80. function book_admin_edit_validate($form, &$form_state) {
  81. if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
  82. form_set_error('', t('This book has been modified by another user, the changes could not be saved.'));
  83. $form_state['rebuild'] = TRUE;
  84. }
  85. }
  86. /**
  87. * Handle submission of the book administrative page form.
  88. *
  89. * This function takes care to save parent menu items before their children.
  90. * Saving menu items in the incorrect order can break the menu tree.
  91. *
  92. * @see book_admin_edit()
  93. * @see menu_overview_form_submit()
  94. */
  95. function book_admin_edit_submit($form, &$form_state) {
  96. // Save elements in the same order as defined in post rather than the form.
  97. // This ensures parents are updated before their children, preventing orphans.
  98. $order = array_flip(array_keys($form['#post']['table']));
  99. $form['table'] = array_merge($order, $form['table']);
  100. foreach (element_children($form['table']) as $key) {
  101. if ($form['table'][$key]['#item']) {
  102. $row = $form['table'][$key];
  103. $values = $form_state['values']['table'][$key];
  104. // Update menu item if moved.
  105. if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
  106. $row['#item']['plid'] = $values['plid'];
  107. $row['#item']['weight'] = $values['weight'];
  108. menu_link_save($row['#item']);
  109. }
  110. // Update the title if changed.
  111. if ($row['title']['#default_value'] != $values['title']) {
  112. $node = node_load($values['nid'], FALSE);
  113. $node->title = $values['title'];
  114. $node->book['link_title'] = $values['title'];
  115. $node->revision = 1;
  116. $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
  117. node_save($node);
  118. watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
  119. }
  120. }
  121. }
  122. drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title)));
  123. }
  124. /**
  125. * Build the table portion of the form for the book administration page.
  126. *
  127. * @see book_admin_edit()
  128. */
  129. function _book_admin_table($node, &$form) {
  130. $form['table'] = array(
  131. '#theme' => 'book_admin_table',
  132. '#tree' => TRUE,
  133. );
  134. $tree = book_menu_subtree_data($node->book);
  135. $tree = array_shift($tree); // Do not include the book item itself.
  136. if ($tree['below']) {
  137. $hash = sha1(serialize($tree['below']));
  138. // Store the hash value as a hidden form element so that we can detect
  139. // if another user changed the book hierarchy.
  140. $form['tree_hash'] = array(
  141. '#type' => 'hidden',
  142. '#default_value' => $hash,
  143. );
  144. $form['tree_current_hash'] = array(
  145. '#type' => 'value',
  146. '#value' => $hash,
  147. );
  148. _book_admin_table_tree($tree['below'], $form['table']);
  149. }
  150. }
  151. /**
  152. * Recursive helper to build the main table in the book administration page form.
  153. *
  154. * @see book_admin_edit()
  155. */
  156. function _book_admin_table_tree($tree, &$form) {
  157. foreach ($tree as $data) {
  158. $form['book-admin-'. $data['link']['nid']] = array(
  159. '#item' => $data['link'],
  160. 'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
  161. 'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
  162. 'href' => array('#type' => 'value', '#value' => $data['link']['href']),
  163. 'title' => array(
  164. '#type' => 'textfield',
  165. '#default_value' => $data['link']['link_title'],
  166. '#maxlength' => 255,
  167. '#size' => 40,
  168. ),
  169. 'weight' => array(
  170. '#type' => 'weight',
  171. '#default_value' => $data['link']['weight'],
  172. '#delta' => 15,
  173. ),
  174. 'plid' => array(
  175. '#type' => 'textfield',
  176. '#default_value' => $data['link']['plid'],
  177. '#size' => 6,
  178. ),
  179. 'mlid' => array(
  180. '#type' => 'hidden',
  181. '#default_value' => $data['link']['mlid'],
  182. ),
  183. );
  184. if ($data['below']) {
  185. _book_admin_table_tree($data['below'], $form);
  186. }
  187. }
  188. return $form;
  189. }
  190. /**
  191. * Theme function for the book administration page form.
  192. *
  193. * @ingroup themeable
  194. * @see book_admin_table()
  195. */
  196. function theme_book_admin_table($form) {
  197. drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
  198. drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');
  199. $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3'));
  200. $rows = array();
  201. $destination = drupal_get_destination();
  202. $access = user_access('administer nodes');
  203. foreach (element_children($form) as $key) {
  204. $nid = $form[$key]['nid']['#value'];
  205. $href = $form[$key]['href']['#value'];
  206. // Add special classes to be used with tabledrag.js.
  207. $form[$key]['plid']['#attributes']['class'] = 'book-plid';
  208. $form[$key]['mlid']['#attributes']['class'] = 'book-mlid';
  209. $form[$key]['weight']['#attributes']['class'] = 'book-weight';
  210. $data = array(
  211. theme('indentation', $form[$key]['depth']['#value'] - 2) . drupal_render($form[$key]['title']),
  212. drupal_render($form[$key]['weight']),
  213. drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
  214. l(t('view'), $href),
  215. $access ? l(t('edit'), 'node/'. $nid .'/edit', array('query' => $destination)) : '&nbsp',
  216. $access ? l(t('delete'), 'node/'. $nid .'/delete', array('query' => $destination) ) : '&nbsp',
  217. );
  218. $row = array('data' => $data);
  219. if (isset($form[$key]['#attributes'])) {
  220. $row = array_merge($row, $form[$key]['#attributes']);
  221. }
  222. $row['class'] = empty($row['class']) ? 'draggable' : $row['class'] .' draggable';
  223. $rows[] = $row;
  224. }
  225. return theme('table', $header, $rows, array('id' => 'book-outline'));
  226. }