|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.newicon.net/ |
|
4
|
|
|
* @copyright Copyright (c) 2018 Newicon Ltd |
|
5
|
|
|
* @license http://www.newicon.net/neon/framework/license/ |
|
6
|
|
|
* @author Steve O'Brien <[email protected]> |
|
7
|
|
|
* @date 1/1/2018 16:13 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace neon\core\grid\column; |
|
11
|
|
|
|
|
12
|
|
|
use neon\core\form\interfaces\IField; |
|
13
|
|
|
use neon\core\helpers\Arr; |
|
14
|
|
|
use \yii\base\Component; |
|
15
|
|
|
use \neon\core\grid\Grid; |
|
16
|
|
|
use yii\data\ActiveDataProvider; |
|
17
|
|
|
use \yii\helpers\Inflector; |
|
18
|
|
|
use \yii\helpers\ArrayHelper; |
|
19
|
|
|
use \neon\core\helpers\Html; |
|
20
|
|
|
use \neon\core\grid\query\IQuery; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Column |
|
24
|
|
|
* A grid is made of columns which in turn has a header, filter, and data cells |
|
25
|
|
|
* for each row of data. Columns are associated with a form field which is used |
|
26
|
|
|
* both for filtering and helping to draw the cell data. |
|
27
|
|
|
* |
|
28
|
|
|
* @package neon\grid\column |
|
29
|
|
|
* |
|
30
|
|
|
* @property string $key readonly |
|
31
|
|
|
* @property \neon\core\grid\Grid $grid readonly |
|
32
|
|
|
* @property boolean $searchable |
|
33
|
|
|
* @property string $title |
|
34
|
|
|
* @property string $dbField - set this is the database field is different to $key |
|
35
|
|
|
* @property string $dbSortField - set this for sorting if the sort field is different to $dbField |
|
36
|
|
|
* @property string $emptyCellDisplay - @see getEmptyCellDisplay - string to display when cell content is empty |
|
37
|
|
|
* @property IField $member - the corresponding form field component |
|
38
|
|
|
*/ |
|
39
|
|
|
class Column extends Component implements IColumn |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var \neon\core\form\interfaces\IField |
|
44
|
|
|
*/ |
|
45
|
|
|
private $_member = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get the form field used to handle the grid columns data |
|
49
|
|
|
* Note: member refers to the name of the form field configuration for the column and not necessarily |
|
50
|
|
|
* a daedalus member |
|
51
|
|
|
* @param IField $member |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setMember(IField $member) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->_member = $member; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the form field member used to handle the columns data, filters, and grid output. |
|
60
|
|
|
* Note: member refers to the name of the form field configuration for the column and not necessarily |
|
61
|
|
|
* a daedalus member |
|
62
|
|
|
* @return IField|null |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getMember() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->_member; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Display options for a data cell in this column |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
public $dataCellOptions = []; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Configuration options for the column filter object |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
public $filterFieldConfig = []; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Html options for the column filter object for use in |
|
83
|
|
|
* HTML::tag('td', 'content', $filterCellOptions) |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
public $filterCellOptions = []; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Display attributes for the header cell |
|
90
|
|
|
* @see setHeaderCellAttributes, getHeaderCellAttributes |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $_headerCellAttributes = []; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* The string to output when the cell content is empty |
|
96
|
|
|
* |
|
97
|
|
|
* @var string |
|
98
|
|
|
*/ |
|
99
|
|
|
private $_emptyCellDisplay = null; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the string to render for the cell if the cells data is empty |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getEmptyCellDisplay() |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->_emptyCellDisplay !== null) |
|
109
|
|
|
return $this->_emptyCellDisplay; |
|
110
|
|
|
return $this->getGrid()->emptyCellDisplay; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Set string the column will output when the cell is empty |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $string |
|
117
|
|
|
* @return $this - chainable |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setEmptyCellDisplay($string) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->_emptyCellDisplay = $string; |
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Set the header cell attributes |
|
128
|
|
|
* @param array $attributes |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setHeaderCellAttributes($attributes) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->_headerCellAttributes = $attributes; |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the header cell attributes |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getHeaderCellAttributes() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->_headerCellAttributes; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected $_width = ''; |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @deprecated for @see setWidth |
|
148
|
|
|
*/ |
|
149
|
|
|
public function width($width) |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->setWidth($width); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Set the column width |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $width |
|
158
|
|
|
* @return $this |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setWidth($width) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->_width = $width; |
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get the column width |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getWidth() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->_width; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function __construct($config=[]) |
|
177
|
|
|
{ |
|
178
|
|
|
parent::__construct($config); |
|
179
|
|
|
if (!isset($config['key'])) { |
|
180
|
|
|
throw new \InvalidConfigException('Config must contain "key" property, a string that identified this column $this->key'); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
if (!isset($config['grid'])) { |
|
183
|
|
|
throw new \InvalidConfigException('Config must contain "grid" property, defining the parent grid this column belongs to'); |
|
184
|
|
|
} |
|
185
|
|
|
$this->_key = $config['key']; |
|
186
|
|
|
$this->_grid = $config['grid']; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Whether or not to show the filter |
|
191
|
|
|
* @var boolean |
|
192
|
|
|
*/ |
|
193
|
|
|
protected $_filter = true; |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @inheritDoc |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getFilter() |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->_filter; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @inheritDoc |
|
205
|
|
|
*/ |
|
206
|
|
|
public function setFilter($filter) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->_filter = $filter; |
|
209
|
|
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @var string |
|
214
|
|
|
*/ |
|
215
|
|
|
protected $_key; |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @inheritDoc |
|
219
|
|
|
*/ |
|
220
|
|
|
public function setKey($key) |
|
221
|
|
|
{ |
|
222
|
|
|
$this->_key = $key; |
|
223
|
|
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @inheritDoc |
|
228
|
|
|
*/ |
|
229
|
|
|
public function getKey() |
|
230
|
|
|
{ |
|
231
|
|
|
return $this->_key; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* A reference to the columns parent Grid object that it belongs to |
|
236
|
|
|
* @var \neon\core\grid\Grid; |
|
237
|
|
|
*/ |
|
238
|
|
|
protected $_grid; |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @inheritDoc |
|
242
|
|
|
*/ |
|
243
|
|
|
public function setGrid($grid) |
|
244
|
|
|
{ |
|
245
|
|
|
$this->_grid = $grid; |
|
246
|
|
|
return $this; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
protected $_visible = true; |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @inheritDoc |
|
253
|
|
|
*/ |
|
254
|
|
|
public function setVisible($visible) |
|
255
|
|
|
{ |
|
256
|
|
|
$this->_visible = $visible; |
|
257
|
|
|
return $this; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @inheritDoc |
|
262
|
|
|
*/ |
|
263
|
|
|
public function getVisible() |
|
264
|
|
|
{ |
|
265
|
|
|
return $this->_visible; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @var boolean store the sortable option |
|
270
|
|
|
*/ |
|
271
|
|
|
protected $_sortable = true; |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* @inheritDoc |
|
275
|
|
|
*/ |
|
276
|
|
|
public function setIsSortable($bool=true) |
|
277
|
|
|
{ |
|
278
|
|
|
$this->_sortable = $bool; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @inheritDoc |
|
283
|
|
|
*/ |
|
284
|
|
|
public function isSortable() |
|
285
|
|
|
{ |
|
286
|
|
|
return $this->_sortable; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
public function setAsIndex($bool=true) |
|
290
|
|
|
{ |
|
291
|
|
|
$this->grid->setIndexedByColumn($this->key); |
|
292
|
|
|
// thi is a bit gnarly but tests for yii Active record data providers specifically |
|
293
|
|
|
if ($this->grid->getDataProvider() instanceof ActiveDataProvider) { |
|
|
|
|
|
|
294
|
|
|
if ($bool) { |
|
295
|
|
|
$this->grid->query->indexBy($this->getDbField()); |
|
296
|
|
|
} else { |
|
297
|
|
|
$this->grid->query->indexBy(null); |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
return $this; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @inheritDoc |
|
305
|
|
|
*/ |
|
306
|
|
|
public function getGrid() |
|
307
|
|
|
{ |
|
308
|
|
|
return $this->_grid; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Gets the posted search data for this column |
|
313
|
|
|
* This can be used to filter the data by this column |
|
314
|
|
|
* typical useage: |
|
315
|
|
|
* |
|
316
|
|
|
* ```php |
|
317
|
|
|
* // note we make sure request data exists before using it otherwise we get the default empty form value |
|
318
|
|
|
* if ($this->hasRequestData()) { |
|
319
|
|
|
* $val = $this->getSearch(); |
|
320
|
|
|
* $this->grid->query->andWhere(['id' => $val]); |
|
321
|
|
|
* } |
|
322
|
|
|
* ``` |
|
323
|
|
|
* |
|
324
|
|
|
* @return mixed |
|
325
|
|
|
*/ |
|
326
|
|
|
public function getSearch() |
|
327
|
|
|
{ |
|
328
|
|
|
$form = $this->getGrid()->getFilterForm(); |
|
329
|
|
|
if ($form->hasField($this->getKey())) { |
|
330
|
|
|
$value = $form->getField($this->getKey())->getValue(); |
|
331
|
|
|
return $value; |
|
332
|
|
|
} |
|
333
|
|
|
return null; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
protected $_dataCellContentFunction; |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* @inheritdoc |
|
340
|
|
|
*/ |
|
341
|
|
|
public function setDataCellContentFunction($callable) |
|
342
|
|
|
{ |
|
343
|
|
|
$this->_dataCellContentFunction = $callable; |
|
344
|
|
|
return $this; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @inheritdoc |
|
349
|
|
|
*/ |
|
350
|
|
|
public function getDataCellContentFunction() |
|
351
|
|
|
{ |
|
352
|
|
|
// function has been hard set |
|
353
|
|
|
if ($this->_dataCellContentFunction !== null) { |
|
354
|
|
|
return $this->_dataCellContentFunction; |
|
355
|
|
|
} |
|
356
|
|
|
// check for convention |
|
357
|
|
|
$functionName = 'render' . $this->key; |
|
358
|
|
|
if (method_exists($this->grid, $functionName) && $functionName !== 'renderfile') { |
|
359
|
|
|
return $functionName; |
|
360
|
|
|
} |
|
361
|
|
|
// otherwise render the default column render function |
|
362
|
|
|
return [$this, 'renderDataCellContent']; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* @var string |
|
367
|
|
|
*/ |
|
368
|
|
|
protected $_title = null; |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @inheritdoc |
|
372
|
|
|
*/ |
|
373
|
|
|
public function setTitle($title) |
|
374
|
|
|
{ |
|
375
|
|
|
$this->_title = $title; |
|
376
|
|
|
return $this; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* @inheritDoc |
|
381
|
|
|
*/ |
|
382
|
|
|
public function getTitle() |
|
383
|
|
|
{ |
|
384
|
|
|
if ($this->_title === null) { |
|
385
|
|
|
$title = Inflector::humanize($this->getKey()); |
|
386
|
|
|
$this->_title = ucwords(trim($title)); |
|
387
|
|
|
} |
|
388
|
|
|
return $this->_title; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
|
|
392
|
|
|
protected $_searchFunction; |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* @inheritDoc |
|
396
|
|
|
*/ |
|
397
|
|
|
public function setSearchFunction($function) |
|
398
|
|
|
{ |
|
399
|
|
|
$this->_searchFunction = $function; |
|
400
|
|
|
return $this; |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
/** |
|
404
|
|
|
* @inheritDoc |
|
405
|
|
|
*/ |
|
406
|
|
|
public function getSearchFunction() |
|
407
|
|
|
{ |
|
408
|
|
|
if ($this->_searchFunction === null) { |
|
409
|
|
|
$searchFunction = 'search'.ucwords($this->key); |
|
410
|
|
|
if (method_exists($this->grid, $searchFunction)) { |
|
411
|
|
|
$this->_searchFunction = $searchFunction; |
|
412
|
|
|
} |
|
413
|
|
|
} |
|
414
|
|
|
return $this->_searchFunction; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* The query field used for this column for sorting, filtering |
|
419
|
|
|
* and getting the value. If this isn't set, key is used. If you require |
|
420
|
|
|
* sorting over a separate field @see setDbSortField |
|
421
|
|
|
* @var string |
|
422
|
|
|
*/ |
|
423
|
|
|
protected $_dbField; |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* @inheritDoc |
|
427
|
|
|
*/ |
|
428
|
|
|
public function setDbField($field){ |
|
429
|
|
|
$this->_dbField = $field; |
|
430
|
|
|
return $this; |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
/** |
|
434
|
|
|
* @inheritDoc |
|
435
|
|
|
*/ |
|
436
|
|
|
public function getDbField(){ |
|
437
|
|
|
if ($this->_dbField === null){ |
|
438
|
|
|
// set to be the key |
|
439
|
|
|
$this->_dbField = $this->key; |
|
440
|
|
|
} |
|
441
|
|
|
return $this->_dbField; |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* The query field used for sorting. If this isn't set then dbField is |
|
446
|
|
|
* used. If that isn't set then key is used |
|
447
|
|
|
* @var string |
|
448
|
|
|
*/ |
|
449
|
|
|
protected $_dbSortField; |
|
450
|
|
|
|
|
451
|
|
|
/** |
|
452
|
|
|
* @inheritDoc |
|
453
|
|
|
*/ |
|
454
|
|
|
public function setDbSortField($field){ |
|
455
|
|
|
$this->_dbSortField = $field; |
|
456
|
|
|
return $this; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* @inheritDoc |
|
461
|
|
|
*/ |
|
462
|
|
|
public function getDbSortField(){ |
|
463
|
|
|
if ($this->_dbSortField === null){ |
|
464
|
|
|
// set to be the key |
|
465
|
|
|
$this->_dbSortField = $this->dbField; |
|
466
|
|
|
} |
|
467
|
|
|
return $this->_dbSortField; |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
/** |
|
471
|
|
|
* @var array |
|
472
|
|
|
*/ |
|
473
|
|
|
protected $_style = []; |
|
474
|
|
|
|
|
475
|
|
|
/** |
|
476
|
|
|
* Set header css styles |
|
477
|
|
|
* @param array $styles |
|
478
|
|
|
* @return $this |
|
479
|
|
|
*/ |
|
480
|
|
|
public function setHeaderStyle(array $styles) |
|
481
|
|
|
{ |
|
482
|
|
|
$this->_style = $styles; |
|
483
|
|
|
return $this; |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
/** |
|
487
|
|
|
* @inheritDoc |
|
488
|
|
|
*/ |
|
489
|
|
|
public function renderHeaderCell() |
|
490
|
|
|
{ |
|
491
|
|
|
$options = $this->getHeaderCellAttributes(); |
|
492
|
|
|
if ($this->grid->hasSort($this->key)) |
|
493
|
|
|
Html::addCssClass($options, ($this->grid->hasSortDescending() ? 'desc' : 'asc')); |
|
494
|
|
|
if (trim($this->width) != '') |
|
|
|
|
|
|
495
|
|
|
Html::addCssStyle($options, ["width" => $this->width]); |
|
496
|
|
|
Html::addCssStyle($options, $this->_style); |
|
497
|
|
|
|
|
498
|
|
|
return Html::tag('th', $this->renderHeaderCellContent(), $options); |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
/** |
|
502
|
|
|
* @inheritdoc |
|
503
|
|
|
*/ |
|
504
|
|
|
public function renderHeaderCellContent() |
|
505
|
|
|
{ |
|
506
|
|
|
if (!$this->isSortable()) |
|
507
|
|
|
return $this->getTitle(); |
|
508
|
|
|
$sort = ''; |
|
509
|
|
|
if ($this->grid->hasSort($this->key)) { |
|
510
|
|
|
$sort = $this->grid->hasSortDescending() ? ' - ' : ' + '; |
|
511
|
|
|
} |
|
512
|
|
|
$sortParam = $this->grid->hasSortDescending() ? $this->key : '-' . $this->key; |
|
513
|
|
|
$url = neon()->getUrlManager()->createUrl([\Yii::$app->controller->getRoute(), $this->grid->id => ['sort' => $sortParam]]); |
|
514
|
|
|
return Html::a($this->getTitle() . $sort, $url, ['data-sort'=>$sortParam]); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
/** |
|
518
|
|
|
* @inheritdoc |
|
519
|
|
|
*/ |
|
520
|
|
|
public function renderFilterCell() |
|
521
|
|
|
{ |
|
522
|
|
|
$content = ''; |
|
523
|
|
|
if ($this->filter !== false) { |
|
|
|
|
|
|
524
|
|
|
$content = $this->renderFilterCellContent(); |
|
525
|
|
|
} |
|
526
|
|
|
if (trim($this->width) != '') { |
|
|
|
|
|
|
527
|
|
|
Html::addCssStyle($this->filterCellOptions, "width:{$this->width};"); |
|
528
|
|
|
} |
|
529
|
|
|
return Html::tag('td', $content, $this->filterCellOptions); |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
/** |
|
533
|
|
|
* @inheritdoc |
|
534
|
|
|
*/ |
|
535
|
|
|
public function renderFilterCellContent() |
|
536
|
|
|
{ |
|
537
|
|
|
if ($this->filter === false) |
|
|
|
|
|
|
538
|
|
|
return ''; |
|
539
|
|
|
if (!$this->getGrid()->getFilterForm()->hasField($this->getKey())) |
|
540
|
|
|
return ''; |
|
541
|
|
|
$field = $this->getGrid()->getFilterForm()->getField($this->getKey()); |
|
542
|
|
|
// remove any automatic validators |
|
543
|
|
|
$field->setValidators([], true); |
|
544
|
|
|
return $field->run(); |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
/** |
|
548
|
|
|
* Get the base definition of the filter field. |
|
549
|
|
|
* Returns the array definition of a form field, an instance of a form field (implementing IField) |
|
550
|
|
|
* or a boolean false to indicate no filtering. |
|
551
|
|
|
* @return array|false|IField |
|
552
|
|
|
*/ |
|
553
|
|
|
protected function getBaseFilterField() |
|
554
|
|
|
{ |
|
555
|
|
|
if ($this->member instanceof IField) { |
|
|
|
|
|
|
556
|
|
|
return $this->member->getFilterField(); |
|
557
|
|
|
} |
|
558
|
|
|
return ['class' => 'text', 'name' => $this->getKey()]; |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
|
|
562
|
|
|
/** |
|
563
|
|
|
* @inheritdoc |
|
564
|
|
|
*/ |
|
565
|
|
|
public function getFilterField() |
|
566
|
|
|
{ |
|
567
|
|
|
$field = $this->getBaseFilterField(); |
|
568
|
|
|
foreach($this->filterFieldConfig as $key => $value) |
|
569
|
|
|
$field[$key] = $value; |
|
570
|
|
|
return $field; |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
/** |
|
574
|
|
|
* @inheritdoc |
|
575
|
|
|
*/ |
|
576
|
|
|
public function setFilterFieldConfig($config) |
|
577
|
|
|
{ |
|
578
|
|
|
$this->filterFieldConfig = $config; |
|
579
|
|
|
return $this; |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
public function setMemberConfig($config) |
|
583
|
|
|
{ |
|
584
|
|
|
foreach($config as $key => $value) { |
|
585
|
|
|
if (isset($this->member->$key)) |
|
586
|
|
|
$this->member->$key = $value; |
|
587
|
|
|
} |
|
588
|
|
|
return $this; |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
/** |
|
592
|
|
|
* @inheritDoc |
|
593
|
|
|
*/ |
|
594
|
|
|
public function getCellData($row, $noValue='-') |
|
595
|
|
|
{ |
|
596
|
|
|
$data = null; |
|
|
|
|
|
|
597
|
|
|
$key = $this->getKey(); |
|
598
|
|
|
if (isset($row[$key])) { |
|
599
|
|
|
$data = $row[$key]; |
|
600
|
|
|
} else { |
|
601
|
|
|
return $noValue; |
|
602
|
|
|
} |
|
603
|
|
|
return $data; |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
|
|
/** |
|
607
|
|
|
* @inheritdoc |
|
608
|
|
|
*/ |
|
609
|
|
|
public function getDataCellOptions($model, $key, $index) |
|
610
|
|
|
{ |
|
611
|
|
|
$options = $this->dataCellOptions; |
|
612
|
|
|
if (trim($this->width) != '') { |
|
|
|
|
|
|
613
|
|
|
Html::addCssStyle($options, "width:{$this->width};"); |
|
614
|
|
|
} |
|
615
|
|
|
return $options; |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
/** |
|
619
|
|
|
* @inheritdoc |
|
620
|
|
|
*/ |
|
621
|
|
|
public function renderDataCell($model, $key, $index) |
|
622
|
|
|
{ |
|
623
|
|
|
$cellContent = $this->callRenderDataCellContentFunction($model, $key, $index); |
|
624
|
|
|
// if row actions: |
|
625
|
|
|
$cellContent .= $this->renderRowActions($model, $key, $index); |
|
626
|
|
|
$options = $this->getDataCellOptions($model, $key, $index); |
|
627
|
|
|
$cell = Html::tag('div', $cellContent, array_merge($options, ['class'=>'neonGrid_cell'])); |
|
628
|
|
|
return Html::tag('td', $cell, $options); |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
/** |
|
632
|
|
|
* @inheritdoc |
|
633
|
|
|
*/ |
|
634
|
|
|
public function renderDataCellContent($model, $key, $index) |
|
635
|
|
|
{ |
|
636
|
|
|
if ($this->getMember() instanceof IField) { |
|
|
|
|
|
|
637
|
|
|
$cellData = $this->getCellData($model, null); |
|
638
|
|
|
$this->member->setValueFromDb($cellData); |
|
639
|
|
|
$display = $this->member->getValueDisplay(); |
|
640
|
|
|
if ($display === null || $display === []) { |
|
|
|
|
|
|
641
|
|
|
return $this->emptyCellDisplay; |
|
642
|
|
|
} |
|
643
|
|
|
$out = $this->highlight($display, $this->getSearch()); |
|
644
|
|
|
if (is_array($display)) { |
|
|
|
|
|
|
645
|
|
|
$display = implode(',', $display); |
|
646
|
|
|
} |
|
647
|
|
|
return "<span title=\"".str_replace('"',"'", html_entity_decode(strip_tags($display)))."\">$out</span>"; |
|
648
|
|
|
} |
|
649
|
|
|
return $this->getCellData($model); |
|
650
|
|
|
} |
|
651
|
|
|
|
|
652
|
|
|
/** |
|
653
|
|
|
* @inheritDoc |
|
654
|
|
|
*/ |
|
655
|
|
|
public function getFieldName() |
|
656
|
|
|
{ |
|
657
|
|
|
return $this->getGrid()->getId() . '[' . $this->key . '][search]'; |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* @inheritDoc |
|
662
|
|
|
*/ |
|
663
|
|
|
public function highlight($stringToHighlight, $search) |
|
664
|
|
|
{ |
|
665
|
|
|
return Html::highlight($search, $stringToHighlight); |
|
666
|
|
|
} |
|
667
|
|
|
|
|
668
|
|
|
/** |
|
669
|
|
|
* @inheritdoc |
|
670
|
|
|
*/ |
|
671
|
|
|
public function processSort(IQuery $query, $descending) |
|
672
|
|
|
{ |
|
673
|
|
|
$sort = $descending ? 'DESC' : 'ASC'; |
|
674
|
|
|
$query->orderBy($this->dbSortField, $sort); |
|
675
|
|
|
} |
|
676
|
|
|
|
|
677
|
|
|
/** |
|
678
|
|
|
* @inheritDoc |
|
679
|
|
|
*/ |
|
680
|
|
|
public function processSearch(IQuery $query) |
|
681
|
|
|
{ |
|
682
|
|
|
$postVal = $this->getSearch(); |
|
683
|
|
|
$form = $this->getGrid()->getFilterForm(); |
|
684
|
|
|
if ($form->hasField($this->getKey())) { |
|
685
|
|
|
$form->getField($this->getKey())->processAsFilter($query, $postVal); |
|
686
|
|
|
} |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
/** |
|
690
|
|
|
* Whether search request data exists for the column |
|
691
|
|
|
* @return bool |
|
692
|
|
|
*/ |
|
693
|
|
|
public function hasRequestData() |
|
694
|
|
|
{ |
|
695
|
|
|
$filterForm = $this->getGrid()->getFilterForm(); |
|
696
|
|
|
return isset($_REQUEST[$this->getGrid()->getId()][$filterForm->getName()][$this->getKey()]); |
|
697
|
|
|
} |
|
698
|
|
|
|
|
699
|
|
|
/** |
|
700
|
|
|
* Calls the render function and passes in the current row $model, $key, $index |
|
701
|
|
|
* |
|
702
|
|
|
* @param $model |
|
703
|
|
|
* @param $key |
|
704
|
|
|
* @param $index |
|
705
|
|
|
* @return mixed |
|
706
|
|
|
* @throws \Exception |
|
707
|
|
|
*/ |
|
708
|
|
|
public function callRenderDataCellContentFunction($model, $key, $index) |
|
709
|
|
|
{ |
|
710
|
|
|
if (($cellFunc = $this->getDataCellContentFunction())) { |
|
711
|
|
|
if (is_string($cellFunc)) { |
|
712
|
|
|
return $this->grid->$cellFunc($model, $key, $index, $this); |
|
713
|
|
|
} else { |
|
714
|
|
|
return call_user_func($cellFunc, $model, $key, $index, $this); |
|
715
|
|
|
} |
|
716
|
|
|
} |
|
717
|
|
|
throw new \Exception('Unable to call a valid data cell render function'); |
|
718
|
|
|
} |
|
719
|
|
|
|
|
720
|
|
|
/** |
|
721
|
|
|
* @inheritDoc |
|
722
|
|
|
*/ |
|
723
|
|
|
public function callSearchFunction() |
|
724
|
|
|
{ |
|
725
|
|
|
if (($search = $this->getSearchFunction())) { |
|
726
|
|
|
return $this->callFunction($search, [$this->getSearch(), $this->getGrid()->getQueryBuilder()]); |
|
727
|
|
|
} |
|
728
|
|
|
throw new \Exception('Unable to call a valid search function'); |
|
729
|
|
|
} |
|
730
|
|
|
|
|
731
|
|
|
protected $_actions =[]; |
|
732
|
|
|
|
|
733
|
|
|
/** |
|
734
|
|
|
* @inheritDoc |
|
735
|
|
|
*/ |
|
736
|
|
|
public function addRowAction($key, $text, $linkOptions=[]) |
|
737
|
|
|
{ |
|
738
|
|
|
$linkOptions['text'] = $text; |
|
739
|
|
|
$linkOptions['data-action'] = $key; |
|
740
|
|
|
$linkOptions['href'] = ''; |
|
741
|
|
|
$linkOptions['data-pjax'] = 0; |
|
742
|
|
|
$this->_actions[$key] = $linkOptions; |
|
743
|
|
|
} |
|
744
|
|
|
|
|
745
|
|
|
public function addRowActionLink($text, $function) |
|
746
|
|
|
{ |
|
747
|
|
|
$linkOptions['text'] = $text; |
|
|
|
|
|
|
748
|
|
|
$linkOptions['function'] = $function; |
|
749
|
|
|
$this->_actions[$text] = $linkOptions; |
|
750
|
|
|
} |
|
751
|
|
|
|
|
752
|
|
|
/** |
|
753
|
|
|
* @inheritDoc |
|
754
|
|
|
*/ |
|
755
|
|
|
public function renderRowActions($model, $key, $index) |
|
756
|
|
|
{ |
|
757
|
|
|
if (empty($this->_actions)) { |
|
758
|
|
|
return ''; |
|
759
|
|
|
} |
|
760
|
|
|
$out = '<div class="neonGrid_rowActions">'; |
|
761
|
|
|
$i = 0; |
|
762
|
|
|
foreach($this->_actions as $actionKey => $linkOptions) { |
|
763
|
|
|
if ($i > 0) $out .= ' | '; |
|
764
|
|
|
if (isset($linkOptions['function'])) { |
|
765
|
|
|
$func = $linkOptions['function']; |
|
766
|
|
|
$out .= $this->callFunction($func, [$model, $key, $index]); |
|
767
|
|
|
} else { |
|
768
|
|
|
|
|
769
|
|
|
if ($this->getGrid()->hasIndexColumn()) { |
|
770
|
|
|
$key = $this->getGrid()->getIndexColumn()->getKey(); |
|
771
|
|
|
$linkOptions['data-index'] = isset($model[$key]) ? $model[$key] : ''; |
|
772
|
|
|
} |
|
773
|
|
|
|
|
774
|
|
|
$text = ArrayHelper::remove($linkOptions, 'text'); |
|
775
|
|
|
$out .= Html::tag('a', $text, $linkOptions); |
|
776
|
|
|
} |
|
777
|
|
|
$i++; |
|
778
|
|
|
} |
|
779
|
|
|
$out .= '</div>'; |
|
780
|
|
|
return $out; |
|
781
|
|
|
} |
|
782
|
|
|
|
|
783
|
|
|
/** |
|
784
|
|
|
* @param mixed $function if a string then assumes this is a string function on the parent grid. |
|
785
|
|
|
* Can be a callable function or array [$object, 'functionName'] |
|
786
|
|
|
* @param array $arguments of arguments |
|
787
|
|
|
* @return mixed |
|
788
|
|
|
*/ |
|
789
|
|
|
public function callFunction($function, $arguments=[]) |
|
790
|
|
|
{ |
|
791
|
|
|
if (is_string($function)) { |
|
792
|
|
|
return call_user_func_array([$this->grid, $function], $arguments); |
|
793
|
|
|
} else { |
|
794
|
|
|
return call_user_func_array($function, $arguments); |
|
795
|
|
|
} |
|
796
|
|
|
} |
|
797
|
|
|
|
|
798
|
|
|
/** |
|
799
|
|
|
* @inheritdoc |
|
800
|
|
|
* Currently the column will respect dataMapFilters applied to the columns form field member |
|
801
|
|
|
* Note: member only refers to the name of the form field configuration for the column and not necessarily |
|
802
|
|
|
* a daedalus member |
|
803
|
|
|
*/ |
|
804
|
|
|
public function prepareDisplay() |
|
805
|
|
|
{ |
|
806
|
|
|
// limit the column results to respect any mapFilters |
|
807
|
|
|
if (isset($this->member->dataMapFilters)) { |
|
|
|
|
|
|
808
|
|
|
$models = $this->getGrid()->dataProvider->getModels(); |
|
809
|
|
|
// extract out non-empty column values only, flatten the data so just an array of keys and remove any duplicates |
|
810
|
|
|
$columnData = array_unique(Arr::flatten(array_filter(Arr::getColumn($models, $this->key)))); |
|
811
|
|
|
if (count($columnData)) { |
|
812
|
|
|
$this->member->dataMapFilters = ['uuid' => $columnData]; |
|
813
|
|
|
} |
|
814
|
|
|
foreach($models as $model) { |
|
815
|
|
|
$this->member->makeMapRequest($this->getCellData($model, null)); |
|
|
|
|
|
|
816
|
|
|
} |
|
817
|
|
|
} |
|
818
|
|
|
} |
|
819
|
|
|
} |
|
820
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths