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

function menu_trail() {

  global $QUERY_STRING;
  static $trail = NULL;

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

  if (empty($trail)) {

    $path = array();
    $item = db_fetch_object(db_query("SELECT * FROM menu WHERE link LIKE '%%%s'", $QUERY_STRING));

    /*
    ** 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_in_trail($item) {
  $trail = menu_trail();

  foreach ($trail as $menu) {
    if ($menu->link == $item->link) {
      return 1;
    }
  }
  return 0;
}

function menu_item($item) {

  /*
  ** 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 = "", $all = 1) {

  if ($all) {
    $result = db_query("SELECT * FROM menu WHERE parent = '%s' ORDER BY weight, name", $parent);
  }

  if (db_num_rows($result)) {
    $output = "<ul>";
    while ($item = db_fetch_object($result)) {
      $all = (stristr(request_uri(), $item->link) == $item->link) ? 1 : 0;
      $output .= "<li>". menu_item($item) ."</li>";
      $output .= menu_tree($item->name, menu_in_trail($item));
    }
    $output .= "</ul>";
  }

  return $output;
}

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

?>