Completed
Push — master ( b7b84b...78616b )
by Nazar
04:19
created

Menu::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * @package   CleverStyle CMS
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
class Menu {
19
	use
20
		Singleton;
21
	const INIT_STATE_METHOD = 'init';
22
	/**
23
	 * @var array
24
	 */
25
	public $section_items;
26
	/**
27
	 * @var array
28
	 */
29
	public $items;
30
31
	protected function init () {
32
		$this->section_items = [];
33
		$this->items         = [];
34
	}
35
	/**
36
	 * Get menu in HTML format
37
	 *
38
	 * @return string
39
	 */
40
	function get_menu () {
41
		Event::instance()->fire('admin/System/Menu');
42
		$current_module = Request::instance()->current_module;
43
		if (isset($this->section_items[$current_module])) {
44
			$content = $this->render_sections($current_module);
45
		} else {
46
			$content = $this->render_items($current_module);
47
		}
48
		return h::{'nav[is=cs-nav-button-group]'}($content ?: false);
49
	}
50
	/**
51
	 * Render sections (automatically includes nested items)
52
	 *
53
	 * @param string $module
54
	 *
55
	 * @return string
56
	 */
57
	protected function render_sections ($module) {
58
		$content = '';
59
		foreach ($this->section_items[$module] as $item) {
60
			$dropdown = $this->render_items($module, $item[1]['href']);
61
			if ($dropdown) {
62
				$dropdown = h::{'nav[is=cs-nav-dropdown] nav[is=cs-nav-button-group][vertical]'}($dropdown);
63
			}
64
			// Render as button without `href` attribute
65
			unset($item[1]['href']);
66
			$content .=
67
				h::{'button[is=cs-button][icon-after=caret-down]'}($item[0], $item[1]).
68
				$dropdown;
69
		}
70
		return $content;
71
	}
72
	/**
73
	 * Render items
74
	 *
75
	 * @param string $module
76
	 * @param string $base_href If passed - only nested elements for this base href will be rendered
77
	 *
78
	 * @return string
79
	 */
80
	protected function render_items ($module, $base_href = '') {
81
		if (!isset($this->items[$module])) {
82
			return '';
83
		}
84
		$content = '';
85
		foreach ($this->items[$module] as $item) {
86
			/**
87
			 * Nested items for parent
88
			 */
89
			if ($base_href && strpos($item[1]['href'], $base_href) !== 0) {
90
				continue;
91
			}
92
			$content .= h::{'a[is=cs-link-button]'}(
93
				$item[0],
94
				$item[1]
95
			);
96
		}
97
		return $content;
98
	}
99
	/**
100
	 * Add second-level item into menu
101
	 *
102
	 * All third-level items which start with the same `$href` will be inside this second-level menu item
103
	 *
104
	 * @param string $module
105
	 * @param string $title
106
	 * @param array  $attributes
107
	 */
108
	function add_section_item ($module, $title, $attributes = []) {
109
		$this->section_items[$module][] = [
110
			$title,
111
			$attributes
112
		];
113
	}
114
	/**
115
	 * Add third-level item into menu (second-level when there is corresponding section items)
116
	 *
117
	 * @param string $module
118
	 * @param string $title
119
	 * @param array  $attributes
120
	 */
121
	function add_item ($module, $title, $attributes = []) {
122
		$this->items[$module][] = [
123
			$title,
124
			$attributes
125
		];
126
	}
127
}
128