-
Notifications
You must be signed in to change notification settings - Fork 1
/
GridView.php
executable file
·388 lines (340 loc) · 13.6 KB
/
GridView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/**
* Copyright (c) 2013, 2amigOS! Consulting Group LLC.
* Copyright (c) 2014, Kartik Visweswaran Krajee.com
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace amilna\yap;
use Closure;
use kartik\grid\DataColumn;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* GridView
*
* A grid view that groups rows by any column(s). Combination of
* https://github.com/2amigos/yii2-grid-view-library/blob/master/GroupGridView.php and
* https://github.com/kartik-v/yii2-grid
*
* @package amilna\yap
*/
class GridView extends \kartik\grid\GridView
{
const MERGE_SIMPLE = 'simple';
const MERGE_NESTED = 'nested';
const MERGE_FIRST_ROW = 'firstrow';
const POS_ABOVE = 'above';
const POS_BELOW = 'below';
/**
* @var array the column names to merge
*/
public $mergeColumns = [];
/**
* @var string the way to merge the columns. Possible values are:
*
* - [[GridView::MERGE_SIMPLE]] Column values are merged independently
* - [[GridView::MERGE_NESTED]] Column values are merged if at least one value of nested columns changes
* (makes sense when several columns in $mergeColumns option)
* - [[GridView::MERGE_FIRST_ROW]] Column values are merged independently, but value is shown in first row of
* group and below cells just cleared (instead of `rowspan`)
*
*/
public $type = self::MERGE_SIMPLE;
/**
* @var string the CSS class to use for the merged cells
*/
public $mergeCellClass = 'groupview-merge-cells';
/**
* @var array the list of columns on which change extra row will be triggered
*/
public $extraRowColumns = [];
/**
* @var string|\Closure an anonymous function that returns the value to be displayed for every extra row.
* The signature of this function is `function ($model, $index, $totals)`.
* If this is not set, `$model[$attribute]` will be used to obtain the value.
*
* You may also set this property to a string representing the attribute name to be displayed in this column.
* This can be used when the attribute to be displayed is different from the [[attribute]] that is used for
* sorting and filtering.
*/
public $extraRowValue;
/**
* @var string the position of the extra row. Possible values are [[GridView::POS_ABOVE]] or
* [[GridView::POS_BELOW]]
*/
public $extraRowPosition = self::POS_ABOVE;
/**
* @var \Closure an anonymous function that returns a calculated value of the totals. Its signature is
* `function($model, $index, $totals)`
*/
public $extraRowTotalsValue;
/**
* @var string the CSS class to add on the extra row TD tag
*/
public $extraRowClass = 'groupview-extra-row';
/**
* @var array stores the groups
*/
private $_groups = [];
/**
* @inheritdoc
*/
public function renderTableBody()
{
if (!empty($this->mergeColumns) || !empty($this->extraRowColumns)) {
$this->groupColumns();
}
return parent::renderTableBody();
}
/**
* Finds and stores changes of grouped columns
*/
public function groupColumns()
{
$models = $this->dataProvider->getModels();
if (count($models) == 0) {
return;
}
$this->mergeColumns = (array)$this->mergeColumns;
$this->extraRowColumns = (array)$this->extraRowColumns;
// store columns for group.
$columns = array_unique(ArrayHelper::merge($this->mergeColumns, $this->extraRowColumns));
foreach ($columns as $key => $name) {
foreach ($this->columns as $column) {
if (property_exists($column, 'attribute') && ArrayHelper::getValue($column, 'attribute') == $name) {
$columns[$key] = $column;
} elseif (in_array($name, $this->extraRowColumns)) {
$columns[$key] = new DataColumn(['attribute' => $name]);
}
}
}
$groups = [];
// values for the first row
$values = $this->getRowValues($columns, $models[0]);
foreach ($values as $key => $value) {
$groups[$key][] = [
'value' => $value,
'column' => $key,
'start' => 0
];
}
// calculate totals for the first row
$totals = [];
// iterate through models
foreach ($models as $index => $model) {
// save row values in array
$rowValues = $this->getRowValues($columns, $model, $index);
// define if any change occurred. Need this extra foreach for correctly process extraRows
$changedColumns = [];
foreach ($rowValues as $name => $value) {
$previous = end($groups[$name]);
if ($value != $previous['value']) {
$changedColumns[] = $name;
}
}
// if this flag is true we will write change for all grouped columns. Its required when a change of any
// column from extraRowColumns occurs
$extraRowColumnChanged = (count(array_intersect($changedColumns, $this->extraRowColumns)) > 0);
// this changeOccured related to foreach below. It is required only for mergeType == self::MERGE_NESTED,
// to write change for all nested columns when change of previous column occurred
$changeOccurred = false;
foreach ($rowValues as $name => $value) {
// value changed
$valueChanged = in_array($name, $changedColumns);
//change already occured in this loop and mergeType set to MERGE_NESTED
$saveChange = $valueChanged || ($changeOccurred && $this->type == self::MERGE_NESTED);
if ($extraRowColumnChanged || $saveChange) {
$changeOccurred = true;
$lastIndex = count($groups[$name]) - 1;
//finalize prev group
$groups[$name][$lastIndex]['end'] = $index - 1;
$groups[$name][$lastIndex]['totals'] = $totals;
//begin new group
$groups[$name][] = array(
'start' => $index,
'column' => $name,
'value' => $value,
);
}
}
if($extraRowColumnChanged) {
$totals = [];
}
$totals = $this->getExtraRowTotals($model, $index, $totals);
}
// finalize group for last row
foreach ($groups as $name => $value) {
$lastIndex = count($groups[$name]) - 1;
$groups[$name][$lastIndex]['end'] = count($models) - 1;
$groups[$name][$lastIndex]['totals'] = $totals;
}
$this->_groups = $groups;
}
/**
* @inheritdoc
*/
public function renderTableRow($model, $key, $index)
{
$rows = [];
if ($this->rowOptions instanceof Closure) {
$options = call_user_func($this->rowOptions, $model, $key, $index, $this);
} else {
$options = $this->rowOptions;
}
$options['data-key'] = is_array($key) ? json_encode($key) : (string)$key;
$cells = [];
/** @var \yii\grid\Column $column */
foreach ($this->columns as $column) {
$name = property_exists($column, 'attribute') ? $column->attribute : null;
$isGroupColumn = in_array($name, $this->mergeColumns);
if (!$isGroupColumn) {
$cells[] = $column->renderDataCell($model, $key, $index);
continue;
}
$edge = $this->isGroupEdge($name, $index);
switch ($this->type) {
case static::MERGE_SIMPLE:
case static::MERGE_NESTED:
if (isset($edge['start'])) {
$column->contentOptions['rowspan'] = $edge['group']['end'] - $edge['group']['start'] + 1;
Html::addCssClass($column->contentOptions, $this->mergeCellClass);
$cells[] = $column->renderDataCell($model, $key, $index);
}
break;
case static::MERGE_FIRST_ROW:
$cells[] = isset($edge['start']) ? $column->renderDataCell($model, $key, $index) : '<td></td>';
}
}
$rows[] = Html::tag('tr', implode('', $cells), $options);
$extraRowEdge = null;
if (count($this->extraRowColumns)) {
$extraRowEdge = $this->isGroupEdge($this->extraRowColumns[0], $index);
if ($this->extraRowPosition == static::POS_ABOVE && isset($extraRowEdge['start'])) {
array_unshift($rows, $this->renderExtraRow($model, $key, $index, $extraRowEdge['group']['totals']));
}
if ($this->extraRowPosition == static::POS_BELOW && isset($extraRowEdge['end'])) {
$rows[] = $this->renderExtraRow($model, $key, $index, $extraRowEdge['group']['totals']);
}
}
return implode('', $rows);
}
/**
* Renders extra row when required
* @param mixed $model
* @param mixed $key
* @param int $index
* @param number $totals
* @return string the extra row
*/
protected function renderExtraRow($model, $key, $index, $totals)
{
if ($this->extraRowValue instanceof Closure) {
$content = call_user_func($this->extraRowValue, $model, $index, $totals);
} else {
$values = [];
foreach ($this->extraRowColumns as $name) {
$values[] = ArrayHelper::getValue($model, $name);
}
$content = '<strong>' . implode(' :: ', $values) . '</strong>';
}
$colspan = count($this->columns);
$cell = Html::tag('td', $content, ['class' => $this->extraRowClass, 'colspan' => $colspan]);
return Html::tag('tr', $cell);
}
/**
* Is current row start or end of group in particular column
* @param string $name the column name
* @param int $row the row index
* @return array
*/
protected function isGroupEdge($name, $row)
{
$result = array();
foreach ($this->_groups[$name] as $column) {
if ($column['start'] == $row) {
$result['start'] = $row;
$result['group'] = $column;
}
if ($column['end'] == $row) {
$result['end'] = $row;
$result['group'] = $column;
}
if (count($result)) break;
}
return $result;
}
/**
* If there is a Closure will return the newly calculated totals on the Closure according to the code specified by
* the user.
* @param mixed $model the data model being rendered
* @param int $index the zero-based index of the data model among the models array
* @param array $totals the calculated totals by the Closure
* @return array|mixed
*/
protected function getExtraRowTotals($model, $index, $totals)
{
return $this->extraRowTotalsValue instanceof Closure
? call_user_func($this->extraRowTotalsValue, $model, $index, $totals)
: [];
}
/**
* Returns the row values of the column
* @param array $columns the columns
* @param mixed $model the model
* @param int $index the zero-base index of the model among the models array
* @return array
*/
protected function getRowValues($columns, $model, $index = 0)
{
$values = [];
$keys = $this->dataProvider->getKeys();
foreach ($columns as $column) {
/** @var \kartik\grid\DataColumn $column */
if ($column instanceof DataColumn) { // we only work with DataColumn types
$values[$column->attribute] = $this->getColumnDataCellContent($column, $model, $keys[$index], $index);
}
}
return $values;
}
/**
* Returns the column data cell content
* @param \kartik\grid\DataColumn $column
* @param mixed $model the data model being rendered
* @param mixed $key the key associated with the data model
* @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]]
* @return string the rendering result
*/
protected function getColumnDataCellContent($column, $model, $key, $index)
{
if ($column->content === null) {
return $this->formatter->format($this->getColumnDataCellValue($column, $model, $key, $index), $column->format);
} else {
if ($column->content !== null) {
return call_user_func($column->content, $model, $key, $index, $column);
} else {
return $this->emptyCell;
}
}
}
/**
* Returns the column data cell value
* @param \kartik\grid\DataColumn $column
* @param mixed $model the data model being rendered
* @param mixed $key the key associated with the data model
* @param int $index the zero-based index of the data model among the models array
* @return mixed|null the result
*/
protected function getColumnDataCellValue($column, $model, $key, $index)
{
if ($column->value !== null) {
if (is_string($column->value)) {
return ArrayHelper::getValue($model, $column->value);
} else {
return call_user_func($column->value, $model, $index, $column);
}
} elseif ($column->attribute !== null) {
return ArrayHelper::getValue($model, $column->attribute);
}
return null;
}
}