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 24 Jan 2005 02:12:46 -0000 @@ -117,42 +117,6 @@ function book_menu($may_cache) { } /** - * Implementation of hook_block(). - * - * Displays the book table of contents in a block when the current page is a - * single-node view of a book node. - */ -function book_block($op = 'list', $delta = 0) { - $block = array(); - if ($op == 'list') { - $block[0]['info'] = t('Book navigation'); - return $block; - } - else if ($op == 'view') { - // Only display this block when the user is browsing a book: - if (arg(0) == 'node' && is_numeric(arg(1))) { - $result = db_query(node_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.nid = %d'), arg(1)); - if (db_num_rows($result) > 0) { - $node = db_fetch_object($result); - - $path = book_location($node); - $path[] = $node; - - $expand = array(); - foreach ($path as $key => $node) { - $expand[] = $node->nid; - } - - $block['subject'] = $path[0]->title; - $block['content'] = book_tree($expand[0], 5, $expand); - } - } - - return $block; - } -} - -/** * Implementation of hook_load(). */ function book_load($node) { @@ -181,6 +145,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 +153,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 +161,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 +219,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 +683,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 +776,66 @@ 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, b.type FROM {menu} m INNER JOIN {blocks} b ON m.mid = b.delta WHERE m.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, 'type'=>$foo->type); + } + //Remove existing module menus + db_query('DELETE FROM {menu} WHERE type=%d OR type=%d', 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) 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)) { + db_query("UPDATE {blocks} SET status=%d, weight=%d, region=%d, throttle=%d, visibility=%d, pages='%s', type='%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], $menublocksettings[$menublock->path][type], $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), "; + } else { + $output .= "($node->mid, $node->pmid, 'node/$node->nid', '$node->title', $node->weight, $item), "; + } + } else { + if ($pid==0) { + $output .= "($node->mid, $node->pmid, 'node/$node->nid', '$node->title', $node->weight, $menu), "; + } else { + $output .= "($node->mid, $node->pmid, 'node/$node->nid', '$node->title', $node->weight, $item), "; + } + } + } + } + +return $output; +} ?> ***** CVS exited normally with code 1 *****