|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Event\Subscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
7
|
|
|
use LAG\AdminBundle\Action\Event\ActionEvents; |
|
8
|
|
|
use LAG\AdminBundle\Action\Event\BeforeConfigurationEvent; |
|
9
|
|
|
use LAG\AdminBundle\Admin\Behaviors\TranslationKeyTrait; |
|
10
|
|
|
use LAG\AdminBundle\Admin\Event\AdminCreateEvent; |
|
11
|
|
|
use LAG\AdminBundle\Application\Configuration\ApplicationConfiguration; |
|
12
|
|
|
use LAG\AdminBundle\Configuration\Factory\ConfigurationFactory; |
|
13
|
|
|
use LAG\AdminBundle\Admin\Event\AdminEvents; |
|
14
|
|
|
use LAG\AdminBundle\Routing\RouteNameGenerator; |
|
15
|
|
|
use LAG\AdminBundle\Utils\FieldTypeGuesser; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Add extra default configuration for actions and fields. Bind to ADMIN_CREATE and ACTION_CREATE events |
|
21
|
|
|
*/ |
|
22
|
|
|
class ExtraConfigurationSubscriber implements EventSubscriberInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use TranslationKeyTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* If is true, the extra configuration will be added. |
|
28
|
|
|
* |
|
29
|
|
|
* @var boolean |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $extraConfigurationEnabled = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var EntityManager |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $entityManager; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ApplicationConfiguration |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $applicationConfiguration; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public static function getSubscribedEvents() |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
1 |
|
AdminEvents::ADMIN_CREATE => 'adminCreate', |
|
50
|
1 |
|
ActionEvents::BEFORE_CONFIGURATION => 'beforeActionConfiguration', |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* ExtraConfigurationSubscriber constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param boolean $extraConfigurationEnabled |
|
58
|
|
|
* @param Registry $doctrine |
|
59
|
|
|
* @param ConfigurationFactory $configurationFactory |
|
60
|
|
|
*/ |
|
61
|
5 |
|
public function __construct( |
|
62
|
|
|
$extraConfigurationEnabled = true, |
|
63
|
|
|
Registry $doctrine, |
|
64
|
|
|
ConfigurationFactory $configurationFactory |
|
65
|
|
|
) { |
|
66
|
5 |
|
$this->extraConfigurationEnabled = $extraConfigurationEnabled; |
|
67
|
|
|
// entity manager can be closed, so its better to inject the Doctrine registry instead |
|
68
|
5 |
|
$this->entityManager = $doctrine->getManager(); |
|
69
|
5 |
|
$this->applicationConfiguration = $configurationFactory->getApplicationConfiguration(); |
|
|
|
|
|
|
70
|
5 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Adding default CRUD if none is defined. |
|
74
|
|
|
* |
|
75
|
|
|
* @param AdminCreateEvent $event |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function adminCreate(AdminCreateEvent $event) |
|
78
|
|
|
{ |
|
79
|
2 |
|
if (!$this->extraConfigurationEnabled) { |
|
80
|
1 |
|
return; |
|
81
|
|
|
} |
|
82
|
1 |
|
$configuration = $event->getAdminConfiguration(); |
|
83
|
|
|
|
|
84
|
|
|
// if no actions are defined, we set default CRUD action |
|
85
|
1 |
|
if (!array_key_exists('actions', $configuration) || !count($configuration['actions'])) { |
|
86
|
1 |
|
$configuration['actions'] = [ |
|
87
|
|
|
'create' => [], |
|
88
|
|
|
'list' => [], |
|
89
|
|
|
'edit' => [], |
|
90
|
|
|
'delete' => [], |
|
91
|
|
|
]; |
|
92
|
1 |
|
$event->setAdminConfiguration($configuration); |
|
93
|
|
|
} |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Add default linked actions and default menu actions. |
|
98
|
|
|
* |
|
99
|
|
|
* @param BeforeConfigurationEvent $event |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function beforeActionConfiguration(BeforeConfigurationEvent $event) |
|
102
|
|
|
{ |
|
103
|
|
|
// add configuration only if extra configuration is enabled |
|
104
|
3 |
|
if (!$this->extraConfigurationEnabled) { |
|
105
|
1 |
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
// Action configuration array |
|
108
|
2 |
|
$configuration = $event->getActionConfiguration(); |
|
109
|
|
|
|
|
110
|
|
|
// current Admin |
|
111
|
2 |
|
$admin = $event->getAdmin(); |
|
112
|
|
|
|
|
113
|
|
|
// allowed Actions according to the admin |
|
114
|
|
|
$keys = $admin |
|
115
|
2 |
|
->getConfiguration() |
|
116
|
2 |
|
->getParameter('actions'); |
|
117
|
2 |
|
$allowedActions = array_keys($keys); |
|
118
|
|
|
|
|
119
|
|
|
// add default menu configuration |
|
120
|
2 |
|
$configuration = $this->addDefaultMenuConfiguration( |
|
121
|
2 |
|
$admin->getName(), |
|
122
|
2 |
|
$event->getActionName(), |
|
123
|
2 |
|
$configuration, |
|
124
|
2 |
|
$allowedActions |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
// guess field configuration if required |
|
128
|
2 |
|
$this->guessFieldConfiguration( |
|
129
|
2 |
|
$configuration, |
|
130
|
2 |
|
$event->getActionName(), |
|
131
|
2 |
|
$admin->getConfiguration()->getParameter('entity') |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
// guess linked actions for list actions |
|
135
|
2 |
|
$this->guessLinkedActionsConfiguration( |
|
136
|
2 |
|
$configuration, |
|
137
|
2 |
|
$allowedActions, |
|
138
|
2 |
|
$event |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
// reset action configuration |
|
142
|
2 |
|
$event->setActionConfiguration($configuration); |
|
143
|
2 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Add default menu configuration for an action. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $adminName |
|
149
|
|
|
* @param string $actionName |
|
150
|
|
|
* @param array $actionConfiguration |
|
151
|
|
|
* @param array $allowedActions |
|
152
|
|
|
* @return array The modified configuration |
|
153
|
|
|
*/ |
|
154
|
2 |
|
protected function addDefaultMenuConfiguration( |
|
155
|
|
|
$adminName, |
|
156
|
|
|
$actionName, |
|
157
|
|
|
array $actionConfiguration, |
|
158
|
|
|
array $allowedActions |
|
159
|
|
|
) { |
|
160
|
|
|
// we add a default top menu item "create" only for list action |
|
161
|
2 |
|
if ($actionName === 'list') { |
|
162
|
|
|
|
|
163
|
|
|
// the create action should enabled |
|
164
|
1 |
|
if (in_array('create', $allowedActions)) { |
|
165
|
|
|
|
|
166
|
1 |
|
if (!array_key_exists('menus', $actionConfiguration)) { |
|
167
|
1 |
|
$actionConfiguration['menus'] = []; |
|
168
|
|
|
} |
|
169
|
|
|
// if the menu is disabled we do not add the menu item |
|
170
|
1 |
|
if ($actionConfiguration['menus'] !== false) { |
|
171
|
1 |
|
$resolver = new OptionsResolver(); |
|
172
|
1 |
|
$resolver->setDefaults([ |
|
173
|
|
|
'top' => [ |
|
174
|
|
|
'items' => [ |
|
175
|
|
|
'create' => [ |
|
176
|
1 |
|
'admin' => $adminName, |
|
177
|
1 |
|
'action' => 'create', |
|
178
|
1 |
|
'icon' => 'fa fa-plus', |
|
179
|
|
|
] |
|
180
|
|
|
] |
|
181
|
|
|
] |
|
182
|
|
|
]); |
|
183
|
|
|
// resolve default menu options |
|
184
|
1 |
|
$actionConfiguration['menus'] = $resolver->resolve($actionConfiguration['menus']); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
2 |
|
return $actionConfiguration; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* If no field was provided, try to guess field |
|
194
|
|
|
* |
|
195
|
|
|
* @param array $configuration |
|
196
|
|
|
* @param $actionName |
|
197
|
|
|
* @param $entityClass |
|
198
|
|
|
*/ |
|
199
|
2 |
|
protected function guessFieldConfiguration(array &$configuration, $actionName, $entityClass) |
|
200
|
|
|
{ |
|
201
|
|
|
// if no field was provided in configuration, we try to take fields from doctrine metadata |
|
202
|
2 |
|
if (empty($configuration['fields']) || !count($configuration['fields'])) { |
|
203
|
1 |
|
$fields = []; |
|
204
|
1 |
|
$guesser = new FieldTypeGuesser(); |
|
205
|
|
|
$metadata = $this |
|
206
|
1 |
|
->entityManager |
|
207
|
1 |
|
->getMetadataFactory() |
|
208
|
1 |
|
->getMetadataFor($entityClass); |
|
209
|
1 |
|
$fieldsName = $metadata->getFieldNames(); |
|
210
|
|
|
|
|
211
|
1 |
|
foreach ($fieldsName as $name) { |
|
212
|
1 |
|
$type = $metadata->getTypeOfField($name); |
|
213
|
|
|
// get field type from doctrine type |
|
214
|
1 |
|
$fieldConfiguration = $guesser->getTypeAndOptions($type); |
|
215
|
|
|
|
|
216
|
|
|
// if a field configuration was found, we take it |
|
217
|
1 |
|
if (count($fieldConfiguration)) { |
|
218
|
1 |
|
$fields[$name] = $fieldConfiguration; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
1 |
|
if (count($fields)) { |
|
222
|
|
|
// adding new fields to action configuration |
|
223
|
|
|
$configuration['fields'] = $fields; |
|
224
|
|
|
|
|
225
|
|
|
if ($actionName == 'list') { |
|
226
|
|
|
$configuration['fields']['_actions'] = null; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
2 |
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Add fake link fields to edit and delete action for the list view. |
|
234
|
|
|
* |
|
235
|
|
|
* @param array $configuration |
|
236
|
|
|
* @param array $allowedActions |
|
237
|
|
|
* @param BeforeConfigurationEvent $event |
|
238
|
|
|
*/ |
|
239
|
2 |
|
protected function guessLinkedActionsConfiguration( |
|
240
|
|
|
array &$configuration, |
|
241
|
|
|
array $allowedActions, |
|
242
|
|
|
BeforeConfigurationEvent $event |
|
243
|
|
|
) { |
|
244
|
2 |
|
if (!array_key_exists('fields', $configuration)) { |
|
245
|
1 |
|
return; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
// configured linked actions : |
|
249
|
|
|
// _action key should exists and be null |
|
250
|
1 |
|
$_actionExists = array_key_exists('_actions', $configuration['fields']); |
|
251
|
1 |
|
$_actionIsNull = $_actionExists && $configuration['fields']['_actions'] === null; |
|
252
|
|
|
|
|
253
|
|
|
// _action is added extra configuration only for the "list" action |
|
254
|
1 |
|
$isListAction = $event->getActionName() == 'list'; |
|
255
|
|
|
|
|
256
|
|
|
//dump($_actionExists, $_actionIsNull, $isListAction); |
|
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
1 |
|
if ($_actionExists && $_actionIsNull && $isListAction) { |
|
260
|
|
|
// in list view, we add by default and an edit and a delete button |
|
261
|
|
|
$translationPattern = $this |
|
262
|
|
|
->applicationConfiguration |
|
263
|
|
|
->getParameter('translation')['pattern']; |
|
264
|
|
|
|
|
265
|
|
|
// add a link to the "delete" action, if it is allowed |
|
266
|
|
|
if (in_array('delete', $allowedActions)) { |
|
267
|
|
|
$generator = new RouteNameGenerator(); |
|
268
|
|
|
$admin = $event->getAdmin(); |
|
269
|
|
|
|
|
270
|
|
|
$configuration['fields']['_actions'] = [ |
|
271
|
|
|
'type'=> 'action', |
|
272
|
|
|
'options' => [ |
|
273
|
|
|
'title' => $this->getTranslationKey($translationPattern, 'delete', $admin->getName()), |
|
274
|
|
|
'route' => $generator->generate('delete', $admin->getName(), $admin->getConfiguration()), |
|
275
|
|
|
'parameters' => [ |
|
276
|
|
|
'id' => false |
|
277
|
|
|
], |
|
278
|
|
|
'icon' => 'remove' |
|
279
|
|
|
] |
|
280
|
|
|
]; |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
1 |
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.