2002-12-24 15:40:32 +00:00
|
|
|
<?php
|
2003-03-12 09:51:30 +00:00
|
|
|
// $Id$
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 10:51:40 +00:00
|
|
|
/**
|
2003-09-28 11:08:17 +00:00
|
|
|
* Register a menu item with the menu system.
|
2003-09-28 10:51:40 +00:00
|
|
|
*/
|
2003-02-20 22:44:51 +00:00
|
|
|
function menu($path, $title, $callback = NULL, $help = NULL, $weight = 0, $hidden = 0) {
|
2003-09-28 10:51:40 +00:00
|
|
|
global $_list;
|
2003-01-14 20:33:42 +00:00
|
|
|
|
2003-09-28 10:51:40 +00:00
|
|
|
// add the menu to the flat list of menu items:
|
|
|
|
$_list[$path] = array("title" => $title, "callback" => $callback, "help" => $help, "weight" => $weight, "hidden" => $hidden);
|
2003-02-20 22:44:51 +00:00
|
|
|
}
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 10:51:40 +00:00
|
|
|
/**
|
2003-09-28 11:08:17 +00:00
|
|
|
* Returns an array with the menu items that lead to the specied path.
|
2003-09-28 10:51:40 +00:00
|
|
|
*/
|
2003-09-28 11:08:17 +00:00
|
|
|
function menu_get_trail($path) {
|
2003-09-28 10:51:40 +00:00
|
|
|
global $_list;
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
$trail = array();
|
2003-09-26 10:04:09 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
while ($path) {
|
|
|
|
if ($_list[$path]) {
|
|
|
|
array_unshift($trail, $path);
|
2003-09-28 10:51:40 +00:00
|
|
|
}
|
2003-09-28 11:08:17 +00:00
|
|
|
|
|
|
|
$path = substr($path, 0, strrpos($path, "/"));
|
2003-09-26 10:04:09 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
return $trail;
|
2003-09-26 10:04:09 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the path of the active menu item.
|
|
|
|
*/
|
|
|
|
function menu_get_active_item() {
|
2003-09-28 10:51:40 +00:00
|
|
|
global $_list;
|
2003-09-28 11:08:17 +00:00
|
|
|
static $path;
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
if (empty($path)) {
|
|
|
|
$path = $_GET["q"];
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
while ($path && !$_list[$path]) {
|
2003-04-01 06:05:15 +00:00
|
|
|
$path = substr($path, 0, strrpos($path, "/"));
|
2003-02-20 22:44:51 +00:00
|
|
|
}
|
2002-12-24 15:40:32 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
return $path;
|
2002-12-24 15:40:32 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the title of the active menu item.
|
|
|
|
*/
|
2003-09-28 10:51:40 +00:00
|
|
|
function menu_get_active_title() {
|
|
|
|
global $_list;
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
if ($path = menu_get_active_item()) {
|
2003-09-28 10:51:40 +00:00
|
|
|
return ucfirst($_list[$path]["title"]);
|
|
|
|
}
|
|
|
|
}
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the help associated with the active menu item.
|
|
|
|
*/
|
2003-09-28 10:51:40 +00:00
|
|
|
function menu_get_active_help() {
|
|
|
|
global $_list;
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
if ($path = menu_get_active_item()) {
|
2003-09-28 10:51:40 +00:00
|
|
|
return $_list[$path]["help"];
|
2002-12-24 15:40:32 +00:00
|
|
|
}
|
2003-09-28 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of rendered menu items in the active breadcrumb trail.
|
|
|
|
*/
|
|
|
|
function menu_get_active_breadcrumb() {
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
$links[] = l(t("Home"), "");
|
2003-09-28 10:51:40 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
$trail = menu_get_trail($_GET["q"]);
|
|
|
|
foreach ($trail as $item) {
|
|
|
|
$links[] = _render_item($item);
|
2002-12-24 15:40:32 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
return $links;
|
2003-09-28 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
/**
|
|
|
|
* Execute the handler associated with the active menu item.
|
|
|
|
*/
|
|
|
|
function menu_execute_active_handler() {
|
|
|
|
global $_list;
|
2003-09-28 10:51:40 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
$path = menu_get_active_item();
|
2003-09-28 10:51:40 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
if ($_list[$path]["callback"]) {
|
|
|
|
$arg = substr($_GET["q"], strlen($path) + 1);
|
|
|
|
if (empty($arg)) {
|
|
|
|
return call_user_func($_list[$path]["callback"]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return call_user_func_array($_list[$path]["callback"], explode("/", $arg));
|
|
|
|
}
|
|
|
|
}
|
2002-12-24 15:40:32 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
/**
|
|
|
|
* Returns true when the path is in the active trail.
|
|
|
|
*/
|
|
|
|
function menu_in_active_trail($path) {
|
|
|
|
static $trail;
|
2003-09-28 10:51:40 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
if (empty($trail)) {
|
|
|
|
$trail = menu_get_trail($_GET["q"]);
|
|
|
|
}
|
2003-09-28 10:51:40 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
return in_array($path, $trail);
|
2003-02-20 22:44:51 +00:00
|
|
|
}
|
2002-12-24 15:40:32 +00:00
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
/**
|
|
|
|
* Returns a rendered menu tree.
|
|
|
|
*/
|
2003-02-20 22:44:51 +00:00
|
|
|
function menu_tree($parent = "") {
|
2003-09-28 10:51:40 +00:00
|
|
|
global $_list;
|
|
|
|
static $trail;
|
2003-02-20 22:44:51 +00:00
|
|
|
|
2003-09-28 10:51:40 +00:00
|
|
|
if (empty($tail)) {
|
2003-09-28 11:08:17 +00:00
|
|
|
$trail = menu_get_trail($_GET["q"]);
|
2003-09-28 10:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($_list[$parent]["children"]) {
|
2003-02-20 22:44:51 +00:00
|
|
|
$output = "\n<ul>\n";
|
2003-09-28 10:51:40 +00:00
|
|
|
usort($_list[$parent]["children"], "_menu_sort");
|
|
|
|
foreach ($_list[$parent]["children"] as $item) {
|
|
|
|
if ($_list[$item]["hidden"] == 0) {
|
2003-09-28 11:08:17 +00:00
|
|
|
$style = ($_list[$item]["children"] ? (menu_in_active_trail($item) ? "expanded" : "collapsed") : "leaf");
|
2003-02-25 19:48:55 +00:00
|
|
|
$output .= "<li class=\"$style\">";
|
2003-09-28 11:08:17 +00:00
|
|
|
$output .= _render_item($item);
|
|
|
|
if (menu_in_active_trail($item, $trail)) {
|
2003-02-20 22:44:51 +00:00
|
|
|
$output .= menu_tree($item);
|
|
|
|
}
|
|
|
|
$output .= "</li>\n";
|
|
|
|
}
|
2002-12-30 15:54:06 +00:00
|
|
|
}
|
2003-02-20 22:44:51 +00:00
|
|
|
$output .= "</ul>\n";
|
2002-12-24 15:40:32 +00:00
|
|
|
}
|
|
|
|
|
2002-12-30 15:54:06 +00:00
|
|
|
return $output;
|
2002-12-24 15:40:32 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
/**
|
|
|
|
* Query to module to build the menu.
|
|
|
|
*/
|
2003-03-12 21:46:14 +00:00
|
|
|
function menu_build($type) {
|
2003-09-28 10:51:40 +00:00
|
|
|
/*
|
|
|
|
** Build a sequential list of all menus items.
|
|
|
|
*/
|
2003-03-12 21:46:14 +00:00
|
|
|
|
|
|
|
module_invoke_all("link", $type);
|
2003-09-28 10:51:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Tree-ify the sequential list of menu items by adding each
|
|
|
|
** menu item to the 'children' array of their direct parent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
global $_list;
|
|
|
|
|
|
|
|
foreach ($_list as $path => $data) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Find $path's direct parent:
|
|
|
|
*/
|
|
|
|
$parent = $path;
|
|
|
|
do {
|
|
|
|
$parent = substr($parent, 0, strrpos($parent, "/"));
|
|
|
|
}
|
|
|
|
while ($parent && !$_list[$parent]);
|
|
|
|
|
|
|
|
if ($path) {
|
|
|
|
$_list[$parent]["children"][] = $path;
|
|
|
|
}
|
|
|
|
}
|
2003-03-12 21:46:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 11:08:17 +00:00
|
|
|
function _menu_sort($a, $b) {
|
|
|
|
global $_list;
|
|
|
|
|
|
|
|
$a = &$_list[$a];
|
|
|
|
$b = &$_list[$b];
|
|
|
|
|
|
|
|
return $a["weight"] < $b["weight"] ? -1 : ($a["weight"] > $b["weight"] ? 1 : ($a["title"] < $b["title"] ? -1 : 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
function _render_item($path) {
|
|
|
|
global $_list;
|
|
|
|
|
|
|
|
if ($path == $_GET["q"]) {
|
|
|
|
$css = " class=\"active\"";
|
|
|
|
}
|
|
|
|
|
2003-09-28 13:42:12 +00:00
|
|
|
return "<a href=\"". url($path) ."\"$css>". $_list[$path]["title"] ."</a>";
|
2003-09-28 11:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-20 22:44:51 +00:00
|
|
|
?>
|