From 277ceae5150ccf3082ca99e73a82da58814ebaf5 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Thu, 20 Feb 2003 22:44:51 +0000 Subject: - New menu houskeeping. Prototyped by Zbynek. The following modules need updating: * glossary module * feed module (Breyten's version) * mailhandler module * notify module * project module * smileys module * admin module * style module * taxonomy_dhtml module To avoid unexpected problems menu_add() is deprecated (it will print an error message when used) and menu() should be used instead. --- includes/conf.php | 2 +- includes/menu.inc | 196 ++++++++++++++++++++++++++---------------------------- 2 files changed, 97 insertions(+), 101 deletions(-) (limited to 'includes') diff --git a/includes/conf.php b/includes/conf.php index d6bc038ba..ae59d1047 100644 --- a/includes/conf.php +++ b/includes/conf.php @@ -42,7 +42,7 @@ $base_url = "http://localhost"; # automatically become the default language. You can add a language # but make sure your SQL table, called locales is updated # appropriately. -$languages = array("en" => "English"); +$languages = array("en" => "english"); # # Custom navigation links: diff --git a/includes/menu.inc b/includes/menu.inc index 780d6582e..7b3e685d2 100644 --- a/includes/menu.inc +++ b/includes/menu.inc @@ -1,160 +1,156 @@ parent) { - $result = db_query("SELECT * FROM menu WHERE name = '%s' ORDER BY weight, name", $item->parent); - if ($item = db_fetch_object($result)) { - $path[] = $item; - } - } - } - - $trail = array_reverse($path); - } - - return $trail; +function menu_add() { + trigger_error(t("the 'menu_add()' function is depricated"), E_USER_ERROR); } -function menu_in_trail($item) { - $trail = menu_trail(); +function menu($path, $title, $callback = NULL, $help = NULL, $weight = 0, $hidden = 0) { + global $_gmenu; - foreach ($trail as $menu) { - if ($menu->link == $item->link) { - return 1; + if (isset($_gmenu[$path])) { + if (empty($_gmenu[$path]["callback"])) { // dummy item -> fill in + $_gmenu[$path] = array("title" => $title, "callback" => $callback, "help" => $help, "weight" => $weight, "hidden" => $hidden, "children" => $_gmenu[$path]["children"]); + return true; + } + else { // real item found + trigger_error(t("While trying to add '%path' to menu, item already exists.", array("%path" => $path)), E_USER_ERROR); + return false; } } - return 0; -} + // if this is reached we need to create a new item + $_gmenu[$path] = array("title" => $title, "callback" => $callback, "help" => $help, "weight" => $weight, "hidden" => $hidden, "children" => array()); + // does the item have an existing parent? + $parent = substr($path, 0, strrpos($path, "/")); + while (!isset($_gmenu[$parent])) { + $_gmenu[$parent] = array("children" => array($path)); + $path = $parent; + $parent = substr($parent, 0, strrpos($parent, "/")); + } + $_gmenu[$parent]["children"][] = $path; -function menu_item($item) { + return true; +} +function menu_item($in_path) { + global $_gmenu; /* ** If you want to theme your links, or if you want to replace them ** by an image, this would be the function to customize. */ + $trail = menu_trail(); + if (end($trail) == $in_path) + return t($_gmenu[$in_path]["title"]); + else + return "". t($_gmenu[$in_path]["title"]) .""; +} - if (stristr(request_uri(), $item->link) == $item->link) { - return t($item->name); - } - else if ($item->title) { - return "link\" title=\"". t($item->title) ."\">". t($item->name) .""; - } - else { - return "link\">". t($item->name) .""; - } +function query_string() { + return $GLOBALS["q"]; } -function menu_path() { +function menu_trail() { + global $_gmenu; + static $_gmenu_trail; // cache - $path = menu_trail(); + if (!isset($_gmenu_trail)) { + $_gmenu_trail = array(); + $cuqs = query_string(); - $links = array(); + while (!empty($cuqs) && !isset($_gmenu[$cuqs])) { + $cuqs = substr($cuqs, 0, strrpos($cuqs, "/")); + } - foreach ($path as $item) { - $links[] = menu_item($item); + if (!empty($cuqs)) { + do { + $_gmenu_trail[] = $cuqs; + $cuqs = substr($cuqs, 0, strrpos($cuqs, "/")); + } while (!empty($cuqs) && isset($_gmenu[$cuqs])); + } + $_gmenu_trail = array_reverse($_gmenu_trail); } - return implode(" > ", $links); + return $_gmenu_trail; } -function menu_menu_row($parent = "") { - - $links = array(); +function menu_path() { - $result = db_query("SELECT * FROM menu WHERE parent = '%s' ORDER BY weight, name", $parent); - while ($menu = db_fetch_object($result)) { - $links[] = menu_item($menu); - } + $trail = menu_trail(); - return $links; -} + $links = array(); -function menu_menu() { - $path = menu_trail(); - if ($path) { - $item = array_pop($path); - $output = implode(" · ", menu_menu_row($item->name)); + foreach ($trail as $item) { + $links[] = menu_item($item); } - return $output; + return implode(" > ", $links); } function menu_help() { + global $_gmenu; $path = menu_trail(); if ($path) { $item = array_pop($path); - $output = $item->help; + $output = $_gmenu[$item]["help"]; } - return $output; + return @$output; } -function menu_tree($parent = "", $all = 1) { - - if ($all) { - $result = db_query("SELECT * FROM menu WHERE parent = '%s' ORDER BY weight, name", $parent); - } +function _menu_sort($a, $b) { + global $_gmenu; + $a = &$_gmenu[$a]; + $b = &$_gmenu[$b]; + return $a["weight"] < $b["weight"] ? -11 : ($a["weight"] > $b["weight"] ? 1 : ($a["name"] < $b["name"] ? -1 : 1)); +} - if (db_num_rows($result)) { - $output = "\n"; } return $output; } function menu_map($parent = "") { + global $_gmenu; - $result = db_query("SELECT * FROM menu WHERE parent = '%s' ORDER BY weight, name", $parent); - - if (db_num_rows($result)) { - $output = ""; return $output; } +function menu_execute_action() { + global $_gmenu; + $trail = menu_trail(); + $selected_menu = array_pop($trail); -function menu_add($name, $link, $title = NULL, $help = NULL, $parent = NULL, $weight = 1) { - if (!db_result(db_query("SELECT name FROM menu WHERE link = '%s'", $link))) { - db_query("INSERT INTO menu (name, link, title, help, parent, weight) VALUES ('%s', '%s', '%s', '%s', '%s', '%d')", $name, $link, $title, $help, $parent, $weight); + if ($_gmenu[$selected_menu]["callback"]) { + return call_user_func_array($_gmenu[$selected_menu]["callback"], explode("/", substr(query_string(), strlen($selected_menu) + 1))); } } -?> \ No newline at end of file +?> -- cgit v1.2.3