1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZfcDatagrid\Renderer\JqGrid\View\Helper; |
4
|
|
|
|
5
|
|
|
use Zend\View\Helper\AbstractHelper; |
6
|
|
|
use ZfcDatagrid\Column; |
7
|
|
|
use ZfcDatagrid\Column\Type; |
8
|
|
|
use ZfcDatagrid\Filter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* View Helper. |
12
|
|
|
*/ |
13
|
|
|
class Columns extends AbstractHelper |
14
|
|
|
{ |
15
|
|
|
/** @var \Zend\I18n\Translator\Translator|null|false */ |
16
|
|
|
private $translator; |
17
|
|
|
|
18
|
|
|
const STYLE_BOLD = 'cellvalue = \'<span style="font-weight: bold;">\' + cellvalue + \'</span>\';'; |
19
|
|
|
|
20
|
|
|
const STYLE_ITALIC = 'cellvalue = \'<span style="font-style: italic;">\' + cellvalue + \'</span>\';'; |
21
|
|
|
|
22
|
|
|
const STYLE_STRIKETHROUGH = 'cellvalue = \'<span style="text-decoration: line-through;">\' + cellvalue + \'</span>\';'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param false|null|\Zend\I18n\Translator\Translator $translator |
26
|
|
|
* |
27
|
|
|
* @return self |
28
|
|
|
*/ |
29
|
|
|
public function setTranslator($translator) |
30
|
|
|
{ |
31
|
|
|
$this->translator = $translator; |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $message |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
private function translate($message) |
42
|
|
|
{ |
43
|
|
|
if (null === $this->translator) { |
44
|
|
|
return $message; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->translator->translate($message); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $columns |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function __invoke(array $columns) |
56
|
|
|
{ |
57
|
|
|
$return = []; |
58
|
|
|
|
59
|
|
|
foreach ($columns as $column) { |
60
|
|
|
/* @var $column \ZfcDatagrid\Column\AbstractColumn */ |
61
|
|
|
|
62
|
|
|
$options = [ |
63
|
|
|
'name' => (string) $column->getUniqueId(), |
64
|
|
|
'index' => (string) $column->getUniqueId(), |
65
|
|
|
'label' => $this->translate((string) $column->getLabel()), |
66
|
|
|
|
67
|
|
|
'width' => $column->getWidth(), |
68
|
|
|
'hidden' => (bool) $column->isHidden(), |
69
|
|
|
'sortable' => (bool) $column->isUserSortEnabled(), |
70
|
|
|
'search' => (bool) $column->isUserFilterEnabled(), |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/* |
74
|
|
|
* Formatting |
75
|
|
|
*/ |
76
|
|
|
$formatter = $this->getFormatter($column); |
77
|
|
|
if ($formatter != '') { |
78
|
|
|
$options['formatter'] = (string) $formatter; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$alignAlreadyDefined = false; |
82
|
|
|
if ($column->hasStyles()) { |
83
|
|
|
foreach ($column->getStyles() as $style) { |
84
|
|
|
/** @var \ZfcDatagrid\Column\Style\Align $style */ |
85
|
|
|
if (get_class($style) == 'ZfcDatagrid\Column\Style\Align') { |
86
|
|
|
$options['align'] = $style->getAlignment(); |
87
|
|
|
$alignAlreadyDefined = true; |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!$alignAlreadyDefined && $column->getType() instanceof Type\Number) { |
94
|
|
|
$options['align'] = Column\Style\Align::$RIGHT; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* |
98
|
|
|
* Cellattr |
99
|
|
|
*/ |
100
|
|
|
$rendererParameters = $column->getRendererParameters('jqGrid'); |
101
|
|
|
if (isset($rendererParameters['cellattr'])) { |
102
|
|
|
$options['cellattr'] = (string) $rendererParameters['cellattr']; |
103
|
|
|
} |
104
|
|
|
if (isset($rendererParameters['classes'])) { |
105
|
|
|
$options['classes'] = (string) $rendererParameters['classes']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/* |
109
|
|
|
* Filtering |
110
|
|
|
*/ |
111
|
|
|
$searchoptions = []; |
112
|
|
|
$searchoptions['clearSearch'] = false; |
113
|
|
|
if ($column->hasFilterSelectOptions() === true) { |
114
|
|
|
$options['stype'] = 'select'; |
115
|
|
|
$searchoptions['value'] = $column->getFilterSelectOptions(); |
116
|
|
|
|
117
|
|
|
if ($column->hasFilterDefaultValue() === true) { |
118
|
|
|
$searchoptions['defaultValue'] = $column->getFilterDefaultValue(); |
119
|
|
|
} else { |
120
|
|
|
$searchoptions['defaultValue'] = ''; |
121
|
|
|
} |
122
|
|
|
} elseif ($column->hasFilterDefaultValue() === true) { |
123
|
|
|
$filter = new \ZfcDatagrid\Filter(); |
124
|
|
|
$filter->setFromColumn($column, $column->getFilterDefaultValue()); |
125
|
|
|
|
126
|
|
|
$searchoptions['defaultValue'] = $filter->getDisplayColumnValue(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (count($searchoptions) > 0) { |
130
|
|
|
$options['searchoptions'] = $searchoptions; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Because with json_encode we get problems, it's custom made! |
135
|
|
|
*/ |
136
|
|
|
$colModel = []; |
137
|
|
|
foreach ($options as $key => $value) { |
138
|
|
|
if (is_array($value)) { |
139
|
|
|
$value = json_encode($value); |
140
|
|
|
} elseif (is_bool($value)) { |
141
|
|
|
if (true === $value) { |
142
|
|
|
$value = 'true'; |
143
|
|
|
} else { |
144
|
|
|
$value = 'false'; |
145
|
|
|
} |
146
|
|
|
} elseif ('formatter' == $key) { |
147
|
|
|
if (stripos($value, 'formatter') === false && stripos($value, 'function') === false) { |
148
|
|
|
$value = '"'.$value.'"'; |
149
|
|
|
} |
150
|
|
|
} elseif ('cellattr' == $key) { |
|
|
|
|
151
|
|
|
// SKIP THIS |
152
|
|
|
} else { |
153
|
|
|
$value = '"'.$value.'"'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$colModel[] = (string) $key.': '.$value; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$return[] = '{'.implode(',', $colModel).'}'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return '['.implode(',', $return).']'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param Column\AbstractColumn $column |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
private function getFormatter(Column\AbstractColumn $column) |
171
|
|
|
{ |
172
|
|
|
/* |
173
|
|
|
* User defined formatter |
174
|
|
|
*/ |
175
|
|
|
$rendererParameters = $column->getRendererParameters('jqGrid'); |
176
|
|
|
if (isset($rendererParameters['formatter'])) { |
177
|
|
|
return $rendererParameters['formatter']; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/* |
181
|
|
|
* Formatter based on column options + styles |
182
|
|
|
*/ |
183
|
|
|
$formatter = ''; |
184
|
|
|
|
185
|
|
|
$formatter .= implode(' ', $this->getStyles($column)); |
186
|
|
|
|
187
|
|
|
switch (get_class($column->getType())) { |
188
|
|
|
|
189
|
|
|
case 'ZfcDatagrid\Column\Type\PhpArray': |
190
|
|
|
$formatter .= 'cellvalue = \'<pre>\' + cellvalue.join(\'<br />\') + \'</pre>\';'; |
191
|
|
|
break; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($column instanceof Column\Action) { |
195
|
|
|
$formatter .= ' cellvalue = cellvalue; '; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ($formatter != '') { |
199
|
|
|
$prefix = 'function (cellvalue, options, rowObject) {'; |
200
|
|
|
$suffix = ' return cellvalue; }'; |
201
|
|
|
|
202
|
|
|
$formatter = $prefix.$formatter.$suffix; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $formatter; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param Column\AbstractColumn $col |
210
|
|
|
* |
211
|
|
|
* @throws \Exception |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
private function getStyles(Column\AbstractColumn $col) |
216
|
|
|
{ |
217
|
|
|
$styleFormatter = []; |
218
|
|
|
|
219
|
|
|
/* |
220
|
|
|
* First all based on value (only one works) @todo |
221
|
|
|
*/ |
222
|
|
|
foreach ($col->getStyles() as $style) { |
223
|
|
|
$prepend = ''; |
224
|
|
|
$append = ''; |
225
|
|
|
|
226
|
|
|
/* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */ |
227
|
|
|
foreach ($style->getByValues() as $rule) { |
228
|
|
|
$colString = $rule['column']->getUniqueId(); |
229
|
|
|
$operator = ''; |
|
|
|
|
230
|
|
|
switch ($rule['operator']) { |
231
|
|
|
|
232
|
|
|
case Filter::EQUAL: |
233
|
|
|
$operator = '=='; |
234
|
|
|
break; |
235
|
|
|
|
236
|
|
|
case Filter::NOT_EQUAL: |
237
|
|
|
$operator = '!='; |
238
|
|
|
break; |
239
|
|
|
|
240
|
|
|
default: |
241
|
|
|
throw new \Exception('Currently not supported filter operation: "'.$rule['operator'].'"'); |
242
|
|
|
break; |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$prepend = 'if (rowObject.'.$colString.' '.$operator.' \''.$rule['value'].'\') {'; |
246
|
|
|
$append .= '}'; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$styleString = ''; |
250
|
|
|
switch (get_class($style)) { |
251
|
|
|
|
252
|
|
|
case 'ZfcDatagrid\Column\Style\Bold': |
253
|
|
|
$styleString = self::STYLE_BOLD; |
254
|
|
|
break; |
255
|
|
|
|
256
|
|
|
case 'ZfcDatagrid\Column\Style\Italic': |
257
|
|
|
$styleString = self::STYLE_ITALIC; |
258
|
|
|
break; |
259
|
|
|
|
260
|
|
|
case 'ZfcDatagrid\Column\Style\Strikethrough': |
261
|
|
|
$styleString = self::STYLE_STRIKETHROUGH; |
262
|
|
|
break; |
263
|
|
|
|
264
|
|
|
case 'ZfcDatagrid\Column\Style\Color': |
265
|
|
|
$styleString = 'cellvalue = \'<span style="color: #'.$style->getRgbHexString().';">\' + cellvalue + \'</span>\';'; |
|
|
|
|
266
|
|
|
break; |
267
|
|
|
|
268
|
|
|
case 'ZfcDatagrid\Column\Style\CSSClass': |
269
|
|
|
$styleString = 'cellvalue = \'<span class="'.$style->getClass().'">\' + cellvalue + \'</span>\';'; |
|
|
|
|
270
|
|
|
break; |
271
|
|
|
|
272
|
|
|
case 'ZfcDatagrid\Column\Style\BackgroundColor': |
273
|
|
|
// do NOTHING! this is done by loadComplete event... |
274
|
|
|
// At this stage jqgrid haven't created the columns... |
275
|
|
|
break; |
276
|
|
|
|
277
|
|
|
case 'ZfcDatagrid\Column\Style\Html': |
278
|
|
|
// do NOTHING! just pass the HTML! |
279
|
|
|
break; |
280
|
|
|
|
281
|
|
|
case 'ZfcDatagrid\Column\Style\Align': |
282
|
|
|
// do NOTHING! we have to add the align style in the gridcell and not in a span! |
283
|
|
|
break; |
284
|
|
|
|
285
|
|
|
default: |
286
|
|
|
throw new \Exception('Not defined style: "'.get_class($style).'"'); |
287
|
|
|
break; |
|
|
|
|
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$styleFormatter[] = $prepend.$styleString.$append; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $styleFormatter; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
This check looks for the bodies of
elseif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elseif
bodies can be removed. If you have an empty elseif but statements in theelse
branch, consider inverting the condition.