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

  class Nav {
    var $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->MoveNext();
      }
      
      foreach($this->entries as $key => $entry)
        if($entry->parent != 0) 
          $this->entries[$entry->parent]->Add(&$this->entries[$key]);
    }
    
    function ParseEntries() {
      $ret = '<ul>';
      
      foreach($this->entries as $entry)
        if($entry->parent == 0)
          $ret .= $entry->Parse();
      
      $ret .= '</ul>';
      
      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 '!':
          $ret = $GLOBALS['links']->GetNavPage(substr($link, 1));
          
          if($ret)
            return $ret;
          
          return $text;
        default:
          return '<a href="' . $GLOBALS['links']->GetExternalLink($link) . '">' . $text . '</a>';
      }
    }
  }
  
  class NavEntry {
    var $parent, $text, $link;
    var $children = array();
    
    function NavEntry($parent, $text, $link) {
      $this->parent = $parent;
      $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) . '">';
      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;
?>