Index: modules/book/book.install =================================================================== RCS file: /cvs/drupal/drupal/modules/book/book.install,v retrieving revision 1.7 diff -u -p -r1.7 book.install --- modules/book/book.install 25 May 2007 12:46:43 -0000 1.7 +++ modules/book/book.install 28 May 2007 04:12:11 -0000 @@ -13,6 +13,7 @@ function book_install() { * Implementation of hook_uninstall(). */ function book_uninstall() { + // TODO: delete menu links // Remove tables. drupal_uninstall_schema('book'); } Index: modules/book/book.module =================================================================== RCS file: /cvs/drupal/drupal/modules/book/book.module,v retrieving revision 1.420 diff -u -p -r1.420 book.module --- modules/book/book.module 14 May 2007 13:43:34 -0000 1.420 +++ modules/book/book.module 28 May 2007 04:12:12 -0000 @@ -6,18 +6,6 @@ * Allows users to collaboratively author a book. */ -/** - * Implementation of hook_node_info(). - */ -function book_node_info() { - return array( - 'book' => array( - 'name' => t('Book page'), - 'module' => 'book', - 'description' => t("A book is a collaborative writing effort: users can collaborate writing the pages of the book, positioning the pages in the right order, and reviewing or modifying pages previously written. So when you have some information to share or when you read a page of the book and you didn't like it, or if you think a certain page could have been written better, you can do something about it."), - ) - ); -} /** * Implementation of hook_theme() @@ -40,35 +28,7 @@ function book_theme() { * Implementation of hook_perm(). */ function book_perm() { - return array('outline posts in books', 'create book pages', 'create new books', 'edit book pages', 'edit own book pages', 'see printer-friendly version'); -} - -/** - * Implementation of hook_access(). - */ -function book_access($op, $node) { - global $user; - - if ($op == 'create') { - // Only registered users can create book pages. Given the nature - // of the book module this is considered to be a good/safe idea. - return user_access('create book pages'); - } - - if ($op == 'update') { - // Only registered users can update book pages. Given the nature - // of the book module this is considered to be a good/safe idea. - // One can only update a book page if there are no suggested updates - // of that page waiting for approval. That is, only updates that - // don't overwrite the current or pending information are allowed. - - if (user_access('edit book pages') || ($node->uid == $user->uid && user_access('edit own book pages'))) { - return TRUE; - } - else { - // do nothing. node-access() will determine further access - } - } + return array('outline posts in books', 'create new books', 'see printer-friendly version'); } /** @@ -78,12 +38,13 @@ function book_link($type, $node = NULL, $links = array(); - if ($type == 'node' && isset($node->parent)) { + if ($type == 'node' && isset($node->book_id)) { if (!$teaser) { - if (book_access('create', $node) && $node->status == 1) { + if (node_access('create', $node->child_type) && $node->status == 1) { $links['book_add_child'] = array( 'title' => t('Add child page'), - 'href' => "node/add/book/parent/$node->nid" + 'href' => "node/add/". str_replace('_', '-', $node->child_type), + 'query' => "parent=". $node->book_link['mlid'], ); } if (user_access('see printer-friendly version')) { @@ -105,21 +66,22 @@ function book_link($type, $node = NULL, function book_menu() { $items['admin/content/book'] = array( 'title' => 'Books', - 'description' => "Manage site's books and orphaned book pages.", - 'page callback' => 'book_admin', - 'access arguments' => array('administer nodes'), + 'description' => "Manage site's books and pages.", + 'page callback' => 'book_admin_overview', + 'access arguments' => array('outline posts in books'), + ); + $items['admin/content/book/%node'] = array( + 'title' => 'Re-order book pages and change titles', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('book_admin_edit', 3), + 'access callback' => '_book_outline_access', + 'access arguments' => array(3), + 'type' => MENU_CALLBACK, ); $items['admin/content/book/list'] = array( 'title' => 'List', 'type' => MENU_DEFAULT_LOCAL_TASK, ); - $items['admin/content/book/orphan'] = array( - 'title' => 'Orphan pages', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('book_admin_orphan'), - 'type' => MENU_LOCAL_TASK, - 'weight' => 8, - ); $items['book'] = array( 'title' => 'Books', 'page callback' => 'book_render', @@ -129,6 +91,7 @@ function book_menu() { $items['book/export/%/%'] = array( 'page callback' => 'book_export', 'page arguments' => array(2, 3), + 'access arguments' => array('see printer-friendly version'), 'type' => MENU_CALLBACK, ); $items['node/%node/outline'] = array( @@ -146,7 +109,7 @@ function book_menu() { function _book_outline_access($node) { // Only add the outline-tab for non-book pages: - return user_access('outline posts in books') && $node && ($node->type != 'book'); + return user_access('outline posts in books') && node_access('view', $node); } function book_init() { @@ -168,35 +131,22 @@ function book_block($op = 'list', $delta 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(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), arg(1)); - if (db_num_rows($result) > 0) { - $node = db_fetch_object($result); - - $path = book_location($node); - $path[] = $node; + $node = node_load(arg(1)); + $book_id = isset($node->book_id) ? $node->book_id : FALSE; + if ($book_id) { + $title = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $book_id)); + if ($title) { - $expand = array(); - foreach ($path as $key => $node) { - $expand[] = $node->nid; + $block['subject'] = check_plain($title); + $block['content'] = menu_tree_output(menu_tree_all_data($node->book_link['menu_name'], $node->book_link)); } - - $block['subject'] = check_plain($path[0]->title); - $block['content'] = book_tree($expand[0], 5, $expand); } } - return $block; } } /** - * Implementation of hook_insert(). - */ -function book_insert($node) { - db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight); -} - -/** * Implementation of hook_submit(). */ function book_submit(&$form_values) { @@ -206,90 +156,201 @@ function book_submit(&$form_values) { $form_values['revision'] = 1; $form_values['uid'] = $user->uid; } + if (empty($form_values['book_link']['link_title'])) { + $form_values['book_link']['link_title'] = $form_values['title']; + $form_values['child_type'] = $form_values['book_link']['child_type']; + } } /** - * Implementation of hook_form(). + * Implementation of hook_form_alter(). */ -function book_form(&$node) { - $type = node_get_types('type', $node); - if (!empty($node->nid) && !$node->parent && !user_access('create new books')) { - $form['parent'] = array('#type' => 'value', '#value' => $node->parent); - } - else { - $form['parent'] = array('#type' => 'select', - '#title' => t('Parent'), - '#default_value' => (isset($node->parent) ? $node->parent : arg(4)), - '#options' => book_toc(isset($node->nid) ? $node->nid : 0), - '#weight' => -4, - '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), - ); - } +function book_form_alter(&$form, $form_id, $form_state) { - $form['title'] = array('#type' => 'textfield', - '#title' => check_plain($type->title_label), - '#required' => TRUE, - '#default_value' => $node->title, - '#weight' => -5, - ); + if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) { + $node = $form['#node']; - $form['body_field'] = node_body_field($node, $type->body_label, 1); + unset($form['book_id']); + if (isset($node->book_id)) { + $form['book_id'] = array( + '#type' => 'value', + '#value' => $node->book_id, + ); + $link_path = 'node/'. $node->nid; + $plid = $node->book_link['plid']; + $child_type = $node->child_type; + $title = $node->book_link['link_title']; - if (user_access('administer nodes')) { - $form['weight'] = array('#type' => 'weight', - '#title' => t('Weight'), - '#default_value' => isset($node->weight) ? $node->weight : 0, - '#delta' => 15, - '#weight' => 5, - '#description' => t('Pages at a given level are ordered first by weight and then by title.'), - ); - } - else { - // If a regular user updates a book page, we preserve the node weight; otherwise - // we use 0 as the default for new pages - $form['weight'] = array( - '#type' => 'value', - '#value' => isset($node->weight) ? $node->weight : 0, - ); - } + } + // Handle "add child page" links: + elseif (empty($node->nid) && isset($_GET['parent']) && is_numeric($_GET['parent'])) { + $parent = book_link_load($_GET['parent']); + if ($parent && $parent['access'] && $parent['child_type'] == $node->type) { + $form['book_id'] = array( + '#type' => 'value', + '#value' => $parent['book_id'] + ); + $child_type = $parent['child_type']; + $title = $link_path = ''; + $plid = $parent['mlid']; + } + } + if (isset($form['book_id'])) { + $form['book_link']['link_title'] = array( + '#type' => 'value', + '#value' => $title, + ); + $form['book_link'] = array( + '#type' => 'fieldset', + '#title' => t('Book outline options'), + '#weight' => -1, + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#tree' => TRUE, + ); + $mlid = isset($node->book_link['mlid']) ? $node->book_link['mlid'] : 0; + $form['book_link']['mlid'] = array( + '#type' => 'value', + '#value' => $mlid, + ); + + $form['book_link']['link_path'] = array( + '#type' => 'value', + '#value' => $link_path, + ); + $form['book_link']['options'] = array( + '#type' => 'value', + '#value' => isset($node->book_link['options']) ? $node->book_link['options'] : array(), + ); + $menu_name = book_menu_name($form['book_id']['#value']); + $form['book_link']['menu_name'] = array( + '#type' => 'value', + '#value' => $menu_name, + ); + + if (isset($node->nid) && $node->nid == $form['book_id']['#value']) { + // This is a book - at the top level + $form['book_link']['plid'] = array( + '#type' => 'value', + '#value' => 0, + ); + $form['book_link']['note'] = array('#value' => t('This is the top-level page in this book')); + } + else { + $form['book_link']['plid'] = array( + '#type' => 'select', + '#title' => t('Parent'), + '#default_value' => $plid, + '#options' => book_toc($menu_name, array($mlid)), + '#description' => t('The parent page in the book.'), + ); + } + + _book_add_form_elements($form, $child_type, isset($node->book_link['weight']) ? $node->book_link['weight'] : 0); + + + + $form['#submit'][] = 'book_submit'; + } + } return $form; } +function _book_add_form_elements(&$form, $child_type, $weight) { + + $form['book_link']['child_type'] = array( + '#type' => 'select', + '#title' => t('Child page type'), + '#default_value' => $child_type, + '#options' => node_get_types('names'), + '#description' => t('Select default type for chidren of the page.'), + '#multiple' => FALSE, + '#access' => user_access('administer nodes'), + ); + + $form['book_link']['weight'] = array( + '#type' => 'weight', + '#title' => t('Weight'), + '#default_value' => $weight, + '#delta' => 15, + '#weight' => 5, + '#description' => t('Pages at a given level are ordered first by weight and then by title.'), + ); +} + /** - * Implementation of function book_outline() * Handles all book outline operations. */ function book_outline($node) { - $form['parent'] = array('#type' => 'select', - '#title' => t('Parent'), - '#default_value' => isset($node->parent) ? $node->parent : 0, - '#options' => book_toc($node->nid), - '#description' => t('The parent page in the book.'), - ); - $form['weight'] = array('#type' => 'weight', - '#title' => t('Weight'), - '#default_value' => isset($node->weight) ? $node->weight : 0, - '#delta' => 15, - '#description' => t('Pages at a given level are ordered first by weight and then by title.'), - ); - $form['log'] = array('#type' => 'textarea', - '#title' => t('Log message'), - '#description' => t('An explanation to help other authors understand your motivations to put this post into the book.'), - ); - $form['nid'] = array('#type' => 'value', '#value' => isset($node->nid) ? $node->nid : 0); - if (isset($node->parent)) { - $form['update'] = array('#type' => 'submit', - '#value' => t('Update book outline'), - ); - $form['remove'] = array('#type' => 'submit', - '#value' => t('Remove from book outline'), + $form['#node'] = $node; + + if (isset($node->book_id)) { + if ($node->nid == $node->book_id) { + $form['plid'] = array( + '#type' => 'value', + '#value' => 0, + ); + $form['note'] = array('#value' => t('This is the top-level page in this book')); + } + else { + $form['plid'] = array( + '#type' => 'select', + '#title' => t('Parent'), + '#default_value' => isset($node->book_link['plid']) ? $node->book_link['plid'] : 0, + '#options' => book_toc($node->book_link['menu_name'], array($node->book_link['mlid'])), + '#description' => t('The parent page in the book.'), + ); + } + _book_add_form_elements($form, $node->child_type, isset($node->book_link['weight']) ? $node->book_link['weight'] : 0); + + $form['log'] = array( + '#type' => 'textarea', + '#rows' => 2, + '#title' => t('Log message'), + '#description' => t('An explanation to help other authors understand your motivations to put this post into the book.'), ); + + $form['nid'] = array('#type' => 'value', '#value' => isset($node->nid) ? $node->nid : 0); + + $form['update'] = array('#type' => 'submit', '#value' => t('Update book outline'),); + $form['remove'] = array( '#type' => 'submit', '#value' => t('Remove from book outline'),); + + $title = t('Select a different book to move this post to'); + $action = t('Move to a different book'); + $book_id = $node->book_id; } else { - $form['add'] = array('#type' => 'submit', '#value' => t('Add to book outline')); - } + $form['#node']->book_link = array('link_title' => $node->title, 'link_path' => 'node/'. $node->nid, 'router_path' => 'node/%'); + $title = t('Select a book to add this post to'); + $action = t('Add to book outline'); + $book_id = 0; + } + + $result = db_query(db_rewrite_sql("SELECT n.title, b.* FROM {book} b INNER JOIN {node} n ON n.nid = b.nid WHERE b.nid = b.book_id ORDER BY n.title")); + + $options = array(); + while ($book = db_fetch_array($result)) { + $options[$book['nid']] = check_plain($book['title']); + } + // Don't show the current book + unset($options[$book_id]); + + if (user_access('create new books') && ($node->nid != $book_id)) { + $options = array($node->nid => '<'. t('create a new book') .'>') + $options; + } + $form['book_id'] = array( + '#type' => 'select', + '#title' => $title, + '#default_value' => 0, + '#options' => $options, + '#access' => (bool)$options, + '#description' => t('Select the book you wish to add/move your page to.'), + '#weight' => 10, + ); + $form['add'] = array('#type' => 'submit', '#value' => $action, '#weight' => 12); + drupal_set_title(check_plain($node->title)); return $form; @@ -300,127 +361,197 @@ function book_outline($node) { */ function book_outline_submit($form_values, $form, &$form_state) { $op = $form_values['op']; - $node = node_load($form_values['nid']); + $node = $form['#node']; + + $link = $node->book_link; switch ($op) { + case t('Move to a different book'): + if ($form_values['book_id'] != $node->book_id) { + if ($form_values['book_id'] == $node->nid) { // Creating a new book + $link['plid'] = 0; + } + else { + $parent = db_fetch_array(db_query("SELECT * FROM {book} WHERE nid = %d", $form_values['book_id'])); + $link['plid'] = $parent['mlid']; + } + $link['menu_name'] = book_menu_name($form_values['book_id']); + if (menu_link_save($link)) { + // Update this page and all children + book_update_book_id($link, $form_values['book_id']); + if ($link['plid']) { + drupal_set_message(t('The post has been added to the book. You may now position it relative to other pages.')); + $form_state['redirect'] = "node/". $node->nid ."/outline"; + } + else { + drupal_set_message(t('The post is now a separate book.')); + $form_state['redirect'] = "node/". $node->nid; + } + } + else { + drupal_set_message(t('There was an error adding the post to the book.')); + } + } + else { + drupal_set_message(t('No changes were made')); + } + break; case t('Add to book outline'): - db_query('INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)', $node->nid, $node->vid, $form_values['parent'], $form_values['weight']); - db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $form_values['log'], $node->vid); - drupal_set_message(t('The post has been added to the book.')); + + if ($form_values['book_id']) { + $link['menu_name'] = book_menu_name($form_values['book_id']); + if ($form_values['book_id'] == $node->nid) { // Creating a new book + $link['plid'] = 0; + $node->child_type = $node->type; + } + else { // Adding to an existing book + $parent = db_fetch_array(db_query("SELECT * FROM {book} WHERE nid = %d", $form_values['book_id'])); + $link['plid'] = $parent['mlid']; + $node->child_type = $parent['child_type']; + } + if (menu_link_save($link)) { + db_query("INSERT INTO {book} (nid, mlid, book_id, child_type) VALUES (%d, %d, %d, '%s')", $node->nid, $link['mlid'], $form_values['book_id'], $node->child_type); + if ($link['plid']) { + drupal_set_message(t('The post has been added to the book. You may now position it relative to other pages.')); + $form_state['redirect'] = "node/". $node->nid ."/outline"; + } + else { + drupal_set_message(t('The post is now a separate book.')); + $form_state['redirect'] = "node/". $node->nid; + } + } + else { + drupal_set_message(t('There was an error adding the post to the book.')); + } + } break; case t('Update book outline'): - db_query('UPDATE {book} SET parent = %d, weight = %d WHERE vid = %d', $form_values['parent'], $form_values['weight'], $node->vid); - db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $form_values['log'], $node->vid); - drupal_set_message(t('The book outline has been updated.')); + $link['weight'] = $form_values['weight']; + $link['plid'] = $form_values['plid']; + $link['link_title'] = empty($form_values['link_title']) ? $node->title : $form_values['link_title']; + if (menu_link_save($link)) { + db_query('UPDATE {book} SET child_type = %d WHERE mlid = %d', $form_values['child_type'], $node->book_link['mlid']); + db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $form_values['log'], $node->vid); + drupal_set_message(t('The book outline has been updated.')); + } + $form_state['redirect'] = "node/$node->nid"; break; case t('Remove from book outline'): + // TODO - handle the case when this is the top-level page + menu_link_delete($link); db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); drupal_set_message(t('The post has been removed from the book.')); + $form_state['redirect'] = "node/$node->nid"; break; } - $form_state['redirect'] = "node/$node->nid"; + return; } /** - * Given a node, this function returns an array of 'book node' objects - * representing the path in the book tree from the root to the - * parent of the given node. - * - * @param $node - * A book node object for which to compute the path. - * - * @return - * An array of book node objects representing the path nodes root to - * parent of the given node. Returns an empty array if the node does - * not exist or is not part of a book hierarchy. + * Update book_id for a page and its children when we move to a new book. */ -function book_location($node, $nodes = array()) { - $parent = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $node->parent)); - if (isset($parent->title)) { - $nodes = book_location($parent, $nodes); - $nodes[] = $parent; +function book_update_book_id($item, $book_id) { + $i = 1; + $match = ''; + while ($i <= MENU_MAX_DEPTH && $item["p$i"]) { + $match .= " AND p$i = %d"; + $args[] = $item["p$i"]; + $i++; + } + array_unshift($args, $item['menu_name']); + $result = db_query("SELECT mlid FROM {menu_links} WHERE menu_name = '%s'". $match, $args); + + $ids = array(); + while ($a = db_fetch_array($result)) { + $ids[] = $a['mlid']; + } + if ($ids) { + db_query("UPDATE {book} SET book_id = %d WHERE mlid IN (". implode(',', $ids) .")", $book_id); } - return $nodes; } -/** - * Given a node, this function returns an array of 'book node' objects - * representing the path in the book tree from the given node down to - * the last sibling of it. - * - * @param $node - * A book node object where the path starts. - * - * @return - * An array of book node objects representing the path nodes from the - * given node. Returns an empty array if the node does not exist or - * is not part of a book hierarchy or there are no siblings. - */ -function book_location_down($node, $nodes = array()) { - $last_direct_child = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d ORDER BY b.weight DESC, n.title DESC'), $node->nid)); - if ($last_direct_child) { - $nodes[] = $last_direct_child; - $nodes = book_location_down($last_direct_child, $nodes); +function book_get_flat_menu($node) { + static $flat = array(); + + if (!isset($flat[$node->nid])) { + $tree = menu_tree_all_data($node->book_link['menu_name'], $node->book_link); + $flat[$node->nid] = array(); + _book_flatten_menu($tree, $flat[$node->nid]); + } + + return $flat[$node->nid]; +} + +function _book_flatten_menu($tree, &$flat) { + + foreach ($tree as $data) { + if (!$data['link']['hidden']) { + $flat[$data['link']['mlid']] = $data['link']; + if ($data['below']) { + _book_flatten_menu($data['below'], &$flat); + } + } } - return $nodes; } /** - * Fetches the node object of the previous page of the book. + * Fetches the menu link for the previous page of the book. */ function book_prev($node) { // If the parent is zero, we are at the start of a book so there is no previous. - if ($node->parent == 0) { + if ($node->book_link['plid'] == 0) { return NULL; } - - // Previous on the same level: - $direct_above = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND (b.weight < %d OR (b.weight = %d AND n.title < '%s')) ORDER BY b.weight DESC, n.title DESC"), $node->parent, $node->weight, $node->weight, $node->title)); - if ($direct_above) { - // Get last leaf of $above. - $path = book_location_down($direct_above); - - return $path ? (count($path) > 0 ? array_pop($path) : NULL) : $direct_above; - } - else { - // Direct parent: - $prev = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d AND n.status = 1'), $node->parent)); + $flat = book_get_flat_menu($node); + $curr = NULL; + do { + $prev = $curr; + list($key, $curr) = each($flat); + } while ($key && $key != $node->book_link['mlid']); + if ($key == $node->book_link['mlid']) { return $prev; } } /** - * Fetches the node object of the next page of the book. + * Fetches the menu link for the next page of the book. */ function book_next($node) { - // get first direct child - $child = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 ORDER BY b.weight ASC, n.title ASC'), $node->nid)); - if ($child) { - return $child; + $flat = book_get_flat_menu($node); + do { + list($key, $curr) = each($flat); + } while ($key && $key != $node->book_link['mlid']); + if ($key == $node->book_link['mlid']) { + return current($flat); } +} - // No direct child: get next for this level or any parent in this book. - $path = book_location($node); // Path to top-level node including this one. - $path[] = $node; - - while (($leaf = array_pop($path)) && count($path)) { - $next = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND (b.weight > %d OR (b.weight = %d AND n.title > '%s')) ORDER BY b.weight ASC, n.title ASC"), $leaf->parent, $leaf->weight, $leaf->weight, $leaf->title)); - if ($next) { - return $next; +/** + * Format the menu links for the child pages of the current page. + */ +function book_children($node) { + $flat = book_get_flat_menu($node); + + $children = array(); + + if ($node->book_link['has_children']) { + do { + list($key, $link) = each($flat); + } while ($key && $key != $node->book_link['mlid']); + + while (list($key, $link) = each($flat) && $link['plid'] == $node->book_link['mlid']) { + $data['link'] = $link; + $data['below'] = ''; + $children[] = $data; } } + + return $children ? book_tree_output($children) : ''; } -/** - * Returns the content of a given node. If $teaser if TRUE, returns - * the teaser rather than full content. Displays the most recently - * approved revision of a node (if any) unless we have to display this - * page in the context of the moderation queue. - */ -function book_content($node, $teaser = FALSE) { - // Return the page body. - return node_prepare($node, $teaser); +function book_menu_name($book_id) { + return 'book-toc-'. $book_id; } /** @@ -431,18 +562,21 @@ function book_content($node, $teaser = F function book_nodeapi(&$node, $op, $teaser, $page) { switch ($op) { case 'load': - return db_fetch_array(db_query('SELECT parent, weight FROM {book} WHERE vid = %d', $node->vid)); + // Note - we cannot use book_link_load() because it will call node_load() + $info = db_fetch_array(db_query('SELECT b.book_id, b.child_type, ml.* FROM {book} b INNER JOIN {menu_links} ml ON b.mlid = ml.mlid WHERE b.nid = %d', $node->nid)); + if ($info) { + $ret['book_id'] = $info['book_id']; + $ret['child_type'] = $info['child_type']; + $info['href'] = $info['link_path']; + $info['title'] = $info['link_title']; + $info['options'] = unserialize($info['options']); + $ret['book_link'] = $info; + return $ret; + } break; case 'view': if (!$teaser) { - if (isset($node->parent)) { - $path = book_location($node); - // Construct the breadcrumb: - $node->breadcrumb = array(); // Overwrite the trail with a book trail. - foreach ($path as $level) { - $node->breadcrumb[] = array('path' => 'node/'. $level->nid, 'title' => $level->title); - } - $node->breadcrumb[] = array('path' => 'node/'. $node->nid); + if (isset($node->book_id)) { $node->content['book_navigation'] = array( '#value' => theme('book_navigation', $node), @@ -450,26 +584,32 @@ function book_nodeapi(&$node, $op, $teas ); if ($page) { - menu_set_location($node->breadcrumb); + menu_set_active_menu_name($node->book_link['menu_name']); } } } break; - case 'update': - if (isset($node->parent)) { - if (!empty($node->revision)) { - db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight); + case 'insert': + if (isset($node->book_id)) { + $node->book_link['link_path'] = 'node/'. $node->nid; + if (menu_link_save($node->book_link)) { + db_query("INSERT INTO {book} (mlid, nid, book_id, child_type) VALUES (%d, %d, %d, '%s')", $node->book_link['mlid'], $node->nid, $node->book_id, $node->child_type); } - else { - db_query("UPDATE {book} SET parent = %d, weight = %d WHERE vid = %d", $node->parent, $node->weight, $node->vid); + } + case 'update': + if (isset($node->book_id)) { + if (menu_link_save($node->book_link)) { + db_query("UPDATE {book} SET book_id = %d, mlid = %d, child_type = '%s' WHERE nid = %d", $node->book_id, $node->book_link['mlid'], $node->child_type, $node->nid); } } - break; - case 'delete revision': - db_query('DELETE FROM {book} WHERE vid = %d', $node->vid); + break; case 'delete': - db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); + if (isset($node->book_id)) { + // TODO handle the case where a book is deleted + menu_link_delete($node->book_link['mlid']); + db_query('DELETE FROM {book} WHERE mlid = %d', $node->book_link['mlid']); + } break; } } @@ -485,19 +625,19 @@ function theme_book_navigation($node) { $links = ''; if ($node->nid) { - $tree = book_tree($node->nid); + $tree = book_children($node); if ($prev = book_prev($node)) { - drupal_add_link(array('rel' => 'prev', 'href' => url('node/'. $prev->nid))); - $links .= l(t('‹ ') . $prev->title, 'node/'. $prev->nid, array('class' => 'page-previous', 'title' => t('Go to previous page'))); + drupal_add_link(array('rel' => 'prev', 'href' => url($prev['href']))); + $links .= l(t('‹ ') . $prev['title'], $prev['href'], array('class' => 'page-previous', 'title' => t('Go to previous page'))); } - if ($node->parent) { - drupal_add_link(array('rel' => 'up', 'href' => url('node/'. $node->parent))); - $links .= l(t('up'), 'node/'. $node->parent, array('class' => 'page-up', 'title' => t('Go to parent page'))); + if ($node->book_link['plid'] && $parent = book_link_load($node->book_link['plid'])) { + drupal_add_link(array('rel' => 'up', 'href' => url($parent['href']))); + $links .= l(t('up'), $parent['href'], array('class' => 'page-up', 'title' => t('Go to parent page'))); } if ($next = book_next($node)) { - drupal_add_link(array('rel' => 'next', 'href' => url('node/'. $next->nid))); - $links .= l($next->title . t(' ›'), 'node/'. $next->nid, array('class' => 'page-next', 'title' => t('Go to next page'))); + drupal_add_link(array('rel' => 'next', 'href' => url($next['href']))); + $links .= l($next['title'] . t(' ›'), $next['href'], array('class' => 'page-next', 'title' => t('Go to next page'))); } if (isset($tree) || isset($links)) { @@ -518,101 +658,34 @@ function theme_book_navigation($node) { /** * This is a helper function for book_toc(). */ -function book_toc_recurse($nid, $indent, $toc, $children, $exclude) { - if (!empty($children[$nid])) { - foreach ($children[$nid] as $foo => $node) { - if (!$exclude || $exclude != $node->nid) { - $toc[$node->nid] = $indent .' '. $node->title; - $toc = book_toc_recurse($node->nid, $indent .'--', $toc, $children, $exclude); +function book_toc_recurse($tree, $indent, &$toc, $exclude) { + foreach ($tree as $data) { + if (!in_array($data['link']['mlid'], $exclude)) { + $toc[$data['link']['mlid']] = $indent .' '. truncate_utf8($data['link']['title'], 30, TRUE, TRUE) ; + if ($data['below']) { + book_toc_recurse($data['below'], $indent .'--', $toc, $exclude); } } } - - return $toc; } /** * Returns an array of titles and nid entries of book pages in table of contents order. */ -function book_toc($exclude = 0) { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title')); - - $children = array(); - while ($node = db_fetch_object($result)) { - if (empty($children[$node->parent])) { - $children[$node->parent] = array(); - } - $children[$node->parent][] = $node; - } - +function book_toc($menu_name, $exclude = array()) { + + $tree = menu_tree_all_data($menu_name); $toc = array(); - // If the user has permission to create new books, add the top-level book page to the menu; - if (user_access('create new books')) { - $toc[0] = '<'. t('top-level') .'>'; - } - - $toc = book_toc_recurse(0, '', $toc, $children, $exclude); + book_toc_recurse($tree, '', $toc, $exclude); return $toc; } /** - * This is a helper function for book_tree() - */ -function book_tree_recurse($nid, $depth, $children, $unfold = array()) { - $output = ''; - if ($depth > 0) { - if (isset($children[$nid])) { - foreach ($children[$nid] as $foo => $node) { - if (in_array($node->nid, $unfold)) { - if ($tree = book_tree_recurse($node->nid, $depth - 1, $children, $unfold)) { - $output .= '
  • '; - $output .= l($node->title, 'node/'. $node->nid); - $output .= ''; - $output .= '
  • '; - } - else { - $output .= '
  • '. l($node->title, 'node/'. $node->nid) .'
  • '; - } - } - else { - if ($tree = book_tree_recurse($node->nid, 1, $children)) { - $output .= ''; - } - else { - $output .= '
  • '. l($node->title, 'node/'. $node->nid) .'
  • '; - } - } - } - } - } - - return $output; -} - -/** - * Returns an HTML nested list (wrapped in a menu-class div) representing the book nodes - * as a tree. - */ -function book_tree($parent = 0, $depth = 3, $unfold = array()) { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title')); - - while ($node = db_fetch_object($result)) { - $list = isset($children[$node->parent]) ? $children[$node->parent] : array(); - $list[] = $node; - $children[$node->parent] = $list; - } - - if ($tree = book_tree_recurse($parent, $depth, $children, $unfold)) { - return ''; - } -} - -/** * Menu callback; prints a listing of all books. */ function book_render() { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = 0 AND n.status = 1 ORDER BY b.weight, n.title')); + $result = db_query(db_rewrite_sql('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE b.book_id = b.nid AND n.status = 1 ORDER BY n.title')); $books = array(); while ($node = db_fetch_object($result)) { @@ -641,16 +714,13 @@ function book_render() { * */ function book_export($type, $nid) { + $type = drupal_strtolower($type); - $node_result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $nid); - if (db_num_rows($node_result) > 0) { - $node = db_fetch_object($node_result); - } - $depth = count(book_location($node)) + 1; + $export_function = 'book_export_'. $type; if (function_exists($export_function)) { - print call_user_func($export_function, $nid, $depth); + print call_user_func($export_function, $nid); } else { drupal_set_message(t('Unknown export format.')); @@ -671,23 +741,26 @@ function book_export($type, $nid) { * * @param nid * - an integer representing the node id (nid) of the node to export - * @param depth - * - an integer giving the depth in the book hierarchy of the node - * which is to be exported * * @return * - string containing HTML representing the node and its children in * the book hierarchy */ -function book_export_html($nid, $depth) { +function book_export_html($nid) { if (user_access('see printer-friendly version')) { + $content = ''; $node = node_load($nid); - for ($i = 1; $i < $depth; $i++) { - $content .= "
    \n"; - } - $content .= book_recurse($nid, $depth, 'book_node_visitor_html_pre', 'book_node_visitor_html_post'); - for ($i = 1; $i < $depth; $i++) { - $content .= "
    \n"; + if (isset($node->book_id)) { + $depth = $node->book_link['depth']; + for ($i = 1; $i < $depth; $i++) { + $content .= "
    \n"; + } + $tree = book_menu_subtree_data($node->book_link); + $content .= book_export_traverse($tree, 'book_node_visitor_html_pre', 'book_node_visitor_html_post'); + + for ($i = 1; $i < $depth; $i++) { + $content .= "
    \n"; + } } return theme('book_export_html', check_plain($node->title), $content); } @@ -721,10 +794,8 @@ function theme_book_export_html($title, * * @todo This is duplicitous with node_build_content(). * - * @param nid - * - the node id (nid) of the root node of the book hierarchy. - * @param depth - * - the depth of the given node in the book hierarchy. + * @param $tree + * - the book's menu hierarchy. * @param visit_pre * - a function callback to be called upon visiting a node in the tree * @param visit_post @@ -733,37 +804,35 @@ function theme_book_export_html($title, * @return * - the output generated in visiting each node */ -function book_recurse($nid = 0, $depth = 1, $visit_pre, $visit_post) { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND n.nid = %d ORDER BY b.weight, n.title'), $nid); - while ($page = db_fetch_object($result)) { - // Load the node: - $node = node_load($page->nid); +function book_export_traverse($tree, $visit_pre, $visit_post) { + + foreach ($tree as $data) { + // Note- access checking should already have been performed when building the tree + $node = node_load($data['link']['nid'], FALSE); if ($node) { + $depth = $node->book_link['depth']; if (function_exists($visit_pre)) { $output .= call_user_func($visit_pre, $node, $depth, $nid); } else { $output .= book_node_visitor_html_pre($node, $depth, $nid); } - - $children = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d ORDER BY b.weight, n.title'), $node->nid); - while ($childpage = db_fetch_object($children)) { - $childnode = node_load($childpage->nid); - if ($childnode->nid != $node->nid) { - $output .= book_recurse($childnode->nid, $depth + 1, $visit_pre, $visit_post); - } + + if ($data['below']) { + $output .= book_export_traverse($data['below'], $visit_pre, $visit_post); } + if (function_exists($visit_post)) { $output .= call_user_func($visit_post, $node, $depth); } else { - # default + // default $output .= book_node_visitor_html_post($node, $depth); } } + } - return $output; } @@ -814,58 +883,59 @@ function book_node_visitor_html_post($no return "\n"; } -function _book_admin_table($nodes = array()) { +function _book_admin_table($node) { $form = array( '#theme' => 'book_admin_table', '#tree' => TRUE, ); - foreach ($nodes as $node) { - $form = array_merge($form, _book_admin_table_tree($node, 0)); - } - + $tree = book_menu_subtree_data($node->book_link); + _book_admin_table_tree($tree, $form); return $form; } -function _book_admin_table_tree($node, $depth) { - $form = array(); - - $form[] = array( - 'nid' => array('#type' => 'value', '#value' => $node->nid), - 'depth' => array('#type' => 'value', '#value' => $depth), - 'title' => array( - '#type' => 'textfield', - '#default_value' => $node->title, - '#maxlength' => 255, - ), - 'weight' => array( - '#type' => 'weight', - '#default_value' => $node->weight, - '#delta' => 15, - ), - ); - - $children = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d ORDER BY b.weight, n.title'), $node->nid); - while ($child = db_fetch_object($children)) { - $form = array_merge($form, _book_admin_table_tree(node_load($child->nid), $depth + 1)); +function _book_admin_table_tree($tree, &$form) { + foreach ($tree as $data) { + $form[] = array( + 'nid' => array('#type' => 'value', '#value' => $data['link']['nid']), + 'mlid' => array('#type' => 'value', '#value' => $data['link']['mlid']), + 'depth' => array('#type' => 'value', '#value' => $data['link']['depth']), + 'href' => array('#type' => 'value', '#value' => $data['link']['href']), + 'title' => array( + '#type' => 'textfield', + '#default_value' => $data['link']['link_title'], + '#maxlength' => 255, + ), + 'weight' => array( + '#type' => 'weight', + '#default_value' => ($data['link']['weight'] - 50000), + '#delta' => 15, + ), + ); + if ($data['below']) { + _book_admin_table_tree($data['below'], &$form); + } } return $form; } function theme_book_admin_table($form) { + $header = array(t('Title'), t('Weight'), array('data' => t('Operations'), 'colspan' => '3')); $rows = array(); + $destination = drupal_get_destination(); + $access = user_access('administer nodes'); foreach (element_children($form) as $key) { $nid = $form[$key]['nid']['#value']; - $pid = $form[0]['nid']['#value']; + $href = $form[$key]['href']['#value']; $rows[] = array( '
    '. drupal_render($form[$key]['title']) .'
    ', drupal_render($form[$key]['weight']), - l(t('view'), 'node/'. $nid), - l(t('edit'), 'node/'. $nid .'/edit'), - l(t('delete'), 'node/'. $nid .'/delete', NULL, 'destination=admin/content/book'. (arg(3) == 'orphan' ? '/orphan' : '') . ($pid != $nid ? '/'. $pid : '')) + l(t('view'), $href), + $access ? l(t('edit'), 'node/'. $nid .'/edit', array('query' => $destination)) : ' ', + $access ? l(t('delete'), 'node/'. $nid .'/delete', array('query' => $destination) ) : ' ', ); } @@ -875,59 +945,16 @@ function theme_book_admin_table($form) { /** * Display an administrative view of the hierarchy of a book. */ -function book_admin_edit($nid) { - $node = node_load($nid); - if ($node->nid) { +function book_admin_edit($node) { + drupal_set_title(check_plain($node->title)); $form = array(); - $form['table'] = _book_admin_table(array($node)); - $form['save'] = array( - '#type' => 'submit', - '#value' => t('Save book pages'), - ); - - return $form; - } - else { - drupal_not_found(); - } -} - -/** - * Menu callback; displays a listing of all orphaned book pages. - */ -function book_admin_orphan() { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, n.status, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid')); - - $pages = array(); - while ($page = db_fetch_object($result)) { - $pages[$page->nid] = $page; - } - - $orphans = array(); - if (count($pages)) { - foreach ($pages as $page) { - if ($page->parent && empty($pages[$page->parent])) { - $orphans[] = node_load($page->nid); - } - } - } - - if (count($orphans)) { - $form['table'] = _book_admin_table($orphans); + $form['table'] = _book_admin_table($node); $form['save'] = array( '#type' => 'submit', '#value' => t('Save book pages'), ); - - } - else { - $form['error'] = array('#value' => '

    '. t('There are no orphan pages.') .'

    '); - } - $form['#submit'][] = 'book_admin_edit_submit'; - $form['#validate'][] = 'book_admin_edit_validate'; - $form['#theme'] = 'book_admin_edit'; return $form; } @@ -935,9 +962,10 @@ function book_admin_edit_submit($form_va foreach ($form_values['table'] as $row) { $node = node_load($row['nid']); - if ($row['title'] != $node->title || $row['weight'] != $node->weight) { + if ($row['title'] != $node->title || $row['weight'] != $node->book_link['weight']) { $node->title = $row['title']; - $node->weight = $row['weight']; + $node->book_link['link_title'] = $row['title']; + $node->book_link['weight'] = $row['weight']; node_save($node); watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid)); @@ -949,32 +977,16 @@ function book_admin_edit_submit($form_va $book = node_load(arg(3)); drupal_set_message(t('Updated book %title.', array('%title' => $book->title))); } - else { - // Updating the orphan pages. - drupal_set_message(t('Updated orphan book pages.')); - } -} - -/** - * Menu callback; displays the book administration page. - */ -function book_admin($nid = 0) { - if ($nid) { - return drupal_get_form('book_admin_edit', $nid); - } - else { - return book_admin_overview(); - } } /** * Returns an administrative overview of all books. */ function book_admin_overview() { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = 0 ORDER BY b.weight, n.title')); + $result = db_query(db_rewrite_sql("SELECT n.title, b.* FROM {book} b INNER JOIN {node} n ON n.nid = b.nid WHERE b.nid = b.book_id ORDER BY n.title")); $rows = array(); while ($book = db_fetch_object($result)) { - $rows[] = array(l($book->title, "node/$book->nid"), l(t('outline'), "admin/content/book/$book->nid")); + $rows[] = array(l($book->title, "node/$book->nid"), l(t('edit order and titles'), "admin/content/book/$book->nid")); } $headers = array(t('Book'), t('Operations')); @@ -1005,4 +1017,65 @@ function book_help($section) { } } +/** + * Like menu_link_load(), but get extra data from the {book} table. + * + * Do not call when loading a node, since this function may call node_load(). + */ +function book_link_load($mlid) { + if ($item = db_fetch_array(db_query("SELECT * FROM {menu_links} ml INNER JOIN {book} b ON b.mlid = ml.mlid LEFT JOIN {menu_router} m ON m.path = ml.router_path WHERE ml.mlid = %d", $mlid))) { + _menu_link_translate($item); + return $item; + } + return FALSE; +} + +/** + * Get the data structure representing a subtree. the root item will be the + * link passed as a parameter, and the returned data will contain all the + * links that are below it in the menu tree.. + * + * @param $menu_name + * The named menu links to return + * @param $item + * A fully loaded menu link, + * @return + * An subtree of menu links in an array, in the order they should be rendered. + */ +function book_menu_subtree_data($item) { + static $tree = array(); + + $mlid = $item['mlid']; + $menu_name = $item['menu_name']; + $cid = 'links:'. $menu_name .':subtree:'. $mlid; + + if (!isset($tree[$cid])) { + $cache = cache_get($cid, 'cache_menu'); + if ($cache && isset($cache->data)) { + $tree[$cid] = $cache->data; + } + else { + $i = 1; + $match = ''; + while ($i <= MENU_MAX_DEPTH && $item["p$i"]) { + $match .= " AND p$i = %d"; + $args[] = $item["p$i"]; + $i++; + } + $sql = " + SELECT *, ml.weight + 50000 AS weight FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path + INNER JOIN {book} b ON ml.mlid = b.mlid + WHERE ml.menu_name = '%s'". $match ." + ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC"; + + array_unshift($args, $menu_name); + list(, $tree[$cid]) = _menu_tree_data(db_query($sql, $args), array(), $item['depth']); + cache_set($cid, $tree[$cid], 'cache_menu'); + } + // TODO: special case node links and access check via db_rewite_sql() + _menu_tree_check_access($tree[$cid]); + } + + return $tree[$cid]; +} Index: modules/book/book.schema =================================================================== RCS file: /cvs/drupal/drupal/modules/book/book.schema,v retrieving revision 1.1 diff -u -p -r1.1 book.schema --- modules/book/book.schema 25 May 2007 12:46:43 -0000 1.1 +++ modules/book/book.schema 28 May 2007 04:12:12 -0000 @@ -4,16 +4,16 @@ function book_schema() { $schema['book'] = array( 'fields' => array( - 'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), - 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), - 'parent' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), - 'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny') + 'mlid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), + 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), + 'book_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), + 'child_type' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''), ), 'indexes' => array( 'nid' => array('nid'), - 'parent' => array('parent') + 'book_id' => array('bookid') ), - 'primary key' => array('vid'), + 'primary key' => array('mlid'), ); return $schema;