--- book.old Fri Dec 10 00:00:46 2004 +++ book.module Fri Dec 10 02:24:37 2004 @@ -124,41 +124,92 @@ return $items; } - /** * 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. + * Shows one or more table of contents for books in separate blocks. + * */ function book_block($op = 'list', $delta = 0) { + // These variables protect from having duplicate book menus on the same page + // by tracking which blocks have already been generated. + static $completed_deltas = array(); + $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('SELECT n.nid, n.title, b.parent FROM {node} n '. node_access_join_sql() .' INNER JOIN {book} b ON n.nid = b.nid WHERE '. node_access_where_sql() .' AND n.nid = %d', arg(1)); - if (db_num_rows($result) > 0) { - $node = db_fetch_object($result); + switch ($op) { + case 'list': + if ($op == 'list') { + $book_blocks[0]['info'] = t('Book navigation'); + + // Add blocks for each book to list + $result = db_query('SELECT n.title, b.nid FROM {book} b INNER JOIN {node} n on n.nid = b.nid WHERE b.parent = 0'); + while ($books = db_fetch_object($result)) { + $book_blocks[$books->nid] = array('info' => $books->title . t(' (book)')); + } + return($book_blocks); + } + break; + case 'view': + // Expand the book menu appropriately if we are viewing a node that belongs to a book + if ($delta && (arg(0) == 'node' && is_numeric(arg(1)))) { - $path = book_location($node); - $path[] = $node; + $result = book_block_query(arg(1)); + + if (db_num_rows($result) > 0) { + $node = db_fetch_object($result); + $path = book_get_path($node); + // See if the node belongs to the book we are currently tyring to create a block for. If not, change + // the path so it display collapsed + if ($path[0]->nid != $delta) { + $result = book_block_query($delta); + $node = db_fetch_object($result); + $path = book_get_path($node); + } + } + } + // If we aren't viewing a node, just diplay the block. Use the parent node of the book we are currently + // trying to generate a block for to seed the menu. + elseif ($delta && !(arg(0) == 'node' && is_numeric(arg(1)))) { + $result = book_block_query($delta); + $node = db_fetch_object($result); + $path = book_get_path($node); + } + // Handles book navigation block + elseif (!$delta && (arg(0) == 'node' && is_numeric(arg(1)))) { + $result = book_block_query(arg(1)); + if (db_num_rows($result) > 0) { + $node = db_fetch_object($result); + $path = book_get_path($node); + } + } + if($path && !in_array($path[0]->nid, $completed_deltas)) { $expand = array(); foreach ($path as $key => $node) { $expand[] = $node->nid; - } - + } $block['subject'] = $path[0]->title; $block['content'] = book_tree($expand[0], 5, $expand); + $completed_deltas[] = $path[0]->nid; } - } - return $block; + break; } +} +/** + * Helper function to book_block function to return the path for a node. + */ +function book_get_path($node) { + $path = book_location($node); + $path[] = $node; + return $path; +} + +/** + * Helper function to book_block function to return results for frequently used queries in the function. + */ +function book_block_query($query_arg) { + return db_query('SELECT n.nid, n.title, b.parent FROM {node} n '. node_access_join_sql() .' INNER JOIN {book} b ON n.nid = b.nid WHERE '. node_access_where_sql() .' AND n.nid = %d', $query_arg); } /**