diff 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
line wrap: on
line diff
--- a/modules/book/book.admin.inc	Tue Dec 23 14:30:08 2008 +0100
+++ b/modules/book/book.admin.inc	Tue Dec 23 14:30:28 2008 +0100
@@ -1,5 +1,5 @@
 <?php
-// $Id: book.admin.inc,v 1.8 2008/01/08 10:35:41 goba Exp $
+// $Id: book.admin.inc,v 1.8.2.2 2008/07/08 10:19:46 goba Exp $
 
 /**
  * @file
@@ -72,7 +72,7 @@
   drupal_set_title(check_plain($node->title));
   $form = array();
   $form['#node'] = $node;
-  $form['table'] = _book_admin_table($node);
+  _book_admin_table($node, $form);
   $form['save'] = array(
     '#type' => 'submit',
     '#value' => t('Save book pages'),
@@ -81,6 +81,18 @@
 }
 
 /**
+ * Check that the book has not been changed while using the form.
+ *
+ * @see book_admin_edit()
+ */
+function book_admin_edit_validate($form, &$form_state) {
+  if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
+    form_set_error('', t('This book has been modified by another user, the changes could not be saved.'));
+    $form_state['rebuild'] = TRUE;
+  }
+}
+
+/**
  * Handle submission of the book administrative page form.
  *
  * This function takes care to save parent menu items before their children.
@@ -128,8 +140,8 @@
  *
  * @see book_admin_edit()
  */
-function _book_admin_table($node) {
-  $form = array(
+function _book_admin_table($node, &$form) {
+  $form['table'] = array(
     '#theme' => 'book_admin_table',
     '#tree' => TRUE,
   );
@@ -137,9 +149,19 @@
   $tree = book_menu_subtree_data($node->book);
   $tree = array_shift($tree); // Do not include the book item itself.
   if ($tree['below']) {
-    _book_admin_table_tree($tree['below'], $form);
+    $hash = sha1(serialize($tree['below']));
+    // Store the hash value as a hidden form element so that we can detect
+    // if another user changed the book hierarchy.
+    $form['tree_hash'] = array(
+      '#type' => 'hidden',
+      '#default_value' => $hash,
+    );
+    $form['tree_current_hash'] = array(
+      '#type' => 'value',
+      '#value' => $hash,
+    );
+    _book_admin_table_tree($tree['below'], $form['table']);
   }
-  return $form;
 }
 
 /**
@@ -226,25 +248,3 @@
   return theme('table', $header, $rows, array('id' => 'book-outline'));
 }
 
-/**
- * Recursive helper to sort each layer of a book tree by weight.
- */
-function _book_admin_sort_tree(&$tree) {
-  uasort($tree, '_book_admin_compare');
-  foreach ($tree as $key => $subtree) {
-    if (!empty($tree[$key]['below'])) {
-      _book_admin_sort_tree($tree[$key]['below']);
-    }
-  }
-}
-
-/**
- * Used by uasort() in _book_admin_sort_tree() to compare items in a book tree.
- */
-function _book_admin_compare($a, $b) {
-  $weight = $a['link']['weight'] - $b['link']['weight'];
-  if ($weight) {
-    return $weight;
-  }
-  return strncmp($a['link']['title'], $b['link']['title']);
-}