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
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* ExtraConfigurationSubscriber constructor. |
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
ApplicationConfigurationStorage $applicationConfigurationStorage, |
67
|
|
|
EntityManagerInterface $entityManager, |
68
|
|
|
ResourceRegistryInterface $registry, |
69
|
|
|
ConfigurationFactory $configurationFactory, |
70
|
|
|
MetadataHelperInterface $metadataHelper, |
71
|
|
|
TranslatorInterface $translator |
72
|
|
|
) { |
73
|
|
|
$this->applicationConfiguration = $applicationConfigurationStorage->getConfiguration(); |
74
|
|
|
$this->registry = $registry; |
75
|
|
|
$this->configurationFactory = $configurationFactory; |
76
|
|
|
$this->metadataHelper = $metadataHelper; |
77
|
|
|
$this->entityManager = $entityManager; |
78
|
|
|
$this->translator = $translator; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function enrichAdminConfiguration(ConfigurationEvent $event) |
82
|
|
|
{ |
83
|
|
|
if (!$this->isExtraConfigurationEnabled()) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
$configuration = $event->getConfiguration(); |
87
|
|
|
|
88
|
|
|
// Actions |
89
|
|
|
$this->addDefaultActions($configuration); |
90
|
|
|
|
91
|
|
|
// Add default field configuration: it provides a type, a form type, and a view according to the found metadata |
92
|
|
|
$helper = new FieldConfigurationHelper( |
93
|
|
|
$this->entityManager, |
94
|
|
|
$this->translator, |
95
|
|
|
$this->applicationConfiguration, |
96
|
|
|
$this->metadataHelper |
97
|
|
|
); |
98
|
|
|
$helper->addDefaultFields($configuration, $event->getEntityClass(), $event->getAdminName()); |
99
|
|
|
$helper->addDefaultStrategy($configuration); |
100
|
|
|
$helper->addDefaultRouteParameters($configuration); |
101
|
|
|
$helper->addDefaultFormUse($configuration); |
102
|
|
|
$helper->provideActionsFieldConfiguration($configuration, $event->getAdminName()); |
103
|
|
|
|
104
|
|
|
// Filters |
105
|
|
|
$this->addDefaultFilters($configuration); |
106
|
|
|
|
107
|
|
|
$event->setConfiguration($configuration); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Defines the default CRUD actions if no action was configured. |
112
|
|
|
*/ |
113
|
|
|
private function addDefaultActions(array &$configuration) |
114
|
|
|
{ |
115
|
|
|
if (!key_exists('actions', $configuration) || !is_array($configuration['actions'])) { |
116
|
|
|
$configuration['actions'] = []; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (0 !== count($configuration['actions'])) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
$configuration['actions'] = [ |
123
|
|
|
'create' => [], |
124
|
|
|
'list' => [], |
125
|
|
|
'edit' => [], |
126
|
|
|
'delete' => [], |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Add default filters for the list actions, guessed using the entity metadata. |
132
|
|
|
*/ |
133
|
|
|
private function addDefaultFilters(array &$configuration): void |
134
|
|
|
{ |
135
|
|
|
// Add the filters only for the "list" action |
136
|
|
|
if (!key_exists('list', $configuration['actions'])) { |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// If some filters are already configured, we do not add the default filters |
141
|
|
|
if (key_exists('filter', $configuration['actions']['list'])) { |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
// TODO add a default unified filter |
145
|
|
|
$metadata = $this->metadataHelper->findMetadata($configuration['entity']); |
146
|
|
|
|
147
|
|
|
if (null === $metadata) { |
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
$filters = []; |
151
|
|
|
|
152
|
|
|
foreach ($metadata->getFieldNames() as $fieldName) { |
153
|
|
|
$type = $metadata->getTypeOfField($fieldName); |
154
|
|
|
$operator = $this->getOperatorFromFieldType($type); |
155
|
|
|
|
156
|
|
|
$filters[$fieldName] = [ |
157
|
|
|
'type' => $type, |
158
|
|
|
'options' => [], |
159
|
|
|
'comparator' => $operator, |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
$configuration['actions']['list']['filters'] = $filters; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private function getOperatorFromFieldType($type) |
166
|
|
|
{ |
167
|
|
|
$mapping = [ |
168
|
|
|
'string' => 'like', |
169
|
|
|
'text' => 'like', |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
if (key_exists($type, $mapping)) { |
173
|
|
|
return $mapping[$type]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return '='; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function isExtraConfigurationEnabled(): bool |
180
|
|
|
{ |
181
|
|
|
return $this->applicationConfiguration->getParameter('enable_extra_configuration'); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|