Index: book.module =================================================================== RCS file: /cvs/drupal/drupal/modules/book.module,v retrieving revision 1.288.2.8 diff -u -w -b -r1.288.2.8 book.module --- book.module 3 Jul 2005 16:09:12 -0000 1.288.2.8 +++ book.module 29 Jul 2005 00:45:28 -0000 @@ -7,6 +7,113 @@ */ /** + * book_current_book() + * returns the nid of the top level node of the book tree to which $nid belongs + * used as the initial value of the book select + */ + function book_top_level_book($nid) { + if ($nid){ + $book_list_query = db_query('SELECT * FROM {book} ORDER BY nid'); + while($book = db_fetch_object($book_list_query)) { + $book_list[$book->nid] = $book; + } + $current->nid = $nid; + while($book_list[$current->nid]->parent != 0) { + $current->nid = $book_list[$current->nid]->parent; + } + return $current->nid; + } + } + + /** + * book_top_level_books() + * returns an array of all top level book nodes + * used to initialize the book select + */ + function book_top_level_booklist() { + if (user_access('administer nodes')) { + $root_books[0] = '<'. t('New book') .'>'; + } + $book_list_query = db_query('SELECT b.*, n.title FROM {book} b JOIN {node} n ON n.nid = b.nid ORDER BY b.weight, n.title'); + while ($node = db_fetch_object($book_list_query)) { + if ($node->parent == 0) $root_books[$node->nid] = $node->title; + } + return $root_books; + } + + /** + * book_booklist() + * returns the array of book data that everything else operates on. + */ + function book_booklist_query() { + $result = db_query(db_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 ORDER BY b.weight, n.title')); + while ($node = db_fetch_object($result)) { + if (!$children[$node->parent]) { + $children[$node->parent] = array(); + } + array_push($children[$node->parent], $node); + } + return $children; + } + + /** + * book_current_book_chapters() + * returns an array of all the books beneath $root_book_nid in the book tree. + * used to initialize the parent select + * @param + * $root_book_nid : the top level book + * @param + * $booklist: result of book_booklist_query() + */ + function book_current_book_chapters($booklist, $root_book_nid) { + if ($root_book_nid){ + return book_toc_recurse($root_book_nid, '', array(), $booklist, $exclude); + } else { + return array(0 => 'First page'); + } + } + /** + * book_bookjs + * @param + * $root_books: array of top level books + * @param + * $booklist: result of book_booklist_query() + */ + function book_insert_bookjs($booklist, $root_books) { + foreach($root_books as $nid => $title) { + $root_book_pages[$nid] = + book_toc_recurse($nid, '', array(), $booklist, $exclude); + } + foreach($root_book_pages as $key => $root_book) { + $key_value = $key ? $key : 0; + $init_option .= "case \"$key_value\":\n"; + $init_option .= 'parentselect.options[0] = new Option("<'. t('top-level') . ">\", $key_value);"; + foreach($root_book as $book_nid => $book_page_title) { + $init_option .= "parentselect.options[parentselect.options.length] = new Option(\"". + htmlspecialchars($book_page_title) . + "\", $book_nid);\n"; + } + $init_option .= "break;\n"; + } + $jscript = " + + "; + drupal_set_html_head($jscript); + } + +/** * Implementation of hook_node_name(). */ function book_node_name($node) { @@ -220,8 +327,20 @@ global $user; $op = $_POST['op']; - - $output = form_select(t('Parent'), 'parent', ($node->parent ? $node->parent : arg(4)), book_toc($node->nid), t('The parent that this page belongs in. Note that pages whose parent is <top-level> are regarded as independent, top-level books.')); + $book_list = book_booklist_query(); + $top_level_book_list = book_top_level_booklist(); + $top_level_book = $node->parent > 0 ? book_top_level_book($node->parent) : $node->nid; + + $output = form_select(t('Book'), 'book', + $top_level_book, + $top_level_book_list, + t('The top level book that this page belongs in.'), + 'onchange="setparentselect(this)"'); + + $output .= form_select(t('Parent'), 'parent', + ($node->parent ? $node->parent : arg(4)), + array($top_level_book => t('top-level')) + book_current_book_chapters($book_list, $top_level_book), + t('The parent of this page.')); if (function_exists('taxonomy_node_form')) { $output .= implode('', taxonomy_node_form('book', $node)); @@ -239,7 +358,7 @@ // authored by that user: $output .= form_hidden('revision', 1); } - + book_insert_bookjs($book_list, $top_level_book_list); return $output; } @@ -275,8 +394,22 @@ default: $page = db_fetch_object(db_query('SELECT * FROM {book} WHERE nid = %d', $node->nid)); + $book_list = book_booklist_query(); + $top_level_book_list = book_top_level_booklist(); + $top_level_book = $page->parent > 0 ? book_top_level_book($page->parent) : $page->nid; + + $output = form_select(t('Book'), 'book', + $top_level_book, + $top_level_book_list, + t('The top level book that this page belongs in.'), + 'onchange="setparentselect(this)"'); + + $output .= form_select(t('Parent'), 'parent', + $page->parent, + array($top_level_book => t('top-level')) + book_current_book_chapters($book_list, $top_level_book), + t('The parent of this page.')); - $output = form_select(t('Parent'), 'parent', $page->parent, book_toc($node->nid), t('The parent page in the book.')); + #$output = form_select(t('Parent'), 'parent', $page->parent, book_toc($node->nid), t('The parent page in the book.')); $output .= form_weight(t('Weight'), 'weight', $page->weight, 15, t('Pages at a given level are ordered first by weight and then by title.')); if ($page->nid) { @@ -286,7 +419,7 @@ else { $output .= form_submit(t('Add to book outline')); } - + book_insert_bookjs($book_list, $top_level_book_list); drupal_set_title(check_plain($node->title)); print theme('page', form($output)); } ***** CVS exited normally with code 1 *****