1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Admin\Factory; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use LAG\AdminBundle\Admin\Action; |
7
|
|
|
use LAG\AdminBundle\Admin\Admin; |
8
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
9
|
|
|
use LAG\AdminBundle\Admin\Configuration\ActionConfiguration; |
10
|
|
|
use LAG\AdminBundle\Admin\Configuration\ApplicationConfiguration; |
11
|
|
|
use LAG\AdminBundle\Field\Factory\FieldFactory; |
12
|
|
|
use Symfony\Component\OptionsResolver\Options; |
13
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
14
|
|
|
|
15
|
|
|
class ActionFactory |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var FieldFactory |
19
|
|
|
*/ |
20
|
|
|
protected $fieldFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var FilterFactory |
24
|
|
|
*/ |
25
|
|
|
protected $filterFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ApplicationConfiguration |
29
|
|
|
*/ |
30
|
|
|
protected $configuration; |
31
|
|
|
|
32
|
1 |
|
public function __construct( |
33
|
|
|
FieldFactory $fieldFactory, |
34
|
|
|
FilterFactory $filterFactory, |
35
|
|
|
ApplicationConfiguration $configuration |
36
|
|
|
) |
37
|
|
|
{ |
38
|
1 |
|
$this->fieldFactory = $fieldFactory; |
39
|
1 |
|
$this->filterFactory = $filterFactory; |
40
|
1 |
|
$this->configuration = $configuration; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create an Action from configuration values. |
45
|
|
|
* |
46
|
|
|
* @param string $actionName |
47
|
|
|
* @param array $actionConfiguration |
48
|
|
|
* @param AdminInterface $admin |
49
|
|
|
* |
50
|
|
|
* @return Action |
51
|
|
|
*/ |
52
|
1 |
|
public function create($actionName, array $actionConfiguration, AdminInterface $admin) |
53
|
|
|
{ |
54
|
|
|
// resolving default options |
55
|
1 |
|
$resolver = new OptionsResolver(); |
56
|
1 |
|
$this->configureOptionsResolver($resolver, $actionName, $admin); |
57
|
1 |
|
$actionConfiguration = $resolver->resolve($actionConfiguration); |
58
|
|
|
|
59
|
|
|
// creating action object from configuration |
60
|
1 |
|
$action = $this->createActionFromConfiguration($actionConfiguration, $actionName); |
61
|
|
|
|
62
|
|
|
// creating actions linked to current action |
63
|
1 |
|
foreach ($actionConfiguration['actions'] as $customActionName => $customActionConfiguration) { |
64
|
|
|
// resolve configuration |
65
|
1 |
|
$customActionConfiguration = $resolver->resolve($customActionConfiguration); |
66
|
|
|
// create action |
67
|
1 |
|
$customAction = $this->createActionFromConfiguration($customActionConfiguration, $customActionName); |
68
|
|
|
// add to the main action |
69
|
1 |
|
$action->addAction($customAction); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
// adding fields items to actions |
73
|
1 |
|
foreach ($actionConfiguration['fields'] as $fieldName => $fieldConfiguration) { |
74
|
1 |
|
$field = $this |
75
|
|
|
->fieldFactory |
76
|
1 |
|
->create($fieldName, $fieldConfiguration); |
77
|
1 |
|
$action->addField($field); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
// adding filters to the action |
81
|
1 |
|
foreach ($actionConfiguration['filters'] as $fieldName => $filterConfiguration) { |
82
|
1 |
|
$filter = $this |
83
|
|
|
->filterFactory |
84
|
1 |
|
->create($fieldName, $filterConfiguration); |
85
|
1 |
|
$action->addFilter($filter); |
86
|
1 |
|
} |
87
|
1 |
|
return $action; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create an action and its configuration object from configuration values |
92
|
|
|
* |
93
|
|
|
* @param array $actionConfiguration |
94
|
|
|
* @param $actionName |
95
|
|
|
* @return Action |
96
|
|
|
*/ |
97
|
1 |
|
protected function createActionFromConfiguration(array $actionConfiguration, $actionName) |
98
|
|
|
{ |
99
|
1 |
|
$configuration = new ActionConfiguration($actionConfiguration); |
100
|
1 |
|
$action = new Action($actionName, $actionConfiguration, $configuration); |
101
|
|
|
|
102
|
1 |
|
return $action; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return action configuration resolver |
107
|
|
|
* |
108
|
|
|
* @param OptionsResolver $resolver |
109
|
|
|
* @param $actionName |
110
|
|
|
* @param AdminInterface|null $admin |
111
|
|
|
*/ |
112
|
1 |
|
protected function configureOptionsResolver(OptionsResolver $resolver, $actionName, AdminInterface $admin = null) |
113
|
|
|
{ |
114
|
1 |
|
$defaultCriteria = []; |
115
|
1 |
|
$defaultLoadStrategy = Admin::LOAD_STRATEGY_UNIQUE; |
116
|
|
|
|
117
|
1 |
|
if ($actionName == 'edit') { |
118
|
|
|
$defaultCriteria = [ |
119
|
|
|
'id' |
120
|
1 |
|
]; |
121
|
1 |
|
} else if ($actionName == 'delete') { |
122
|
|
|
$defaultCriteria = [ |
123
|
|
|
'id' |
124
|
1 |
|
]; |
125
|
1 |
|
} else if ($actionName == 'create') { |
126
|
1 |
|
$defaultLoadStrategy = Admin::LOAD_STRATEGY_NONE; |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
$resolver |
130
|
1 |
|
->setDefaults([ |
131
|
1 |
|
'title' => null, |
132
|
|
|
'fields' => [ |
133
|
1 |
|
'id' => [], |
134
|
1 |
|
], |
135
|
|
|
'permissions' => [ |
136
|
|
|
'ROLE_ADMIN' |
137
|
1 |
|
], |
138
|
|
|
'export' => [ |
139
|
1 |
|
'json', |
140
|
1 |
|
'html', |
141
|
1 |
|
'csv', |
142
|
|
|
'xls' |
143
|
1 |
|
], |
144
|
1 |
|
'order' => [], |
145
|
1 |
|
'actions' => [], |
146
|
1 |
|
'submit_actions' => [], |
147
|
1 |
|
'target' => '_self', |
148
|
1 |
|
'route' => '', |
149
|
1 |
|
'parameters' => [], |
150
|
1 |
|
'icon' => null, |
151
|
1 |
|
'filters' => [], |
152
|
1 |
|
'batch' => [], |
153
|
1 |
|
'load_strategy' => $defaultLoadStrategy, |
154
|
1 |
|
'pager' => 'pagerfanta', |
155
|
|
|
'criteria' => $defaultCriteria |
156
|
1 |
|
]) |
157
|
1 |
|
->setAllowedValues('pager', [ |
158
|
1 |
|
null, |
159
|
1 |
|
'pagerfanta', |
160
|
1 |
|
]) |
161
|
1 |
|
->setAllowedValues('load_strategy', [ |
162
|
1 |
|
Admin::LOAD_STRATEGY_NONE, |
163
|
1 |
|
Admin::LOAD_STRATEGY_UNIQUE, |
164
|
1 |
|
Admin::LOAD_STRATEGY_MULTIPLE, |
165
|
1 |
|
]) |
166
|
1 |
|
->setAllowedTypes('actions', 'array') |
167
|
|
|
->setNormalizer('route', function (Options $options, $value) use ($admin, $actionName) { |
168
|
1 |
|
if (!$value) { |
169
|
|
|
// if no route was provided, it should be linked to an Admin |
170
|
1 |
|
if (!$admin) { |
171
|
|
|
throw new Exception('No route was provided for action : ' . $actionName); |
172
|
|
|
} |
173
|
|
|
return $admin |
174
|
1 |
|
->generateRouteName($actionName); |
175
|
|
|
} |
176
|
1 |
|
return $value; |
177
|
1 |
|
}) |
178
|
|
|
->setNormalizer('title', function (Options $options, $value) use ($admin, $actionName) { |
179
|
1 |
|
if (!$value) { |
180
|
1 |
|
$adminKey = ''; |
181
|
|
|
// if an Admin is linked to this action, we use its name in translation key |
182
|
1 |
|
if ($admin) { |
183
|
1 |
|
$adminKey = $admin->getName(); |
184
|
1 |
|
} |
185
|
1 |
|
return $this->configuration->getTranslationKey($actionName, $adminKey); |
186
|
|
|
} |
187
|
1 |
|
return $value; |
188
|
1 |
|
}) |
189
|
1 |
|
->setNormalizer('batch', function (Options $options, $value) use ($admin, $actionName) { |
190
|
1 |
|
if ($value) { |
191
|
1 |
|
if (!is_array($value)) { |
192
|
1 |
|
$value = [$value]; |
193
|
1 |
|
} |
194
|
1 |
|
foreach ($value as $key => $title) { |
195
|
1 |
|
if (!$title) { |
196
|
1 |
|
$adminKey = ''; |
197
|
|
|
// if an Admin is linked to this action, we use its name in translation key |
198
|
1 |
|
if ($admin) { |
199
|
1 |
|
$adminKey = $admin->getName(); |
200
|
1 |
|
} |
201
|
1 |
|
$value[$key] = $this->configuration->getTranslationKey('batch.' . $key, $adminKey); |
202
|
1 |
|
} |
203
|
1 |
|
} |
204
|
1 |
|
} |
205
|
1 |
|
return $value; |
206
|
1 |
|
}) |
207
|
|
|
; |
208
|
1 |
|
} |
209
|
|
|
} |
210
|
|
|
|