cvs: pear /HTML_Menu Menu.php MenuBrowser.php package.xml
| From: | Sebastian Bergmann | Date: | Sun, 28 Apr 2002 07:14:48 +0000 |
| Subject: | cvs: pear /HTML_Menu Menu.php MenuBrowser.php package.xml | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-3592@lists.php.net to get a copy of this message | ||
sebastian Sun Apr 28 03:14:48 2002 EDT
Added files:
/pear/HTML_Menu Menu.php MenuBrowser.php package.xml
Log:
Move Menu* to /pear.
Index: pear/HTML_Menu/Menu.php +++ pear/HTML_Menu/Menu.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> | // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // // $Id: Menu.php,v 1.1 2002/04/28 07:14:48 sebastian Exp $ // /** * Generates a HTML menu from a multidimensional hash. * * Special thanks to the original author: Alex Vorobiev <sasha@mathforum.com>. * * @version $Id: Menu.php,v 1.1 2002/04/28 07:14:48 sebastian Exp $ * @author Ulf Wendel <ulf.wendel@phpdoc.de> * @access public * @package HTML */ class HTML_Menu { /** * URL Environment Variable * * @var string */ var $url_env_var = 'PHP_SELF'; /** * Menu structure as a multidimensional hash. * * @var array * @see setMenu(), Menu() */ var $menu = array(); /** * Mapping from URL to menu path. * * @var array * @see getPath() */ var $urlmap = array(); /** * Menu type: tree, rows, you-are-here. * * @var array * @see setMenuType() */ var $menu_type = 'tree'; /** * Path to a certain menu item. * * Internal class variable introduced to save some recursion overhead. * * @var array * @see get(), getPath() */ var $path = array(); /** * Generated HTML menu. * * @var string * @see get() */ var $html = ''; /** * URL of the current page. * * This can be the URL of the current page but it must not be exactly the * return value of getCurrentURL(). If there's no entry for the return value * in the menu hash getPath() tries to find the menu item that fits best * by shortening the URL sign by sign until it finds an entry that fits. * * @see getCurrentURL(), getPath() */ var $current_url = ''; /** * Initializes the menu, sets the type and menu structure. * * @param array * @param string * @see setMenuType(), setMenu() */ function HTML_Menu($menu = '', $type = 'tree', $url_env_var = 'PHP_SELF') { if (is_array($menu)) $this->setMenu($menu); $this->setMenuType($type); $this->setURLEnvVar($url_env_var); } /** * Sets the menu structure. * * The menu structure is defined by a multidimensional hash. This is * quite "dirty" but simple and easy to traverse. An example * show the structure. To get the following menu: * * 1 - Projects * 11 - Projects => PHPDoc * 12 - Projects => Forms * 2 - Stuff * * you need the array: * * $menu = array( * 1 => array( * 'title' => 'Projects', * 'url' => '/projects/index.php', * 'sub' => array( * 11 => array( * 'title' => 'PHPDoc', * ... * ), * 12 => array( ... ), * ) * ), * 2 => array( 'title' => 'Stuff', 'url' => '/stuff/index.php' ) * ) * * Note the index 'sub' and the nesting. Note also that 1, 11, 12, 2 * must be unique. The class uses them as ID's. * * @param array * @access public * @see append(), update() */ function setMenu($menu) { $this->menu = $menu; $this->urlmap = array(); } /** * Sets the type / format of the menu: tree, rows or urhere. * * @param string "tree", "rows", "urhere", "prevnext" * @access public */ function setMenuType($menu_type) { $this->menu_type = strtolower($menu_type); } /** * Sets the environment variable to use to get the current URL. * * @param string environment variable for current URL * @access public */ function setURLEnvVar($url_env_var) { $this->url_env_var = $url_env_var; } /** * Returns the HTML menu. * * @param string Menu type: tree, urhere, rows, prevnext, sitemap * @return string HTML of the menu * @access public */ function get($menu_type = '') { if ('' != $menu_type) $this->setMenuType($menu_type); $this->html = ''; // buildMenu for rows cares on this itself if ('rows' != $this->menu_type) $this->html .= $this->getStart(); // storing to a class variable saves some recursion overhead $this->path = $this->getPath(); if ('sitemap' == $this->menu_type) { $this->setMenuType('tree'); $this->buildSitemap($this->menu); $this->setMenuType('sitemap'); } else { $this->buildMenu($this->menu); } if ('rows' != $this->menu_type) $this->html .= $this->getEnd(); return $this->html; } /** * Prints the HTML menu. * * @brother get() * @return void */ function show($menu_type = '') { print $this->get($menu_type); } /** * Returns the prefix of the HTML menu items. * * @return string HTML menu prefix * @see getEnd(), get() */ function getStart() { $html = ''; switch ($this->menu_type) { case 'rows': case 'prevnext': $html .= '<table border><tr>'; break; case 'tree': case 'urhere': case 'sitemap': $html .= '<table border>'; break; } return $html; } /** * Returns the postfix of the HTML menu items. * * @return string HTML menu postfix * @see getStart(), get() */ function getEnd() { $html = ''; switch ($this->menu_type) { case 'rows': case 'prevnext': $html .= '</tr></table>'; break; case 'tree': case 'urhere': case 'sitemap': $html .= '</table>'; break; } return $html; } /** * Build the menu recursively. * * @param array first call: $this->menu, recursion: submenu * @param integer level of recursion, current depth in the tree structure * @param integer prevnext flag */ function buildMenu($menu, $level = 0, $flag_stop_level = -1) { static $last_node = array(), $up_node = array(); // the recursion goes slightly different for every menu type switch ($this->menu_type) { case 'tree': // loop through the (sub)menu foreach ($menu as $node_id => $node) { if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { // not selected, not a part of the path to the active menu item $type = 0; } $this->html .= $this->getEntry($node, $level, $type); // follow the subtree if the active menu item is in it if ($type && isset($node['sub'])) $this->buildMenu($node['sub'], $level + 1); } break; case 'rows': // every (sub)menu has it's own table $this->html .= $this->getStart(); $submenu = false; // loop through the (sub)menu foreach ($menu as $node_id => $node) { if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { // not selected, not a part of the path to the active menu item $type = 0; } $this->html .= $this->getEntry($node, $level, $type); // remember the subtree if ($type && isset($node['sub'])) $submenu = $node['sub']; } // close the table for this level $this->html .= $this->getEnd(); // go deeper if neccessary if ($submenu) $this->buildMenu($submenu, $level + 1); break; case 'urhere': // loop through the (sub)menu foreach ($menu as $node_id => $node) { if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { // not selected, not a part of the path to the active menu item $type = 0; } // follow the subtree if the active menu item is in it if ($type) { $this->html .= $this->getEntry($node, $level, $type); if (isset($node['sub'])) { $this->buildMenu($node['sub'], $level + 1); continue; } } } break; case 'prevnext': // loop through the (sub)menu foreach ($menu as $node_id => $node) { if (-1 != $flag_stop_level) { // add this item to the menu and stop recursion - (next >>) node if ($flag_stop_level == $level) { $this->html .= $this->getEntry($node, $level, 4); $flag_stop_level = -1; } break; } else if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; $flag_stop_level = $level; if (0 != count($last_node)) { $this->html .= $this->getEntry($last_node, $level, 3); } else { // WARNING: if there's no previous take the first menu entry - you might not like this rule! reset($this->menu); list($node_id, $first_node) = each($this->menu); $this->html .= $this->getEntry($first_node, $level, 3); } if (0 != count($up_node)) { $this->html .= $this->getEntry($up_node, $level, 5); } else { // WARNING: if there's no up take the first menu entry - you might not like this rule! reset($this->menu); list($node_id, $first_node) = each($this->menu); $this->html .= $this->getEntry($first_node, $level, 5); } } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { $type = 0; } // remember the last (<< prev) node $last_node = $node; // follow the subtree if the active menu item is in it if ($type && isset($node['sub'])) { $up_node = $node; $flag_stop_level = $this->buildMenu($node['sub'], $level + 1, (-1 != $flag_stop_level) ? $flag_stop_level + 1 : -1); } } break; } return ($flag_stop_level) ? $flag_stop_level - 1 : -1; } /** * Build the menu recursively. * * @param array first call: $this->menu, recursion: submenu * @param int level of recursion, current depth in the tree structure */ function buildSitemap($menu, $level = 0) { // loop through the (sub)menu foreach ($menu as $node_id => $node) { if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { // not selected, not a part of the path to the active menu item $type = 0; } $this->html .= $this->getEntry($node, $level, $type); // follow the subtree if the active menu item is in it if (isset($node['sub'])) $this->buildSitemap($node['sub'], $level + 1); } } /** * Returns the HTML of one menu item. * * @param array menu item data (node data) * @param integer level in the menu tree * @param integer menu item type: 0, 1, 2. 0 => plain menu item, * 1 => selected (active) menu item, 2 => item * is a member of the path to the selected (active) menu item * 3 => previous entry, 4 => next entry * @return string HTML * @see buildMenu() */ function getEntry(&$node, $level, $item_type) { $html = ''; $indent = ''; if ('tree' == $this->menu_type) { // tree menu $html .= '<tr>'; $indent = ''; if ($level) for ($i = 0; $i < $level; $i++) $indent .= ' '; } // draw the <td></td> cell depending on the type of the menu item switch ($item_type) { case 0: // plain menu item $html .= sprintf('<td>%s<a href="%s">%s</a></td>', $indent, $node['url'], $node['title'] ); break; case 1: // selected (active) menu item $html .= sprintf('<td>%s<b>%s</b></td>', $indent, $node['title'] ); break; case 2: // part of the path to the selected (active) menu item $html .= sprintf('<td>%s<b><a href="%s">%s</a></b>%s</td>', $indent, $node['url'], $node['title'], ('urhere' == $this->menu_type) ? ' >> ' : '' ); break; case 3: // << previous url $html .= sprintf('<td>%s<a href="%s"><< %s</a></td>', $indent, $node['url'], $node['title'] ); break; case 4: // next url >> $html .= sprintf('<td>%s<a href="%s">%s >></a></td>', $indent, $node['url'], $node['title'] ); break; case 5: // up url ^^ $html .= sprintf('<td>%s<a href="%s">^ %s ^</a></td>', $indent, $node['url'], $node['title'] ); break; } if ('tree' == $this->menu_type) $html .= '</tr>'; return $html; } /* * Returns the path of the current page in the menu 'tree'. * * @return array path to the selected menu item * @see buildPath(), $urlmap */ function getPath() { $this->current_url = $this->getCurrentURL(); $this->buildPath($this->menu, array()); while ($this->current_url && !isset($this->urlmap[$this->current_url])) $this->current_url = substr($this->current_url, 0, -1); return $this->urlmap[$this->current_url]; } /** * Computes the path of the current page in the menu 'tree'. * * @param array first call: menu hash / recursion: submenu hash * @param array first call: array() / recursion: path * @return boolean true if the path to the current page was found, * otherwise false. Only meaningful for the recursion. * @global $PHP_SELF * @see getPath(), $urlmap */ function buildPath($menu, $path) { // loop through the (sub)menu foreach ($menu as $node_id => $node) { // save the path of the current node in the urlmap $this->urlmap[$node['url']] = $path; // we got'em - stop the search by returning true // KLUDGE: getPath() works with the best alternative for a URL if there's // no entry for a URL in the menu hash, buildPath() does not perform this test // and might do some unneccessary recursive runs. if ($node['url'] == $this->current_url) return true; // current node has a submenu if (isset($node['sub'])) { // submenu path = current path + current node $subpath = $path; $subpath[] = $node_id; // continue search recursivly - return is the inner loop finds the // node that belongs to the current url if ($this->buildPath($node['sub'], $subpath)) return true; } } // not found return false; } /** * Returns the URL of the currently selected page. * * The returned string is used for all test against the URL's * in the menu structure hash. * * @return string */ function getCurrentURL() { if (isset($_SERVER[$this->url_env_var])) return $_SERVER[$this->url_env_var]; else if (isset($GLOBALS[$this->url_env_var])) return $GLOBALS[$this->url_env_var]; else return ''; } } ?> Index: pear/HTML_Menu/MenuBrowser.php +++ pear/HTML_Menu/MenuBrowser.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Ulf Wendel <ulf.wendel@phpdoc.de> | // +----------------------------------------------------------------------+ // // $Id: MenuBrowser.php,v 1.1 2002/04/28 07:14:48 sebastian Exp $ /** * Simple filesystem browser that can be used to generated menu (3) hashes based on the directory structure. * * Together with menu (3) and the (userland) cache you can use this * browser to generate simple fusebox like applications / content systems. * * Let the menubrowser scan your document root and generate a menu (3) structure * hash which maps the directory structure, pass it to menu's setMethod() and optionally * wrap the cache around all this to save script runs. If you do so, it looks * like this: * * // document root directory * define('DOC_ROOT', '/home/server/www.example.com/'); * * // instantiate the menubrowser * $browser = new menubrowser(DOC_ROOT); * * // instantiate menu (3) * $menu = new menu($browser->getMenu()); * * // output the sitemap * $menu->show('sitemap'); * * Now, use e.g. simple XML files to store your content and additional menu informations * (title!). Subclass exploreFile() depending on your file format. * * @author Ulf Wendel <ulf.wendel@phpdoc.de> * @version $Id: MenuBrowser.php,v 1.1 2002/04/28 07:14:48 sebastian Exp $ */ class HTML_MenuBrowser { /** * Filesuffix of your XML files. * * @var string * @see menubrowser() */ var $file_suffix = 'xml'; /** * Number of characters of the file suffix. * * @var int * @see menubrowser() */ var $file_suffix_length = 3; /** * Filename (without suffix) of your index / start pages. * * @var string * @see menubrowser() */ var $index = 'index'; /** * Full filename of your index / start pages. * * @var string * @see $file_suffix, $index */ var $index_file = ''; /** * Directory to scan. * * @var string * @see setDirectory() */ var $dir = ''; /** * Prefix for every menu hash entry. * * Set the ID prefix if you want to merge the browser menu * hash with another (static) menu hash so that there're no * name clashes with the ids. * * @var string * @see setIDPrefix() */ var $id_prefix = ''; /** * Menu (3)'s setMenu() hash. * * @var array */ var $menu = array(); /** * Creates the object and optionally sets the directory to scan. * * @param string * @see $dir */ function HTML_MenuBrowser($dir = '', $index = '', $file_suffix = '') { if ($dir) $this->dir = $dir; if ($index) $this->index = $index; if ($file_suffix) $this->file_suffix = $file_suffix; $this->index_file = $this->index . '.' . $this->file_suffix; $this->file_suffix_length = strlen($this->file_suffix); } /** * Sets the directory to scan. * * @param string directory to scan * @access public */ function setDirectory($dir) { $this->dir = $dir; } /** * Sets the prefix for every id in the menu hash. * * @param string * @access public */ function setIDPrefix($prefix) { $this->id_prefix = $prefix; } /** * Returns a hash to be used with menu(3)'s setMenu(). * * @param string directory to scan * @param string id prefix * @access public */ function getMenu($dir = '', $prefix = '') { if ($dir) $this->setDirectory($dir); if ($prefix) $this->setIDPrefix($prefix); // drop the result of previous runs $this->files = array(); $this->menu = $this->browse($this->dir); $this->menu = $this->addFileInfo($this->menu); return $this->menu; } /** * Recursive function that does the scan and builds the menu (3) hash. * * @param string directory to scan * @param integer entry id - used only for recursion * @param boolean ??? - used only for recursion * @return array */ function browse($dir, $id = 0, $noindex = false) { $struct = array(); $dh = opendir($dir); while ($file = readdir($dh)) { if ('.' == $file || '..' == $file) { continue; } $ffile = $dir . $file; if (is_dir($ffile)) { $ffile .= '/'; if (file_exists($ffile . $this->index_file)) { $id++; $struct[$this->id_prefix . $id]['url'] = $ffile . $this->index_file; $sub = $this->browse($ffile, $id + 1, true); if (0 != count($sub)) { $struct[$this->id_prefix . $id]['sub'] = $sub; } } } else { if ($this->file_suffix == substr($file, strlen($file) - $this->file_suffix_length, $this->file_suffix_length) && !($noindex && $this->index_file == $file) ) { $id++; $struct[$this->id_prefix . $id]['url'] = $dir . $file; } } } return $struct; } /** * Adds further informations to the menu hash gathered from the files in it * * @var array Menu hash to examine * @return array Modified menu hash with the new informations */ function addFileInfo($menu) { // no foreach - it works on a copy - the recursive // structure requires already lots of memory reset($menu); while (list($id, $data) = each($menu)) { $menu[$id] = array_merge($data, $this->exploreFile($data['url'])); if (isset($data['sub'])) $menu[$id]['sub'] = $this->addFileInfo($data['sub']); } return $menu; } /** * Returns additional menu informations decoded in the file that appears in the menu. * * You should subclass this method to make it work with your own * file formats. I used a simple XML format to store the content. * * @param string filename */ function exploreFile($file) { $xml = join('', @file($file)); if (!$xml) return array(); $doc = xmldoc($xml); $xpc = xpath_new_context($doc); $menu = xpath_eval($xpc, '//menu'); $node = &$menu->nodeset[0]; return array('title' => $node->content); } } ?> Index: pear/HTML_Menu/package.xml +++ pear/HTML_Menu/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "../package.dtd"> <package version="1.0"> <name>HTML_Menu</name> <summary>Generates HTML menus from multidimensional hashes.</summary> <description> With the HTML_Menu class one can easily create and maintain a navigation structure for websites, with simply configuring through a multidimensional hash structure. Different modes for the HTML output are supported. </description> <maintainers> <maintainer> <user>uw</user> <role>lead</role> <name>Ulf Wendel</name> <email>ulf.wendel@phpdoc.de</email> </maintainer> </maintainers> <license>PHP License</license> <release> <version>0.1</version> <state>stable</state> <date>2002-04-28</date> <notes> Initial independent PEAR release. </notes> <filelist> <dir name="/" baseinstalldir="HTML"> <file role="php" name="Menu.php"/> <file role="php" name="MenuBrowser.php"/> </dir> </filelist> </release> </package>
Index: pear/HTML_Menu/Menu.php +++ pear/HTML_Menu/Menu.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> | // | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // // $Id: Menu.php,v 1.1 2002/04/28 07:14:48 sebastian Exp $ // /** * Generates a HTML menu from a multidimensional hash. * * Special thanks to the original author: Alex Vorobiev <sasha@mathforum.com>. * * @version $Id: Menu.php,v 1.1 2002/04/28 07:14:48 sebastian Exp $ * @author Ulf Wendel <ulf.wendel@phpdoc.de> * @access public * @package HTML */ class HTML_Menu { /** * URL Environment Variable * * @var string */ var $url_env_var = 'PHP_SELF'; /** * Menu structure as a multidimensional hash. * * @var array * @see setMenu(), Menu() */ var $menu = array(); /** * Mapping from URL to menu path. * * @var array * @see getPath() */ var $urlmap = array(); /** * Menu type: tree, rows, you-are-here. * * @var array * @see setMenuType() */ var $menu_type = 'tree'; /** * Path to a certain menu item. * * Internal class variable introduced to save some recursion overhead. * * @var array * @see get(), getPath() */ var $path = array(); /** * Generated HTML menu. * * @var string * @see get() */ var $html = ''; /** * URL of the current page. * * This can be the URL of the current page but it must not be exactly the * return value of getCurrentURL(). If there's no entry for the return value * in the menu hash getPath() tries to find the menu item that fits best * by shortening the URL sign by sign until it finds an entry that fits. * * @see getCurrentURL(), getPath() */ var $current_url = ''; /** * Initializes the menu, sets the type and menu structure. * * @param array * @param string * @see setMenuType(), setMenu() */ function HTML_Menu($menu = '', $type = 'tree', $url_env_var = 'PHP_SELF') { if (is_array($menu)) $this->setMenu($menu); $this->setMenuType($type); $this->setURLEnvVar($url_env_var); } /** * Sets the menu structure. * * The menu structure is defined by a multidimensional hash. This is * quite "dirty" but simple and easy to traverse. An example * show the structure. To get the following menu: * * 1 - Projects * 11 - Projects => PHPDoc * 12 - Projects => Forms * 2 - Stuff * * you need the array: * * $menu = array( * 1 => array( * 'title' => 'Projects', * 'url' => '/projects/index.php', * 'sub' => array( * 11 => array( * 'title' => 'PHPDoc', * ... * ), * 12 => array( ... ), * ) * ), * 2 => array( 'title' => 'Stuff', 'url' => '/stuff/index.php' ) * ) * * Note the index 'sub' and the nesting. Note also that 1, 11, 12, 2 * must be unique. The class uses them as ID's. * * @param array * @access public * @see append(), update() */ function setMenu($menu) { $this->menu = $menu; $this->urlmap = array(); } /** * Sets the type / format of the menu: tree, rows or urhere. * * @param string "tree", "rows", "urhere", "prevnext" * @access public */ function setMenuType($menu_type) { $this->menu_type = strtolower($menu_type); } /** * Sets the environment variable to use to get the current URL. * * @param string environment variable for current URL * @access public */ function setURLEnvVar($url_env_var) { $this->url_env_var = $url_env_var; } /** * Returns the HTML menu. * * @param string Menu type: tree, urhere, rows, prevnext, sitemap * @return string HTML of the menu * @access public */ function get($menu_type = '') { if ('' != $menu_type) $this->setMenuType($menu_type); $this->html = ''; // buildMenu for rows cares on this itself if ('rows' != $this->menu_type) $this->html .= $this->getStart(); // storing to a class variable saves some recursion overhead $this->path = $this->getPath(); if ('sitemap' == $this->menu_type) { $this->setMenuType('tree'); $this->buildSitemap($this->menu); $this->setMenuType('sitemap'); } else { $this->buildMenu($this->menu); } if ('rows' != $this->menu_type) $this->html .= $this->getEnd(); return $this->html; } /** * Prints the HTML menu. * * @brother get() * @return void */ function show($menu_type = '') { print $this->get($menu_type); } /** * Returns the prefix of the HTML menu items. * * @return string HTML menu prefix * @see getEnd(), get() */ function getStart() { $html = ''; switch ($this->menu_type) { case 'rows': case 'prevnext': $html .= '<table border><tr>'; break; case 'tree': case 'urhere': case 'sitemap': $html .= '<table border>'; break; } return $html; } /** * Returns the postfix of the HTML menu items. * * @return string HTML menu postfix * @see getStart(), get() */ function getEnd() { $html = ''; switch ($this->menu_type) { case 'rows': case 'prevnext': $html .= '</tr></table>'; break; case 'tree': case 'urhere': case 'sitemap': $html .= '</table>'; break; } return $html; } /** * Build the menu recursively. * * @param array first call: $this->menu, recursion: submenu * @param integer level of recursion, current depth in the tree structure * @param integer prevnext flag */ function buildMenu($menu, $level = 0, $flag_stop_level = -1) { static $last_node = array(), $up_node = array(); // the recursion goes slightly different for every menu type switch ($this->menu_type) { case 'tree': // loop through the (sub)menu foreach ($menu as $node_id => $node) { if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { // not selected, not a part of the path to the active menu item $type = 0; } $this->html .= $this->getEntry($node, $level, $type); // follow the subtree if the active menu item is in it if ($type && isset($node['sub'])) $this->buildMenu($node['sub'], $level + 1); } break; case 'rows': // every (sub)menu has it's own table $this->html .= $this->getStart(); $submenu = false; // loop through the (sub)menu foreach ($menu as $node_id => $node) { if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { // not selected, not a part of the path to the active menu item $type = 0; } $this->html .= $this->getEntry($node, $level, $type); // remember the subtree if ($type && isset($node['sub'])) $submenu = $node['sub']; } // close the table for this level $this->html .= $this->getEnd(); // go deeper if neccessary if ($submenu) $this->buildMenu($submenu, $level + 1); break; case 'urhere': // loop through the (sub)menu foreach ($menu as $node_id => $node) { if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { // not selected, not a part of the path to the active menu item $type = 0; } // follow the subtree if the active menu item is in it if ($type) { $this->html .= $this->getEntry($node, $level, $type); if (isset($node['sub'])) { $this->buildMenu($node['sub'], $level + 1); continue; } } } break; case 'prevnext': // loop through the (sub)menu foreach ($menu as $node_id => $node) { if (-1 != $flag_stop_level) { // add this item to the menu and stop recursion - (next >>) node if ($flag_stop_level == $level) { $this->html .= $this->getEntry($node, $level, 4); $flag_stop_level = -1; } break; } else if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; $flag_stop_level = $level; if (0 != count($last_node)) { $this->html .= $this->getEntry($last_node, $level, 3); } else { // WARNING: if there's no previous take the first menu entry - you might not like this rule! reset($this->menu); list($node_id, $first_node) = each($this->menu); $this->html .= $this->getEntry($first_node, $level, 3); } if (0 != count($up_node)) { $this->html .= $this->getEntry($up_node, $level, 5); } else { // WARNING: if there's no up take the first menu entry - you might not like this rule! reset($this->menu); list($node_id, $first_node) = each($this->menu); $this->html .= $this->getEntry($first_node, $level, 5); } } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { $type = 0; } // remember the last (<< prev) node $last_node = $node; // follow the subtree if the active menu item is in it if ($type && isset($node['sub'])) { $up_node = $node; $flag_stop_level = $this->buildMenu($node['sub'], $level + 1, (-1 != $flag_stop_level) ? $flag_stop_level + 1 : -1); } } break; } return ($flag_stop_level) ? $flag_stop_level - 1 : -1; } /** * Build the menu recursively. * * @param array first call: $this->menu, recursion: submenu * @param int level of recursion, current depth in the tree structure */ function buildSitemap($menu, $level = 0) { // loop through the (sub)menu foreach ($menu as $node_id => $node) { if ($this->current_url == $node['url']) { // menu item that fits to this url - 'active' menu item $type = 1; } else if (isset($this->path[$level]) && $this->path[$level] == $node_id) { // processed menu item is part of the path to the active menu item $type = 2; } else { // not selected, not a part of the path to the active menu item $type = 0; } $this->html .= $this->getEntry($node, $level, $type); // follow the subtree if the active menu item is in it if (isset($node['sub'])) $this->buildSitemap($node['sub'], $level + 1); } } /** * Returns the HTML of one menu item. * * @param array menu item data (node data) * @param integer level in the menu tree * @param integer menu item type: 0, 1, 2. 0 => plain menu item, * 1 => selected (active) menu item, 2 => item * is a member of the path to the selected (active) menu item * 3 => previous entry, 4 => next entry * @return string HTML * @see buildMenu() */ function getEntry(&$node, $level, $item_type) { $html = ''; $indent = ''; if ('tree' == $this->menu_type) { // tree menu $html .= '<tr>'; $indent = ''; if ($level) for ($i = 0; $i < $level; $i++) $indent .= ' '; } // draw the <td></td> cell depending on the type of the menu item switch ($item_type) { case 0: // plain menu item $html .= sprintf('<td>%s<a href="%s">%s</a></td>', $indent, $node['url'], $node['title'] ); break; case 1: // selected (active) menu item $html .= sprintf('<td>%s<b>%s</b></td>', $indent, $node['title'] ); break; case 2: // part of the path to the selected (active) menu item $html .= sprintf('<td>%s<b><a href="%s">%s</a></b>%s</td>', $indent, $node['url'], $node['title'], ('urhere' == $this->menu_type) ? ' >> ' : '' ); break; case 3: // << previous url $html .= sprintf('<td>%s<a href="%s"><< %s</a></td>', $indent, $node['url'], $node['title'] ); break; case 4: // next url >> $html .= sprintf('<td>%s<a href="%s">%s >></a></td>', $indent, $node['url'], $node['title'] ); break; case 5: // up url ^^ $html .= sprintf('<td>%s<a href="%s">^ %s ^</a></td>', $indent, $node['url'], $node['title'] ); break; } if ('tree' == $this->menu_type) $html .= '</tr>'; return $html; } /* * Returns the path of the current page in the menu 'tree'. * * @return array path to the selected menu item * @see buildPath(), $urlmap */ function getPath() { $this->current_url = $this->getCurrentURL(); $this->buildPath($this->menu, array()); while ($this->current_url && !isset($this->urlmap[$this->current_url])) $this->current_url = substr($this->current_url, 0, -1); return $this->urlmap[$this->current_url]; } /** * Computes the path of the current page in the menu 'tree'. * * @param array first call: menu hash / recursion: submenu hash * @param array first call: array() / recursion: path * @return boolean true if the path to the current page was found, * otherwise false. Only meaningful for the recursion. * @global $PHP_SELF * @see getPath(), $urlmap */ function buildPath($menu, $path) { // loop through the (sub)menu foreach ($menu as $node_id => $node) { // save the path of the current node in the urlmap $this->urlmap[$node['url']] = $path; // we got'em - stop the search by returning true // KLUDGE: getPath() works with the best alternative for a URL if there's // no entry for a URL in the menu hash, buildPath() does not perform this test // and might do some unneccessary recursive runs. if ($node['url'] == $this->current_url) return true; // current node has a submenu if (isset($node['sub'])) { // submenu path = current path + current node $subpath = $path; $subpath[] = $node_id; // continue search recursivly - return is the inner loop finds the // node that belongs to the current url if ($this->buildPath($node['sub'], $subpath)) return true; } } // not found return false; } /** * Returns the URL of the currently selected page. * * The returned string is used for all test against the URL's * in the menu structure hash. * * @return string */ function getCurrentURL() { if (isset($_SERVER[$this->url_env_var])) return $_SERVER[$this->url_env_var]; else if (isset($GLOBALS[$this->url_env_var])) return $GLOBALS[$this->url_env_var]; else return ''; } } ?> Index: pear/HTML_Menu/MenuBrowser.php +++ pear/HTML_Menu/MenuBrowser.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Ulf Wendel <ulf.wendel@phpdoc.de> | // +----------------------------------------------------------------------+ // // $Id: MenuBrowser.php,v 1.1 2002/04/28 07:14:48 sebastian Exp $ /** * Simple filesystem browser that can be used to generated menu (3) hashes based on the directory structure. * * Together with menu (3) and the (userland) cache you can use this * browser to generate simple fusebox like applications / content systems. * * Let the menubrowser scan your document root and generate a menu (3) structure * hash which maps the directory structure, pass it to menu's setMethod() and optionally * wrap the cache around all this to save script runs. If you do so, it looks * like this: * * // document root directory * define('DOC_ROOT', '/home/server/www.example.com/'); * * // instantiate the menubrowser * $browser = new menubrowser(DOC_ROOT); * * // instantiate menu (3) * $menu = new menu($browser->getMenu()); * * // output the sitemap * $menu->show('sitemap'); * * Now, use e.g. simple XML files to store your content and additional menu informations * (title!). Subclass exploreFile() depending on your file format. * * @author Ulf Wendel <ulf.wendel@phpdoc.de> * @version $Id: MenuBrowser.php,v 1.1 2002/04/28 07:14:48 sebastian Exp $ */ class HTML_MenuBrowser { /** * Filesuffix of your XML files. * * @var string * @see menubrowser() */ var $file_suffix = 'xml'; /** * Number of characters of the file suffix. * * @var int * @see menubrowser() */ var $file_suffix_length = 3; /** * Filename (without suffix) of your index / start pages. * * @var string * @see menubrowser() */ var $index = 'index'; /** * Full filename of your index / start pages. * * @var string * @see $file_suffix, $index */ var $index_file = ''; /** * Directory to scan. * * @var string * @see setDirectory() */ var $dir = ''; /** * Prefix for every menu hash entry. * * Set the ID prefix if you want to merge the browser menu * hash with another (static) menu hash so that there're no * name clashes with the ids. * * @var string * @see setIDPrefix() */ var $id_prefix = ''; /** * Menu (3)'s setMenu() hash. * * @var array */ var $menu = array(); /** * Creates the object and optionally sets the directory to scan. * * @param string * @see $dir */ function HTML_MenuBrowser($dir = '', $index = '', $file_suffix = '') { if ($dir) $this->dir = $dir; if ($index) $this->index = $index; if ($file_suffix) $this->file_suffix = $file_suffix; $this->index_file = $this->index . '.' . $this->file_suffix; $this->file_suffix_length = strlen($this->file_suffix); } /** * Sets the directory to scan. * * @param string directory to scan * @access public */ function setDirectory($dir) { $this->dir = $dir; } /** * Sets the prefix for every id in the menu hash. * * @param string * @access public */ function setIDPrefix($prefix) { $this->id_prefix = $prefix; } /** * Returns a hash to be used with menu(3)'s setMenu(). * * @param string directory to scan * @param string id prefix * @access public */ function getMenu($dir = '', $prefix = '') { if ($dir) $this->setDirectory($dir); if ($prefix) $this->setIDPrefix($prefix); // drop the result of previous runs $this->files = array(); $this->menu = $this->browse($this->dir); $this->menu = $this->addFileInfo($this->menu); return $this->menu; } /** * Recursive function that does the scan and builds the menu (3) hash. * * @param string directory to scan * @param integer entry id - used only for recursion * @param boolean ??? - used only for recursion * @return array */ function browse($dir, $id = 0, $noindex = false) { $struct = array(); $dh = opendir($dir); while ($file = readdir($dh)) { if ('.' == $file || '..' == $file) { continue; } $ffile = $dir . $file; if (is_dir($ffile)) { $ffile .= '/'; if (file_exists($ffile . $this->index_file)) { $id++; $struct[$this->id_prefix . $id]['url'] = $ffile . $this->index_file; $sub = $this->browse($ffile, $id + 1, true); if (0 != count($sub)) { $struct[$this->id_prefix . $id]['sub'] = $sub; } } } else { if ($this->file_suffix == substr($file, strlen($file) - $this->file_suffix_length, $this->file_suffix_length) && !($noindex && $this->index_file == $file) ) { $id++; $struct[$this->id_prefix . $id]['url'] = $dir . $file; } } } return $struct; } /** * Adds further informations to the menu hash gathered from the files in it * * @var array Menu hash to examine * @return array Modified menu hash with the new informations */ function addFileInfo($menu) { // no foreach - it works on a copy - the recursive // structure requires already lots of memory reset($menu); while (list($id, $data) = each($menu)) { $menu[$id] = array_merge($data, $this->exploreFile($data['url'])); if (isset($data['sub'])) $menu[$id]['sub'] = $this->addFileInfo($data['sub']); } return $menu; } /** * Returns additional menu informations decoded in the file that appears in the menu. * * You should subclass this method to make it work with your own * file formats. I used a simple XML format to store the content. * * @param string filename */ function exploreFile($file) { $xml = join('', @file($file)); if (!$xml) return array(); $doc = xmldoc($xml); $xpc = xpath_new_context($doc); $menu = xpath_eval($xpc, '//menu'); $node = &$menu->nodeset[0]; return array('title' => $node->content); } } ?> Index: pear/HTML_Menu/package.xml +++ pear/HTML_Menu/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "../package.dtd"> <package version="1.0"> <name>HTML_Menu</name> <summary>Generates HTML menus from multidimensional hashes.</summary> <description> With the HTML_Menu class one can easily create and maintain a navigation structure for websites, with simply configuring through a multidimensional hash structure. Different modes for the HTML output are supported. </description> <maintainers> <maintainer> <user>uw</user> <role>lead</role> <name>Ulf Wendel</name> <email>ulf.wendel@phpdoc.de</email> </maintainer> </maintainers> <license>PHP License</license> <release> <version>0.1</version> <state>stable</state> <date>2002-04-28</date> <notes> Initial independent PEAR release. </notes> <filelist> <dir name="/" baseinstalldir="HTML"> <file role="php" name="Menu.php"/> <file role="php" name="MenuBrowser.php"/> </dir> </filelist> </release> </package>