|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Field\Field; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Field\AbstractField; |
|
6
|
|
|
use LAG\AdminBundle\Field\EntityAwareInterface; |
|
7
|
|
|
use LAG\AdminBundle\Field\TwigAwareInterface; |
|
8
|
|
|
use LogicException; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
12
|
|
|
use Twig_Environment; |
|
13
|
|
|
|
|
14
|
|
|
class ActionCollection extends AbstractField implements TwigAwareInterface, EntityAwareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Twig_Environment |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $twig; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var mixed |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $entity; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Render value of the field. |
|
28
|
|
|
* |
|
29
|
|
|
* @param mixed $value Value to render |
|
30
|
|
|
* |
|
31
|
|
|
* @return mixed |
|
32
|
|
|
*/ |
|
33
|
|
|
public function render($value) |
|
34
|
|
|
{ |
|
35
|
|
|
$actions = $this |
|
36
|
|
|
->options |
|
37
|
|
|
->get('actions') |
|
38
|
|
|
; |
|
39
|
|
|
$normalizedActions = []; |
|
40
|
|
|
|
|
41
|
|
|
foreach ($actions as $action => $options) { |
|
42
|
|
|
$this->normalizeRoute($options, $action); |
|
43
|
|
|
$normalizedActions[$action] = $options; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
return $this |
|
48
|
|
|
->twig |
|
49
|
|
|
->render('@LAGAdmin/Field/actionCollection.html.twig', [ |
|
50
|
|
|
'actions' => $normalizedActions, |
|
51
|
|
|
]) |
|
52
|
|
|
; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
56
|
|
|
{ |
|
57
|
|
|
$resolver |
|
58
|
|
|
->setRequired('actions') |
|
59
|
|
|
->setAllowedTypes('actions', 'array') |
|
60
|
|
|
->setNormalizer('actions', function (Options $options, $actions) { |
|
61
|
|
|
|
|
62
|
|
|
$normalizedActions = []; |
|
63
|
|
|
|
|
64
|
|
|
foreach ($actions as $action => $options) { |
|
65
|
|
|
$this->normalizeIcon($options, $action); |
|
66
|
|
|
$this->normalizeClass($options, $action); |
|
67
|
|
|
$this->normalizeText($options, $action); |
|
68
|
|
|
|
|
69
|
|
|
$normalizedActions[$action] = $options; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $normalizedActions; |
|
73
|
|
|
}) |
|
74
|
|
|
; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Return field type. |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getType() |
|
83
|
|
|
{ |
|
84
|
|
|
return AbstractField::TYPE_ACTION_COLLECTION; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Defines entity for field. |
|
89
|
|
|
* |
|
90
|
|
|
* @param $entity |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setEntity($entity) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->entity = $entity; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Define twig environment. |
|
101
|
|
|
* |
|
102
|
|
|
* @param Twig_Environment $twig |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setTwig(Twig_Environment $twig) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->twig = $twig; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function normalizeIcon(array &$options, $action) |
|
112
|
|
|
{ |
|
113
|
|
|
$iconMapping = [ |
|
114
|
|
|
'delete' => 'fa fa-times', |
|
115
|
|
|
'edit' => 'fa fa-pencil', |
|
116
|
|
|
]; |
|
117
|
|
|
|
|
118
|
|
|
if (!array_key_exists('icon', $options)) { |
|
119
|
|
|
$options['icon'] = ''; |
|
120
|
|
|
|
|
121
|
|
|
if (array_key_exists($action, $iconMapping)) { |
|
122
|
|
|
$options['icon'] = $iconMapping[$action]; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
protected function normalizeClass(array &$options, $action) |
|
128
|
|
|
{ |
|
129
|
|
|
$classMapping = [ |
|
130
|
|
|
'delete' => 'btn btn-danger', |
|
131
|
|
|
'edit' => 'btn btn-default', |
|
132
|
|
|
]; |
|
133
|
|
|
|
|
134
|
|
|
if (!array_key_exists('class', $options)) { |
|
135
|
|
|
$options['class'] = ''; |
|
136
|
|
|
|
|
137
|
|
|
if (array_key_exists($action, $classMapping)) { |
|
138
|
|
|
$options['class'] = $classMapping[$action]; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
$normalizedActions[$action] = $options; |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function normalizeRoute(array &$options, $action) |
|
145
|
|
|
{ |
|
146
|
|
|
if (!array_key_exists('route', $options)) { |
|
147
|
|
|
throw new LogicException( |
|
148
|
|
|
'You should provide a route for the Action "'.$action.'" in ActionCollection Field' |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if (array_key_exists('parameters', $options)) { |
|
153
|
|
|
|
|
154
|
|
|
if (!is_array($options['parameters'])) { |
|
155
|
|
|
throw new LogicException( |
|
156
|
|
|
'You should provide an array of parameters for route configuration in action "'.$action.'"' |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
|
160
|
|
|
$normalizedParameters = []; |
|
161
|
|
|
|
|
162
|
|
|
foreach ($options['parameters'] as $parameter => $parameterOptions) { |
|
163
|
|
|
$normalizedParameters[$parameter] = $accessor->getValue($this->entity, $parameter); |
|
164
|
|
|
} |
|
165
|
|
|
$options['parameters'] = $normalizedParameters; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
protected function normalizeText(array &$options, $action) |
|
170
|
|
|
{ |
|
171
|
|
|
if (!array_key_exists('text', $options)) { |
|
172
|
|
|
$options['text'] = ucfirst($action); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.