summaryrefslogtreecommitdiff
path: root/includes/menu.inc
blob: 1793bfdc4d54def50c7596ef0532bd21438add1f (plain)
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
<?php
// $Id$

function menu($path, $title, $callback = NULL, $help = NULL, $weight = 0, $hidden = 0) {
  global $_gmenu;

  if (empty($_gmenu[$path])) {
    // add the menu to the flat list of menu items:
    $_gmenu[$path] = array("title" => $title, "callback" => $callback, "help" => $help, "weight" => $weight, "hidden" => $hidden, "children" => array());

    // find the best matching parent item:
    $parent = substr($path, 0, strrpos($path, "/"));
    while ($parent && !$_gmenu[$parent]) {
      $parent = substr($parent, 0, strrpos($parent, "/"));
    }

    // check if any items need to be lowered:
    if ($parent) {
      foreach ($_gmenu[$parent]["children"] as $key => $item) {
        if (strstr($item, $path)) {
          // remove the item from its parent:
          unset($_gmenu[$parent]["children"][$key]);

          // add the item to its new parent:
          $_gmenu[$path]["children"][] = $item;
        }
      }
    }

    // add the menu to the best matching parent:
    $_gmenu[$parent]["children"][] = $path;
  }
}

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) {
    $css = " class=\"current\"";
  }

  return "<a href=\"". url($in_path) ."\"$css>". t($_gmenu[$in_path]["title"]) ."</a>";
}

function menu_trail() {
  global $_gmenu, $q;
  static $trail; // cache

  if (empty($trail)) {
    $trail = array();
    $path = $q;

    while ($path) {
      if ($_gmenu[$path]) {
        $trail[] = $path;
      }

      $path = substr($path, 0, strrpos($path, "/"));
    }

    $trail = array_reverse($trail);
  }

  return $trail;
}

function menu_path() {

  $trail = menu_trail();

  $links = array();

  foreach ($trail as $item) {
    $links[] = menu_item($item);
  }

  return implode(" &raquo; ", $links);
}

function menu_help() {
  global $_gmenu;
  $path = menu_trail();
  if ($path) {
    $item = array_pop($path);
    $output = $_gmenu[$item]["help"];
  }

  return @$output;
}

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));
}

function menu_tree($parent = "") {
  global $_gmenu;

  if ($_gmenu[$parent]["children"]) {
    $output = "\n<ul>\n";
    usort($_gmenu[$parent]["children"], "_menu_sort");
    foreach ($_gmenu[$parent]["children"] as $item) {
      if ($_gmenu[$item]["hidden"] == 0) {
        $trail = menu_trail($item);
        $style = ($_gmenu[$item]["children"] ? (in_array($item, $trail)  ? "expanded" : "collapsed") : "leaf");
        $output .= "<li class=\"$style\">";
        $output .= menu_item($item);
        if (in_array($item, $trail)) {
          $output .= menu_tree($item);
        }
        $output .= "</li>\n";
      }
    }
    $output .= "</ul>\n";
  }

  return $output;
}

function menu_map($parent = "") {
  global $_gmenu;

  $output = "<ul>";
  usort($_gmenu[$parent]["children"], "_menu_sort");
  foreach ($_gmenu[$parent]["children"] as $item) {
    if ($_gmenu[$item]["hidden"] == 0) {
      $output .= "<li>";
      $output .= menu_item($item);
      $output .= menu_map($item);
      $output .= "</li>";
    }
  }
  $output .= "</ul>";

  return $output;
}

function menu_execute_action() {
  global $_gmenu, $q;
  $trail = menu_trail();
  $selected_menu = array_pop($trail);

  if ($_gmenu[$selected_menu]["callback"]) {
    $arg = substr($q, strlen($selected_menu) + 1);
    if (empty($arg)) {
      return call_user_func($_gmenu[$selected_menu]["callback"]);
    }
    else {
      return call_user_func_array($_gmenu[$selected_menu]["callback"], explode("/", $arg));
    }
  }
}

function menu_build($type) {

  // Empty the existing menu tree (if any):
  unset($GLOBALS["_gmenu"]);

  // Build the menu tree:
  module_invoke_all("link", $type);
}

?>