Index: book.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/book.module,v
retrieving revision 1.280
diff -u -F^f -r1.280 book.module
--- book.module	19 Jan 2005 22:13:14 -0000	1.280
+++ book.module	20 Jan 2005 01:47:24 -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,67 @@ function book_help($section) {
   }
 }
 
+/**
+* Create menus for books in the menu system and update their associated blocks
+*/
+function book_build_menu_module_menu() {
+  //remember menu_module_menu block settings
+  $result = db_query('SELECT m.path, b.status, b.weight, b.region, b.throttle, b.visibility, b.pages FROM {menu} m INNER JOIN {blocks} b ON m.mid = b.delta WHERE type=%d', MENU_MODULE_MENU | MENU_MODIFIED_BY_MODULE);
+  while ($foo = db_fetch_object($result)) {
+    $menublocksettings[$foo->path] = array('status'=>$foo->status, 'weight'=>$foo->weight, 'region'=>$foo->region, 'throttle'=>$foo->throttle, 'visibility'=>$foo->visibility, 'pages'=>$foo->pages);
+  }
+  //Remove existing module menus
+  db_query('DELETE FROM {menu} WHERE module="%s" 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(node_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 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 - rehash blocks - restore block settings
+  if ($tree = book_menu_tree_recurse(0, $children, 0, 0, MENU_MODULE_MENU | MENU_MODIFIED_BY_MODULE, MENU_MODULE_ITEM | MENU_MODIFIED_BY_MODULE)) {
+    $q = rtrim($tree, ', ');
+	db_query('INSERT INTO {menu} (mid, pid, path, title, weight, type, module) VALUES ' . $q);
+    menu_rebuild();
+	_block_rehash();
+    //restore menu_module_menu block settings
+    $result = db_query('SELECT m.path, b.delta FROM {menu} m INNER JOIN {blocks} b ON m.mid = b.delta WHERE m.type=%d', MENU_MODULE_MENU | MENU_MODIFIED_BY_MODULE);
+    while ($menublock = db_fetch_object($result)) {
+      if (array_key_exists($menublock->path, $menublocksettings)) {
+        echo"UPDATE {blocks} SET status=".$menublocksettings[$menublock->path][status].", weight=".$menublocksettings[$menublock->path][weight].", region=".$menublocksettings[$menublock->path][region].", throttle=".$menublocksettings[$menublock->path][throttle].", visibility=".$menublocksettings[$menublock->path][visibility].", pages='".$menublocksettings[$menublock->path][pages]."' WHERE delta=".$menublock->delta." <br/>";
+        db_query("UPDATE {blocks} SET status=%d, weight=%d, region=%d, throttle=%d, visibility=%d, pages='%s' WHERE delta=%d", $menublocksettings[$menublock->path][status], $menublocksettings[$menublock->path][weight], $menublocksettings[$menublock->path][region], $menublocksettings[$menublock->path][throttle], $menublocksettings[$menublock->path][visibility], $menublocksettings[$menublock->path][pages], $menublock->delta);
+      }
+    }
+  }
+}
+
+/**
+*  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 *****