Book Nav View By Category With Auto-Add
This should work for a block. Make sure you don't have those three supporting functions declared elsewhere (though I seriously doubt it)...
book_struct_recurse() --from the original script
book_struct_recurse_caller() --from the original script
book_location_hacked() --from book.module
Sorry about the added complexity, I have several book-like modules that I pipe through this stuff, so I need to be able to pass the $tablename param.
Notes:
a) make sure to set the $book_taxonomy_vid to whatever taxonomy you're assigning to books.
b) if you don't assign the appropriate vid when creating the book node, it won't show
c) you can futz with the params in book_struct_recurse_caller() just like you could in the original script.
d) the menu hook is currently unneeded
e) there needs to be some sort of sorting to the categories. Currently, it is alpha.
<?php
/*Implementation of hook_help(). */
function catbooks_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Poorly written code.');
}
}
/*Implementation of hook_menu(). */
function catbooks_menu($may_cache) {
global $user;
$items = array();
if ($may_cache) {
$items[] = array('path' => 'catbooks', 'title' => t('catbooks'),
'callback' => 'catbooks',
'access' => 1,
'type' => MENU_SUGGESTED_ITEM);
}
return $items;
}
/*Implementation of hook_block(). */
function catbooks_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
if ($op == 'list') {
$blocks[0]['info'] = t('catbooks');
return $blocks;
}elseif ($op == 'view' && $user->uid != 0){
$block = array();
switch ($delta) {
case 0:
$book_taxonomy_vid = 2;
$sql = 'SELECT td.name, b.nid, n.title
FROM book b
INNER JOIN term_node tn ON b.nid = tn.nid
INNER JOIN term_data td ON td.tid = tn.tid
INNER JOIN node n ON b.vid = n.vid
WHERE td.vid = 2 AND
b.parent = 0
ORDER BY td.name DESC
';
$return = db_query($sql);
while($row = db_fetch_object($return)){
$current_tax = $row->name;
if($current_tax != $last_tax){
$output .= '<div id = "catbook_header">'.$row->name.'</div>';
}
$output .= '<ul class="profilebook"><li class="leaf">'.l($row->title, 'node/'.$row->nid).'</li>';
$output .= book_struct_recurse_caller($row->nid, 0, true, 'book');
$output .= '</ul>';
$last_tax = $current_tax;
}
$block['subject'] = t('catbook');
$block['content'] = $output;
return $block;
}
}
}
/*The following functions are the supporting api for the book navigation blocks. */
function book_struct_recurse($nid, $levels_deep, $children, $current_lineage = array(), $emulate_book_block = true) {
$struct = '';
if ($children[$nid] && ($levels_deep > 0 || ($emulate_book_block && in_array($nid, $current_lineage)))) {
$struct = '<ul class="profilebook">';
foreach ($children[$nid] as $key => $node) {
if ($tree = book_struct_recurse($node->nid, $levels_deep - 1, $children, $current_lineage, $emulate_book_block)) {
$struct .= '<li class="expanded">';
$struct .= l($node->title, 'node/'. $node->nid);
$struct .= $tree;
$struct .= '</li>';
}else {
if ($children[$node->nid]){
$struct .= '<li class="collapsed">'. l($node->title, 'node/'. $node->nid) .'</li>';
}else {
$struct .= '<li class="leaf">'. l($node->title, 'node/'. $node->nid) .'</li>';
}
}
}
$struct .= '</ul>';
return $struct;
}
}
function book_struct_recurse_caller($book_top_page = 4, $levels_deep = 12, $emulate_book_block = true, $tablename){
$current_lineage = array();
//$sql = 'SELECT DISTINCT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN '.'{'.$tablename.'}'.' b ON n.nid = b.nid WHERE n.status = 1 ORDER BY b.weight, n.title';
$sql = 'SELECT DISTINCT n.nid, n.title, b.parent, b.weight
FROM node n INNER JOIN '.'{'.$tablename.'}'.' b
ON n.nid = b.nid AND n.vid = b.vid
ORDER BY b.weight, n.title';
$result = db_query(db_rewrite_sql($sql));
while ($node = db_fetch_object($result)) {
if (!$children[$node->parent]) {
$children[$node->parent] = array();
}
array_push($children[$node->parent], $node);
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(1) == $node->nid) {
$_temp = book_location_hacked($node, $tablename);
//$_temp = book_location2($node);
foreach ($_temp as $key => $val){
$current_lineage[] = $val->nid;
}
$current_lineage[] = arg(1);
}
}
$output .= book_struct_recurse($book_top_page, $levels_deep, $children, $current_lineage, $emulate_book_block);
return $output;
}
function book_location_hacked($node, $tablename, $nodes = array() ) {
$sql = 'SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {'.$tablename.'} b ON n.vid = b.vid WHERE n.nid = '.$node->parent;
$parent = db_fetch_object(db_query(db_rewrite_sql($sql)));
if ($parent->title) {
$nodes = book_location_hacked($parent, $tablename, $nodes);
$nodes[] = $parent;
}
return $nodes;
}
?>