Commit 7cd8427f authored by Dries's avatar Dries

- Patch #257730 by Senpai: code clean-up for book module.

parent a7d345d7
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// $Id$ // $Id$
/** /**
* @file book-export-html.tpl.php * @file
* Default theme implementation for printed version of book outline. * Default theme implementation for printed version of book outline.
* *
* Available variables: * Available variables:
...@@ -45,9 +45,7 @@ ...@@ -45,9 +45,7 @@
<div class="section-<?php print $i; ?>"> <div class="section-<?php print $i; ?>">
<?php $div_close .= '</div>'; ?> <?php $div_close .= '</div>'; ?>
<?php endfor; ?> <?php endfor; ?>
<?php print $contents; ?> <?php print $contents; ?>
<?php print $div_close; ?> <?php print $div_close; ?>
</body> </body>
</html> </html>
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// $Id$ // $Id$
/** /**
* @file book-navigation.tpl.php * @file
* Default theme implementation to navigate books. Presented under nodes that * Default theme implementation to navigate books. Presented under nodes that
* are a part of book outlines. * are a part of book outlines.
* *
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// $Id$ // $Id$
/** /**
* @file book-node-export-html.tpl.php * @file
* Default theme implementation for rendering a single node in a printer * Default theme implementation for rendering a single node in a printer
* friendly outline. * friendly outline.
* *
......
...@@ -46,6 +46,7 @@ function book_admin_settings() { ...@@ -46,6 +46,7 @@ function book_admin_settings() {
); );
$form['array_filter'] = array('#type' => 'value', '#value' => TRUE); $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
$form['#validate'][] = 'book_admin_settings_validate'; $form['#validate'][] = 'book_admin_settings_validate';
return system_settings_form($form); return system_settings_form($form);
} }
...@@ -77,6 +78,7 @@ function book_admin_edit($form_state, $node) { ...@@ -77,6 +78,7 @@ function book_admin_edit($form_state, $node) {
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Save book pages'), '#value' => t('Save book pages'),
); );
return $form; return $form;
} }
...@@ -139,6 +141,7 @@ function _book_admin_table($node) { ...@@ -139,6 +141,7 @@ function _book_admin_table($node) {
if ($tree['below']) { if ($tree['below']) {
_book_admin_table_tree($tree['below'], $form); _book_admin_table_tree($tree['below'], $form);
} }
return $form; return $form;
} }
...@@ -246,5 +249,6 @@ function _book_admin_compare($a, $b) { ...@@ -246,5 +249,6 @@ function _book_admin_compare($a, $b) {
if ($weight) { if ($weight) {
return $weight; return $weight;
} }
return strncmp($a['link']['title'], $b['link']['title']); return strncmp($a['link']['title'], $b['link']['title']);
} }
; $Id$ ; $Id$
name = Book name = Book
description = Allows users to structure site pages in a hierarchy or outline. description = Allows users to structure site pages in a hierarchy or outline.
package = Core - optional package = Core - optional
......
...@@ -23,7 +23,7 @@ function book_uninstall() { ...@@ -23,7 +23,7 @@ function book_uninstall() {
} }
function _book_install_type_create() { function _book_install_type_create() {
// Create an additional node type // Create an additional node type.
$book_node_type = array( $book_node_type = array(
'type' => 'book', 'type' => 'book',
'name' => t('Book page'), 'name' => t('Book page'),
...@@ -149,7 +149,7 @@ function book_update_6000() { ...@@ -149,7 +149,7 @@ function book_update_6000() {
} }
elseif ($_SESSION['book_update_6000_orphans']) { elseif ($_SESSION['book_update_6000_orphans']) {
// Do the first batched part of the update - collect orphans. // Do the first batched part of the update - collect orphans.
$update_count = 400; // Update this many at a time $update_count = 400; // Update this many at a time.
$result = db_query_range("SELECT * FROM {book_temp}", $_SESSION['book_update_6000_orphans']['from'], $update_count); $result = db_query_range("SELECT * FROM {book_temp}", $_SESSION['book_update_6000_orphans']['from'], $update_count);
$has_rows = FALSE; $has_rows = FALSE;
...@@ -175,7 +175,6 @@ function book_update_6000() { ...@@ -175,7 +175,6 @@ function book_update_6000() {
$_SESSION['book_update_6000_orphans']['from'] += $update_count; $_SESSION['book_update_6000_orphans']['from'] += $update_count;
} }
else { else {
// Done with this part
if (!empty($_SESSION['book_update_6000_orphans']['book'])) { if (!empty($_SESSION['book_update_6000_orphans']['book'])) {
// The orphans' parent is added last, so it will be processed first. // The orphans' parent is added last, so it will be processed first.
$_SESSION['book_update_6000'][] = $_SESSION['book_update_6000_orphans']['book']; $_SESSION['book_update_6000'][] = $_SESSION['book_update_6000_orphans']['book'];
...@@ -183,17 +182,18 @@ function book_update_6000() { ...@@ -183,17 +182,18 @@ function book_update_6000() {
$_SESSION['book_update_6000_orphans'] = FALSE; $_SESSION['book_update_6000_orphans'] = FALSE;
} }
$ret['#finished'] = FALSE; $ret['#finished'] = FALSE;
return $ret; return $ret;
} }
else { else {
// Do the next batched part of the update // Run the next batched part of the update.
$update_count = 100; // Update this many at a time $update_count = 100; // Update this many at a time
while ($update_count && $_SESSION['book_update_6000']) { while ($update_count && $_SESSION['book_update_6000']) {
// Get the last node off the stack. // Get the last node off the stack.
$book = array_pop($_SESSION['book_update_6000']); $book = array_pop($_SESSION['book_update_6000']);
// Add all of this node's children to the stack // Add all of this node's children to the stack.
$result = db_query("SELECT * FROM {book_temp} WHERE parent = %d", $book['nid']); $result = db_query("SELECT * FROM {book_temp} WHERE parent = %d", $book['nid']);
while ($a = db_fetch_array($result)) { while ($a = db_fetch_array($result)) {
$_SESSION['book_update_6000'][] = $a; $_SESSION['book_update_6000'][] = $a;
...@@ -205,7 +205,7 @@ function book_update_6000() { ...@@ -205,7 +205,7 @@ function book_update_6000() {
$book = array_merge($book, $parent); $book = array_merge($book, $parent);
} }
else { else {
// There is not a parent - this is a new book. // There is no parent - this is a new book.
$book['plid'] = 0; $book['plid'] = 0;
$book['bid'] = $book['nid']; $book['bid'] = $book['nid'];
} }
...@@ -286,5 +286,3 @@ function book_schema() { ...@@ -286,5 +286,3 @@ function book_schema() {
return $schema; return $schema;
} }
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
*/ */
/** /**
* Implementation of hook_theme() * Implementation of hook_theme().
*/ */
function book_theme() { function book_theme() {
return array( return array(
...@@ -64,6 +64,7 @@ function book_link($type, $node = NULL, $teaser = FALSE) { ...@@ -64,6 +64,7 @@ function book_link($type, $node = NULL, $teaser = FALSE) {
'query' => "parent=" . $node->book['mlid'], 'query' => "parent=" . $node->book['mlid'],
); );
} }
if (user_access('access printer-friendly version')) { if (user_access('access printer-friendly version')) {
$links['book_printer'] = array( $links['book_printer'] = array(
'title' => t('Printer-friendly version'), 'title' => t('Printer-friendly version'),
...@@ -73,6 +74,7 @@ function book_link($type, $node = NULL, $teaser = FALSE) { ...@@ -73,6 +74,7 @@ function book_link($type, $node = NULL, $teaser = FALSE) {
} }
} }
} }
return $links; return $links;
} }
...@@ -140,6 +142,7 @@ function book_menu() { ...@@ -140,6 +142,7 @@ function book_menu() {
'access arguments' => array('access content'), 'access arguments' => array('access content'),
'type' => MENU_CALLBACK, 'type' => MENU_CALLBACK,
); );
return $items; return $items;
} }
...@@ -158,7 +161,7 @@ function _book_outline_remove_access($node) { ...@@ -158,7 +161,7 @@ function _book_outline_remove_access($node) {
} }
/** /**
* Implementation of hook_init(). Add's the book module's CSS. * Implementation of hook_init().
*/ */
function book_init() { function book_init() {
drupal_add_css(drupal_get_path('module', 'book') . '/book.css'); drupal_add_css(drupal_get_path('module', 'book') . '/book.css');
...@@ -176,12 +179,15 @@ function book_block($op = 'list', $delta = '', $edit = array()) { ...@@ -176,12 +179,15 @@ function book_block($op = 'list', $delta = '', $edit = array()) {
case 'list': case 'list':
$block['navigation']['info'] = t('Book navigation'); $block['navigation']['info'] = t('Book navigation');
$block['navigation']['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE; $block['navigation']['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE;
return $block; return $block;
case 'view': case 'view':
$current_bid = 0; $current_bid = 0;
if ($node = menu_get_object()) { if ($node = menu_get_object()) {
$current_bid = empty($node->book['bid']) ? 0 : $node->book['bid']; $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
} }
if (variable_get('book_block_mode', 'all pages') == 'all pages') { if (variable_get('book_block_mode', 'all pages') == 'all pages') {
$block['subject'] = t('Book navigation'); $block['subject'] = t('Book navigation');
$book_menus = array(); $book_menus = array();
...@@ -214,7 +220,9 @@ function book_block($op = 'list', $delta = '', $edit = array()) { ...@@ -214,7 +220,9 @@ function book_block($op = 'list', $delta = '', $edit = array()) {
$block['content'] = ($data['below']) ? menu_tree_output($data['below']) : ''; $block['content'] = ($data['below']) ? menu_tree_output($data['below']) : '';
} }
} }
return $block; return $block;
case 'configure': case 'configure':
$options = array( $options = array(
'all pages' => t('Show block on all pages'), 'all pages' => t('Show block on all pages'),
...@@ -227,7 +235,9 @@ function book_block($op = 'list', $delta = '', $edit = array()) { ...@@ -227,7 +235,9 @@ function book_block($op = 'list', $delta = '', $edit = array()) {
'#default_value' => variable_get('book_block_mode', 'all pages'), '#default_value' => variable_get('book_block_mode', 'all pages'),
'#description' => t("If <em>Show block on all pages</em> is selected, the block will contain the automatically generated menus for all of the site's books. If <em>Show block only on book pages</em> is selected, the block will contain only the one menu corresponding to the current page's book. In this case, if the current page is not in a book, no block will be displayed. The <em>Page specific visibility settings</em> or other visibility settings can be used in addition to selectively display this block."), '#description' => t("If <em>Show block on all pages</em> is selected, the block will contain the automatically generated menus for all of the site's books. If <em>Show block only on book pages</em> is selected, the block will contain only the one menu corresponding to the current page's book. In this case, if the current page is not in a book, no block will be displayed. The <em>Page specific visibility settings</em> or other visibility settings can be used in addition to selectively display this block."),
); );
return $form; return $form;
case 'save': case 'save':
variable_set('book_block_mode', $edit['book_block_mode']); variable_set('book_block_mode', $edit['book_block_mode']);
break; break;
...@@ -241,6 +251,7 @@ function book_block($op = 'list', $delta = '', $edit = array()) { ...@@ -241,6 +251,7 @@ function book_block($op = 'list', $delta = '', $edit = array()) {
*/ */
function theme_book_title_link($link) { function theme_book_title_link($link) {
$link['options']['attributes']['class'] = 'book-title'; $link['options']['attributes']['class'] = 'book-title';
return l($link['title'], $link['href'], $link['options']); return l($link['title'], $link['href'], $link['options']);
} }
...@@ -260,6 +271,7 @@ function book_get_books() { ...@@ -260,6 +271,7 @@ function book_get_books() {
while ($book = db_fetch_array($result)) { while ($book = db_fetch_array($result)) {
$nids[] = $book['bid']; $nids[] = $book['bid'];
} }
if ($nids) { if ($nids) {
$result2 = db_query(db_rewrite_sql("SELECT n.type, n.title, b.*, ml.* FROM {book} b INNER JOIN {node} n on b.nid = n.nid INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE n.nid IN (" . implode(',', $nids) . ") AND n.status = 1 ORDER BY ml.weight, ml.link_title")); $result2 = db_query(db_rewrite_sql("SELECT n.type, n.title, b.*, ml.* FROM {book} b INNER JOIN {node} n on b.nid = n.nid INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE n.nid IN (" . implode(',', $nids) . ") AND n.status = 1 ORDER BY ml.weight, ml.link_title"));
while ($link = db_fetch_array($result2)) { while ($link = db_fetch_array($result2)) {
...@@ -269,11 +281,14 @@ function book_get_books() { ...@@ -269,11 +281,14 @@ function book_get_books() {
} }
} }
} }
return $all_books; return $all_books;
} }
/** /**
* Implementation of hook_form_alter(). Adds the book fieldset to the node form. * Implementation of hook_form_alter().
*
* Adds the book fieldset to the node form.
* *
* @see book_pick_book_submit() * @see book_pick_book_submit()
* @see book_submit() * @see book_submit()
...@@ -281,13 +296,13 @@ function book_get_books() { ...@@ -281,13 +296,13 @@ function book_get_books() {
function book_form_alter(&$form, $form_state, $form_id) { function book_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) { if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
// Add elements to the node form // Add elements to the node form.
$node = $form['#node']; $node = $form['#node'];
$access = user_access('administer book outlines'); $access = user_access('administer book outlines');
if (!$access) { if (!$access) {
if (user_access('add content to books') && ((!empty($node->book['mlid']) && !empty($node->nid)) || book_type_is_allowed($node->type))) { if (user_access('add content to books') && ((!empty($node->book['mlid']) && !empty($node->nid)) || book_type_is_allowed($node->type))) {
// Already in the book hierarchy or this node type is allowed // Already in the book hierarchy, or this node type is allowed.
$access = TRUE; $access = TRUE;
} }
} }
...@@ -310,7 +325,7 @@ function book_form_alter(&$form, $form_state, $form_id) { ...@@ -310,7 +325,7 @@ function book_form_alter(&$form, $form_state, $form_id) {
} }
/** /**
* Build the parent selection form element for the node form or outline tab * Build the parent selection form element for the node form or outline tab.
* *
* This function is also called when generating a new set of options during the * This function is also called when generating a new set of options during the
* AJAX callback, so an array is returned that can be used to replace an existing * AJAX callback, so an array is returned that can be used to replace an existing
...@@ -350,6 +365,7 @@ function _book_parent_select($book_link) { ...@@ -350,6 +365,7 @@ function _book_parent_select($book_link) {
'#attributes' => array('class' => 'book-title-select'), '#attributes' => array('class' => 'book-title-select'),
); );
} }
return $form; return $form;
} }
...@@ -405,7 +421,7 @@ function _book_add_form_elements(&$form, $node) { ...@@ -405,7 +421,7 @@ function _book_add_form_elements(&$form, $node) {
$options = array($nid => '<' . t('create a new book') . '>') + $options; $options = array($nid => '<' . t('create a new book') . '>') + $options;
} }
if (!$node->book['mlid']) { if (!$node->book['mlid']) {
// The node is not currently in a the hierarchy. // The node is not currently in the hierarchy.
$options = array(0 => '<' . t('none') . '>') + $options; $options = array(0 => '<' . t('none') . '>') + $options;
} }
...@@ -457,6 +473,7 @@ function _book_update_outline(&$node) { ...@@ -457,6 +473,7 @@ function _book_update_outline(&$node) {
$node->book['parent_mismatch'] = TRUE; // Likely when JS is disabled. $node->book['parent_mismatch'] = TRUE; // Likely when JS is disabled.
} }
} }
if (menu_link_save($node->book)) { if (menu_link_save($node->book)) {
if ($new) { if ($new) {
// Insert new. // Insert new.
...@@ -468,9 +485,11 @@ function _book_update_outline(&$node) { ...@@ -468,9 +485,11 @@ function _book_update_outline(&$node) {
book_update_bid($node->book); book_update_bid($node->book);
} }
} }
return TRUE; return TRUE;
} }
// Failed to save the menu link
// Failed to save the menu link.
return FALSE; return FALSE;
} }
...@@ -481,7 +500,6 @@ function _book_update_outline(&$node) { ...@@ -481,7 +500,6 @@ function _book_update_outline(&$node) {
* A fully loaded menu link that is part of the book hierarchy. * A fully loaded menu link that is part of the book hierarchy.
*/ */
function book_update_bid($book_link) { function book_update_bid($book_link) {
for ($i = 1; $i <= MENU_MAX_DEPTH && $book_link["p$i"]; $i++) { for ($i = 1; $i <= MENU_MAX_DEPTH && $book_link["p$i"]; $i++) {
$match[] = "p$i = %d"; $match[] = "p$i = %d";
$args[] = $book_link["p$i"]; $args[] = $book_link["p$i"];
...@@ -492,6 +510,7 @@ function book_update_bid($book_link) { ...@@ -492,6 +510,7 @@ function book_update_bid($book_link) {
while ($a = db_fetch_array($result)) { while ($a = db_fetch_array($result)) {
$mlids[] = $a['mlid']; $mlids[] = $a['mlid'];
} }
if ($mlids) { if ($mlids) {
db_query("UPDATE {book} SET bid = %d WHERE mlid IN (" . implode(',', $mlids) . ")", $book_link['bid']); db_query("UPDATE {book} SET bid = %d WHERE mlid IN (" . implode(',', $mlids) . ")", $book_link['bid']);
} }
...@@ -517,6 +536,7 @@ function book_get_flat_menu($book_link) { ...@@ -517,6 +536,7 @@ function book_get_flat_menu($book_link) {
$flat[$book_link['mlid']] = array(); $flat[$book_link['mlid']] = array();
_book_flatten_menu($tree, $flat[$book_link['mlid']]); _book_flatten_menu($tree, $flat[$book_link['mlid']]);
} }
return $flat[$book_link['mlid']]; return $flat[$book_link['mlid']];
} }
...@@ -559,6 +579,7 @@ function book_prev($book_link) { ...@@ -559,6 +579,7 @@ function book_prev($book_link) {
while ($data['below']) { while ($data['below']) {
$data = end($data['below']); $data = end($data['below']);
} }
return $data['link']; return $data['link'];
} }
else { else {
...@@ -575,7 +596,9 @@ function book_next($book_link) { ...@@ -575,7 +596,9 @@ function book_next($book_link) {
// Assigning the array to $flat resets the array pointer for use with each(). // Assigning the array to $flat resets the array pointer for use with each().
do { do {
list($key, $curr) = each($flat); list($key, $curr) = each($flat);
} while ($key && $key != $book_link['mlid']); }
while ($key && $key != $book_link['mlid']);
if ($key == $book_link['mlid']) { if ($key == $book_link['mlid']) {
return current($flat); return current($flat);
} }
...@@ -593,7 +616,8 @@ function book_children($book_link) { ...@@ -593,7 +616,8 @@ function book_children($book_link) {
// Walk through the array until we find the current page. // Walk through the array until we find the current page.
do { do {
$link = array_shift($flat); $link = array_shift($flat);
} while ($link && ($link['mlid'] != $book_link['mlid'])); }
while ($link && ($link['mlid'] != $book_link['mlid']));
// Continue though the array and collect the links whose parent is this page. // Continue though the array and collect the links whose parent is this page.
while (($link = array_shift($flat)) && $link['plid'] == $book_link['mlid']) { while (($link = array_shift($flat)) && $link['plid'] == $book_link['mlid']) {
$data['link'] = $link; $data['link'] = $link;
...@@ -601,6 +625,7 @@ function book_children($book_link) { ...@@ -601,6 +625,7 @@ function book_children($book_link) {
$children[] = $data; $children[] = $data;
} }
} }
return $children ? menu_tree_output($children) : ''; return $children ? menu_tree_output($children) : '';
} }
...@@ -638,6 +663,7 @@ function book_build_active_trail($book_link) { ...@@ -638,6 +663,7 @@ function book_build_active_trail($book_link) {
} }
} }
} }
return $trail; return $trail;
} }
...@@ -651,20 +677,22 @@ function book_nodeapi(&$node, $op, $teaser, $page) { ...@@ -651,20 +677,22 @@ function book_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) { switch ($op) {
case 'load': case 'load':
if (in_array($node->type, variable_get('book_allowed_types', array('book')))) { if (in_array($node->type, variable_get('book_allowed_types', array('book')))) {
// Note - we cannot use book_link_load() because it will call node_load() // Note - we cannot use book_link_load() because it will call node_load().
$info['book'] = db_fetch_array(db_query('SELECT * FROM {book} b INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE b.nid = %d', $node->nid)); $info['book'] = db_fetch_array(db_query('SELECT * FROM {book} b INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE b.nid = %d', $node->nid));
if ($info['book']) { if ($info['book']) {
$info['book']['href'] = $info['book']['link_path']; $info['book']['href'] = $info['book']['link_path'];
$info['book']['title'] = $info['book']['link_title']; $info['book']['title'] = $info['book']['link_title'];
$info['book']['options'] = unserialize($info['book']['options']); $info['book']['options'] = unserialize($info['book']['options']);
return $info; return $info;
} }
} }
break; break;
case 'view': case 'view':
if (!$teaser) { if (!$teaser) {
if (!empty($node->book['bid']) && $node->build_mode == NODE_BUILD_NORMAL) { if (!empty($node->book['bid']) && $node->build_mode == NODE_BUILD_NORMAL) {
$node->content['book_navigation'] = array( $node->content['book_navigation'] = array(
'#value' => theme('book_navigation', $node->book), '#value' => theme('book_navigation', $node->book),
'#weight' => 100, '#weight' => 100,
...@@ -677,6 +705,7 @@ function book_nodeapi(&$node, $op, $teaser, $page) { ...@@ -677,6 +705,7 @@ function book_nodeapi(&$node, $op, $teaser, $page) {
} }
} }
break; break;
case 'presave': case 'presave':
// Always save a revision for non-administrators. // Always save a revision for non-administrators.
if (!empty($node->book['bid']) && !user_access('administer nodes')) { if (!empty($node->book['bid']) && !user_access('administer nodes')) {
...@@ -687,6 +716,7 @@ function book_nodeapi(&$node, $op, $teaser, $page) { ...@@ -687,6 +716,7 @@ function book_nodeapi(&$node, $op, $teaser, $page) {
$node->book['mlid'] = NULL; $node->book['mlid'] = NULL;
} }
break; break;
case 'insert': case 'insert':
case 'update': case 'update':
if (!empty($node->book['bid'])) { if (!empty($node->book['bid'])) {
...@@ -699,6 +729,7 @@ function book_nodeapi(&$node, $op, $teaser, $page) { ...@@ -699,6 +729,7 @@ function book_nodeapi(&$node, $op, $teaser, $page) {
_book_update_outline($node); _book_update_outline($node);
} }
break; break;
case 'delete':