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