1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Twig; |
4
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Application\Configuration\ApplicationConfiguration; |
6
|
|
|
use LAG\AdminBundle\Configuration\Factory\ConfigurationFactory; |
7
|
|
|
use LAG\AdminBundle\Field\Field; |
8
|
|
|
use LAG\AdminBundle\Field\EntityFieldInterface; |
9
|
|
|
use LAG\AdminBundle\Field\FieldInterface; |
10
|
|
|
use LAG\AdminBundle\Utils\TranslationKeyTrait; |
11
|
|
|
use Symfony\Component\DependencyInjection\Container; |
12
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
13
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
14
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
15
|
|
|
use Twig_Extension; |
16
|
|
|
use Twig_SimpleFilter; |
17
|
|
|
use Twig_SimpleFunction; |
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class AdminExtension. |
23
|
|
|
* |
24
|
|
|
* Admin utils functions for twig |
25
|
|
|
*/ |
26
|
|
|
class AdminExtension extends Twig_Extension |
27
|
|
|
{ |
28
|
|
|
use TranslationKeyTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ApplicationConfiguration |
32
|
|
|
*/ |
33
|
|
|
protected $configuration; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RouterInterface |
37
|
|
|
*/ |
38
|
|
|
protected $router; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TranslatorInterface |
42
|
|
|
*/ |
43
|
|
|
protected $translator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* AdminExtension constructor. |
47
|
|
|
* |
48
|
|
|
* @param RouterInterface $router |
49
|
|
|
* @param TranslatorInterface $translator |
50
|
|
|
* @param ConfigurationFactory $configurationFactory |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
RouterInterface $router, |
54
|
|
|
TranslatorInterface $translator, |
55
|
|
|
ConfigurationFactory $configurationFactory |
56
|
|
|
) { |
57
|
|
|
$this->router = $router; |
58
|
|
|
$this->translator = $translator; |
59
|
|
|
$this->configuration = $configurationFactory->getApplicationConfiguration(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function getFunctions() |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
new Twig_SimpleFunction('getSortColumnUrl', [$this, 'getSortColumnUrl']), |
69
|
|
|
new Twig_SimpleFunction('getSortColumnIconClass', [$this, 'getSortColumnIconClass']), |
70
|
|
|
new Twig_SimpleFunction('field', [$this, 'field']), |
71
|
|
|
new Twig_SimpleFunction('field_title', [$this, 'fieldTitle']), |
72
|
|
|
new Twig_SimpleFunction('route_parameters', [$this, 'routeParameters']), |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getFilters() |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
new Twig_SimpleFilter('camelize', [$this, 'camelize']), |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return sort column url, with existing request parameters, according to field name. |
88
|
|
|
* |
89
|
|
|
* @param Request $request |
90
|
|
|
* @param $fieldName |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
* TODO rename function |
94
|
|
|
*/ |
95
|
|
|
public function getSortColumnUrl(Request $request, $fieldName) |
96
|
|
|
{ |
97
|
|
|
// get query string to not delete existing parameters |
98
|
|
|
$queryString = $request->query->all(); |
99
|
|
|
$queryString['sort'] = $fieldName; |
100
|
|
|
|
101
|
|
|
if (array_key_exists('order', $queryString)) { |
102
|
|
|
// sort by opposite sorting than current |
103
|
|
|
$sort = $queryString['order'] == 'ASC' ? 'DESC' : 'ASC'; |
104
|
|
|
$queryString['order'] = $sort; |
105
|
|
|
} else { |
106
|
|
|
// if no order was provided, it means that list is sorted by default order (ASC), so we must sort by DESC |
107
|
|
|
$queryString['order'] = 'DESC'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this |
111
|
|
|
->router |
112
|
|
|
->generate($request->get('_route'), $queryString); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return an array of query string parameters, updated with sort field name. |
117
|
|
|
* |
118
|
|
|
* @param ParameterBagInterface $parameters |
119
|
|
|
* @param $fieldName |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getOrderQueryString(ParameterBagInterface $parameters, $fieldName) |
123
|
|
|
{ |
124
|
|
|
$parameters->set('sort', $fieldName); |
125
|
|
|
|
126
|
|
|
if ($parameters->has('order')) { |
127
|
|
|
// sort by opposite order |
128
|
|
|
$order = $parameters->get('order') == 'ASC' ? 'DESC' : 'ASC'; |
129
|
|
|
$parameters->set('order', $order); |
130
|
|
|
} else { |
131
|
|
|
// if no order was provided, it means that list is sorted by default order (ASC), so we must sort by DESC |
132
|
|
|
$parameters->set('order', 'DESC'); |
133
|
|
|
} |
134
|
|
|
return $parameters->all(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param null $order |
139
|
|
|
* @param $fieldName |
140
|
|
|
* @param $sort |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getSortColumnIconClass($order = null, $fieldName, $sort) |
144
|
|
|
{ |
145
|
|
|
// when no order is provided, no icon should be displayed |
146
|
|
|
$class = ''; |
147
|
|
|
|
148
|
|
|
if ($fieldName == $sort) { |
149
|
|
|
if ($order == 'ASC') { |
150
|
|
|
$class = 'fa fa-sort-asc'; |
151
|
|
|
} elseif ($order == 'DESC') { |
152
|
|
|
$class = 'fa fa-sort-desc'; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $class; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Render a field of an entity. |
161
|
|
|
* |
162
|
|
|
* @param FieldInterface $field |
163
|
|
|
* @param $entity |
164
|
|
|
* |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public function field(FieldInterface $field, $entity) |
168
|
|
|
{ |
169
|
|
|
$accessor = PropertyAccess::createPropertyAccessorBuilder() |
170
|
|
|
->enableMagicCall() |
171
|
|
|
->getPropertyAccessor(); |
172
|
|
|
$value = null; |
173
|
|
|
// if name starts with a underscore, it is a custom field, not mapped to the entity |
174
|
|
View Code Duplication |
if (substr($field->getName(), 0, 1) != '_') { |
175
|
|
|
// get raw value from object |
176
|
|
|
$value = $accessor->getValue($entity, $field->getName()); |
177
|
|
|
} |
178
|
|
|
if ($field instanceof EntityFieldInterface) { |
179
|
|
|
$field->setEntity($entity); |
180
|
|
|
} |
181
|
|
|
$render = $field->render($value); |
182
|
|
|
|
183
|
|
|
return $render; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param $fieldName |
188
|
|
|
* @param null $adminName |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function fieldTitle($fieldName, $adminName = null) |
192
|
|
|
{ |
193
|
|
|
if ($this->configuration->getParameter('translation')['enabled']) { |
194
|
|
|
$title = $this |
195
|
|
|
->translator |
196
|
|
|
->trans($this->getTranslationKey($this->configuration, $fieldName, $adminName)); |
197
|
|
|
} else { |
198
|
|
|
$title = $this->camelize($fieldName); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $title; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param array $parameters |
206
|
|
|
* @param $entity |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function routeParameters(array $parameters, $entity) |
210
|
|
|
{ |
211
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
212
|
|
|
$routeParameters = []; |
213
|
|
|
|
214
|
|
|
foreach ($parameters as $parameterName => $fieldName) { |
215
|
|
|
if (is_array($fieldName) && !count($fieldName)) { |
216
|
|
|
$fieldName = $parameterName; |
217
|
|
|
} |
218
|
|
|
$routeParameters[$parameterName] = $accessor->getValue($entity, $fieldName); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $routeParameters; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Camelize a string (using Container camelize method) |
226
|
|
|
* |
227
|
|
|
* @param $string |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function camelize($string) |
231
|
|
|
{ |
232
|
|
|
return Container::camelize($string); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Returns the name of the extension. |
237
|
|
|
* |
238
|
|
|
* @return string The extension name |
239
|
|
|
*/ |
240
|
|
|
public function getName() |
241
|
|
|
{ |
242
|
|
|
return 'lag.admin'; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|