1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
<?php
// $Id$
/**
* Register a menu item to the menu system.
*/
function menu($path, $title, $callback = NULL, $help = NULL, $weight = 0, $hidden = 0) {
global $_list;
// add the menu to the flat list of menu items:
$_list[$path] = array("title" => $title, "callback" => $callback, "help" => $help, "weight" => $weight, "hidden" => $hidden);
}
/**
* Returns the path of the active menu item.
*/
function menu_get_active() {
global $_list;
static $path;
if (empty($path)) {
$path = $_GET["q"];
while ($path && !$_list[$path]) {
$path = substr($path, 0, strrpos($path, "/"));
}
}
return $path;
}
function menu_get_path($path) {
global $_list;
static $trail; // cache
if (empty($trail)) {
$trail = array();
while ($path) {
if ($_list[$path]) {
array_unshift($trail, $path);
}
$path = substr($path, 0, strrpos($path, "/"));
}
}
return $trail;
}
function menu_get_active_title() {
global $_list;
if ($path = menu_get_active()) {
return ucfirst($_list[$path]["title"]);
}
}
function menu_get_active_help() {
global $_list;
if ($path = menu_get_active()) {
return $_list[$path]["help"];
}
}
function menu_is_active($path) {
}
function menu_render_item($path) {
global $_list;
if ($path == $_GET["q"]) {
$css = " class=\"active\"";
}
return "<a href=\"". url($path) ."\"$css>". t($_list[$path]["title"]) ."</a>";
}
function menu_active_breadcrumb() {
$links[] = l(t("Home"), "");
$trail = menu_get_path($_GET["q"]);
foreach ($trail as $item) {
$links[] = menu_render_item($item);
}
return $links;
}
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 menu_tree($parent = "") {
global $_list;
static $trail;
if (empty($tail)) {
$trail = menu_get_path($_GET["q"]);
}
if ($_list[$parent]["children"]) {
$output = "\n<ul>\n";
usort($_list[$parent]["children"], "_menu_sort");
foreach ($_list[$parent]["children"] as $item) {
if ($_list[$item]["hidden"] == 0) {
$style = ($_list[$item]["children"] ? (in_array($item, $trail) ? "expanded" : "collapsed") : "leaf");
$output .= "<li class=\"$style\">";
$output .= menu_render_item($item);
if (in_array($item, $trail)) {
$output .= menu_tree($item);
}
$output .= "</li>\n";
}
}
$output .= "</ul>\n";
}
return $output;
}
function menu_execute_action() {
global $_list;
$path = menu_get_active();
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));
}
}
}
function menu_build($type) {
/*
** Build a sequential list of all menus items.
*/
module_invoke_all("link", $type);
/*
** 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;
}
}
}
?>
|