|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of cwdFancyGridBundle |
|
4
|
|
|
* |
|
5
|
|
|
* (c)2017 cwd.at GmbH <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Cwd\FancyGridBundle\Column; |
|
13
|
|
|
|
|
14
|
|
|
use bar\baz\source_with_namespace; |
|
15
|
|
|
use Cwd\FancyGridBundle\Grid\Exception\InvalidArgumentException; |
|
16
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
17
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
|
18
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class AbstractColumn |
|
22
|
|
|
* @package Cwd\FancyGridBundle\Column |
|
23
|
|
|
* @author Ludwig Ruderstaler <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class AbstractColumn implements ColumnInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $name; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
protected $field; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $options = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var TranslatorInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $translator; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* AbstractColumn constructor. |
|
49
|
|
|
* @param string $name |
|
50
|
|
|
* @param string $field |
|
51
|
|
|
* @param array $options |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($name, $field, array $options = []) |
|
54
|
|
|
{ |
|
55
|
|
|
$resolver = new OptionsResolver(); |
|
56
|
|
|
$this->configureOptions($resolver); |
|
57
|
|
|
|
|
58
|
|
|
$this->options = $resolver->resolve($options); |
|
59
|
|
|
$this->name = $name; |
|
60
|
|
|
$this->field = $field; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getName() : ?string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->name; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getField() : ?string |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->field; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param mixed $value |
|
81
|
|
|
* @param mixed $object |
|
82
|
|
|
* @param mixed $primary |
|
83
|
|
|
* @param \Twig_Environment $twig |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
public function render($value, $object, $primary, \Twig_Environment $twig) |
|
88
|
|
|
{ |
|
89
|
|
|
/** dont use twig if no template is provided */ |
|
90
|
|
|
if (null === $this->getOption('template')) { |
|
91
|
|
|
return $value; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->renderTemplate( |
|
95
|
|
|
$twig, |
|
96
|
|
|
$this->getOption('template'), |
|
97
|
|
|
[ |
|
98
|
|
|
'value' => $value, |
|
99
|
|
|
'object' => $object, |
|
100
|
|
|
'primary' => $primary, |
|
101
|
|
|
] |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param \Twig_Environment $twig |
|
107
|
|
|
* @param string $template |
|
108
|
|
|
* @param array $options |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function renderTemplate(\Twig_Environment $twig, $template, $options) |
|
113
|
|
|
{ |
|
114
|
|
|
$options = array_merge($options, $this->getOptions()); |
|
115
|
|
|
|
|
116
|
|
|
return $twig->render($template, $options); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* set defaults options |
|
121
|
|
|
* @param OptionsResolver $resolver |
|
122
|
|
|
*/ |
|
123
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
124
|
|
|
{ |
|
125
|
|
|
$resolver->setDefaults(array( |
|
126
|
|
|
'align' => 'left', |
|
127
|
|
|
'cellAlign' => 'left', |
|
128
|
|
|
'cellTip' => null, |
|
129
|
|
|
'cls' => null, |
|
130
|
|
|
'draggable' => null, |
|
131
|
|
|
'editable' => null, |
|
132
|
|
|
'ellipsis' => true, |
|
133
|
|
|
'flex' => 1, |
|
134
|
|
|
'hidden' => false, |
|
135
|
|
|
'index' => null, |
|
136
|
|
|
'lockable' => null, |
|
137
|
|
|
'locked' => null, |
|
138
|
|
|
'maxWidth' => null, |
|
139
|
|
|
'menu' => null, |
|
140
|
|
|
'minWidth' => null, |
|
141
|
|
|
'render' => null, |
|
142
|
|
|
'resizable' => null, |
|
143
|
|
|
'rightLocked' => null, |
|
144
|
|
|
'sortable' => true, |
|
145
|
|
|
'title' => null, |
|
146
|
|
|
'type' => null, |
|
147
|
|
|
'vtype' => null, |
|
148
|
|
|
'width' => null, |
|
149
|
|
|
'searchable' => true, |
|
150
|
|
|
'filter' => [ |
|
151
|
|
|
'header' => true, |
|
152
|
|
|
'headerNote' => true, |
|
153
|
|
|
], |
|
154
|
|
|
|
|
155
|
|
|
'translation_domain' => null, |
|
156
|
|
|
'translatable' => false, |
|
157
|
|
|
'attr' => array(), |
|
158
|
|
|
'template' => null, |
|
159
|
|
|
|
|
160
|
|
|
// Legacy |
|
161
|
|
|
'identifier' => null, |
|
162
|
|
|
'label' => null, |
|
163
|
|
|
'visible' => null, |
|
164
|
|
|
|
|
165
|
|
|
)); |
|
166
|
|
|
|
|
167
|
|
|
$resolver->setAllowedTypes('attr', 'array'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
|
|
public function buildColumnOptions() : array |
|
174
|
|
|
{ |
|
175
|
|
|
$printOptions = [ |
|
176
|
|
|
// Fancy grid doesnt like . in index name |
|
177
|
|
|
'index' => str_replace('.', '_', $this->getName()), |
|
178
|
|
|
]; |
|
179
|
|
|
|
|
180
|
|
|
// Legacy Mapping |
|
181
|
|
|
if ($this->getOption('visible')) { |
|
182
|
|
|
$printOptions['hidden'] = $this->getOption('visible'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if ($this->getOption('label')) { |
|
186
|
|
|
$printOptions['title'] = $this->translator->trans($this->getOption('label'), [], $this->getOption('translation_domain')); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$options = $this->options; |
|
190
|
|
|
|
|
191
|
|
|
foreach ($options as $key => $value) { |
|
192
|
|
|
// Ignore this options they are used differently |
|
193
|
|
|
if (in_array($key, ['attr', 'template', 'header_align','label', 'translation_domain', 'translatable', 'visible', 'identifier', 'index', 'class', 'em', 'query_builder', 'choice_loader'])) { |
|
194
|
|
|
continue; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// if null we dont need to print the option |
|
198
|
|
|
if (null === $value) { |
|
199
|
|
|
continue; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$printOptions[$key] = $value; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $printOptions; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param mixed $object |
|
210
|
|
|
* @param string $field |
|
211
|
|
|
* @param string $primary |
|
212
|
|
|
* @param PropertyAccessor $accessor |
|
213
|
|
|
* |
|
214
|
|
|
* @return mixed |
|
215
|
|
|
*/ |
|
216
|
|
|
public function getValue($object, $field, $primary, $accessor) |
|
217
|
|
|
{ |
|
218
|
|
|
/** Special case handling for e.g. count() */ |
|
219
|
|
|
if (is_array($object) && isset($object[$field])) { |
|
220
|
|
|
return $object[$field]; |
|
221
|
|
|
} elseif (is_array($object)) { |
|
222
|
|
|
$object = $object[0]; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
if (!$accessor->isReadable($object, $field)) { |
|
226
|
|
|
// if not found, try to strip alias. |
|
227
|
|
|
if (strstr($field, '.')) { |
|
228
|
|
|
$field = substr($field, strpos($field, '.')+1); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
if (!$accessor->isReadable($object, $field)) { |
|
233
|
|
|
return null; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return $accessor->getValue($object, $field); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param string $name |
|
241
|
|
|
* @return bool |
|
242
|
|
|
*/ |
|
243
|
|
|
public function hasOption($name) : bool |
|
244
|
|
|
{ |
|
245
|
|
|
return array_key_exists($name, $this->options); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param string $name |
|
250
|
|
|
* @param string|null $default |
|
251
|
|
|
* @return misc |
|
252
|
|
|
*/ |
|
253
|
|
|
public function getOption(string $name, $default = null) |
|
254
|
|
|
{ |
|
255
|
|
|
return array_key_exists($name, $this->options) ? $this->options[$name] : $default; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @return array |
|
260
|
|
|
*/ |
|
261
|
|
|
public function getOptions() : array |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->options; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @return TranslatorInterface |
|
268
|
|
|
*/ |
|
269
|
|
|
public function getTranslator() : TranslatorInterface |
|
270
|
|
|
{ |
|
271
|
|
|
return $this->translator; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @param TranslatorInterface $translator |
|
276
|
|
|
*/ |
|
277
|
|
|
public function setTranslator(TranslatorInterface $translator) |
|
278
|
|
|
{ |
|
279
|
|
|
$this->translator = $translator; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
|