forked from kartik-v/yii2-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormulaColumn.php
104 lines (95 loc) · 3.19 KB
/
FormulaColumn.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
<?php
/**
* @package yii2-grid
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2015
* @version 3.0.0
*/
namespace kartik\grid;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* A FormulaColumn to calculate values based on other column indexes
* for the Grid widget [[\kartik\widgets\GridView]]
*
* DataColumn is the default column type for the [[GridView]] widget.
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.0
*/
class FormulaColumn extends DataColumn
{
const SUMMARY = -10000;
const FOOTER = -20000;
/**
* @var boolean automatically generate the footer. If set to `true`, it will
* use the same formula to generate the footer. If set to `false`, will use
* the default footer.
*/
public $autoFooter = true;
/**
* Gets the value of a column
*
* @param integer $i the index of the grid column (the first column
* in the grid will be zero indexed). Note a column's index is to be
* considered, even if the `visible` property is set to false.
* @param array $params which will contain these keys:
* - model: mixed the data model being rendered
* - key: mixed the key associated with the data model
* - index: integer the zero-based index of the data item among
* the item array returned by [[GridView::dataProvider]].
* - widget: the current column widget instance
* @throws InvalidConfigException
*/
public function col($i, $params = [])
{
if (empty($this->grid->columns[$i])) {
throw new InvalidConfigException("Invalid column index {$i} used in FormulaColumn.");
}
if (!isset($this->value) || !$this->value instanceof \Closure) {
throw new InvalidConfigException(
"The 'value' must be set and defined as a `Closure` function for a FormulaColumn."
);
}
$col = $this->grid->columns[$i];
if ($col === $this) {
throw new InvalidConfigException("Self-referencing FormulaColumn at column {$i}.");
}
$model = null;
$key = null;
$index = null;
extract($params);
if ($index == self::SUMMARY) {
return $col->getPageSummaryCellContent();
} elseif ($index == self::FOOTER) {
return $col->getFooterCellContent();
} else {
return $col->getDataCellValue($model, $key, $index);
}
}
/**
* Get the raw footer cell content.
*
* @return string the rendering result
*/
protected function getFooterCellContent()
{
if ($this->autoFooter) {
return call_user_func($this->value, null, self::FOOTER, self::FOOTER, $this);
}
return parent::getFooterCellContent();
}
/**
* Formatted footer cell content.
*
* @return string the rendering result
*/
protected function renderFooterCellContent()
{
if ($this->autoFooter) {
return $this->grid->formatter->format($this->getFooterCellContent(), $this->format);
}
return parent::renderFooterCellContent();
}
}