comparison modules/book/book.admin.inc @ 7:fff6d4c8c043 6.3

Drupal 6.3
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:30:28 +0100
parents c1f4ac30525a
children 8b6c45761e01
comparison
equal deleted inserted replaced
6:2cfdc3c92142 7:fff6d4c8c043
1 <?php 1 <?php
2 // $Id: book.admin.inc,v 1.8 2008/01/08 10:35:41 goba Exp $ 2 // $Id: book.admin.inc,v 1.8.2.2 2008/07/08 10:19:46 goba Exp $
3 3
4 /** 4 /**
5 * @file 5 * @file
6 * Admin page callbacks for the book module. 6 * Admin page callbacks for the book module.
7 */ 7 */
70 */ 70 */
71 function book_admin_edit($form_state, $node) { 71 function book_admin_edit($form_state, $node) {
72 drupal_set_title(check_plain($node->title)); 72 drupal_set_title(check_plain($node->title));
73 $form = array(); 73 $form = array();
74 $form['#node'] = $node; 74 $form['#node'] = $node;
75 $form['table'] = _book_admin_table($node); 75 _book_admin_table($node, $form);
76 $form['save'] = array( 76 $form['save'] = array(
77 '#type' => 'submit', 77 '#type' => 'submit',
78 '#value' => t('Save book pages'), 78 '#value' => t('Save book pages'),
79 ); 79 );
80 return $form; 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 }
81 } 93 }
82 94
83 /** 95 /**
84 * Handle submission of the book administrative page form. 96 * Handle submission of the book administrative page form.
85 * 97 *
126 /** 138 /**
127 * Build the table portion of the form for the book administration page. 139 * Build the table portion of the form for the book administration page.
128 * 140 *
129 * @see book_admin_edit() 141 * @see book_admin_edit()
130 */ 142 */
131 function _book_admin_table($node) { 143 function _book_admin_table($node, &$form) {
132 $form = array( 144 $form['table'] = array(
133 '#theme' => 'book_admin_table', 145 '#theme' => 'book_admin_table',
134 '#tree' => TRUE, 146 '#tree' => TRUE,
135 ); 147 );
136 148
137 $tree = book_menu_subtree_data($node->book); 149 $tree = book_menu_subtree_data($node->book);
138 $tree = array_shift($tree); // Do not include the book item itself. 150 $tree = array_shift($tree); // Do not include the book item itself.
139 if ($tree['below']) { 151 if ($tree['below']) {
140 _book_admin_table_tree($tree['below'], $form); 152 $hash = sha1(serialize($tree['below']));
141 } 153 // Store the hash value as a hidden form element so that we can detect
142 return $form; 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 }
143 } 165 }
144 166
145 /** 167 /**
146 * Recursive helper to build the main table in the book administration page form. 168 * Recursive helper to build the main table in the book administration page form.
147 * 169 *
224 } 246 }
225 247
226 return theme('table', $header, $rows, array('id' => 'book-outline')); 248 return theme('table', $header, $rows, array('id' => 'book-outline'));
227 } 249 }
228 250
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 }