From 267044f0b320e5b88bfd39ba935a3b020884e5fb Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Mon, 17 Dec 2001 19:45:47 +0000 Subject: [PATCH] - Back-ported Foxen/Revar's improved book_toc() function. --- modules/book.module | 35 +++++++++++++++++++++++------------ modules/book/book.module | 35 +++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/modules/book.module b/modules/book.module index 9c486fe6e6f..fb1d47e523e 100644 --- a/modules/book.module +++ b/modules/book.module @@ -411,13 +411,27 @@ function book_view($node, $main = 0) { } } +function book_toc_recurse($nid, $indent, $toc, $children) { + + if ($children[$nid]) { + foreach ($children[$nid] as $foo => $node) { + $toc[$node->nid] = "$indent $node->title"; + $toc = book_toc_recurse($node->nid, "$indent--", $toc, $children); + } + } + + return $toc; +} + function book_toc($parent = "", $indent = "", $toc = array()) { - /* - ** Select all child nodes: - */ + $result = db_query("SELECT n.nid, n.title, b.parent FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.type = 'book' AND n.status = '1' ORDER BY b.weight, n.title"); - $result = db_query("SELECT n.nid, n.title FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight"); + while ($node = db_fetch_object($result)) { + $list = $children[$node->parent] ? $children[$node->parent] : array(); + array_push($list, $node); + $children[$node->parent] = $list; + } /* ** If the user is an administrator, add the root node; only @@ -429,13 +443,10 @@ function book_toc($parent = "", $indent = "", $toc = array()) { } /* - ** Build the table of contents: + ** Iterate root book nodes: */ - while ($node = db_fetch_object($result)) { - $toc[$node->nid] = "$indent $node->title"; - $toc = book_toc($node->nid, "$indent-", $toc); - } + $toc = book_toc_recurse(0, $indent, $toc, $children, $titles); return $toc; } @@ -541,12 +552,12 @@ function book_export_html($id = "", $depth = 1) { } } - $output .= book_export_html_recursive($id, $depth); + $output .= book_export_html_recurse($id, $depth); return $output; } -function book_export_html_recursive($parent = "", $depth = 1) { +function book_export_html_recurse($parent = "", $depth = 1) { $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight"); while ($page = db_fetch_object($result)) { @@ -566,7 +577,7 @@ function book_export_html_recursive($parent = "", $depth = 1) { $output .= ""; } - $output .= book_export_html_recursive($node->nid, $depth + 1); + $output .= book_export_html_recurse($node->nid, $depth + 1); } } diff --git a/modules/book/book.module b/modules/book/book.module index 9c486fe6e6f..fb1d47e523e 100644 --- a/modules/book/book.module +++ b/modules/book/book.module @@ -411,13 +411,27 @@ function book_view($node, $main = 0) { } } +function book_toc_recurse($nid, $indent, $toc, $children) { + + if ($children[$nid]) { + foreach ($children[$nid] as $foo => $node) { + $toc[$node->nid] = "$indent $node->title"; + $toc = book_toc_recurse($node->nid, "$indent--", $toc, $children); + } + } + + return $toc; +} + function book_toc($parent = "", $indent = "", $toc = array()) { - /* - ** Select all child nodes: - */ + $result = db_query("SELECT n.nid, n.title, b.parent FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.type = 'book' AND n.status = '1' ORDER BY b.weight, n.title"); - $result = db_query("SELECT n.nid, n.title FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight"); + while ($node = db_fetch_object($result)) { + $list = $children[$node->parent] ? $children[$node->parent] : array(); + array_push($list, $node); + $children[$node->parent] = $list; + } /* ** If the user is an administrator, add the root node; only @@ -429,13 +443,10 @@ function book_toc($parent = "", $indent = "", $toc = array()) { } /* - ** Build the table of contents: + ** Iterate root book nodes: */ - while ($node = db_fetch_object($result)) { - $toc[$node->nid] = "$indent $node->title"; - $toc = book_toc($node->nid, "$indent-", $toc); - } + $toc = book_toc_recurse(0, $indent, $toc, $children, $titles); return $toc; } @@ -541,12 +552,12 @@ function book_export_html($id = "", $depth = 1) { } } - $output .= book_export_html_recursive($id, $depth); + $output .= book_export_html_recurse($id, $depth); return $output; } -function book_export_html_recursive($parent = "", $depth = 1) { +function book_export_html_recurse($parent = "", $depth = 1) { $result = db_query("SELECT n.nid FROM node n LEFT JOIN book b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = '$parent' AND (n.moderate = 0 OR n.revisions != '') ORDER BY b.weight"); while ($page = db_fetch_object($result)) { @@ -566,7 +577,7 @@ function book_export_html_recursive($parent = "", $depth = 1) { $output .= ""; } - $output .= book_export_html_recursive($node->nid, $depth + 1); + $output .= book_export_html_recurse($node->nid, $depth + 1); } }