1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\Bundle\HookBundle\Helper; |
15
|
|
|
|
16
|
|
|
use Zikula\Bundle\HookBundle\Collector\HookCollectorInterface; |
17
|
|
|
use Zikula\Bundle\HookBundle\Dispatcher\HookDispatcherInterface; |
18
|
|
|
use Zikula\ExtensionsModule\Entity\ExtensionEntity; |
19
|
|
|
use Zikula\ExtensionsModule\Entity\RepositoryInterface\ExtensionRepositoryInterface; |
20
|
|
|
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* HookUiHelper class. |
24
|
|
|
*/ |
25
|
|
|
class HookUiHelper |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var HookCollectorInterface |
29
|
|
|
*/ |
30
|
|
|
private $collector; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ExtensionRepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $extensionRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var PermissionApiInterface |
39
|
|
|
*/ |
40
|
|
|
private $permissionApi; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var HookDispatcherInterface |
44
|
|
|
*/ |
45
|
|
|
private $dispatcher; |
46
|
|
|
|
47
|
|
|
public function __construct( |
48
|
|
|
HookCollectorInterface $collector, |
49
|
|
|
ExtensionRepositoryInterface $extensionRepository, |
50
|
|
|
HookDispatcherInterface $dispatcher, |
51
|
|
|
PermissionApiInterface $permissionApi |
52
|
|
|
) { |
53
|
|
|
$this->collector = $collector; |
54
|
|
|
$this->extensionRepository = $extensionRepository; |
55
|
|
|
$this->dispatcher = $dispatcher; |
56
|
|
|
$this->permissionApi = $permissionApi; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function prepareSubscriberAreasForSubscriber(array $subscriberAreas, iterable $subscribers): array |
60
|
|
|
{ |
61
|
|
|
$result = [ |
62
|
|
|
'titles' => [], |
63
|
|
|
'categories' => [], |
64
|
|
|
'categoryGroups' => [] |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
foreach ($subscriberAreas as $subscriberArea) { |
68
|
|
|
$category = null; |
69
|
|
|
if (isset($subscribers[$subscriberArea])) { |
70
|
|
|
$result['titles'][$subscriberArea] = $subscribers[$subscriberArea]->getTitle(); |
71
|
|
|
$category = $subscribers[$subscriberArea]->getCategory(); |
72
|
|
|
} |
73
|
|
|
$result['categories'][$subscriberArea] = $category; |
74
|
|
|
if (null !== $category) { |
75
|
|
|
$result['categoryGroups'][$category][] = $subscriberArea; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function prepareAvailableSubscriberAreasForProvider(string $moduleName, iterable $subscribers): array |
83
|
|
|
{ |
84
|
|
|
/** @var ExtensionEntity[] $hookSubscribers */ |
85
|
|
|
$hookSubscribers = $this->getExtensionsCapableOf(HookCollectorInterface::HOOK_SUBSCRIBER); |
86
|
|
|
$amountOfAvailableSubscriberAreas = 0; |
87
|
|
|
foreach ($hookSubscribers as $i => $hookSubscriber) { |
88
|
|
|
$hookSubscribers[$i] = $hookSubscriber->toArray(); |
89
|
|
|
// don't allow subscriber and provider to be the same |
90
|
|
|
// unless subscriber has the ability to connect to it's own providers |
91
|
|
|
if ($moduleName === $hookSubscriber->getName()) { |
92
|
|
|
unset($hookSubscribers[$i]); |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
// does the user have admin permissions on the subscriber module? |
96
|
|
|
if (!$this->permissionApi->hasPermission($hookSubscriber->getName() . '::', '::', ACCESS_ADMIN)) { |
97
|
|
|
unset($hookSubscribers[$i]); |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// get the areas of the subscriber |
102
|
|
|
$subscriberAreas = $this->collector->getSubscriberAreasByOwner($hookSubscriber->getName()); |
103
|
|
|
$hookSubscribers[$i]['areas'] = $subscriberAreas; |
104
|
|
|
$amountOfAvailableSubscriberAreas += count($subscriberAreas); |
105
|
|
|
|
106
|
|
|
$subscriberAreasToTitles = []; // and get the titles |
107
|
|
|
$subscriberAreasToCategories = []; // and get the categories |
108
|
|
|
foreach ($subscriberAreas as $area) { |
109
|
|
|
$category = null; |
110
|
|
|
if (isset($subscribers[$area])) { |
111
|
|
|
$subscriberAreasToTitles[$area] = $subscribers[$area]->getTitle(); |
112
|
|
|
$category = $subscribers[$area]->getCategory(); |
113
|
|
|
} |
114
|
|
|
$subscriberAreasToCategories[$area] = $category; |
115
|
|
|
} |
116
|
|
|
$hookSubscribers[$i]['areasToTitles'] = $subscriberAreasToTitles; |
117
|
|
|
$hookSubscribers[$i]['areasToCategories'] = $subscriberAreasToCategories; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return [$hookSubscribers, $amountOfAvailableSubscriberAreas]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function prepareAttachedProvidersForSubscriber(array $subscriberAreas, iterable $providers): array |
124
|
|
|
{ |
125
|
|
|
$sorting = []; |
126
|
|
|
$sortingTitles = []; |
127
|
|
|
$amountOfAttachedProviderAreas = 0; |
128
|
|
|
foreach ($subscriberAreas as $hookSubscriber) { |
129
|
|
|
$sortsByArea = $this->dispatcher->getBindingsFor($hookSubscriber); |
130
|
|
|
foreach ($sortsByArea as $sba) { |
131
|
|
|
$areaname = $sba['areaname']; |
132
|
|
|
$category = $sba['category']; |
133
|
|
|
|
134
|
|
|
if (!isset($sorting[$category])) { |
135
|
|
|
$sorting[$category] = []; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!isset($sorting[$category][$hookSubscriber])) { |
139
|
|
|
$sorting[$category][$hookSubscriber] = []; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$sorting[$category][$hookSubscriber][] = $areaname; |
143
|
|
|
$amountOfAttachedProviderAreas++; |
144
|
|
|
|
145
|
|
|
// get the bundle title |
146
|
|
|
if (isset($providers[$areaname])) { |
147
|
|
|
$sortingTitles[$areaname] = $providers[$areaname]->getTitle(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return [$sorting, $sortingTitles, $amountOfAttachedProviderAreas]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function prepareAvailableProviderAreasForSubscriber(string $moduleName, iterable $providers, bool $isSubscriberSelfCapable): array |
156
|
|
|
{ |
157
|
|
|
/** @var ExtensionEntity[] $hookProviders */ |
158
|
|
|
$hookProviders = $this->getExtensionsCapableOf(HookCollectorInterface::HOOK_PROVIDER); |
159
|
|
|
$amountOfAvailableProviderAreas = 0; |
160
|
|
|
foreach ($hookProviders as $i => $hookProvider) { |
161
|
|
|
$hookProviders[$i] = $hookProvider->toArray(); |
162
|
|
|
// don't allow subscriber and provider to be the same |
163
|
|
|
// unless subscriber has the ability to connect to it's own providers |
164
|
|
|
if (!$isSubscriberSelfCapable && $moduleName === $hookProvider->getName()) { |
165
|
|
|
unset($hookProviders[$i]); |
166
|
|
|
continue; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// does the user have admin permissions on the provider module? |
170
|
|
|
if (!$this->permissionApi->hasPermission($hookProvider->getName() . '::', '::', ACCESS_ADMIN)) { |
171
|
|
|
unset($hookProviders[$i]); |
172
|
|
|
continue; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// get the areas of the provider |
176
|
|
|
$providerAreas = $collector->getProviderAreasByOwner($hookProvider->getName()); |
|
|
|
|
177
|
|
|
$hookProviders[$i]['areas'] = $providerAreas; |
178
|
|
|
$amountOfAvailableProviderAreas += count($providerAreas); |
179
|
|
|
|
180
|
|
|
$providerAreasToTitles = []; // and get the titles |
181
|
|
|
$providerAreasToCategories = []; // and get the categories |
182
|
|
|
$providerAreasAndCategories = []; // and build array with category => areas |
183
|
|
|
foreach ($providerAreas as $area) { |
184
|
|
|
$category = null; |
185
|
|
|
if (isset($providers[$area])) { |
186
|
|
|
$providerAreasToTitles[$area] = $providers[$area]->getTitle(); |
187
|
|
|
$category = $providers[$area]->getCategory(); |
188
|
|
|
} |
189
|
|
|
$providerAreasToCategories[$area] = $category; |
190
|
|
|
$providerAreasAndCategories[$category][] = $area; |
191
|
|
|
} |
192
|
|
|
$hookProviders[$i]['areasToTitles'] = $providerAreasToTitles; |
193
|
|
|
$hookProviders[$i]['areasToCategories'] = $providerAreasToCategories; |
194
|
|
|
$hookProviders[$i]['areasAndCategories'] = $providerAreasAndCategories; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return [$hookProviders, $amountOfAvailableProviderAreas]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function getExtensionsCapableOf(string $type): array |
201
|
|
|
{ |
202
|
|
|
$owners = $this->collector->getOwnersCapableOf($type); |
203
|
|
|
$extensions = []; |
204
|
|
|
foreach ($owners as $owner) { |
205
|
|
|
$extensions[] = $this->extensionRepository->findOneBy(['name' => $owner]); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $extensions; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|