File: CMenu.php

package info (click to toggle)
zabbix 1%3A6.0.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 283,384 kB
  • sloc: sql: 868,673; ansic: 322,351; php: 235,311; javascript: 62,116; sh: 5,555; makefile: 2,257; java: 1,397; cpp: 662; xml: 49; perl: 41
file content (198 lines) | stat: -rw-r--r-- 4,727 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php declare(strict_types = 0);
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


class CMenu extends CTag {

	/**
	 * @var CMenuItem[]
	 */
	private $menu_items = [];

	/**
	 * Create menu.
	 *
	 * @param CMenuItem[] $menu_items  Array of menu items.
	 */
	public function __construct(array $menu_items = []) {
		parent::__construct('ul', true);

		foreach ($menu_items as $item) {
			$this->add($item);
		}
	}

	/**
	 * Return all menu items.
	 *
	 * @return CMenuItem[]
	 */
	public function getMenuItems(): array {
		return $this->menu_items;
	}

	/**
	 * Add menu item.
	 *
	 * @param CMenuItem $menu_item  Menu item object.
	 *
	 * @return CMenu
	 */
	public function add(CMenuItem $menu_item): self {
		$this->menu_items[] = $menu_item;

		return $this;
	}

	/**
	 * Find menu item by label.
	 *
	 * @param string $label  Visual label.
	 *
	 * @return CMenuItem|null
	 */
	public function find(string $label): ?CMenuItem {
		foreach ($this->menu_items as $item) {
			if ($item->getLabel() === $label) {
				return $item;
			}
		}

		return null;
	}

	/**
	 * Find menu item by label or add new one, if not exists.
	 *
	 * @param string $label  Visual label.
	 *
	 * @return CMenuItem
	 */
	public function findOrAdd(string $label): CMenuItem {
		$item = $this->find($label);

		if ($item === null) {
			$item = new CMenuItem($label);
			$this->add($item);
		}

		return $item;
	}

	/**
	 * Find selected menu item.
	 *
	 * @return CMenuItem|null
	 */
	public function findSelected(): ?CMenuItem {
		foreach ($this->menu_items as $item) {
			if ($item->isSelected()) {
				return $item;
			}
		}

		return null;
	}

	/**
	 * Insert new menu item after the one with specified label, or insert as the last item, if not found.
	 *
	 * @param string    $label      Visual label to insert item after.
	 * @param CMenuItem $menu_item  Menu item object.
	 *
	 * @return CMenu
	 */
	public function insertAfter(string $label, CMenuItem $menu_item): self {
		return $this->insert($label, $menu_item, true);
	}

	/**
	 * Insert new menu item before the one with specified label, or insert as the first item, if not found.
	 *
	 * @param string    $label      Visual label to insert item before.
	 * @param CMenuItem $menu_item  Menu item object.
	 *
	 * @return CMenu
	 */
	public function insertBefore(string $label, CMenuItem $menu_item): self {
		return $this->insert($label, $menu_item);
	}

	/**
	 * Remove menu item by label.
	 *
	 * @param string $label  Visual label.
	 *
	 * @return CMenu
	 */
	public function remove(string $label): self {
		foreach ($this->menu_items as $index => $item) {
			if ($item->getLabel() === $label) {
				array_splice($this->menu_items, $index, 1);
				break;
			}
		}

		return $this;
	}

	/**
	 * Deep find menu item by action name and mark the whole chain as selected.
	 *
	 * @param string $action_name     Action name to search for.
	 * @param array  $request_params  Parameters of current HTTP request to compare in search process.
	 * @param bool   $expand          Add 'is-expanded' class for selected submenus.
	 *
	 * @return bool  True, if menu item was selected.
	 */
	public function setSelectedByAction(string $action_name, array $request_params, bool $expand = false): bool {
		foreach ($this->menu_items as $item) {
			if ($item->setSelectedByAction($action_name, $request_params)) {
				if ($expand && $item->hasSubMenu()) {
					$item->addClass('is-expanded');
				}
				return true;
			}
		}

		return false;
	}

	private function insert(string $label, CMenuItem $menu_item, $after = false): self {
		$count = count($this->menu_items);

		for ($i = 0; $i < $count; $i++) {
			if ($this->menu_items[$i]->getLabel() === $label) {
				break;
			}
		}

		$position = ($count == $i && !$after) ? 0 : $i + (($i < $count && $after) ? 1 : 0);
		array_splice($this->menu_items, $position, 0, [$menu_item]);

		return $this;
	}

	public function toString($destroy = true) {
		$this->addItem($this->menu_items);

		return parent::toString($destroy);
	}
}