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 | if ($i + 1 < count($this->pinned) && $this->itemSpace != '') { |
---|
66 | $res .= preg_replace('|</li>$|', $this->itemSpace . '</li>', $this->pinned[$i]); |
---|
67 | $res .= "\n"; |
---|
68 | } else { |
---|
69 | $res .= $this->pinned[$i] . "\n"; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | // 2. Display unpinned itmes (sorted) |
---|
74 | $i = 0; |
---|
75 | dcUtils::lexicalKeySort($this->items); |
---|
76 | foreach ($this->items as $title => $item) { |
---|
77 | if ($i + 1 < count($this->items) && $this->itemSpace != '') { |
---|
78 | $res .= preg_replace('|</li>$|', $this->itemSpace . '</li>', $item); |
---|
79 | $res .= "\n"; |
---|
80 | } else { |
---|
81 | $res .= $item . "\n"; |
---|
82 | } |
---|
83 | $i++; |
---|
84 | } |
---|
85 | |
---|
86 | $res .= '</ul></div>' . "\n"; |
---|
87 | |
---|
88 | return $res; |
---|
89 | } |
---|
90 | |
---|
91 | protected function itemDef($title, $url, $img, $active, $id = null, $class = null) |
---|
92 | { |
---|
93 | if (is_array($url)) { |
---|
94 | $link = $url[0]; |
---|
95 | $ahtml = (!empty($url[1])) ? ' ' . $url[1] : ''; |
---|
96 | } else { |
---|
97 | $link = $url; |
---|
98 | $ahtml = ''; |
---|
99 | } |
---|
100 | |
---|
101 | $img = dc_admin_icon_url($img); |
---|
102 | |
---|
103 | return |
---|
104 | '<li' . (($active || $class) ? ' class="' . (($active) ? 'active ' : '') . (($class) ? $class : '') . '"' : '') . |
---|
105 | (($id) ? ' id="' . $id . '"' : '') . |
---|
106 | (($img) ? ' style="background-image: url(' . $img . ');"' : '') . |
---|
107 | '>' . |
---|
108 | |
---|
109 | '<a href="' . $link . '"' . $ahtml . '>' . $title . '</a></li>' . "\n"; |
---|
110 | } |
---|
111 | } |
---|