summaryrefslogtreecommitdiff
path: root/includes/menu.inc
blob: fff05a4d8cc0e5483a70f76a11b020537741fdeb (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
<?php

function menu_trail() {

  global $REQUEST_URI;
  static $trail = NULL;

  /*
  ** Retrieve the currently active link.
  */

  if (empty($trail)) {

    $path = array();
    $link = substr($REQUEST_URI, strrpos($REQUEST_URI, "/") + 1, strlen($REQUEST_URI));
    $item = db_fetch_object(db_query("SELECT * FROM menu WHERE link = '%s'", $link));

    /*
    ** Compile an array of menu objects that represent the path to
    ** the root link.
    */

    if ($item) {
      $path[] = $item;

      while ($item->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_item($item) {
  global $REQUEST_URI;

  /*
  ** If you want to theme your links, or if you want to replace them
  ** by an image, this would be the function to customize.
  */

  if (stristr($REQUEST_URI, $item->link) == $item->link) {
    return t($item->name);
  }
  else if ($item->title) {
    return "<a href=\"$item->link\" title=\"". t($item->title) ."\">". t($item->name) ."</a>";
  }
  else {
    return "<a href=\"$item->link\">". t($item->name) ."</a>";
  }
}

function menu_path() {

  $path = menu_trail();

  $links = array();

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

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

function menu_menu_row($parent = "") {

  $links = array();

  $result = db_query("SELECT * FROM menu WHERE parent = '%s' ORDER BY weight, name", $parent);
  while ($menu = db_fetch_object($result)) {
    $links[] = menu_item($menu);
  }

  return $links;
}

function menu_menu() {
  $path = menu_trail();
  if ($path) {
    $item = array_pop($path);
    $output = implode(" &middot; ", menu_menu_row($item->name));
  }

  return $output;
}

function menu_help() {
  $path = menu_trail();
  if ($path) {
    $item = array_pop($path);
    $output = $item->help;
  }

  return $output;
}

function menu_tree($parent = "", $overview = 0) {

  $result = db_query("SELECT * FROM menu WHERE parent = '%s' AND overview = '%d' ORDER BY weight, name", $parent, $overview);

  if (db_num_rows($result)) {
    $output = "<ul>";
    while ($item = db_fetch_object($result)) {
      $output .= "<li>". menu_item($item) ."</li>";
      $output .= menu_tree($item->name, 1);
    }
    $output .= "</ul>";
  }

  return $output;
}

function menu_add($name, $link, $title = NULL, $help = NULL, $parent = NULL, $weight = 1, $overview = 0) {
  if (!db_result(db_query("SELECT name FROM menu WHERE link = '%s'", $link))) {
    db_query("INSERT INTO menu (name, link, title, help, parent, weight, overview) VALUES ('%s', '%s', '%s', '%s', '%s', '%d', '%d')", $name, $link, $title, $help, $parent, $weight, $overview);
  }
}

?>