📄 menu.php
字号:
// 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 = '';
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() {
return $GLOBALS[$this->url_env_var];
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -