|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Action\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
6
|
|
|
use LAG\AdminBundle\Admin\Behaviors\TranslationKeyTrait; |
|
7
|
|
|
use LAG\AdminBundle\Configuration\Configuration; |
|
8
|
|
|
use LAG\AdminBundle\Menu\Configuration\MenuConfiguration; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
|
11
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
13
|
|
|
|
|
14
|
|
|
class ActionConfiguration extends Configuration |
|
15
|
|
|
{ |
|
16
|
|
|
use TranslationKeyTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Related Action name. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $actionName; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Related Admin (optional) |
|
27
|
|
|
* |
|
28
|
|
|
* @var AdminInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $admin = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* ActionConfiguration constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param $actionName |
|
36
|
|
|
* @param AdminInterface $admin |
|
37
|
|
|
*/ |
|
38
|
8 |
|
public function __construct($actionName, AdminInterface $admin) |
|
39
|
|
|
{ |
|
40
|
8 |
|
parent::__construct(); |
|
41
|
|
|
|
|
42
|
8 |
|
$this->actionName = $actionName; |
|
43
|
8 |
|
$this->admin = $admin; |
|
44
|
8 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Define allowed parameters and values for this configuration, using optionsResolver component. |
|
48
|
|
|
* |
|
49
|
|
|
* @param OptionsResolver $resolver |
|
50
|
|
|
*/ |
|
51
|
7 |
|
public function configureOptions(OptionsResolver $resolver) |
|
52
|
|
|
{ |
|
53
|
|
|
// action title, default to action's name |
|
54
|
|
|
$resolver |
|
55
|
7 |
|
->setDefault('title', Container::camelize($this->actionName)) |
|
56
|
7 |
|
->setAllowedTypes('title', 'string'); |
|
57
|
|
|
|
|
58
|
|
|
// displayed fields for this action |
|
59
|
|
|
$resolver |
|
60
|
7 |
|
->setDefault('fields', [ |
|
61
|
|
|
'id' => [] |
|
62
|
7 |
|
]) |
|
63
|
7 |
|
->setAllowedTypes('fields', 'array') |
|
64
|
|
|
->setNormalizer('fields', function(Options $options, $fields) { |
|
65
|
7 |
|
$normalizedFields = []; |
|
66
|
|
|
|
|
67
|
7 |
|
foreach ($fields as $name => $field) { |
|
68
|
|
|
|
|
69
|
7 |
|
if ($field === null) { |
|
70
|
|
|
$field = []; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
7 |
|
$normalizedFields[$name] = $field; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
7 |
|
return $normalizedFields; |
|
77
|
7 |
|
}) |
|
78
|
|
|
; |
|
79
|
|
|
|
|
80
|
|
|
// action permissions. By default, only admin are allowed |
|
81
|
|
|
$resolver |
|
82
|
7 |
|
->setDefault('permissions', [ |
|
83
|
|
|
'ROLE_ADMIN' |
|
84
|
7 |
|
]); |
|
85
|
|
|
|
|
86
|
|
|
// by default, all exports type are allowed |
|
87
|
|
|
$resolver |
|
88
|
7 |
|
->setDefault('export', [ |
|
89
|
7 |
|
'json', |
|
90
|
|
|
'html', |
|
91
|
|
|
'csv', |
|
92
|
|
|
'xls' |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
// entity will be retrived with this order. It should be an array of field/order mapping |
|
96
|
|
|
$resolver |
|
97
|
7 |
|
->setDefault('order', []) |
|
98
|
7 |
|
->setAllowedTypes('order', 'array'); |
|
99
|
|
|
|
|
100
|
|
|
// the action route should be a string |
|
101
|
|
|
$resolver |
|
102
|
7 |
|
->setDefault('route', '') |
|
103
|
7 |
|
->setAllowedTypes('route', 'string') |
|
104
|
|
|
->setNormalizer('route', function (Options $options, $value) { |
|
105
|
7 |
|
if (!$value) { |
|
106
|
|
|
// if no route was provided, it should be linked to an Admin |
|
107
|
7 |
|
if (!$this->admin) { |
|
108
|
|
|
throw new InvalidOptionsException('No route was provided for action : ' . $this->actionName); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// generate default route from admin |
|
112
|
|
|
return $this |
|
113
|
7 |
|
->admin |
|
114
|
7 |
|
->generateRouteName($this->actionName); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
return $value; |
|
118
|
7 |
|
}); |
|
119
|
|
|
|
|
120
|
|
|
// action parameters should be an array |
|
121
|
|
|
$resolver |
|
122
|
7 |
|
->setDefault('route_parameters', []) |
|
123
|
7 |
|
->setAllowedTypes('route_parameters', 'array'); |
|
124
|
|
|
|
|
125
|
|
|
// font awesome icons |
|
126
|
|
|
$resolver |
|
127
|
7 |
|
->setDefault('icon', '') |
|
128
|
7 |
|
->setAllowedTypes('icon', 'string'); |
|
129
|
|
|
|
|
130
|
|
|
// load strategy : determine which method should be called in the data provider |
|
131
|
|
|
$resolver |
|
132
|
7 |
|
->setDefault('load_strategy', null) |
|
133
|
7 |
|
->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_NONE) |
|
134
|
7 |
|
->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_UNIQUE) |
|
135
|
7 |
|
->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_MULTIPLE) |
|
136
|
7 |
|
->addAllowedValues('load_strategy', null) |
|
137
|
|
|
->setNormalizer('load_strategy', function (Options $options, $value) { |
|
138
|
|
|
|
|
139
|
7 |
|
if (!$value) { |
|
140
|
4 |
|
if ($this->actionName == 'create') { |
|
141
|
1 |
|
$value = AdminInterface::LOAD_STRATEGY_NONE; |
|
142
|
4 |
|
} else if ($this->actionName == 'list') { |
|
143
|
|
|
$value = AdminInterface::LOAD_STRATEGY_MULTIPLE; |
|
144
|
|
|
} else { |
|
145
|
4 |
|
$value = AdminInterface::LOAD_STRATEGY_UNIQUE; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
7 |
|
return $value; |
|
150
|
7 |
|
}); |
|
151
|
|
|
|
|
152
|
|
|
// pagination configuration |
|
153
|
|
|
$resolver |
|
154
|
7 |
|
->setDefault('pager', 'pagerfanta') |
|
155
|
7 |
|
->addAllowedValues('pager', 'pagerfanta') |
|
156
|
7 |
|
->addAllowedValues('pager', false) |
|
157
|
|
|
; |
|
158
|
|
|
|
|
159
|
|
|
// criteria used to find entity in the data provider |
|
160
|
|
|
$resolver |
|
161
|
7 |
|
->setDefault('criteria', []) |
|
162
|
|
|
->setNormalizer('criteria', function (Options $options, $value) { |
|
163
|
|
|
|
|
164
|
7 |
|
if (!$value) { |
|
165
|
|
|
$idActions = [ |
|
166
|
7 |
|
'edit', |
|
167
|
|
|
'delete' |
|
168
|
|
|
]; |
|
169
|
|
|
|
|
170
|
7 |
|
if (in_array($this->actionName, $idActions)) { |
|
171
|
|
|
$value = [ |
|
172
|
|
|
'id' |
|
173
|
1 |
|
]; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
7 |
|
return $value; |
|
178
|
7 |
|
}) |
|
179
|
|
|
; |
|
180
|
|
|
|
|
181
|
|
|
// filters |
|
182
|
7 |
|
$resolver->setDefault('filters', []); |
|
183
|
|
|
|
|
184
|
|
|
// menus |
|
185
|
|
|
$resolver |
|
186
|
7 |
|
->setDefault('menus', []) |
|
187
|
|
|
->setNormalizer('menus', function (Options $options, $menus) { |
|
188
|
|
|
// set default to an array |
|
189
|
7 |
|
if ($menus === false) { |
|
190
|
|
|
$menus = []; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
7 |
|
return $menus; |
|
194
|
7 |
|
}) |
|
195
|
|
|
; |
|
196
|
|
|
|
|
197
|
|
|
// batch actions |
|
198
|
|
|
$resolver |
|
199
|
|
|
// by default, the batch actions is desactivated |
|
200
|
7 |
|
->setDefault('batch', null) |
|
201
|
7 |
|
->setNormalizer('batch', function(Options $options, $batch) { |
|
202
|
|
|
|
|
203
|
|
|
// if batch is desactivated, no more checks should be done |
|
204
|
7 |
|
if ($batch === false) { |
|
205
|
|
|
return $batch; |
|
206
|
|
|
} |
|
207
|
|
|
// for list actions, we add a default configuration |
|
208
|
7 |
|
if ($batch === null) { |
|
209
|
|
|
// delete action should be allowed in order to be place in batch actions |
|
210
|
7 |
|
$allowedActions = array_keys($this |
|
211
|
7 |
|
->admin |
|
212
|
7 |
|
->getConfiguration() |
|
213
|
7 |
|
->getParameter('actions')); |
|
214
|
|
|
|
|
215
|
7 |
|
if ($this->actionName == 'list' && in_array('delete', $allowedActions)) { |
|
216
|
|
|
$pattern = $this |
|
217
|
|
|
->admin |
|
218
|
|
|
->getConfiguration() |
|
219
|
|
|
->getParameter('translation_pattern')['pattern']; |
|
220
|
|
|
|
|
221
|
|
|
$batch = [ |
|
222
|
|
|
'items' => [ |
|
223
|
|
|
'delete' => [ |
|
224
|
|
|
'admin' => $this->admin->getName(), |
|
225
|
|
|
'action' => 'delete', |
|
226
|
|
|
'text' => $this->getTranslationKey($pattern, 'delete', $this->admin->getName()) |
|
227
|
|
|
] |
|
228
|
|
|
] |
|
229
|
|
|
]; |
|
230
|
|
|
} else { |
|
231
|
7 |
|
return $batch; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
$resolver = new OptionsResolver(); |
|
235
|
|
|
$configuration = new MenuConfiguration(); |
|
236
|
|
|
$configuration->configureOptions($resolver); |
|
237
|
|
|
$batch = $resolver->resolve($batch); |
|
238
|
|
|
|
|
239
|
|
|
return $batch; |
|
240
|
7 |
|
}) |
|
241
|
|
|
; |
|
242
|
7 |
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|