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