cvs: php4 /pear/HTML Menu.php Menu_Browser.php

From: Date: Wed, 08 Aug 2001 13:53:47 +0000
Subject: cvs: php4 /pear/HTML Menu.php Menu_Browser.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-439@lists.php.net to get a copy of this message
sebastian Wed Aug 8 09:53:47 2001 EDT Modified files: /php4/pear/HTML Menu.php Menu_Browser.php Log: Whitespace fixes. Added setURLEnvVar().

Index: php4/pear/HTML/Menu.php diff -u php4/pear/HTML/Menu.php:1.3 php4/pear/HTML/Menu.php:1.4 --- php4/pear/HTML/Menu.php:1.3 Wed Aug 8 06:44:28 2001 +++ php4/pear/HTML/Menu.php Wed Aug 8 09:53:47 2001 @@ -13,10 +13,11 @@ // | 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 | +// | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> | +// | Sebastian Bergmann <sb@sebastian-bergmann.de> | // +----------------------------------------------------------------------+ // -// $Id: Menu.php,v 1.3 2001/08/08 10:44:28 sebastian Exp $ +// $Id: Menu.php,v 1.4 2001/08/08 13:53:47 sebastian Exp $ // /** @@ -24,13 +25,20 @@ * * Special thanks to the original author: Alex Vorobiev <sasha@mathforum.com>. * -* @version $Id: Menu.php,v 1.3 2001/08/08 10:44:28 sebastian Exp $ +* @version $Id: Menu.php,v 1.4 2001/08/08 13:53:47 sebastian Exp $ * @author Ulf Wendel <ulf.wendel@phpdoc.de> * @access public * @package HTML */ -class menu { +class HTML_Menu { + /** + * URL Environment Variable + * + * @var array + */ + var $url_env_var = 'PHP_SELF'; + /** * Menu structure as a multidimensional hash. * @@ -38,8 +46,7 @@ * @see setMenu(), Menu() */ var $menu = array(); - - + /** * Mapping from URL to menu path. * @@ -47,8 +54,7 @@ * @see getPath() */ var $urlmap = array(); - - + /** * Menu type: tree, rows, you-are-here. * @@ -56,8 +62,7 @@ * @see setMenuType() */ var $menu_type = 'tree'; - - + /** * Path to a certain menu item. * @@ -67,8 +72,7 @@ * @see get(), getPath() */ var $path = array(); - - + /** * Generated HTML menu. * @@ -76,8 +80,7 @@ * @see get() */ var $html = ''; - - + /** * URL of the current page. * @@ -90,7 +93,6 @@ */ var $current_url = ''; - /** * Initializes the menu, sets the type and menu structure. * @@ -98,16 +100,14 @@ * @param string * @see setMenuType(), setMenu() */ - function menu($menu = '', $type = 'tree') { - + function HTML_Menu($menu = '', $type = 'tree', $url_env_var = 'PHP_SELF') { if (is_array($menu)) $this->setMenu($menu); $this->setMenuType($type); - - } // end constructor - - + $this->setURLEnvVar($url_env_var); + } + /** * Sets the menu structure. * @@ -145,13 +145,10 @@ * @see append(), update(), delete() */ function setMenu($menu) { - $this->menu = $menu; $this->urlmap = array(); - - } // end func setMenu - - + } + /** * Sets the type / format of the menu: tree, rows or urhere. * @@ -159,11 +156,18 @@ * @access public */ function setMenuType($menu_type) { - $this->menu_type = strtolower($menu_type); - - } // end func setMenuType + } + /** + * 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. @@ -194,14 +198,13 @@ } else { $this->buildMenu($this->menu); } - + if ('rows' != $this->menu_type) $this->html .= $this->getEnd(); return $this->html; - } // end func get - - + } + /** * Prints the HTML menu. * @@ -210,8 +213,7 @@ */ function show($menu_type = '') { print $this->get($menu_type); - } // end func show - + } /** * Returns the prefix of the HTML menu items. @@ -220,7 +222,6 @@ * @see getEnd(), get() */ function getStart() { - $html = ''; switch ($this->menu_type) { case 'rows': @@ -234,11 +235,10 @@ $html .= '<table border>'; break; } - + return $html; - } // end func getStart + } - /** * Returns the postfix of the HTML menu items. * @@ -246,7 +246,6 @@ * @see getStart(), get() */ function getEnd() { - $html = ''; switch ($this->menu_type) { case 'rows': @@ -262,9 +261,8 @@ } return $html; - } // end func getEnd - - + } + /** * Build the menu recursively. * @@ -277,12 +275,9 @@ // 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; @@ -293,7 +288,7 @@ // 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 @@ -301,17 +296,15 @@ $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; @@ -324,13 +317,12 @@ } $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(); @@ -341,10 +333,8 @@ 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; @@ -355,7 +345,7 @@ // 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); @@ -364,17 +354,13 @@ 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); @@ -382,53 +368,38 @@ } 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; @@ -436,14 +407,11 @@ } } break; - - - } // end switch menu_type - + } + return ($flag_stop_level) ? $flag_stop_level - 1 : -1; - } // end func buildMenu - - + } + /** * Build the menu recursively. * @@ -451,10 +419,8 @@ * @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; @@ -472,10 +438,8 @@ if (isset($node['sub'])) $this->buildSitemap($node['sub'], $level + 1); } + } - } // end func buildSitemap - - /** * Returns the HTML of one menu item. * @@ -489,9 +453,8 @@ * @see buildMenu() */ function getEntry(&$node, $level, $item_type) { - $html = ''; - + if ('tree' == $this->menu_type) { // tree menu $html .= '<tr>'; @@ -500,7 +463,7 @@ for ($i = 0; $i < $level; $i++) $indent .= '&nbsp;&nbsp;&nbsp;'; } - + // draw the <td></td> cell depending on the type of the menu item switch ($item_type) { case 0: @@ -511,7 +474,7 @@ $node['title'] ); break; - + case 1: // selected (active) menu item $html .= sprintf('<td>%s<b>%s</b></td>', @@ -519,7 +482,7 @@ $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>', @@ -529,7 +492,7 @@ ('urhere' == $this->menu_type) ? ' &gt;&gt; ' : '' ); break; - + case 3: // << previous url $html .= sprintf('<td>%s<a href="%s">&lt;&lt; %s</a></td>', @@ -556,15 +519,13 @@ $node['title'] ); break; - } - + if ('tree' == $this->menu_type) $html .= '</tr>'; return $html; - } // end func getEnty - + } /* * Returns the path of the current page in the menu 'tree'. @@ -573,7 +534,6 @@ * @see buildPath(), $urlmap */ function getPath() { - $this->current_url = $this->getCurrentURL(); $this->buildPath($this->menu, array()); @@ -581,9 +541,8 @@ $this->current_url = substr($this->current_url, 0, -1); return $this->urlmap[$this->current_url]; - } // end func getPath - - + } + /** * Computes the path of the current page in the menu 'tree'. * @@ -595,10 +554,8 @@ * @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; @@ -621,14 +578,12 @@ if ($this->buildPath($node['sub'], $subpath)) return true; } - } // not found return false; - } // end func buildPath - - + } + /** * Returns the URL of the currently selected page. * @@ -638,8 +593,7 @@ * @return string */ function getCurrentURL() { - return $GLOBALS['PHP_SELF']; - } // end func getCurrentURL - -} // end class menu + return $GLOBALS[$this->url_env_var]; + } +} ?> Index: php4/pear/HTML/Menu_Browser.php diff -u php4/pear/HTML/Menu_Browser.php:1.1 php4/pear/HTML/Menu_Browser.php:1.2 --- php4/pear/HTML/Menu_Browser.php:1.1 Tue Jun 12 14:15:04 2001 +++ php4/pear/HTML/Menu_Browser.php Wed Aug 8 09:53:47 2001 @@ -16,7 +16,7 @@ // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> | // +----------------------------------------------------------------------+ // -// $Id: Menu_Browser.php,v 1.1 2001/06/12 18:15:04 uw Exp $ +// $Id: Menu_Browser.php,v 1.2 2001/08/08 13:53:47 sebastian Exp $ /** * Simple filesystem browser that can be used to generated menu (3) hashes based on the directory structure. @@ -30,7 +30,7 @@ * like this: * * // document root directory -* define("DOC_ROOT", "/home/server/www.example.com/"); +* define('DOC_ROOT', '/home/server/www.example.com/'); * * // instantiate the menubrowser * $browser = new menubrowser(DOC_ROOT); @@ -39,25 +39,23 @@ * $menu = new menu($browser->getMenu()); * * // output the sitemap -* $menu->show("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: Menu_Browser.php,v 1.1 2001/06/12 18:15:04 uw Exp $ +* @version $Id: Menu_Browser.php,v 1.2 2001/08/08 13:53:47 sebastian Exp $ */ -class menubrowser { - +class HTML_Menu_Browser { /** * Filesuffix of your XML files. * * @var string * @see menubrowser() */ - var $file_suffix = "xml"; - - + var $file_suffix = 'xml'; + /** * Number of characters of the file suffix. * @@ -65,35 +63,31 @@ * @see menubrowser() */ var $file_suffix_length = 3; - - + /** * Filename (without suffix) of your index / start pages. * * @var string * @see menubrowser() */ - var $index = "index"; - - + var $index = 'index'; + /** * Full filename of your index / start pages. * * @var string * @see $file_suffix, $index */ - var $index_file = ""; - - + var $index_file = ''; + /** * Directory to scan. * * @var string * @see setDirectory() */ - var $dir = ""; - - + var $dir = ''; + /** * Prefix for every menu hash entry. * @@ -104,25 +98,22 @@ * @var string * @see setIDPrefix() */ - var $id_prefix = ""; - - + 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 menubrowser($dir = "", $index = "", $file_suffix = "") { - + function HTML_Menu_Browser($dir = '', $index = '', $file_suffix = '') { if ($dir) $this->dir = $dir; if ($index) @@ -130,12 +121,10 @@ if ($file_suffix) $this->file_suffix = $file_suffix; - $this->index_file = $this->index . "." . $this->file_suffix; + $this->index_file = $this->index . '.' . $this->file_suffix; $this->file_suffix_length = strlen($this->file_suffix); - - } // end constructor - - + } + /** * Sets the directory to scan. * @@ -143,12 +132,9 @@ * @access public */ function setDirectory($dir) { - $this->dir = $dir; - - } // end func setDirectory - - + } + /** * Sets the prefix for every id in the menu hash. * @@ -156,12 +142,9 @@ * @access public */ function setIDPrefix($prefix) { - $this->id_prefix = $prefix; - - } // end func setIDPrefix - - + } + /** * Returns a hash to be used with menu(3)'s setMenu(). * @@ -169,8 +152,7 @@ * @param string id prefix * @access public */ - function getMenu($dir = "", $prefix = "") { - + function getMenu($dir = '', $prefix = '') { if ($dir) $this->setDirectory($dir); if ($prefix) @@ -183,9 +165,8 @@ $this->menu = $this->addFileInfo($this->menu); return $this->menu; - } // end func getMenu + } - /** * Recursive function that does the scan and builds the menu (3) hash. * @@ -195,45 +176,38 @@ * @return array */ function browse($dir, $id = 0, $noindex = false) { - $struct = array(); $dh = opendir($dir); while ($file = readdir($dh)) { - if ("." == $file || ".." == $file) + if ('.' == $file || '..' == $file) continue; $ffile = $dir . $file; if (is_dir($file)) { - $ffile .= "/"; + $ffile .= '/'; if (file_exists($ffile . $this->index_file)) { $id++; - $struct[$this->id_prefix . $id]["url"] = $ffile . $this->index_file; + $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; - + $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; + $struct[$this->id_prefix . $id]['url'] = $dir . $file; } - } - } return $struct; - } // end func browse - - + } + /** * Adds further informations to the menu hash gathered from the files in it * @@ -241,22 +215,18 @@ * @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"]); - + $menu[$id] = array_merge($data, $this->exploreFile($data['url'])); + if (isset($data['sub'])) + $menu[$id]['sub'] = $this->addFileInfo($data['sub']); } return $menu; - } // end func addFileInfo - - + } + /** * Returns additional menu informations decoded in the file that appears in the menu. * @@ -266,20 +236,17 @@ * @param string filename */ function exploreFile($file) { - - $xml = join("", @file($file)); + $xml = join('', @file($file)); if (!$xml) return array(); $doc = xmldoc($xml); $xpc = xpath_new_context($doc); - - $menu = xpath_eval($xpc, "//menu"); + + $menu = xpath_eval($xpc, '//menu'); $node = &$menu->nodeset[0]; - - return array("title" => $node->content); - } // end func exploreFile - -} // end class menubrowser + return array('title' => $node->content); + } +} ?>
« previous php.pear.cvs (#439) next »