Berlin (Germany)

nid; $levels_deep = 3; $emulate_book_block = true; if (!function_exists('book_struct_recurse')){ 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 = ''; foreach ($children[$nid] as $key => $node) { if ($tree = book_struct_recurse($node->nid, $levels_deep - 1, $children, $current_lineage, $emulate_book_block)) { $struct .= node_view(node_load(array('nid' => $node->nid)), 1); $struct .= $tree; } else { if ($children[$node->nid]){ $struct .= node_view(node_load(array('nid' => $node->nid)), 1); } else { $struct .= node_view(node_load(array('nid' => $node->nid)), 1); } } } return $struct; } } } if ($node = menu_get_object('node', 1, 'node/'.$book_top_page)) { // Only display this block when $book_top_page refers to an actual book page // first retrieve the book structure from the menu system $result = db_query('SELECT link_path, link_title, mlid, plid FROM {menu_links},{node} WHERE link_title=title AND status=1 AND menu_name="%s" ORDER BY weight, link_title', $node->book['menu_name']); $menu = array(); while ($m = db_fetch_object($result)) { $menu[$m->mlid] = $m; $menu[$m->mlid]->nid = $nid = (int)substr($m->link_path, 5); } // now use the $menu array to build a proper $children and $parent array $children = array(); $parent = array(); foreach ($menu as $key => $val) { $nid = is_null($menu[$val->plid]->nid) ? 0 : $menu[$val->plid]->nid; if (!$children[$nid]) { $children[$nid] = array(); } array_push($children[$nid], $val); $parent[is_null($val->nid) ? 0 : (int)$val->nid] = $nid; } // $children array properly formed, now get the $current_lineage array $current_lineage = array(); if (arg(0) == 'node' && is_numeric(arg(2)) && $parent[arg(2)]) { $nid = arg(2); while ($nid) { array_unshift($current_lineage, $nid); $nid = $parent[$nid]; } } echo book_struct_recurse($book_top_page, $levels_deep, $children, $current_lineage, $emulate_book_block); } ?>