1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Event\Subscriber; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use LAG\AdminBundle\Bridge\Doctrine\ORM\Metadata\MetadataHelperInterface; |
7
|
|
|
use LAG\AdminBundle\Configuration\ApplicationConfiguration; |
8
|
|
|
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage; |
9
|
|
|
use LAG\AdminBundle\Event\Events; |
10
|
|
|
use LAG\AdminBundle\Event\Events\ConfigurationEvent; |
11
|
|
|
use LAG\AdminBundle\Event\Menu\MenuConfigurationEvent; |
12
|
|
|
use LAG\AdminBundle\Factory\ConfigurationFactory; |
13
|
|
|
use LAG\AdminBundle\Field\Helper\FieldConfigurationHelper; |
14
|
|
|
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface; |
15
|
|
|
use LAG\AdminBundle\Utils\TranslationUtils; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
17
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Add extra default configuration for actions and fields. |
21
|
|
|
*/ |
22
|
|
|
class ExtraConfigurationSubscriber implements EventSubscriberInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ApplicationConfiguration |
26
|
|
|
*/ |
27
|
|
|
private $applicationConfiguration; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ResourceRegistryInterface |
31
|
|
|
*/ |
32
|
|
|
private $registry; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ConfigurationFactory |
36
|
|
|
*/ |
37
|
|
|
private $configurationFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MetadataHelperInterface |
41
|
|
|
*/ |
42
|
|
|
private $metadataHelper; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var EntityManagerInterface |
46
|
|
|
*/ |
47
|
|
|
private $entityManager; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var TranslatorInterface |
51
|
|
|
*/ |
52
|
|
|
private $translator; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
2 |
|
public static function getSubscribedEvents() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
2 |
|
Events::CONFIGURATION_ADMIN => 'enrichAdminConfiguration', |
61
|
|
|
//Events::MENU_CONFIGURATION => 'enrichMenuConfiguration', |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* ExtraConfigurationSubscriber constructor. |
67
|
|
|
*/ |
68
|
|
|
public function __construct( |
69
|
|
|
ApplicationConfigurationStorage $applicationConfigurationStorage, |
70
|
|
|
EntityManagerInterface $entityManager, |
71
|
|
|
ResourceRegistryInterface $registry, |
72
|
|
|
ConfigurationFactory $configurationFactory, |
73
|
|
|
MetadataHelperInterface $metadataHelper, |
74
|
|
|
TranslatorInterface $translator |
75
|
|
|
) { |
76
|
|
|
$this->applicationConfiguration = $applicationConfigurationStorage->getConfiguration(); |
77
|
|
|
$this->registry = $registry; |
78
|
|
|
$this->configurationFactory = $configurationFactory; |
79
|
|
|
$this->metadataHelper = $metadataHelper; |
80
|
|
|
$this->entityManager = $entityManager; |
81
|
|
|
$this->translator = $translator; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function enrichAdminConfiguration(ConfigurationEvent $event) |
85
|
|
|
{ |
86
|
|
|
if (!$this->isExtraConfigurationEnabled()) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
$configuration = $event->getConfiguration(); |
90
|
|
|
|
91
|
|
|
// Actions |
92
|
|
|
$this->addDefaultActions($configuration); |
93
|
|
|
|
94
|
|
|
// Add default field configuration: it provides a type, a form type, and a view according to the found metadata |
95
|
|
|
$helper = new FieldConfigurationHelper( |
96
|
|
|
$this->entityManager, |
97
|
|
|
$this->translator, |
98
|
|
|
$this->applicationConfiguration, |
99
|
|
|
$this->metadataHelper |
100
|
|
|
); |
101
|
|
|
$helper->addDefaultFields($configuration, $event->getEntityClass(), $event->getAdminName()); |
102
|
|
|
$helper->addDefaultStrategy($configuration); |
103
|
|
|
$helper->addDefaultRouteParameters($configuration); |
104
|
|
|
$helper->addDefaultFormUse($configuration); |
105
|
|
|
$helper->provideActionsFieldConfiguration($configuration, $event->getAdminName()); |
106
|
|
|
|
107
|
|
|
// Menus |
108
|
|
|
// $this->addDefaultRightMenu($configuration); |
109
|
|
|
// $this->addDefaultLeftMenu($configuration); |
110
|
|
|
// $this->addDefaultTopMenu($configuration, $event->getAdminName()); |
111
|
|
|
|
112
|
|
|
// Filters |
113
|
|
|
$this->addDefaultFilters($configuration); |
114
|
|
|
|
115
|
|
|
$event->setConfiguration($configuration); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Defines the default CRUD actions if no action was configured. |
122
|
|
|
*/ |
123
|
|
|
private function addDefaultActions(array &$configuration) |
124
|
|
|
{ |
125
|
|
|
if (!key_exists('actions', $configuration) || !is_array($configuration['actions'])) { |
126
|
|
|
$configuration['actions'] = []; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (0 !== count($configuration['actions'])) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
$configuration['actions'] = [ |
133
|
|
|
'create' => [], |
134
|
|
|
'list' => [], |
135
|
|
|
'edit' => [], |
136
|
|
|
'delete' => [], |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add the default left menu configuration. One item for each Admin. |
142
|
|
|
*/ |
143
|
|
|
private function addDefaultLeftMenu(array &$configuration) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
if (!$this->applicationConfiguration->getParameter('enable_menus')) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
$menus = $this->configurationFactory->createResourceMenuConfiguration(); |
|
|
|
|
149
|
|
|
|
150
|
|
|
// Add the resources menu for each action of the admin |
151
|
|
|
foreach ($configuration['actions'] as $name => $action) { |
152
|
|
|
if (null === $action) { |
153
|
|
|
$action = []; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (key_exists('menus', $action) && key_exists('left', $action)) { |
157
|
|
|
continue; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$configuration['actions'][$name]['menus']['left'] = $menus; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Add the default right menu. |
166
|
|
|
*/ |
167
|
|
|
private function addDefaultRightMenu(array &$configuration) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
if (!$this->applicationConfiguration->getParameter('enable_menus')) { |
170
|
|
|
return; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (!key_exists('list', $configuration['actions']) || null === $configuration['actions']['list']) { |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ( |
178
|
|
|
key_exists('menus', $configuration['actions']['list']) && |
179
|
|
|
is_array($configuration['actions']['list']['menus']) && |
180
|
|
|
key_exists('right', $configuration['actions']['list']['menus']) |
181
|
|
|
) { |
182
|
|
|
return; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$configuration['actions']['list']['menus']['right'] = []; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
private function addDefaultTopMenu(array &$configuration, string $adminName) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
if (!$this->applicationConfiguration->getParameter('enable_menus')) { |
191
|
|
|
return; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (key_exists('list', $configuration['actions'])) { |
195
|
|
|
// Add a "Create" link in the top bar if the create action is allowed |
196
|
|
|
if (!key_exists('create', $configuration['actions'])) { |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
$configuration['actions']['list']['menus']['top']['items'][] = [ |
200
|
|
|
'admin' => $adminName, |
201
|
|
|
'action' => 'create', |
202
|
|
|
'text' => $this->getText('create', $adminName), |
203
|
|
|
'icon' => 'plus', |
204
|
|
|
]; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if (key_exists('create', $configuration['actions'])) { |
208
|
|
|
// Add a "Return" link in the top bar if the list action is allowed |
209
|
|
|
if (!key_exists('list', $configuration['actions'])) { |
210
|
|
|
return; |
211
|
|
|
} |
212
|
|
|
// $configuration['actions']['create']['menus']['top']['items'][] = [ |
213
|
|
|
// 'admin' => $adminName, |
214
|
|
|
// 'action' => 'list', |
215
|
|
|
// 'text' => $this->getText('return', $adminName), |
216
|
|
|
// 'icon' => 'arrow-left', |
217
|
|
|
// ]; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if (key_exists('edit', $configuration['actions'])) { |
221
|
|
|
// Add a "Return" link in the top bar if the list action is allowed |
222
|
|
|
if (!key_exists('list', $configuration['actions'])) { |
223
|
|
|
return; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
if (!key_exists('menus', $configuration['actions']['edit'])) { |
227
|
|
|
$configuration['actions']['edit']['menus'] = []; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (!key_exists('top', $configuration['actions']['edit']['menus'])) { |
231
|
|
|
$configuration['actions']['edit']['menus']['top'] = []; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if (!key_exists('items', $configuration['actions']['edit']['menus']['top'])) { |
235
|
|
|
$configuration['actions']['edit']['menus']['top']['items'] = []; |
236
|
|
|
} |
237
|
|
|
// array_unshift($configuration['actions']['edit']['menus']['top']['items'], [ |
238
|
|
|
// 'admin' => $adminName, |
239
|
|
|
// 'action' => 'list', |
240
|
|
|
// 'text' => $this->getText('return', $adminName), |
241
|
|
|
// 'icon' => 'arrow-left', |
242
|
|
|
// ]); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Add default filters for the list actions, guessed using the entity metadata. |
248
|
|
|
*/ |
249
|
|
|
private function addDefaultFilters(array &$configuration): void |
250
|
|
|
{ |
251
|
|
|
// Add the filters only for the "list" action |
252
|
|
|
if (!key_exists('list', $configuration['actions'])) { |
253
|
|
|
return; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// If some filters are already configured, we do not add the default filters |
257
|
|
|
if (key_exists('filter', $configuration['actions']['list'])) { |
258
|
|
|
return; |
259
|
|
|
} |
260
|
|
|
// TODO add a default unified filter |
261
|
|
|
$metadata = $this->metadataHelper->findMetadata($configuration['entity']); |
262
|
|
|
|
263
|
|
|
if (null === $metadata) { |
264
|
|
|
return; |
265
|
|
|
} |
266
|
|
|
$filters = []; |
267
|
|
|
|
268
|
|
|
foreach ($metadata->getFieldNames() as $fieldName) { |
269
|
|
|
$type = $metadata->getTypeOfField($fieldName); |
270
|
|
|
$operator = $this->getOperatorFromFieldType($type); |
271
|
|
|
|
272
|
|
|
$filters[$fieldName] = [ |
273
|
|
|
'type' => $type, |
274
|
|
|
'options' => [], |
275
|
|
|
'comparator' => $operator, |
276
|
|
|
]; |
277
|
|
|
} |
278
|
|
|
$configuration['actions']['list']['filters'] = $filters; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
private function getOperatorFromFieldType($type) |
282
|
|
|
{ |
283
|
|
|
$mapping = [ |
284
|
|
|
'string' => 'like', |
285
|
|
|
'text' => 'like', |
286
|
|
|
]; |
287
|
|
|
|
288
|
|
|
if (key_exists($type, $mapping)) { |
289
|
|
|
return $mapping[$type]; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return '='; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
private function isExtraConfigurationEnabled(): bool |
296
|
|
|
{ |
297
|
|
|
return $this->applicationConfiguration->getParameter('enable_extra_configuration'); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
private function getText(string $text, $adminName = null): string |
301
|
|
|
{ |
302
|
|
|
$text = ucfirst($text); |
303
|
|
|
|
304
|
|
|
if (!$this->applicationConfiguration->isTranslationEnabled()) { |
305
|
|
|
$text = TranslationUtils::getTranslationKey( |
306
|
|
|
$this->applicationConfiguration->getTranslationPattern(), |
307
|
|
|
$adminName, |
308
|
|
|
'create' |
309
|
|
|
); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $text; |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.