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