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
|
|
|
'index' => $this->getName(), |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
// Legacy Mapping |
180
|
|
|
if ($this->getOption('visible')) { |
181
|
|
|
$printOptions['hidden'] = $this->getOption('visible'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ($this->getOption('label')) { |
185
|
|
|
$printOptions['title'] = $this->translator->trans($this->getOption('label'), [], $this->getOption('translation_domain')); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$options = $this->options; |
189
|
|
|
|
190
|
|
|
foreach ($options as $key => $value) { |
191
|
|
|
// Ignore this options they are used differently |
192
|
|
|
if (in_array($key, ['attr', 'template', 'header_align','label', 'translation_domain', 'translatable', 'visible', 'identifier', 'index'])) { |
193
|
|
|
continue; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// if null we dont need to print the option |
197
|
|
|
if (null === $value) { |
198
|
|
|
continue; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$printOptions[$key] = $value; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $printOptions; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param mixed $object |
209
|
|
|
* @param string $field |
210
|
|
|
* @param string $primary |
211
|
|
|
* @param PropertyAccessor $accessor |
212
|
|
|
* |
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
|
|
public function getValue($object, $field, $primary, $accessor) |
216
|
|
|
{ |
217
|
|
|
/** Special case handling for e.g. count() */ |
218
|
|
|
if (is_array($object) && isset($object[$field])) { |
219
|
|
|
return $object[$field]; |
220
|
|
|
} elseif (is_array($object)) { |
221
|
|
|
$object = $object[0]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if (!$accessor->isReadable($object, $field)) { |
225
|
|
|
// if not found, try to strip alias. |
226
|
|
|
if (strstr($field, '.')) { |
227
|
|
|
$field = substr($field, strpos($field, '.')+1); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if (!$accessor->isReadable($object, $field)) { |
232
|
|
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $accessor->getValue($object, $field); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $name |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
|
|
public function hasOption($name) : bool |
243
|
|
|
{ |
244
|
|
|
return array_key_exists($name, $this->options); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param string $name |
249
|
|
|
* @param string|null $default |
250
|
|
|
* @return misc |
251
|
|
|
*/ |
252
|
|
|
public function getOption(string $name, $default = null) |
253
|
|
|
{ |
254
|
|
|
return array_key_exists($name, $this->options) ? $this->options[$name] : $default; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function getOptions() : array |
261
|
|
|
{ |
262
|
|
|
return $this->options; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return TranslatorInterface |
267
|
|
|
*/ |
268
|
|
|
public function getTranslator() : TranslatorInterface |
269
|
|
|
{ |
270
|
|
|
return $this->translator; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param TranslatorInterface $translator |
275
|
|
|
*/ |
276
|
|
|
public function setTranslator(TranslatorInterface $translator) |
277
|
|
|
{ |
278
|
|
|
$this->translator = $translator; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
} |
283
|
|
|
|