summaryrefslogtreecommitdiffstats
path: root/code/nav.inc.php
blob: 7d1e38b561cee4857f55ab2b252910cca2464910 (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
<?PHP
  Uses('links', 'user');

  class Nav {
    var $entries = array();
    var $root_entries = array();
    
    function Nav() {
      $res = $GLOBALS['db']->Execute('SELECT * FROM nav ORDER BY id');
      
      while(!$res->EOF) {
        $this->entries[$res->fields[0]] = new NavEntry($res->fields[1], $res->fields[2], $res->fields[3], $res->fields[4]);
        
        $res->MoveNext();
      }
      
      foreach($this->entries as $key => $entry)
        if($entry->parent == 0)
          $this->root_entries[$entry->gid][$entry->text] = &$this->entries[$key];
        elseif(isset($this->entries[$entry->parent]))
          $this->entries[$entry->parent]->Add(&$this->entries[$key]);
    }
    
    function ParseNav($name, $gid = NULL) {
      if($gid === NULL) $gid = $GLOBALS['user']->getGid();
      
      $ret = '<ul>';
      
      foreach($this->root_entries[$gid][$name]->children as $entry)
        $ret .= $entry->Parse();
      
      $ret .= '</ul>';
      
      return $ret;
    }
    
    function ParseInsert($name, $data) {
      $res = $GLOBALS['db']->Execute('SELECT code FROM nav_inserts WHERE name = ?', $name);
      
      if(!$res->RecordCount()) return '&nbsp;';
      
      ob_start();
      
      eval('?>' . $res->fields[0]);
      
      $ret = ob_get_contents();
      ob_end_clean();
      
      return $ret;
    }
    
    function ParseLink($text, $link) {
      if(!$link) return $text;
        
      switch($link[0]) {
        case ':':
          $ret = $GLOBALS['links']->GetNeonLink(substr($link, 1));
          
          if($ret)
            return '<a href="' . $ret . '">' . $text . '</a>';
          
          return $text;
        case '@':
          return '<a href="' . $GLOBALS['links']->GetMailtoLink(substr($link, 1)) . '">' . $text . '</a>';
        case '-':
          return $this->ParseInsert(substr($link, 1), $text);
        case '!':
          return '<a href="' . $GLOBALS['links']->GetExternalLink(substr($link, 1)) . '">' . $text . '</a>';
      }
    }
  }
  
  class NavEntry {
    var $parent, $gid, $text, $link;
    var $children = array();
    
    function NavEntry($parent, $gid, $text, $link) {
      $this->parent = $parent;
      $this->gid = $gid;
      $this->text = $text;
      $this->link = $link;
    }
    
    function Add($entry) {
      array_push($this->children, &$entry);
    }
    
    function Parse() {
      $ccount = 0;
      
      if($this->link)  $ret = '<li class="nav' . urlencode($this->link[0]) . ' nav_' . urlencode($this->link) . '">';
      else $ret = '<li>';
      
      $a = $GLOBALS['nav']->ParseLink($this->text, $this->link);
      
      $ret .= $a;
      
      if(count($this->children) > 0) {
        $ret .= '<ul>';
        
        foreach($this->children as $child) {
          $cret = $child->Parse();
          
          if($cret) {
            $ret .= $cret;
            
            $ccount++;
          }
        }
        
        $ret .= '</ul>';
      }
      
      if(!$ccount && $a == $this->text)
        return '';
      
      return $ret . '</li>';
    }
  }
  
  $GLOBALS['nav'] = new Nav;
?>