1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Configuration; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use JK\Configuration\Configuration; |
9
|
|
|
use LAG\AdminBundle\Admin\Action; |
10
|
|
|
use LAG\AdminBundle\Admin\Admin; |
11
|
|
|
use LAG\AdminBundle\Controller\AdminAction; |
12
|
|
|
use LAG\AdminBundle\Exception\Exception; |
13
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
14
|
|
|
use Symfony\Component\OptionsResolver\Options; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
use function Symfony\Component\String\u; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Ease Admin configuration manipulation. |
20
|
|
|
*/ |
21
|
|
|
class AdminConfiguration extends Configuration |
22
|
|
|
{ |
23
|
10 |
|
protected function configureOptions(OptionsResolver $resolver): void |
24
|
|
|
{ |
25
|
|
|
$resolver |
26
|
10 |
|
->setRequired('entity') |
27
|
10 |
|
->setAllowedTypes('entity', 'string') |
28
|
10 |
|
->setRequired('name') |
29
|
10 |
|
->setAllowedTypes('name', 'string') |
30
|
|
|
|
31
|
10 |
|
->setDefault('actions', [ |
32
|
10 |
|
'list' => [], |
33
|
|
|
'create' => [], |
34
|
|
|
'edit' => [], |
35
|
|
|
'delete' => [], |
36
|
|
|
]) |
37
|
10 |
|
->setAllowedTypes('actions', 'array') |
38
|
10 |
|
->setNormalizer('actions', $this->getActionNormalizer()) |
39
|
|
|
|
40
|
10 |
|
->setDefault('controller', AdminAction::class) |
41
|
10 |
|
->setAllowedTypes('controller', 'string') |
42
|
|
|
|
43
|
10 |
|
->setDefault('batch', []) |
44
|
10 |
|
->setAllowedTypes('batch', 'array') |
45
|
|
|
|
46
|
10 |
|
->setDefault('admin_class', Admin::class) |
47
|
10 |
|
->setAllowedTypes('admin_class', 'string') |
48
|
10 |
|
->setDefault('action_class', Action::class) |
49
|
10 |
|
->setAllowedTypes('action_class', 'string') |
50
|
|
|
|
51
|
10 |
|
->setDefault('routes_pattern', 'lag_admin.{admin}.{action}') |
52
|
10 |
|
->setAllowedTypes('routes_pattern', 'string') |
53
|
10 |
|
->setNormalizer('routes_pattern', $this->getRoutesPatternNormalizer()) |
54
|
|
|
|
55
|
10 |
|
->setDefault('pager', 'pagerfanta') |
56
|
10 |
|
->setAllowedValues('pager', ['pagerfanta', false]) |
57
|
10 |
|
->setDefault('max_per_page', 25) |
58
|
10 |
|
->setAllowedTypes('max_per_page', 'integer') |
59
|
10 |
|
->setDefault('page_parameter', 'page') |
60
|
10 |
|
->setAllowedTypes('page_parameter', 'string') |
61
|
|
|
|
62
|
10 |
|
->setDefault('permissions', 'ROLE_ADMIN') |
63
|
10 |
|
->setAllowedTypes('permissions', 'string') |
64
|
|
|
|
65
|
10 |
|
->setDefault('date_format', 'Y-m-d') |
66
|
10 |
|
->setAllowedTypes('date_format', 'string') |
67
|
|
|
|
68
|
10 |
|
->setDefault('data_provider', 'doctrine') |
69
|
10 |
|
->setAllowedTypes('data_provider', 'string') |
70
|
10 |
|
->setDefault('data_persister', 'doctrine') |
71
|
10 |
|
->setAllowedTypes('data_persister', 'string') |
72
|
|
|
|
73
|
10 |
|
->setDefault('create_template', '@LAGAdmin/crud/create.html.twig') |
74
|
10 |
|
->setAllowedTypes('create_template', 'string') |
75
|
10 |
|
->setDefault('edit_template', '@LAGAdmin/crud/edit.html.twig') |
76
|
10 |
|
->setAllowedTypes('edit_template', 'string') |
77
|
10 |
|
->setDefault('list_template', '@LAGAdmin/crud/list.html.twig') |
78
|
10 |
|
->setAllowedTypes('list_template', 'string') |
79
|
10 |
|
->setDefault('delete_template', '@LAGAdmin/crud/delete.html.twig') |
80
|
10 |
|
->setAllowedTypes('delete_template', 'string') |
81
|
|
|
|
82
|
10 |
|
->setDefault('menus', []) |
83
|
10 |
|
->setAllowedTypes('menus', 'array') |
84
|
|
|
; |
85
|
10 |
|
} |
86
|
|
|
|
87
|
1 |
|
public function getName(): string |
88
|
|
|
{ |
89
|
1 |
|
return $this->getString('name'); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function getAdminClass(): string |
93
|
|
|
{ |
94
|
2 |
|
return $this->getString('admin_class'); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function getActionClass(): string |
98
|
|
|
{ |
99
|
1 |
|
return $this->getString('action_class'); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
public function getActions(): array |
103
|
|
|
{ |
104
|
2 |
|
return $this->get('actions'); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function hasAction(string $actionName): bool |
108
|
|
|
{ |
109
|
1 |
|
return \array_key_exists($actionName, $this->getActions()); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
public function getAction(string $actionName): array |
113
|
|
|
{ |
114
|
2 |
|
return $this->getActions()[$actionName]; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
public function getEntityClass(): string |
118
|
|
|
{ |
119
|
1 |
|
return $this->getString('entity'); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
public function getController(): string |
123
|
|
|
{ |
124
|
1 |
|
return $this->getString('controller'); |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
public function getBatch(): array |
128
|
|
|
{ |
129
|
1 |
|
return $this->get('batch'); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
public function getRoutesPattern(): string |
133
|
|
|
{ |
134
|
1 |
|
return $this->getString('routes_pattern'); |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
public function isPaginationEnabled(): bool |
138
|
|
|
{ |
139
|
2 |
|
$pager = $this->get('pager'); |
140
|
|
|
|
141
|
2 |
|
return !($pager === false); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
public function getPager(): string |
145
|
|
|
{ |
146
|
2 |
|
if (!$this->isPaginationEnabled()) { |
147
|
1 |
|
throw new Exception(sprintf('The pagination is not enabled for the admin "%s"', $this->getString('name'))); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
return $this->get('pager'); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
public function getMaxPerPage(): int |
154
|
|
|
{ |
155
|
1 |
|
return $this->getInt('max_per_page'); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
public function getPageParameter(): string |
159
|
|
|
{ |
160
|
1 |
|
return $this->get('page_parameter'); |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
public function getPermissions(): array |
164
|
|
|
{ |
165
|
1 |
|
$roles = explode(',', $this->get('permissions')); |
166
|
|
|
|
167
|
1 |
|
foreach ($roles as $index => $role) { |
168
|
1 |
|
$roles[$index] = trim($role); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
return $roles; |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
public function getDateFormat(): string |
175
|
|
|
{ |
176
|
1 |
|
return $this->getString('date_format'); |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
public function getActionRouteParameters(string $actionName): array |
180
|
|
|
{ |
181
|
1 |
|
$actionConfiguration = $this->getAction($actionName); |
182
|
|
|
|
183
|
1 |
|
if (empty($actionConfiguration['route_parameters'])) { |
184
|
1 |
|
return []; |
185
|
|
|
} |
186
|
|
|
|
187
|
1 |
|
return $actionConfiguration['route_parameters']; |
188
|
|
|
} |
189
|
|
|
|
190
|
3 |
|
public function getDataProvider(): string |
191
|
|
|
{ |
192
|
3 |
|
return $this->getString('data_provider'); |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
public function getDataPersister(): string |
196
|
|
|
{ |
197
|
1 |
|
return $this->getString('data_persister'); |
198
|
|
|
} |
199
|
|
|
|
200
|
1 |
|
public function getCreateTemplate(): string |
201
|
|
|
{ |
202
|
1 |
|
return $this->getString('create_template'); |
203
|
|
|
} |
204
|
|
|
|
205
|
1 |
|
public function getEditTemplate(): string |
206
|
|
|
{ |
207
|
1 |
|
return $this->getString('edit_template'); |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
public function getListTemplate(): string |
211
|
|
|
{ |
212
|
1 |
|
return $this->getString('list_template'); |
213
|
|
|
} |
214
|
|
|
|
215
|
1 |
|
public function getDeleteTemplate(): string |
216
|
|
|
{ |
217
|
1 |
|
return $this->getString('delete_template'); |
218
|
|
|
} |
219
|
|
|
|
220
|
1 |
|
public function getMenus(): array |
221
|
|
|
{ |
222
|
1 |
|
return $this->get('menus'); |
223
|
|
|
} |
224
|
|
|
|
225
|
10 |
|
private function getActionNormalizer(): Closure |
226
|
|
|
{ |
227
|
10 |
|
return function (Options $options, $actions) { |
228
|
9 |
|
$normalizedActions = []; |
229
|
|
|
// $addBatchAction = false; |
230
|
|
|
|
231
|
9 |
|
foreach ($actions as $name => $action) { |
232
|
|
|
// action configuration is an array by default |
233
|
9 |
|
if (null === $action) { |
234
|
1 |
|
$action = []; |
235
|
|
|
} |
236
|
|
|
|
237
|
9 |
|
if (!\array_key_exists('route_parameters', $action)) { |
238
|
9 |
|
if ($name === 'edit' || $name === 'delete') { |
239
|
8 |
|
$action['route_parameters'] = ['id' => null]; |
240
|
|
|
} |
241
|
|
|
} |
242
|
9 |
|
$normalizedActions[$name] = $action; |
243
|
|
|
|
244
|
|
|
// in list action, if no batch was configured or disabled, we add a batch action |
245
|
|
|
// if ('list' == $name && (!\array_key_exists('batch', $action) || null === $action['batch'])) { |
246
|
|
|
// $addBatchAction = true; |
247
|
|
|
// } |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// add empty default batch action |
251
|
|
|
// if ($addBatchAction) { |
252
|
|
|
// TODO enable mass action |
253
|
|
|
// $normalizedActions['batch'] = []; |
254
|
|
|
// } |
255
|
|
|
|
256
|
9 |
|
return $normalizedActions; |
257
|
10 |
|
}; |
258
|
|
|
} |
259
|
|
|
|
260
|
10 |
|
private function getRoutesPatternNormalizer(): Closure |
261
|
|
|
{ |
262
|
10 |
|
return function (Options $options, $value) { |
263
|
9 |
|
if (!u($value)->containsAny('{action}')) { |
264
|
1 |
|
throw new InvalidOptionsException(sprintf('The "%s" parameters in admin "%s" should contains the "%s" parameters', 'routes_pattern', $options->offsetGet('name'), '{action}')); |
265
|
|
|
} |
266
|
|
|
|
267
|
8 |
|
if (!u($value)->containsAny('{admin}')) { |
268
|
1 |
|
throw new InvalidOptionsException(sprintf('The "%s" parameters in admin "%s" should contains the "%s" parameters', 'routes_pattern', $options->offsetGet('name'), '{admin}')); |
269
|
|
|
} |
270
|
|
|
|
271
|
7 |
|
return $value; |
272
|
10 |
|
}; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|