diff -urp book_orig/book.admin.inc book/book.admin.inc --- book_orig/book.admin.inc 2008-10-22 21:26:01.000000000 +0200 +++ book/book.admin.inc 2010-05-21 22:49:03.000000000 +0200 @@ -160,7 +160,12 @@ function _book_admin_table($node, &$form '#type' => 'value', '#value' => $hash, ); - _book_admin_table_tree($tree['below'], $form['table']); + // Delta must be an integer > half the number of nodes in the book. + $delta = ceil(count($tree['below']) / 2) + 1; + if ($delta < 15) { + $delta = 15; + } + _book_admin_table_tree($tree['below'], $form['table'], $delta); } } @@ -169,7 +174,7 @@ function _book_admin_table($node, &$form * * @see book_admin_edit() */ -function _book_admin_table_tree($tree, &$form) { +function _book_admin_table_tree($tree, &$form, $delta) { foreach ($tree as $data) { $form['book-admin-'. $data['link']['nid']] = array( '#item' => $data['link'], @@ -185,7 +190,7 @@ function _book_admin_table_tree($tree, & 'weight' => array( '#type' => 'weight', '#default_value' => $data['link']['weight'], - '#delta' => 15, + '#delta' => $delta, ), 'plid' => array( '#type' => 'textfield', @@ -198,7 +203,7 @@ function _book_admin_table_tree($tree, & ), ); if ($data['below']) { - _book_admin_table_tree($data['below'], $form); + _book_admin_table_tree($data['below'], $form, $delta); } } diff -urp book_orig/book.module book/book.module --- book_orig/book.module 2009-02-25 12:47:37.000000000 +0100 +++ book/book.module 2010-05-21 21:51:46.000000000 +0200 @@ -214,6 +214,38 @@ function book_block($op = 'list', $delta // There should only be one element at the top level. $data = array_shift($tree); $block['subject'] = theme('book_title_link', $data['link']); + /* With many pages on the same depth one level below the index page for the book + the bookmenu becomes very long. This code was added to create a "context sensitive" + menu when browsing a book: an x number of pages above the current one and the same number + of pages below the current one is shown in the book menu. + At the index page and the first (x + 1) pages only the first (2*x + 1) pages are shown. */ + if (isset($data['below'])) { + $no_shown_above_below = 10; + $totaal = count($data['below']); + if ($totaal > (2 * $no_shown_above_below + 1)) { + $nummer_in_array = 0; + $on_index_page = 1; + // we need to know where we are in the menu array + foreach($data['below'] as $key => $subdata) { + $nummer_in_array++; + if ($subdata['link']['in_active_trail'] == 1) { + $on_index_page--; + break; // active page found + } + } + if (($nummer_in_array < ($no_shown_above_below + 1) && $on_index_page == 0) | $on_index_page == 1) { + // early page or on index page + $data['below'] = array_slice($data['below'], 0, (2 * $no_shown_above_below + 1)); + } + elseif ($totaal - $nummer_in_array < $no_shown_above_below && $on_index_page == 0) { + // close to the end of the number of pages + $data['below'] = array_slice($data['below'], ($totaal - (2 * $no_shown_above_below + 1)), (2 * $no_shown_above_below + 1)); + } + else { + $data['below'] = array_slice($data['below'], ($nummer_in_array - $no_shown_above_below - 1), (2 * $no_shown_above_below + 1)); + } + } + } $block['content'] = ($data['below']) ? menu_tree_output($data['below']) : ''; } } @@ -381,12 +413,13 @@ function _book_add_form_elements(&$form, } $form['book']['plid'] = _book_parent_select($node->book); - + // Delta must be an integer > the number of nodes in the book on the same level. + $delta = _book_get_delta_for_weight(); $form['book']['weight'] = array( '#type' => 'weight', '#title' => t('Weight'), '#default_value' => $node->book['weight'], - '#delta' => 15, + '#delta' => $delta, '#weight' => 5, '#description' => t('Pages at a given level are ordered first by weight and then by title.'), ); @@ -1094,3 +1127,23 @@ function book_menu_subtree_data($item) { return $tree[$cid]; } +/** + * To be able to sort pages in a book the dropdown for weight should contain enough values. + */ + function _book_get_delta_for_weight() { + // Delta must be an integer > the number of nodes in the book on the same level. + $delta_results = db_query(db_rewrite_sql("SELECT COUNT(*) AS total, MIN(ml.weight) AS min_weight, MAX(ml.weight) as max_weight FROM {book} b INNER JOIN {node} n ON b.nid = n.nid INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE n.type = 'book' and ml.plid > 0 GROUP BY ml.plid")); + $delta = 14; + while ($d_result = db_fetch_array($delta_results)) { + if ($d_result['total'] > $delta) { + $delta = $d_result['total']; + } + elseif (abs($d_result['min_weight']) > $delta) { + $delta = abs($d_result['min_weight']); + } + elseif ($d_result['max_weight'] > $delta) { + $delta = $d_result['max_weight']; + } + } + return $delta + 1; +}