1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Events\Listeners; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Constant\Routes; |
8
|
|
|
use AbterPhp\Framework\Constant\Html5; |
9
|
|
|
use AbterPhp\Framework\Events\NavigationReady; |
10
|
|
|
use AbterPhp\Framework\Html\Component\ButtonFactory; |
11
|
|
|
use AbterPhp\Framework\Navigation\Dropdown; |
12
|
|
|
use AbterPhp\Framework\Navigation\Item; |
13
|
|
|
use AbterPhp\Framework\Navigation\Navigation; |
14
|
|
|
use AbterPhp\Framework\Navigation\UserBlock; |
15
|
|
|
use Opulence\Sessions\ISession; |
16
|
|
|
|
17
|
|
|
class NavigationBuilder |
18
|
|
|
{ |
19
|
|
|
const FIRST_ITEM_WEIGHT = 0; |
20
|
|
|
|
21
|
|
|
const DEFAULT_BASE_WEIGHT = 1000; |
22
|
|
|
|
23
|
|
|
/** @var ISession */ |
24
|
|
|
protected $session; |
25
|
|
|
|
26
|
|
|
/** @var ButtonFactory */ |
27
|
|
|
protected $buttonFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* NavigationRegistrar constructor. |
31
|
|
|
* |
32
|
|
|
* @param ISession $session |
33
|
|
|
* @param ButtonFactory $buttonFactory |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ISession $session, ButtonFactory $buttonFactory) |
36
|
|
|
{ |
37
|
|
|
$this->session = $session; |
38
|
|
|
$this->buttonFactory = $buttonFactory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param NavigationReady $event |
43
|
|
|
* |
44
|
|
|
* @throws \Opulence\Routing\Urls\URLException |
45
|
|
|
*/ |
46
|
|
|
public function handle(NavigationReady $event) |
47
|
|
|
{ |
48
|
|
|
$navigation = $event->getNavigation(); |
49
|
|
|
|
50
|
|
|
if (!$navigation->hasIntent(Navigation::INTENT_PRIMARY)) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->insertFirstItem($navigation); |
55
|
|
|
|
56
|
|
|
$dropdown = new Dropdown(); |
57
|
|
|
$dropdown[] = $this->createUserItem(); |
58
|
|
|
$dropdown[] = $this->createUserGroupItem(); |
59
|
|
|
$dropdown[] = $this->createLogoutItem(); |
60
|
|
|
|
61
|
|
|
$item = $this->createUserItem(); |
62
|
|
|
$item->setIntent(Item::INTENT_DROPDOWN); |
63
|
|
|
$item->setAttribute(Html5::ATTR_ID, 'nav-users'); |
64
|
|
|
$item[0]->setAttribute(Html5::ATTR_HREF, 'javascript:void(0);'); |
|
|
|
|
65
|
|
|
$item[1] = $dropdown; |
66
|
|
|
|
67
|
|
|
$navigation->addItem($item, static::DEFAULT_BASE_WEIGHT); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Navigation $navigation |
72
|
|
|
*/ |
73
|
|
|
protected function insertFirstItem(Navigation $navigation) |
74
|
|
|
{ |
75
|
|
|
$firstItem = new Item(null, [UserBlock::class]); |
76
|
|
|
|
77
|
|
|
$firstItem[] = $this->createUserBlock(); |
78
|
|
|
$firstItem[] = $this->createDropdown(); |
79
|
|
|
|
80
|
|
|
$navigation->addItem($firstItem, static::FIRST_ITEM_WEIGHT); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return UserBlock |
85
|
|
|
*/ |
86
|
|
|
protected function createUserBlock(): UserBlock |
87
|
|
|
{ |
88
|
|
|
return new UserBlock($this->session); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Dropdown |
93
|
|
|
*/ |
94
|
|
|
protected function createDropdown(): Dropdown |
95
|
|
|
{ |
96
|
|
|
$text = 'framework:logout'; |
97
|
|
|
|
98
|
|
|
$button = $this->buttonFactory->createFromName($text, Routes::ROUTE_LOGOUT, []); |
99
|
|
|
|
100
|
|
|
return new Dropdown(new Item($button)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Item |
105
|
|
|
* @throws \Opulence\Routing\Urls\UrlException |
106
|
|
|
*/ |
107
|
|
|
protected function createUserItem(): Item |
108
|
|
|
{ |
109
|
|
|
$text = 'admin:users'; |
110
|
|
|
$icon = 'person'; |
111
|
|
|
|
112
|
|
|
$button = $this->buttonFactory->createFromName($text, Routes::ROUTE_USERS, [], $icon); |
113
|
|
|
$resource = $this->getAdminResource(Routes::ROUTE_USERS); |
114
|
|
|
|
115
|
|
|
$item = new Item($button); |
116
|
|
|
$item->setResource($resource); |
117
|
|
|
|
118
|
|
|
return $item; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return Item |
123
|
|
|
* @throws \Opulence\Routing\Urls\UrlException |
124
|
|
|
*/ |
125
|
|
|
protected function createUserGroupItem(): Item |
126
|
|
|
{ |
127
|
|
|
$text = 'admin:userGroups'; |
128
|
|
|
$icon = 'group'; |
129
|
|
|
|
130
|
|
|
$button = $this->buttonFactory->createFromName($text, Routes::ROUTE_USER_GROUPS, [], $icon); |
131
|
|
|
$resource = $this->getAdminResource(Routes::ROUTE_USER_GROUPS); |
132
|
|
|
|
133
|
|
|
$item = new Item($button); |
134
|
|
|
$item->setResource($resource); |
135
|
|
|
|
136
|
|
|
return $item; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return Item |
141
|
|
|
* @throws \Opulence\Routing\Urls\UrlException |
142
|
|
|
*/ |
143
|
|
|
protected function createLogoutItem(): Item |
144
|
|
|
{ |
145
|
|
|
$text = 'admin:logout'; |
146
|
|
|
$icon = 'settings_power'; |
147
|
|
|
|
148
|
|
|
$button = $this->buttonFactory->createFromName($text, Routes::ROUTE_LOGOUT, [], $icon); |
149
|
|
|
$resource = $this->getAdminResource(Routes::ROUTE_LOGOUT); |
150
|
|
|
|
151
|
|
|
$item = new Item($button); |
152
|
|
|
$item->setResource($resource); |
153
|
|
|
|
154
|
|
|
return $item; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $resource |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
protected function getAdminResource(string $resource): string |
163
|
|
|
{ |
164
|
|
|
return sprintf('admin_resource_%s', $resource); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|