1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2014-2016, 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 |
|
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::{'nav[is=cs-nav-button-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::{'nav[is=cs-nav-dropdown] nav[is=cs-nav-button-group][vertical]'}($dropdown); |
65
|
|
|
} |
66
|
|
|
// Render as button without `href` attribute |
67
|
4 |
|
unset($item[1]['href']); |
68
|
|
|
$content .= |
69
|
4 |
|
h::{'button[is=cs-button][icon-after=caret-down]'}($item[0], $item[1]). |
70
|
4 |
|
$dropdown; |
71
|
|
|
} |
72
|
4 |
|
return $content; |
73
|
|
|
} |
74
|
|
|
/** |
75
|
|
|
* Render items |
76
|
|
|
* |
77
|
|
|
* @param string $module |
78
|
|
|
* @param string $base_href If passed - only nested elements for this base href will be rendered |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
6 |
|
protected function render_items ($module, $base_href = '') { |
83
|
6 |
|
if (!isset($this->items[$module])) { |
84
|
2 |
|
return ''; |
85
|
|
|
} |
86
|
4 |
|
$content = ''; |
87
|
4 |
|
foreach ($this->items[$module] as $item) { |
88
|
|
|
/** |
89
|
|
|
* Nested items for parent |
90
|
|
|
*/ |
91
|
4 |
|
if ($base_href && strpos($item[1]['href'], $base_href) !== 0) { |
92
|
2 |
|
continue; |
93
|
|
|
} |
94
|
4 |
|
$content .= h::{'a[is=cs-link-button]'}( |
95
|
4 |
|
$item[0], |
96
|
4 |
|
$item[1] |
97
|
|
|
); |
98
|
|
|
} |
99
|
4 |
|
return $content; |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* Add second-level item into menu |
103
|
|
|
* |
104
|
|
|
* All third-level items which start with the same `$href` will be inside this second-level menu item |
105
|
|
|
* |
106
|
|
|
* @param string $module |
107
|
|
|
* @param string $title |
108
|
|
|
* @param array $attributes |
109
|
|
|
*/ |
110
|
4 |
|
function add_section_item ($module, $title, $attributes = []) { |
111
|
4 |
|
$this->section_items[$module][] = [ |
112
|
4 |
|
$title, |
113
|
4 |
|
$attributes |
114
|
|
|
]; |
115
|
4 |
|
} |
116
|
|
|
/** |
117
|
|
|
* Add third-level item into menu (second-level when there is corresponding section items) |
118
|
|
|
* |
119
|
|
|
* @param string $module |
120
|
|
|
* @param string $title |
121
|
|
|
* @param array $attributes |
122
|
|
|
*/ |
123
|
4 |
|
function add_item ($module, $title, $attributes = []) { |
124
|
4 |
|
$this->items[$module][] = [ |
125
|
4 |
|
$title, |
126
|
4 |
|
$attributes |
127
|
|
|
]; |
128
|
4 |
|
} |
129
|
|
|
} |
130
|
|
|
|