-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavbarMenuItem.php
More file actions
148 lines (122 loc) · 3.02 KB
/
Copy pathNavbarMenuItem.php
File metadata and controls
148 lines (122 loc) · 3.02 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
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
<?php
namespace ADT\FancyAdmin\Model\Menu;
use ADT\FancyAdmin\Model\Security\SecurityUser;
use Nette\Application\UI\Component;
use Nette\Security\Resource;
class NavbarMenuItem
{
protected string $label = 'Test';
protected string $faIcon = 'chart-simple';
protected ?NavbarSubmenu $submenu = null;
protected ?string $link = null;
protected array $linkArgs = [];
protected ?Resource $resource = null;
protected ?\Closure $condition = null;
public function getLabel(): string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getFaIcon(): string
{
return $this->faIcon;
}
public function setFaIcon(string $faIcon): self
{
$this->faIcon = $faIcon;
return $this;
}
public function getSubmenu(): ?NavbarSubmenu
{
return $this->submenu;
}
public function setCondition(\Closure $condition): self
{
$this->condition = $condition;
return $this;
}
public function isVisible(SecurityUser $user, Component $presenter): bool
{
if ($this->getAclResource() && !$user->isAllowed($this->getAclResource())) {
return false;
}
return $this->condition === null || ($this->condition)($user, $presenter);
}
public function isEnabledSubmenu(SecurityUser $user, Component $presenter): bool
{
if ($this->getAclResource()) {
return $user->isAllowed($this->getAclResource());
}
if ($this->condition !== null && !($this->condition)($user, $presenter)) {
return false;
}
if (!$this->getSubmenu() || count($this->getSubmenu()->getSubMenuItems()) === 0) {
return true;
}
foreach ($this->getSubmenu()->getSubMenuItems() as $subMenuItem) {
if ($subMenuItem instanceof NavbarSubmenuHeading) {
continue;
}
if ($subMenuItem->isVisible($user, $presenter)) {
return true;
}
}
return false;
}
private function setSubmenu(NavbarSubmenu $submenu): self
{
$this->submenu = $submenu;
return $this;
}
public function getLink(): ?string
{
return $this->link;
}
public function setLink(string $link): self
{
$this->link = $link;
return $this;
}
public function getLinkArgs(): array
{
return $this->linkArgs;
}
public function setLinkArgs(array $linkArgs): self
{
$this->linkArgs = $linkArgs;
return $this;
}
public function setupSubmenuItems(callable $setupSubmenuItem): self
{
$submenu = $this->submenu ?? new NavbarSubmenu($this);
$setupSubmenuItem($submenu);
$this->setSubmenu($submenu);
return $this;
}
public function isCurrent(Component $presenter): bool {
if ($this->getSubmenu()) {
foreach ($this->getSubmenu()->getSubMenuItems() as $submenuItem) {
if ($submenuItem instanceof NavbarSubmenuHeading) {
continue;
}
if ($submenuItem->isCurrent($presenter)) {
return true;
}
}
}
return $presenter->isLinkCurrent($this->getLink(), $this->getLinkArgs());
}
public function getAclResource(): ?Resource
{
return $this->resource;
}
public function setAclResource(?Resource $resource): self
{
$this->resource = $resource;
return $this;
}
}