Index: book.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/book.module,v
retrieving revision 1.276
diff -u -F^f -r1.276 book.module
--- book.module	22 Dec 2004 20:47:47 -0000	1.276
+++ book.module	7 Jan 2005 09:09:56 -0000
@@ -181,6 +181,7 @@ function book_load($node) {
  */
 function book_insert($node) {
   db_query("INSERT INTO {book} (nid, parent, weight, log) VALUES (%d, %d, %d, '%s')", $node->nid, $node->parent, $node->weight, $node->log);
+  book_build_menu_module_menu();
 }
 
 /**
@@ -188,6 +189,7 @@ function book_insert($node) {
  */
 function book_update($node) {
   db_query("UPDATE {book} SET parent = %d, weight = %d, log = '%s' WHERE nid = %d", $node->parent, $node->weight, $node->log, $node->nid);
+  book_build_menu_module_menu();
 }
 
 /**
@@ -195,6 +197,7 @@ function book_update($node) {
  */
 function book_delete(&$node) {
   db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
+  book_build_menu_module_menu();
 }
 
 /**
@@ -252,18 +255,21 @@ function book_outline() {
     switch ($op) {
       case t('Add to book outline'):
         db_query('INSERT INTO {book} (nid, parent, weight) VALUES (%d, %d, %d)', $node->nid, $edit['parent'], $edit['weight']);
+        book_build_menu_module_menu();
         drupal_set_message(t('Added the post to the book.'));
         drupal_goto("node/$node->nid");
         break;
 
       case t('Update book outline'):
         db_query('UPDATE {book} SET parent = %d, weight = %d WHERE nid = %d', $edit['parent'], $edit['weight'], $node->nid);
+        book_build_menu_module_menu();
         drupal_set_message(t('Updated the book outline.'));
         drupal_goto("node/$node->nid");
         break;
 
       case t('Remove from book outline'):
         db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
+        book_build_menu_module_menu();
         drupal_set_message(t('Removed the post from the book.'));
         drupal_goto("node/$node->nid");
         break;
@@ -713,12 +719,14 @@ function book_admin_save($nid, $edit = a
       $title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $nid));
       if ($title != $value['title']) {
         db_query("UPDATE {node} SET title = '%s' WHERE nid = %d", $value['title'], $nid);
+        book_build_menu_module_menu();
       }
 
       // Check to see whether the weight needs updating:
       $weight = db_result(db_query('SELECT weight FROM {book} WHERE nid = %d', $nid));
       if ($weight != $value['weight']) {
         db_query('UPDATE {book} SET weight = %d WHERE nid = %d', $value['weight'], $nid);
+        book_build_menu_module_menu();
       }
     }
 
@@ -804,4 +812,56 @@ function book_help($section) {
   }
 }
 
+/**
+* Create menus for books in the menu system
+*/
+function book_build_menu_module_menu() {
+  $menu = MENU_MODULE_MENU | MENU_MODIFIED_BY_MODULE;
+  $item = MENU_MODULE_ITEM | MENU_MODIFIED_BY_MODULE;
+
+  //Remove existing module menus
+  db_query('DELETE FROM {menu} WHERE module=%d AND (type=%d OR type=%d)', 'book', MENU_MODULE_MENU | MENU_MODIFIED_BY_MODULE, MENU_MODULE_ITEM | MENU_MODIFIED_BY_MODULE);
+  //Get book hierarchies
+  $result = db_query('SELECT DISTINCT(n.nid), n.title, b.parent, b.weight FROM {node} n ' . node_access_join_sql() . ' INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND ' . node_access_where_sql() . ' AND n.moderate = 0 ORDER BY b.parent, b.weight, n.title'); 
+  while ($node = db_fetch_object($result)) {
+    $list = $children[$node->parent] ? $children[$node->parent] : array();
+    array_push($list, $node);
+    $children[$node->parent] = $list;
+  }
+
+  //Build and execute insert of new module menu - rebuild menu system array
+  if ($tree = book_menu_tree_recurse(0, $children, 0, 0, $menu, $item)) {
+    $q = rtrim($tree, ', ');
+    db_query('INSERT INTO {menu} (mid, pid, path, title, weight, type, module) VALUES ' . $q);
+    menu_rebuild();
+  }
+}
+
+/**
+*  Recursively traverse books' hiearchies and return SQL insert parameters
+*/
+function book_menu_tree_recurse($pid, $children, $pmid, $mid, $menu, $item) {
+
+  if ($children[$pid]) {
+    foreach ($children[$pid] as $foo => $node) {
+      $node->mid = db_next_id('{menu}_mid');
+      $node->pmid = $pmid;
+      if ($output .= book_menu_tree_recurse($node->nid, $children, $node->mid, db_next_id('{menu}_mid'), $menu, $item)) {
+        if ($pmid==0) {
+          $output .= "($node->mid, $node->pmid, 'node/$node->nid', '$node->title', $node->weight, $menu, 'book'), ";
+        } else {
+          $output .= "($node->mid, $node->pmid, 'node/$node->nid', '$node->title', $node->weight, $item, 'book'), ";
+        }
+      } else {
+        if ($pid==0) {
+          $output .= "($node->mid, $node->pmid, 'node/$node->nid', '$node->title', $node->weight, $menu, 'book'), ";
+        } else {
+          $output .= "($node->mid, $node->pmid, 'node/$node->nid', '$node->title', $node->weight, $item, 'book'), ";
+        }
+      }
+    }  
+  }
+	
+return $output;
+}
 ?>

***** CVS exited normally with code 1 *****