comparison modules/book/book.admin.inc @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children fff6d4c8c043
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: book.admin.inc,v 1.8 2008/01/08 10:35:41 goba Exp $
3
4 /**
5 * @file
6 * Admin page callbacks for the book module.
7 */
8
9 /**
10 * Returns an administrative overview of all books.
11 */
12 function book_admin_overview() {
13 $rows = array();
14 foreach (book_get_books() as $book) {
15 $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), "admin/content/book/". $book['nid']));
16 }
17 $headers = array(t('Book'), t('Operations'));
18
19 return theme('table', $headers, $rows);
20 }
21
22 /**
23 * Builds and returns the book settings form.
24 *
25 * @see book_admin_settings_validate()
26 *
27 * @ingroup forms
28 */
29 function book_admin_settings() {
30 $types = node_get_types('names');
31 $form['book_allowed_types'] = array(
32 '#type' => 'checkboxes',
33 '#title' => t('Allowed book outline types'),
34 '#default_value' => variable_get('book_allowed_types', array('book')),
35 '#options' => $types,
36 '#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'))),
37 '#required' => TRUE,
38 );
39 $form['book_child_type'] = array(
40 '#type' => 'radios',
41 '#title' => t('Default child page type'),
42 '#default_value' => variable_get('book_child_type', 'book'),
43 '#options' => $types,
44 '#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'))),
45 '#required' => TRUE,
46 );
47 $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
48 $form['#validate'][] = 'book_admin_settings_validate';
49 return system_settings_form($form);
50 }
51
52 /**
53 * Validate the book settings form.
54 *
55 * @see book_admin_settings()
56 */
57 function book_admin_settings_validate($form, &$form_state) {
58 $child_type = $form_state['values']['book_child_type'];
59 if (empty($form_state['values']['book_allowed_types'][$child_type])) {
60 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'))));
61 }
62 }
63
64 /**
65 * Build the form to administrate the hierarchy of a single book.
66 *
67 * @see book_admin_edit_submit()
68 *
69 * @ingroup forms.
70 */
71 function book_admin_edit($form_state, $node) {
72 drupal_set_title(check_plain($node->title));
73 $form = array();
74 $form['#node'] = $node;
75 $form['table'] = _book_admin_table($node);
76 $form['save'] = array(
77 '#type' => 'submit',
78 '#value' => t('Save book pages'),
79 );
80 return $form;
81 }
82
83 /**
84 * Handle submission of the book administrative page form.
85 *
86 * This function takes care to save parent menu items before their children.
87 * Saving menu items in the incorrect order can break the menu tree.
88 *
89 * @see book_admin_edit()
90 * @see menu_overview_form_submit()
91 */
92 function book_admin_edit_submit($form, &$form_state) {
93 // Save elements in the same order as defined in post rather than the form.
94 // This ensures parents are updated before their children, preventing orphans.
95 $order = array_flip(array_keys($form['#post']['table']));
96 $form['table'] = array_merge($order, $form['table']);
97
98 foreach (element_children($form['table']) as $key) {
99 if ($form['table'][$key]['#item']) {
100 $row = $form['table'][$key];
101 $values = $form_state['values']['table'][$key];
102
103 // Update menu item if moved.
104 if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
105 $row['#item']['plid'] = $values['plid'];
106 $row['#item']['weight'] = $values['weight'];
107 menu_link_save($row['#item']);
108 }
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
123 drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title)));
124 }
125
126 /**
127 * Build the table portion of the form for the book administration page.
128 *
129 * @see book_admin_edit()
130 */
131 function _book_admin_table($node) {
132 $form = array(
133 '#theme' => 'book_admin_table',
134 '#tree' => TRUE,
135 );
136
137 $tree = book_menu_subtree_data($node->book);
138 $tree = array_shift($tree); // Do not include the book item itself.
139 if ($tree['below']) {
140 _book_admin_table_tree($tree['below'], $form);
141 }
142 return $form;
143 }
144
145 /**
146 * Recursive helper to build the main table in the book administration page form.
147 *
148 * @see book_admin_edit()
149 */
150 function _book_admin_table_tree($tree, &$form) {
151 foreach ($tree as $key => $data) {
152 $form[$key] = array(
153 '#item' => $data['link'],
154 'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
155 'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
156 'href' => array('#type' => 'value', '#value' => $data['link']['href']),
157 'title' => array(
158 '#type' => 'textfield',
159 '#default_value' => $data['link']['link_title'],
160 '#maxlength' => 255,
161 '#size' => 40,
162 ),
163 'weight' => array(
164 '#type' => 'weight',
165 '#default_value' => $data['link']['weight'],
166 '#delta' => 15,
167 ),
168 'plid' => array(
169 '#type' => 'textfield',
170 '#default_value' => $data['link']['plid'],
171 '#size' => 6,
172 ),
173 'mlid' => array(
174 '#type' => 'hidden',
175 '#default_value' => $data['link']['mlid'],
176 ),
177 );
178 if ($data['below']) {
179 _book_admin_table_tree($data['below'], $form);
180 }
181 }
182
183 return $form;
184 }
185
186 /**
187 * Theme function for the book administration page form.
188 *
189 * @ingroup themeable
190 * @see book_admin_table()
191 */
192 function theme_book_admin_table($form) {
193 drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
194 drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');
195
196 $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3'));
197
198 $rows = array();
199 $destination = drupal_get_destination();
200 $access = user_access('administer nodes');
201 foreach (element_children($form) as $key) {
202 $nid = $form[$key]['nid']['#value'];
203 $href = $form[$key]['href']['#value'];
204
205 // Add special classes to be used with tabledrag.js.
206 $form[$key]['plid']['#attributes']['class'] = 'book-plid';
207 $form[$key]['mlid']['#attributes']['class'] = 'book-mlid';
208 $form[$key]['weight']['#attributes']['class'] = 'book-weight';
209
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
226 return theme('table', $header, $rows, array('id' => 'book-outline'));
227 }
228
229 /**
230 * Recursive helper to sort each layer of a book tree by weight.
231 */
232 function _book_admin_sort_tree(&$tree) {
233 uasort($tree, '_book_admin_compare');
234 foreach ($tree as $key => $subtree) {
235 if (!empty($tree[$key]['below'])) {
236 _book_admin_sort_tree($tree[$key]['below']);
237 }
238 }
239 }
240
241 /**
242 * Used by uasort() in _book_admin_sort_tree() to compare items in a book tree.
243 */
244 function _book_admin_compare($a, $b) {
245 $weight = $a['link']['weight'] - $b['link']['weight'];
246 if ($weight) {
247 return $weight;
248 }
249 return strncmp($a['link']['title'], $b['link']['title']);
250 }