|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Action\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
6
|
|
|
use LAG\AdminBundle\Configuration\Configuration; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11
|
|
|
|
|
12
|
|
|
class ActionConfiguration extends Configuration |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Related Action name. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $actionName; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Related Admin (optional) |
|
23
|
|
|
* |
|
24
|
|
|
* @var AdminInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $admin = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* ActionConfiguration constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param $actionName |
|
32
|
|
|
* @param AdminInterface $admin |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function __construct($actionName, AdminInterface $admin) |
|
35
|
|
|
{ |
|
36
|
7 |
|
parent::__construct(); |
|
37
|
|
|
|
|
38
|
7 |
|
$this->actionName = $actionName; |
|
39
|
7 |
|
$this->admin = $admin; |
|
40
|
7 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Define allowed parameters and values for this configuration, using optionsResolver component. |
|
44
|
|
|
* |
|
45
|
|
|
* @param OptionsResolver $resolver |
|
46
|
|
|
*/ |
|
47
|
7 |
|
public function configureOptions(OptionsResolver $resolver) |
|
48
|
|
|
{ |
|
49
|
|
|
// action title, default to action's name |
|
50
|
|
|
$resolver |
|
51
|
7 |
|
->setDefault('title', Container::camelize($this->actionName)) |
|
52
|
7 |
|
->setAllowedTypes('title', 'string'); |
|
53
|
|
|
|
|
54
|
|
|
// displayed fields for this action |
|
55
|
|
|
$resolver |
|
56
|
7 |
|
->setDefault('fields', [ |
|
57
|
7 |
|
'id' => [] |
|
58
|
7 |
|
]) |
|
59
|
7 |
|
->setAllowedTypes('fields', 'array') |
|
60
|
|
|
->setNormalizer('fields', function(Options $options, $fields) { |
|
61
|
7 |
|
$normalizedFields = []; |
|
62
|
|
|
|
|
63
|
7 |
|
foreach ($fields as $name => $field) { |
|
64
|
|
|
|
|
65
|
7 |
|
if ($field === null) { |
|
66
|
|
|
$field = []; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
7 |
|
$normalizedFields[$name] = $field; |
|
70
|
7 |
|
} |
|
71
|
|
|
|
|
72
|
7 |
|
return $normalizedFields; |
|
73
|
7 |
|
}) |
|
74
|
|
|
; |
|
75
|
|
|
|
|
76
|
|
|
// action permissions. By default, only admin are allowed |
|
77
|
|
|
$resolver |
|
78
|
7 |
|
->setDefault('permissions', [ |
|
79
|
|
|
'ROLE_ADMIN' |
|
80
|
7 |
|
]); |
|
81
|
|
|
|
|
82
|
|
|
// by default, all exports type are allowed |
|
83
|
|
|
$resolver |
|
84
|
7 |
|
->setDefault('export', [ |
|
85
|
7 |
|
'json', |
|
86
|
7 |
|
'html', |
|
87
|
7 |
|
'csv', |
|
88
|
|
|
'xls' |
|
89
|
7 |
|
]); |
|
90
|
|
|
|
|
91
|
|
|
// entity will be retrived with this order. It should be an array of field/order mapping |
|
92
|
|
|
$resolver |
|
93
|
7 |
|
->setDefault('order', []) |
|
94
|
7 |
|
->setAllowedTypes('order', 'array'); |
|
95
|
|
|
|
|
96
|
|
|
// the action route should be a string |
|
97
|
|
|
$resolver |
|
98
|
7 |
|
->setDefault('route', '') |
|
99
|
7 |
|
->setAllowedTypes('route', 'string') |
|
100
|
|
|
->setNormalizer('route', function (Options $options, $value) { |
|
101
|
7 |
|
if (!$value) { |
|
102
|
|
|
// if no route was provided, it should be linked to an Admin |
|
103
|
7 |
|
if (!$this->admin) { |
|
104
|
|
|
throw new InvalidOptionsException('No route was provided for action : ' . $this->actionName); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// generate default route from admin |
|
108
|
7 |
|
return $this |
|
109
|
|
|
->admin |
|
110
|
7 |
|
->generateRouteName($this->actionName); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
return $value; |
|
114
|
7 |
|
}); |
|
115
|
|
|
|
|
116
|
|
|
// action parameters should be an array |
|
117
|
|
|
$resolver |
|
118
|
7 |
|
->setDefault('route_parameters', []) |
|
119
|
7 |
|
->setAllowedTypes('route_parameters', 'array'); |
|
120
|
|
|
|
|
121
|
|
|
// font awesome icons |
|
122
|
|
|
$resolver |
|
123
|
7 |
|
->setDefault('icon', '') |
|
124
|
7 |
|
->setAllowedTypes('icon', 'string'); |
|
125
|
|
|
|
|
126
|
|
|
// load strategy : determine which method should be called in the data provider |
|
127
|
|
|
$resolver |
|
128
|
7 |
|
->setDefault('load_strategy', AdminInterface::LOAD_STRATEGY_UNIQUE) |
|
129
|
7 |
|
->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_NONE) |
|
130
|
7 |
|
->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_UNIQUE) |
|
131
|
7 |
|
->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_MULTIPLE) |
|
132
|
|
|
->setNormalizer('load_strategy', function (Options $options, $value) { |
|
133
|
|
|
|
|
134
|
7 |
|
if (!$value) { |
|
135
|
|
|
if ($this->actionName == 'create') { |
|
136
|
|
|
$value = AdminInterface::LOAD_STRATEGY_NONE; |
|
137
|
|
|
} else if ($this->actionName == 'list') { |
|
138
|
|
|
$value = AdminInterface::LOAD_STRATEGY_MULTIPLE; |
|
139
|
|
|
} else { |
|
140
|
|
|
$value = AdminInterface::LOAD_STRATEGY_UNIQUE; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
7 |
|
return $value; |
|
145
|
7 |
|
}); |
|
146
|
|
|
|
|
147
|
|
|
// pagination configuration |
|
148
|
|
|
$resolver |
|
149
|
7 |
|
->setDefault('pager', 'pagerfanta') |
|
150
|
7 |
|
->addAllowedValues('pager', 'pagerfanta') |
|
151
|
7 |
|
->addAllowedValues('pager', false) |
|
152
|
|
|
; |
|
153
|
|
|
|
|
154
|
|
|
// criteria used to find entity in the data provider |
|
155
|
|
|
$resolver |
|
156
|
7 |
|
->setDefault('criteria', []) |
|
157
|
7 |
|
->setNormalizer('criteria', function (Options $options, $value) { |
|
158
|
|
|
|
|
159
|
7 |
|
if (!$value) { |
|
160
|
|
|
$idActions = [ |
|
161
|
7 |
|
'edit', |
|
162
|
|
|
'delete' |
|
163
|
7 |
|
]; |
|
164
|
|
|
|
|
165
|
7 |
|
if (in_array($this->actionName, $idActions)) { |
|
166
|
|
|
$value = [ |
|
167
|
|
|
'id' |
|
168
|
1 |
|
]; |
|
169
|
1 |
|
} |
|
170
|
7 |
|
} |
|
171
|
|
|
|
|
172
|
7 |
|
return $value; |
|
173
|
7 |
|
}) |
|
174
|
|
|
; |
|
175
|
|
|
|
|
176
|
|
|
// filters |
|
177
|
7 |
|
$resolver->setDefault('filters', []); |
|
178
|
|
|
|
|
179
|
|
|
// menus |
|
180
|
7 |
|
$resolver->setDefault('menu', false); |
|
181
|
|
|
// $resolver->setNormalizer('menu', function(Options $options, $value) { |
|
|
|
|
|
|
182
|
|
|
// |
|
183
|
|
|
// // if menu is enabled, either user provided an existing action name or a route, or list action is choosen |
|
184
|
|
|
// // by default, for each item |
|
185
|
|
|
// if ($value && is_array($value)) { |
|
186
|
|
|
// |
|
187
|
|
|
// foreach ($value as $menu) { |
|
188
|
|
|
// if (!array_key_exists('route', $menu)) { |
|
189
|
|
|
// |
|
190
|
|
|
// if (!array_key_exists('action', $menu)) |
|
191
|
|
|
// |
|
192
|
|
|
// } |
|
193
|
|
|
// } |
|
194
|
|
|
// |
|
195
|
|
|
// } |
|
196
|
|
|
// }); |
|
197
|
7 |
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.