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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
<?php declare(strict_types = 1);
/*
** Zabbix
** Copyright (C) 2001-2021 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 CMenuItem extends CTag {
/**
* @var string
*/
private $action;
/**
* @var array
*/
private $aliases = [];
/**
* @var string
*/
private $icon_class;
/**
* @var string
*/
private $label;
/**
* @var CMenu
*/
private $sub_menu;
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $target;
/**
* @var CUrl
*/
private $url;
/**
* @var bool
*/
private $is_selected = false;
/**
* Create menu item.
*
* @param string $label Menu item visual label.
*/
public function __construct(string $label) {
parent::__construct('li', true);
$this->label = $label;
}
/**
* Get action name.
*
* @return string|null
*/
public function getAction(): ?string {
return $this->action;
}
/**
* Set action name and derive a corresponding URL for menu item link.
*
* @param string $action_name Action name.
*
* @return CMenuItem
*/
public function setAction(string $action_name): self {
return $this->setUrl((new CUrl('zabbix.php'))->setArgument('action', $action_name), $action_name);
}
/**
* Get action name aliases.
*
* @return array
*/
public function getAliases(): array {
return $this->aliases;
}
/**
* Set action name aliases.
*
* @param array $aliases The aliases of menu item. Is able to specify the alias in following formats:
* - {action_name} - The alias is applicable to page with specified action name with any GET
* parameters in URL or without them;
* - {action_name}?{param}={value} - The alias is applicable to page with specified action
* when specified GET parameter exists in URL and have the same value;
* - {action_name}?{param}=* - The alias is applicable to page with specified action
* when specified GET parameter exists in URL and have any value;
* - {action_name}?!{param}={value} - The alias is applicable to page with specified action
* when specified GET parameter not exists in URL or have different value;
* - {action_name}?!{param}=* - The alias is applicable to page with specified action
* when specified GET parameter not exists in URL.
*
* @return CMenuItem
*/
public function setAliases(array $aliases): self {
foreach ($aliases as $alias) {
['path' => $action_name, 'query' => $query_string] = parse_url($alias) + ['query' => ''];
parse_str($query_string, $query_params);
$this->aliases[$action_name][] = $query_params;
}
return $this;
}
/**
* Set icon CSS class for menu item link.
*
* @param string $icon_class
*
* @return CMenuItem
*/
public function setIcon(string $icon_class): self {
$this->icon_class = $icon_class;
return $this;
}
/**
* Get visual label of menu item.
*
* @return string
*/
public function getLabel(): string {
return $this->label;
}
/**
* Check if menu item is marked as selected.
*
* @return bool
*/
public function isSelected(): bool {
return $this->is_selected;
}
/**
* Mark menu item as selected.
*
* @return CMenuItem
*/
public function setSelected(): self {
$this->is_selected = true;
$this->addClass('is-selected');
return $this;
}
/**
* Deep find menu item (including this one) 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 = true): bool {
if (array_key_exists($action_name, $this->aliases)) {
foreach ($this->aliases[$action_name] as $alias_params) {
$no_unacceptable_params = true;
$unacceptable_params = [];
foreach ($alias_params as $name => $value) {
if ($name[0] === '!') {
$unacceptable_params[substr($name, 1)] = $value;
unset($alias_params[$name]);
}
}
if ($unacceptable_params) {
$unacceptable_params_existing = array_intersect_assoc($unacceptable_params, $request_params);
foreach ($unacceptable_params as $name => $value) {
if ($value === '*' && array_key_exists($name, $request_params)) {
$unacceptable_params_existing[$name] = '*';
}
}
$no_unacceptable_params = array_diff_assoc($unacceptable_params, $unacceptable_params_existing)
? true
: false;
}
$alias_params_diff = array_diff_assoc($alias_params, $request_params);
foreach ($alias_params_diff as $name => $value) {
if ($value === '*') {
unset($alias_params_diff[$name]);
}
}
if ($no_unacceptable_params && !$alias_params_diff) {
$this->setSelected();
return true;
}
}
}
if ($this->sub_menu !== null && $this->sub_menu->setSelectedByAction($action_name, $request_params, $expand)) {
$this->setSelected();
return true;
}
return false;
}
/**
* Get submenu of menu item or create new one, if not exists.
*
* @return CMenu
*/
public function getSubMenu(): CMenu {
if ($this->sub_menu === null) {
$this->setSubMenu(new CMenu());
}
return $this->sub_menu;
}
/**
* Set submenu for menu item.
*
* @param CMenu $sub_menu
*
* @return CMenuItem
*/
public function setSubMenu(CMenu $sub_menu): self {
$this->sub_menu = $sub_menu->addClass('submenu');
$this->addClass('has-submenu');
return $this;
}
/**
* Check if menu item has submenu.
*
* @return bool
*/
public function hasSubMenu(): bool {
return ($this->sub_menu !== null);
}
/**
* Set target attribute for the menu item link.
*
* @param string $target
*
* @return CMenuItem
*/
public function setTarget(string $target): self {
$this->target = $target;
return $this;
}
/**
* Set title attribute for the menu item link.
*
* @param string $title
*
* @return CMenuItem
*/
public function setTitle($title): self {
$this->title = $title;
return $this;
}
/**
* Get url of the menu item link.
*
* @return CUrl|null
*/
public function getUrl(): ?CUrl {
return $this->url;
}
/**
* Set url for the menu item link.
*
* @param CUrl $url
* @param string|null $action_name Associate action name to be matched by setSelected method.
*
* @return CMenuItem
*/
public function setUrl(CUrl $url, string $action_name = null): self {
$action = null;
if ($action_name !== null) {
$this->setAliases([$action_name]);
['path' => $action] = parse_url($action_name);
}
$this->url = $url;
$this->action = $action;
return $this;
}
public function toString($destroy = true)
{
if ($this->url !== null || $this->sub_menu !== null) {
$this->addItem([
(new CLink($this->label, $this->sub_menu !== null ? '#' : $this->url->getUrl()))
->addClass($this->icon_class)
->setTitle($this->title)
->setTarget($this->target),
$this->sub_menu
]);
}
else {
$this->addItem(
(new CSpan($this->label))
->addClass($this->icon_class)
->setTitle($this->title)
);
}
return parent::toString($destroy);
}
}
|