|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle Framework |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2014-2017, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace cs; |
|
9
|
|
|
use |
|
10
|
|
|
h; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Menu class is used in administration for generating second and third level of menu |
|
14
|
|
|
* |
|
15
|
|
|
* Provides next events:<br> |
|
16
|
|
|
* admin/System/Menu |
|
17
|
|
|
* |
|
18
|
|
|
* @method static $this instance($check = false) |
|
19
|
|
|
*/ |
|
20
|
|
|
class Menu { |
|
21
|
|
|
use |
|
22
|
|
|
Singleton; |
|
23
|
|
|
const INIT_STATE_METHOD = 'init'; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
public $section_items; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
public $items; |
|
32
|
|
|
|
|
33
|
6 |
|
protected function init () { |
|
34
|
6 |
|
$this->section_items = []; |
|
35
|
6 |
|
$this->items = []; |
|
36
|
6 |
|
} |
|
37
|
|
|
/** |
|
38
|
|
|
* Get menu in HTML format |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
6 |
|
public function get_menu () { |
|
43
|
6 |
|
Event::instance()->fire('admin/System/Menu'); |
|
44
|
6 |
|
$current_module = Request::instance()->current_module; |
|
45
|
6 |
|
if (isset($this->section_items[$current_module])) { |
|
46
|
4 |
|
$content = $this->render_sections($current_module); |
|
47
|
|
|
} else { |
|
48
|
2 |
|
$content = $this->render_items($current_module); |
|
49
|
|
|
} |
|
50
|
6 |
|
return h::cs_group($content ?: false); |
|
51
|
|
|
} |
|
52
|
|
|
/** |
|
53
|
|
|
* Render sections (automatically includes nested items) |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $module |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
4 |
|
protected function render_sections ($module) { |
|
60
|
4 |
|
$content = ''; |
|
61
|
4 |
|
foreach ($this->section_items[$module] as $item) { |
|
62
|
4 |
|
$dropdown = $this->render_items($module, $item[1]['href']); |
|
63
|
4 |
|
if ($dropdown) { |
|
64
|
2 |
|
$dropdown = h::{'cs-dropdown cs-group[vertical]'}($dropdown); |
|
65
|
|
|
} |
|
66
|
|
|
// Render as button without `href` attribute |
|
67
|
4 |
|
unset($item[1]['href']); |
|
68
|
|
|
$content .= |
|
69
|
4 |
|
h::{'cs-button[icon-after=caret-down]'}( |
|
70
|
4 |
|
h::button( |
|
71
|
4 |
|
$item[0], |
|
72
|
|
|
array_filter( |
|
73
|
4 |
|
$item[1], |
|
74
|
|
|
function ($item) { |
|
75
|
4 |
|
return $item == 'type'; |
|
76
|
4 |
|
}, |
|
77
|
4 |
|
ARRAY_FILTER_USE_KEY |
|
78
|
|
|
) |
|
79
|
|
|
), |
|
80
|
|
|
array_filter( |
|
81
|
4 |
|
$item[1], |
|
82
|
|
|
function ($item) { |
|
83
|
4 |
|
return $item != 'type'; |
|
84
|
4 |
|
}, |
|
85
|
4 |
|
ARRAY_FILTER_USE_KEY |
|
86
|
|
|
) |
|
87
|
|
|
). |
|
88
|
4 |
|
$dropdown; |
|
89
|
|
|
} |
|
90
|
4 |
|
return $content; |
|
91
|
|
|
} |
|
92
|
|
|
/** |
|
93
|
|
|
* Render items |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $module |
|
96
|
|
|
* @param string $base_href If passed - only nested elements for this base href will be rendered |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
6 |
|
protected function render_items ($module, $base_href = '') { |
|
101
|
6 |
|
if (!isset($this->items[$module])) { |
|
102
|
2 |
|
return ''; |
|
103
|
|
|
} |
|
104
|
4 |
|
$content = ''; |
|
105
|
4 |
|
foreach ($this->items[$module] as $item) { |
|
106
|
|
|
/** |
|
107
|
|
|
* Nested items for parent |
|
108
|
|
|
*/ |
|
109
|
4 |
|
if ($base_href && strpos($item[1]['href'], $base_href) !== 0) { |
|
110
|
2 |
|
continue; |
|
111
|
|
|
} |
|
112
|
4 |
|
$content .= h::cs_link_button( |
|
113
|
4 |
|
h::a( |
|
114
|
4 |
|
$item[0], |
|
115
|
|
|
array_filter( |
|
116
|
4 |
|
$item[1], |
|
117
|
|
|
function ($item) { |
|
118
|
4 |
|
return in_array($item, ['href', 'target']); |
|
119
|
4 |
|
}, |
|
120
|
4 |
|
ARRAY_FILTER_USE_KEY |
|
121
|
|
|
) |
|
122
|
|
|
), |
|
123
|
|
|
array_filter( |
|
124
|
4 |
|
$item[1], |
|
125
|
4 |
|
function ($item) { |
|
126
|
4 |
|
return !in_array($item, ['href', 'target']); |
|
127
|
4 |
|
}, |
|
128
|
4 |
|
ARRAY_FILTER_USE_KEY |
|
129
|
|
|
) |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
4 |
|
return $content; |
|
133
|
|
|
} |
|
134
|
|
|
/** |
|
135
|
|
|
* Add second-level item into menu |
|
136
|
|
|
* |
|
137
|
|
|
* All third-level items which start with the same `$href` will be inside this second-level menu item |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $module |
|
140
|
|
|
* @param string $title |
|
141
|
|
|
* @param array $attributes `href` and `target` if present will go to `a` element, all other attributes will go to `cs-link-button` element |
|
142
|
|
|
*/ |
|
143
|
4 |
|
public function add_section_item ($module, $title, $attributes = []) { |
|
144
|
4 |
|
$this->section_items[$module][] = [ |
|
145
|
4 |
|
$title, |
|
146
|
4 |
|
$attributes |
|
147
|
|
|
]; |
|
148
|
4 |
|
} |
|
149
|
|
|
/** |
|
150
|
|
|
* Add third-level item into menu (second-level when there is corresponding section items) |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $module |
|
153
|
|
|
* @param string $title |
|
154
|
|
|
* @param array $attributes |
|
155
|
|
|
*/ |
|
156
|
4 |
|
public function add_item ($module, $title, $attributes = []) { |
|
157
|
4 |
|
$this->items[$module][] = [ |
|
158
|
4 |
|
$title, |
|
159
|
4 |
|
$attributes |
|
160
|
|
|
]; |
|
161
|
4 |
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|