1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | class dcMenu |
---|
15 | { |
---|
16 | private $id; |
---|
17 | public $title; |
---|
18 | |
---|
19 | public function __construct($id,$title,$itemSpace='') |
---|
20 | { |
---|
21 | $this->id = $id; |
---|
22 | $this->title = $title; |
---|
23 | $this->itemSpace = $itemSpace; |
---|
24 | $this->pinned = array(); |
---|
25 | $this->items = array(); |
---|
26 | } |
---|
27 | |
---|
28 | public function addItem($title,$url,$img,$active,$show=true,$id=null,$class=null,$pinned=false) |
---|
29 | { |
---|
30 | if($show) { |
---|
31 | $item = $this->itemDef($title,$url,$img,$active,$id,$class); |
---|
32 | if ($pinned) { |
---|
33 | $this->pinned[] = $item; |
---|
34 | } else { |
---|
35 | $this->items[$title] = $item; |
---|
36 | } |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | public function prependItem($title,$url,$img,$active,$show=true,$id=null,$class=null,$pinned=false) |
---|
41 | { |
---|
42 | if ($show) { |
---|
43 | $item = $this->itemDef($title,$url,$img,$active,$id,$class); |
---|
44 | if ($pinned) { |
---|
45 | array_unshift($this->pinned,$item); |
---|
46 | } else { |
---|
47 | $this->items[$title] = $item; |
---|
48 | } |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | public function draw() |
---|
53 | { |
---|
54 | if (count($this->items) + count($this->pinned) == 0) { |
---|
55 | return ''; |
---|
56 | } |
---|
57 | |
---|
58 | $res = |
---|
59 | '<div id="'.$this->id.'">'. |
---|
60 | ($this->title ? '<h3>'.$this->title.'</h3>' : ''). |
---|
61 | '<ul>'."\n"; |
---|
62 | |
---|
63 | // 1. Display pinned items (unsorted) |
---|
64 | for ($i=0; $i<count($this->pinned); $i++) |
---|
65 | { |
---|
66 | if ($i+1 < count($this->pinned) && $this->itemSpace != '') { |
---|
67 | $res .= preg_replace('|</li>$|',$this->itemSpace.'</li>',$this->pinned[$i]); |
---|
68 | $res .= "\n"; |
---|
69 | } else { |
---|
70 | $res .= $this->pinned[$i]."\n"; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | // 2. Display unpinned itmes (sorted) |
---|
75 | $i = 0; |
---|
76 | dcUtils::lexicalKeySort($this->items); |
---|
77 | foreach ($this->items as $title => $item) |
---|
78 | { |
---|
79 | if ($i+1 < count($this->items) && $this->itemSpace != '') { |
---|
80 | $res .= preg_replace('|</li>$|',$this->itemSpace.'</li>',$item); |
---|
81 | $res .= "\n"; |
---|
82 | } else { |
---|
83 | $res .= $item."\n"; |
---|
84 | } |
---|
85 | $i++; |
---|
86 | } |
---|
87 | |
---|
88 | $res .= '</ul></div>'."\n"; |
---|
89 | |
---|
90 | return $res; |
---|
91 | } |
---|
92 | |
---|
93 | protected function itemDef($title,$url,$img,$active,$id=null,$class=null) |
---|
94 | { |
---|
95 | if (is_array($url)) { |
---|
96 | $link = $url[0]; |
---|
97 | $ahtml = (!empty($url[1])) ? ' '.$url[1] : ''; |
---|
98 | } else { |
---|
99 | $link = $url; |
---|
100 | $ahtml = ''; |
---|
101 | } |
---|
102 | |
---|
103 | $img = dc_admin_icon_url($img); |
---|
104 | |
---|
105 | return |
---|
106 | '<li'.(($active || $class) ? ' class="'.(($active) ? 'active ' : '').(($class) ? $class : '').'"' : ''). |
---|
107 | (($id) ? ' id="'.$id.'"' : ''). |
---|
108 | (($img) ? ' style="background-image: url('.$img.');"' : ''). |
---|
109 | '>'. |
---|
110 | |
---|
111 | '<a href="'.$link.'"'.$ahtml.'>'.$title.'</a></li>'."\n"; |
---|
112 | } |
---|
113 | } |
---|