📄 menu.php
字号:
<?php//// +----------------------------------------------------------------------+// | PHP Version 4 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2003 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> |// | Alexey Borzov <avb@php.net> |// +----------------------------------------------------------------------+//// $Id: Menu.php,v 1.11 2004/02/21 15:54:09 avb Exp $//// Types of the menu entries, instead of former magic numbersdefine('HTML_MENU_ENTRY_INACTIVE', 0);define('HTML_MENU_ENTRY_ACTIVE', 1);define('HTML_MENU_ENTRY_ACTIVEPATH', 2);define('HTML_MENU_ENTRY_PREVIOUS', 3);define('HTML_MENU_ENTRY_NEXT', 4);define('HTML_MENU_ENTRY_UPPER', 5);define('HTML_MENU_ENTRY_BREADCRUMB', 6); // like activepath, but for 'urhere' type/*** Generates a HTML menu from a multidimensional hash.** Special thanks to the original author: Alex Vorobiev <sasha@mathforum.com>.** @version $Revision: 1.11 $* @author Ulf Wendel <ulf.wendel@phpdoc.de>* @author Alexey Borzov <avb@php.net>* @access public* @package HTML_Menu*/class HTML_Menu { /** * 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(); /** * Path to the current menu item. * @var array * @see get(), getPath() */ var $_path = array(); /** * Menu type: tree, rows, you-are-here. * @var array * @see setMenuType() */ var $_menuType = 'tree'; /** * URL Environment Variable * @var string */ var $_urlEnvVar = 'PHP_SELF'; /** * The URL to use an URL for the current page, instead of the one normally * taken from env. variables * @var string * @see forceCurrentUrl(), getCurrentUrl() */ var $_forcedUrl = ''; /** * URL of the current page. * @see getCurrentURL(), getPath() */ var $_currentUrl = ''; /** * The renderer being used to output the menu * @var object HTML_Menu_Renderer * @see render() */ var $_renderer = null; /** * Prefix for menu URLs * @var string * @see setUrlPrefix() */ var $_urlPrefix = ''; /** * Initializes the menu, sets the type and menu structure. * * @param array menu structure * @param string menu type * @param string env. variable used to determine current URL * @see setMenuType(), setMenu(), setURLEnvVar() */ function HTML_Menu($menu = null, $type = 'tree', $urlEnvVar = 'PHP_SELF') { if (is_array($menu)) { $this->setMenu($menu); } $this->setMenuType($type); $this->setURLEnvVar($urlEnvVar); } /** * 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 */ function setMenu($menu) { $this->_menu = $menu; $this->_urlMap = array(); } /** * Sets the type of the menu. * * Available types are: 'tree', 'rows', 'urhere', 'prevnext', 'sitemap'. * * @param string type name * @access public */ function setMenuType($menuType) { $menuType = strtolower($menuType); if (in_array($menuType, array('tree', 'rows', 'urhere', 'prevnext', 'sitemap'))) { $this->_menuType = $menuType; } else { $this->_menuType = 'tree'; } } /** * Sets the environment variable to use to get the current URL. * * @param string environment variable for current URL * @access public */ function setURLEnvVar($urlEnvVar) { $this->_urlEnvVar = $urlEnvVar; } /** * Returns the HTML menu. * * @param string Menu type: tree, urhere, rows, prevnext, sitemap * @return string HTML of the menu * @access public * @see render() */ function get($menuType = '') { include_once 'HTML/Menu/DirectRenderer.php'; $renderer =& new HTML_Menu_DirectRenderer(); $this->render($renderer, $menuType); return $renderer->toHtml(); } /** * Prints the HTML menu. * * @access public * @param string Menu type: tree, urhere, rows, prevnext, sitemap * @see get(), render() */ function show($menuType = '') { print $this->get($menuType); } /** * Renders the menu. * * @access public * @param object HTML_Menu_Renderer Renderer to use * @param string type of the menu * @throws PEAR_Error */ function render(&$renderer, $menuType = '') { if ('' != $menuType) { $this->setMenuType($menuType); } $this->_renderer =& $renderer; // the renderer will throw an error if it is unable to process this menu type $res = $this->_renderer->setMenuType($this->_menuType); if (is_object($res) && is_a($res, 'PEAR_Error')) { return $res; } // storing to a class variable saves some recursion overhead $this->_path = $this->getPath(); switch ($this->_menuType) { case 'rows': $this->_renderRows($this->_menu); break; case 'prevnext': $this->_renderPrevNext($this->_menu); break; case 'urhere': $this->_renderURHere($this->_menu); break; default: $this->_renderTree($this->_menu); } // switch } /** * Finds the type for the node.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -