📄 menu.php
字号:
<?php
//
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2001 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.5.2.2 2001/11/13 01:26:46 ssb 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.5.2.2 2001/11/13 01:26:46 ssb Exp $
* @author Ulf Wendel <ulf.wendel@phpdoc.de>
* @access public
* @package HTML
*/
class HTML_Menu {
/**
* URL Environment Variable
*
* @var array
*/
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':
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -