|
1 <?php |
|
2 // $Id: book.admin.inc,v 1.8.2.3 2008/10/22 19:26:01 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 _book_admin_table($node, $form); |
|
76 $form['save'] = array( |
|
77 '#type' => 'submit', |
|
78 '#value' => t('Save book pages'), |
|
79 ); |
|
80 return $form; |
|
81 } |
|
82 |
|
83 /** |
|
84 * Check that the book has not been changed while using the form. |
|
85 * |
|
86 * @see book_admin_edit() |
|
87 */ |
|
88 function book_admin_edit_validate($form, &$form_state) { |
|
89 if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) { |
|
90 form_set_error('', t('This book has been modified by another user, the changes could not be saved.')); |
|
91 $form_state['rebuild'] = TRUE; |
|
92 } |
|
93 } |
|
94 |
|
95 /** |
|
96 * Handle submission of the book administrative page form. |
|
97 * |
|
98 * This function takes care to save parent menu items before their children. |
|
99 * Saving menu items in the incorrect order can break the menu tree. |
|
100 * |
|
101 * @see book_admin_edit() |
|
102 * @see menu_overview_form_submit() |
|
103 */ |
|
104 function book_admin_edit_submit($form, &$form_state) { |
|
105 // Save elements in the same order as defined in post rather than the form. |
|
106 // This ensures parents are updated before their children, preventing orphans. |
|
107 $order = array_flip(array_keys($form['#post']['table'])); |
|
108 $form['table'] = array_merge($order, $form['table']); |
|
109 |
|
110 foreach (element_children($form['table']) as $key) { |
|
111 if ($form['table'][$key]['#item']) { |
|
112 $row = $form['table'][$key]; |
|
113 $values = $form_state['values']['table'][$key]; |
|
114 |
|
115 // Update menu item if moved. |
|
116 if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) { |
|
117 $row['#item']['plid'] = $values['plid']; |
|
118 $row['#item']['weight'] = $values['weight']; |
|
119 menu_link_save($row['#item']); |
|
120 } |
|
121 |
|
122 // Update the title if changed. |
|
123 if ($row['title']['#default_value'] != $values['title']) { |
|
124 $node = node_load($values['nid'], FALSE); |
|
125 $node->title = $values['title']; |
|
126 $node->book['link_title'] = $values['title']; |
|
127 $node->revision = 1; |
|
128 $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title'])); |
|
129 node_save($node); |
|
130 watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid)); |
|
131 } |
|
132 } |
|
133 } |
|
134 |
|
135 drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title))); |
|
136 } |
|
137 |
|
138 /** |
|
139 * Build the table portion of the form for the book administration page. |
|
140 * |
|
141 * @see book_admin_edit() |
|
142 */ |
|
143 function _book_admin_table($node, &$form) { |
|
144 $form['table'] = array( |
|
145 '#theme' => 'book_admin_table', |
|
146 '#tree' => TRUE, |
|
147 ); |
|
148 |
|
149 $tree = book_menu_subtree_data($node->book); |
|
150 $tree = array_shift($tree); // Do not include the book item itself. |
|
151 if ($tree['below']) { |
|
152 $hash = sha1(serialize($tree['below'])); |
|
153 // Store the hash value as a hidden form element so that we can detect |
|
154 // if another user changed the book hierarchy. |
|
155 $form['tree_hash'] = array( |
|
156 '#type' => 'hidden', |
|
157 '#default_value' => $hash, |
|
158 ); |
|
159 $form['tree_current_hash'] = array( |
|
160 '#type' => 'value', |
|
161 '#value' => $hash, |
|
162 ); |
|
163 _book_admin_table_tree($tree['below'], $form['table']); |
|
164 } |
|
165 } |
|
166 |
|
167 /** |
|
168 * Recursive helper to build the main table in the book administration page form. |
|
169 * |
|
170 * @see book_admin_edit() |
|
171 */ |
|
172 function _book_admin_table_tree($tree, &$form) { |
|
173 foreach ($tree as $data) { |
|
174 $form['book-admin-'. $data['link']['nid']] = array( |
|
175 '#item' => $data['link'], |
|
176 'nid' => array('#type' => 'value', '#value' => $data['link']['nid']), |
|
177 'depth' => array('#type' => 'value', '#value' => $data['link']['depth']), |
|
178 'href' => array('#type' => 'value', '#value' => $data['link']['href']), |
|
179 'title' => array( |
|
180 '#type' => 'textfield', |
|
181 '#default_value' => $data['link']['link_title'], |
|
182 '#maxlength' => 255, |
|
183 '#size' => 40, |
|
184 ), |
|
185 'weight' => array( |
|
186 '#type' => 'weight', |
|
187 '#default_value' => $data['link']['weight'], |
|
188 '#delta' => 15, |
|
189 ), |
|
190 'plid' => array( |
|
191 '#type' => 'textfield', |
|
192 '#default_value' => $data['link']['plid'], |
|
193 '#size' => 6, |
|
194 ), |
|
195 'mlid' => array( |
|
196 '#type' => 'hidden', |
|
197 '#default_value' => $data['link']['mlid'], |
|
198 ), |
|
199 ); |
|
200 if ($data['below']) { |
|
201 _book_admin_table_tree($data['below'], $form); |
|
202 } |
|
203 } |
|
204 |
|
205 return $form; |
|
206 } |
|
207 |
|
208 /** |
|
209 * Theme function for the book administration page form. |
|
210 * |
|
211 * @ingroup themeable |
|
212 * @see book_admin_table() |
|
213 */ |
|
214 function theme_book_admin_table($form) { |
|
215 drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2); |
|
216 drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight'); |
|
217 |
|
218 $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3')); |
|
219 |
|
220 $rows = array(); |
|
221 $destination = drupal_get_destination(); |
|
222 $access = user_access('administer nodes'); |
|
223 foreach (element_children($form) as $key) { |
|
224 $nid = $form[$key]['nid']['#value']; |
|
225 $href = $form[$key]['href']['#value']; |
|
226 |
|
227 // Add special classes to be used with tabledrag.js. |
|
228 $form[$key]['plid']['#attributes']['class'] = 'book-plid'; |
|
229 $form[$key]['mlid']['#attributes']['class'] = 'book-mlid'; |
|
230 $form[$key]['weight']['#attributes']['class'] = 'book-weight'; |
|
231 |
|
232 $data = array( |
|
233 theme('indentation', $form[$key]['depth']['#value'] - 2) . drupal_render($form[$key]['title']), |
|
234 drupal_render($form[$key]['weight']), |
|
235 drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']), |
|
236 l(t('view'), $href), |
|
237 $access ? l(t('edit'), 'node/'. $nid .'/edit', array('query' => $destination)) : ' ', |
|
238 $access ? l(t('delete'), 'node/'. $nid .'/delete', array('query' => $destination) ) : ' ', |
|
239 ); |
|
240 $row = array('data' => $data); |
|
241 if (isset($form[$key]['#attributes'])) { |
|
242 $row = array_merge($row, $form[$key]['#attributes']); |
|
243 } |
|
244 $row['class'] = empty($row['class']) ? 'draggable' : $row['class'] .' draggable'; |
|
245 $rows[] = $row; |
|
246 } |
|
247 |
|
248 return theme('table', $header, $rows, array('id' => 'book-outline')); |
|
249 } |
|
250 |