-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavbarMenu.php
More file actions
81 lines (67 loc) · 2 KB
/
Copy pathNavbarMenu.php
File metadata and controls
81 lines (67 loc) · 2 KB
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
<?php
namespace ADT\FancyAdmin\Model\Menu;
use Nette\Application\LinkGenerator;
class NavbarMenu
{
/** @var NavbarMenuItem[] */
protected array $menuItems = [];
protected LinkGenerator $linkGenerator;
public function addMenuItem(NavbarMenuItem $menuItem): self
{
$this->menuItems[] = $menuItem;
return $this;
}
/**
* @return NavbarMenuItem[]
*/
public function getMenuItems(): array
{
return $this->menuItems;
}
public function setLinkGenerator(LinkGenerator $linkGenerator): self
{
$this->linkGenerator = $linkGenerator;
return $this;
}
public function getLinkGenerator(): LinkGenerator
{
return $this->linkGenerator;
}
/**
* Auto-sets ACL resources on menu items that don't have one explicitly set,
* based on the link's presenter name and the current module.
*
* E.g. module "PortalBackoffice" + link "Devices:default" → resource "portalBackoffice.devices"
*/
public function resolveAclResources(string $module): self
{
foreach ($this->menuItems as $menuItem) {
if (!$menuItem->getAclResource() && $menuItem->getLink()) {
$presenterName = explode(':', $menuItem->getLink())[0];
$menuItem->setAclResource(new StringResource(lcfirst($module) . '.' . lcfirst($presenterName)));
}
if ($menuItem->getSubmenu()) {
foreach ($menuItem->getSubmenu()->getSubMenuItems() as $subItem) {
if ($subItem instanceof NavbarSubmenuHeading) {
continue;
}
if (!$subItem->getAclResource()) {
$presenterName = explode(':', $subItem->getLink())[0];
$subItem->setAclResource(new StringResource(lcfirst($module) . '.' . lcfirst($presenterName)));
}
}
}
}
return $this;
}
public function isLinkCurrent(?string $destination = null, $args = []): bool
{
if ($destination !== null) {
$args = func_num_args() < 3 && is_array($args)
? $args
: array_slice(func_get_args(), 1);
$this->linkGenerator->createRequest($this, $destination, $args, 'test');
}
return (bool)$this->linkGenerator->lastRequest?->hasFlag('current');
}
}