/[drupal]/drupal/modules/book/book.admin.inc
ViewVC logotype

Contents of /drupal/modules/book/book.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.25 - (show annotations) (download) (as text)
Sun Nov 1 12:11:10 2009 UTC (3 weeks, 3 days ago) by dries
Branch: MAIN
Changes since 1.24: +3 -3 lines
File MIME type: text/x-php
- Patch #595084 by c960657: use type hinting for .
1 <?php
2 // $Id: book.admin.inc,v 1.24 2009/10/11 03:07:16 webchick 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
15 $headers = array(t('Book'), t('Operations'));
16
17 // Add any recognized books to the table list.
18 foreach (book_get_books() as $book) {
19 $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), 'admin/content/book/' . $book['nid']));
20 }
21
22 // If no books were found, let the user know.
23 if (empty($rows)) {
24 $rows[] = array(array('data' => t('No books available.'), 'colspan' => 2));
25 }
26
27 return theme('table', array('header' => $headers, 'rows' => $rows));
28 }
29
30 /**
31 * Builds and returns the book settings form.
32 *
33 * @see book_admin_settings_validate()
34 *
35 * @ingroup forms
36 */
37 function book_admin_settings() {
38 $types = node_type_get_names();
39 $form['book_allowed_types'] = array(
40 '#type' => 'checkboxes',
41 '#title' => t('Allowed book outline types'),
42 '#default_value' => array('book'),
43 '#options' => $types,
44 '#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'))),
45 '#required' => TRUE,
46 );
47 $form['book_child_type'] = array(
48 '#type' => 'radios',
49 '#title' => t('Default child page type'),
50 '#default_value' => 'book',
51 '#options' => $types,
52 '#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'))),
53 '#required' => TRUE,
54 );
55 $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
56 $form['#validate'][] = 'book_admin_settings_validate';
57
58 return system_settings_form($form, TRUE);
59 }
60
61 /**
62 * Validate the book settings form.
63 *
64 * @see book_admin_settings()
65 */
66 function book_admin_settings_validate($form, &$form_state) {
67 $child_type = $form_state['values']['book_child_type'];
68 if (empty($form_state['values']['book_allowed_types'][$child_type])) {
69 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'))));
70 }
71 }
72
73 /**
74 * Build the form to administrate the hierarchy of a single book.
75 *
76 * @see book_admin_edit_submit()
77 *
78 * @ingroup forms.
79 */
80 function book_admin_edit($form, $form_state, stdClass $node) {
81 drupal_set_title($node->title[FIELD_LANGUAGE_NONE][0]['value']);
82 $form['#node'] = $node;
83 _book_admin_table($node, $form);
84 $form['save'] = array(
85 '#type' => 'submit',
86 '#value' => t('Save book pages'),
87 );
88
89 return $form;
90 }
91
92 /**
93 * Check that the book has not been changed while using the form.
94 *
95 * @see book_admin_edit()
96 */
97 function book_admin_edit_validate($form, &$form_state) {
98 if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
99 form_set_error('', t('This book has been modified by another user, the changes could not be saved.'));
100 $form_state['rebuild'] = TRUE;
101 }
102 }
103
104 /**
105 * Handle submission of the book administrative page form.
106 *
107 * This function takes care to save parent menu items before their children.
108 * Saving menu items in the incorrect order can break the menu tree.
109 *
110 * @see book_admin_edit()
111 * @see menu_overview_form_submit()
112 */
113 function book_admin_edit_submit($form, &$form_state) {
114 // Save elements in the same order as defined in post rather than the form.
115 // This ensures parents are updated before their children, preventing orphans.
116 $order = array_flip(array_keys($form_state['input']['table']));
117 $form['table'] = array_merge($order, $form['table']);
118
119 foreach (element_children($form['table']) as $key) {
120 if ($form['table'][$key]['#item']) {
121 $row = $form['table'][$key];
122 $values = $form_state['values']['table'][$key];
123
124 // Update menu item if moved.
125 if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
126 $row['#item']['plid'] = $values['plid'];
127 $row['#item']['weight'] = $values['weight'];
128 menu_link_save($row['#item']);
129 }
130
131 // Update the title if changed.
132 if ($row['title']['#default_value'] != $values['title']) {
133 $node = node_load($values['nid'], FALSE);
134 $langcode = FIELD_LANGUAGE_NONE;
135 $node->title = array($langcode => array(array('value' => $values['title'])));
136 $node->book['link_title'] = $values['title'];
137 $node->revision = 1;
138 $node->log = t('Title changed from %original to %current.', array('%original' => $node->title[$langcode][0]['value'], '%current' => $values['title']));
139
140 node_save($node);
141 watchdog('content', 'book: updated %title.', array('%title' => $node->title[$langcode][0]['value']), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
142 }
143 }
144 }
145
146 drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title[$langcode][0]['value'])));
147 }
148
149 /**
150 * Build the table portion of the form for the book administration page.
151 *
152 * @see book_admin_edit()
153 */
154 function _book_admin_table(stdClass $node, &$form) {
155 $form['table'] = array(
156 '#theme' => 'book_admin_table',
157 '#tree' => TRUE,
158 );
159
160 $tree = book_menu_subtree_data($node->book);
161 $tree = array_shift($tree); // Do not include the book item itself.
162 if ($tree['below']) {
163 $hash = sha1(serialize($tree['below']));
164 // Store the hash value as a hidden form element so that we can detect
165 // if another user changed the book hierarchy.
166 $form['tree_hash'] = array(
167 '#type' => 'hidden',
168 '#default_value' => $hash,
169 );
170 $form['tree_current_hash'] = array(
171 '#type' => 'value',
172 '#value' => $hash,
173 );
174 _book_admin_table_tree($tree['below'], $form['table']);
175 }
176
177 }
178
179 /**
180 * Recursive helper to build the main table in the book administration page form.
181 *
182 * @see book_admin_edit()
183 */
184 function _book_admin_table_tree($tree, &$form) {
185 foreach ($tree as $data) {
186 $form['book-admin-' . $data['link']['nid']] = array(
187 '#item' => $data['link'],
188 'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
189 'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
190 'href' => array('#type' => 'value', '#value' => $data['link']['href']),
191 'title' => array(
192 '#type' => 'textfield',
193 '#default_value' => $data['link']['link_title'],
194 '#maxlength' => 255,
195 '#size' => 40,
196 ),
197 'weight' => array(
198 '#type' => 'weight',
199 '#default_value' => $data['link']['weight'],
200 '#delta' => 15,
201 ),
202 'plid' => array(
203 '#type' => 'textfield',
204 '#default_value' => $data['link']['plid'],
205 '#size' => 6,
206 ),
207 'mlid' => array(
208 '#type' => 'hidden',
209 '#default_value' => $data['link']['mlid'],
210 ),
211 );
212 if ($data['below']) {
213 _book_admin_table_tree($data['below'], $form);
214 }
215 }
216
217 return $form;
218 }
219
220 /**
221 * Theme function for the book administration page form.
222 *
223 * @ingroup themeable
224 * @see book_admin_table()
225 */
226 function theme_book_admin_table($variables) {
227 $form = $variables['form'];
228
229 drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
230 drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');
231
232 $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3'));
233
234 $rows = array();
235 $destination = drupal_get_destination();
236 $access = user_access('administer nodes');
237 foreach (element_children($form) as $key) {
238 $nid = $form[$key]['nid']['#value'];
239 $href = $form[$key]['href']['#value'];
240
241 // Add special classes to be used with tabledrag.js.
242 $form[$key]['plid']['#attributes']['class'] = array('book-plid');
243 $form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
244 $form[$key]['weight']['#attributes']['class'] = array('book-weight');
245
246 $data = array(
247 theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']),
248 drupal_render($form[$key]['weight']),
249 drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
250 l(t('view'), $href),
251 $access ? l(t('edit'), 'node/' . $nid . '/edit', array('query' => $destination)) : '&nbsp',
252 $access ? l(t('delete'), 'node/' . $nid . '/delete', array('query' => $destination) ) : '&nbsp',
253 );
254 $row = array('data' => $data);
255 if (isset($form[$key]['#attributes'])) {
256 $row = array_merge($row, $form[$key]['#attributes']);
257 }
258 $row['class'][] = 'draggable';
259 $rows[] = $row;
260 }
261
262 return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'book-outline')));
263 }
264

  ViewVC Help
Powered by ViewVC 1.1.2