|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Contact\Events\Listeners; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Contact\Constant\Resource; |
|
8
|
|
|
use AbterPhp\Contact\Constant\Route; |
|
9
|
|
|
use AbterPhp\Framework\Constant\Html5; |
|
10
|
|
|
use AbterPhp\Framework\Events\NavigationReady; |
|
11
|
|
|
use AbterPhp\Framework\Html\Component\ButtonFactory; |
|
12
|
|
|
use AbterPhp\Framework\Html\ITag; |
|
13
|
|
|
use AbterPhp\Framework\Navigation\Dropdown; |
|
14
|
|
|
use AbterPhp\Framework\Navigation\Item; |
|
15
|
|
|
use AbterPhp\Framework\Navigation\Navigation; |
|
16
|
|
|
|
|
17
|
|
|
class NavigationBuilder |
|
18
|
|
|
{ |
|
19
|
|
|
const BASE_WEIGHT = 900; |
|
20
|
|
|
|
|
21
|
|
|
/** @var ButtonFactory */ |
|
22
|
|
|
protected $buttonFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* NavigationRegistrar constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param ButtonFactory $buttonFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(ButtonFactory $buttonFactory) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->buttonFactory = $buttonFactory; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param NavigationReady $event |
|
36
|
|
|
*/ |
|
37
|
|
|
public function handle(NavigationReady $event) |
|
38
|
|
|
{ |
|
39
|
|
|
$navigation = $event->getNavigation(); |
|
40
|
|
|
|
|
41
|
|
|
if (!$navigation->hasIntent(Navigation::INTENT_PRIMARY)) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$item = $this->createContactItem(); |
|
46
|
|
|
|
|
47
|
|
|
$navigation->addItem($item, static::BASE_WEIGHT); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return Item |
|
52
|
|
|
* @throws \Opulence\Routing\Urls\UrlException |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function createFormsItem(): Item |
|
55
|
|
|
{ |
|
56
|
|
|
$text = 'contact:forms'; |
|
57
|
|
|
$icon = 'assignment'; |
|
58
|
|
|
|
|
59
|
|
|
$button = $this->buttonFactory->createFromName($text, Route::CONTACT_FORMS_LIST, [], $icon); |
|
60
|
|
|
$resource = $this->getAdminResource(Resource::CONTACT_FORMS); |
|
61
|
|
|
|
|
62
|
|
|
$item = new Item($button); |
|
63
|
|
|
$item->setResource($resource); |
|
64
|
|
|
|
|
65
|
|
|
return $item; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return Item |
|
70
|
|
|
* @throws \Opulence\Routing\Urls\UrlException |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function createContactItem(): Item |
|
73
|
|
|
{ |
|
74
|
|
|
$text = 'contact:contact'; |
|
75
|
|
|
$icon = 'contacts'; |
|
76
|
|
|
|
|
77
|
|
|
$button = $this->buttonFactory->createFromName($text, Route::CONTACT_FORMS_LIST, [], $icon); |
|
78
|
|
|
$resource = $this->getAdminResource(Resource::CONTACT_FORMS); |
|
79
|
|
|
|
|
80
|
|
|
$item = new Item($button); |
|
81
|
|
|
$item->setResource($resource); |
|
82
|
|
|
|
|
83
|
|
|
$item->setIntent(Item::INTENT_DROPDOWN); |
|
84
|
|
|
$item->setAttribute(Html5::ATTR_ID, 'nav-contact'); |
|
85
|
|
|
|
|
86
|
|
|
if (!empty($item[0]) && $item[0] instanceof ITag) { |
|
87
|
|
|
$item[0]->setAttribute(Html5::ATTR_HREF, 'javascript:void(0);'); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$item[1] = $this->createDropdown(); |
|
91
|
|
|
|
|
92
|
|
|
return $item; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return Dropdown |
|
97
|
|
|
* @throws \Opulence\Routing\Urls\UrlException |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function createDropdown(): Dropdown |
|
100
|
|
|
{ |
|
101
|
|
|
$dropdown = new Dropdown(); |
|
102
|
|
|
$dropdown[] = $this->createFormsItem(); |
|
103
|
|
|
|
|
104
|
|
|
return $dropdown; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $resource |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getAdminResource(string $resource): string |
|
113
|
|
|
{ |
|
114
|
|
|
return sprintf('admin_resource_%s', $resource); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|