1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\grid; |
9
|
|
|
|
10
|
|
|
use Closure; |
11
|
|
|
use yii\base\Model; |
12
|
|
|
use yii\data\ActiveDataProvider; |
13
|
|
|
use yii\data\ArrayDataProvider; |
14
|
|
|
use yii\db\ActiveQueryInterface; |
15
|
|
|
use yii\db\ActiveRecord; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
use yii\helpers\Html; |
18
|
|
|
use yii\helpers\Inflector; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DataColumn is the default column type for the [[GridView]] widget. |
22
|
|
|
* |
23
|
|
|
* It is used to show data columns and allows [[enableSorting|sorting]] and [[filter|filtering]] them. |
24
|
|
|
* |
25
|
|
|
* A simple data column definition refers to an attribute in the data model of the |
26
|
|
|
* GridView's data provider. The name of the attribute is specified by [[attribute]]. |
27
|
|
|
* |
28
|
|
|
* By setting [[value]] and [[label]], the header and cell content can be customized. |
29
|
|
|
* |
30
|
|
|
* A data column differentiates between the [[getDataCellValue|data cell value]] and the |
31
|
|
|
* [[renderDataCellContent|data cell content]]. The cell value is an un-formatted value that |
32
|
|
|
* may be used for calculation, while the actual cell content is a [[format|formatted]] version of that |
33
|
|
|
* value which may contain HTML markup. |
34
|
|
|
* |
35
|
|
|
* For more details and usage information on DataColumn, see the [guide article on data widgets](guide:output-data-widgets). |
36
|
|
|
* |
37
|
|
|
* @author Qiang Xue <[email protected]> |
38
|
|
|
* @since 2.0 |
39
|
|
|
*/ |
40
|
|
|
class DataColumn extends Column |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var string the attribute name associated with this column. When neither [[content]] nor [[value]] |
44
|
|
|
* is specified, the value of the specified attribute will be retrieved from each data model and displayed. |
45
|
|
|
* |
46
|
|
|
* Also, if [[label]] is not specified, the label associated with the attribute will be displayed. |
47
|
|
|
*/ |
48
|
|
|
public $attribute; |
49
|
|
|
/** |
50
|
|
|
* @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting |
51
|
|
|
* link label when sorting is enabled for this column. |
52
|
|
|
* If it is not set and the models provided by the GridViews data provider are instances |
53
|
|
|
* of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]]. |
54
|
|
|
* Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label. |
55
|
|
|
*/ |
56
|
|
|
public $label; |
57
|
|
|
/** |
58
|
|
|
* @var bool whether the header label should be HTML-encoded. |
59
|
|
|
* @see label |
60
|
|
|
* @since 2.0.1 |
61
|
|
|
*/ |
62
|
|
|
public $encodeLabel = true; |
63
|
|
|
/** |
64
|
|
|
* @var string|Closure an anonymous function or a string that is used to determine the value to display in the current column. |
65
|
|
|
* |
66
|
|
|
* If this is an anonymous function, it will be called for each row and the return value will be used as the value to |
67
|
|
|
* display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`. |
68
|
|
|
* Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered |
69
|
|
|
* and `$column` is a reference to the [[DataColumn]] object. |
70
|
|
|
* |
71
|
|
|
* You may also set this property to a string representing the attribute name to be displayed in this column. |
72
|
|
|
* This can be used when the attribute to be displayed is different from the [[attribute]] that is used for |
73
|
|
|
* sorting and filtering. |
74
|
|
|
* |
75
|
|
|
* If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]]. |
76
|
|
|
*/ |
77
|
|
|
public $value; |
78
|
|
|
/** |
79
|
|
|
* @var string|array|Closure in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`, |
80
|
|
|
* `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by |
81
|
|
|
* the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when |
82
|
|
|
* [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView. |
83
|
|
|
* @see \yii\i18n\Formatter::format() |
84
|
|
|
*/ |
85
|
|
|
public $format = 'text'; |
86
|
|
|
/** |
87
|
|
|
* @var bool whether to allow sorting by this column. If true and [[attribute]] is found in |
88
|
|
|
* the sort definition of [[GridView::dataProvider]], then the header cell of this column |
89
|
|
|
* will contain a link that may trigger the sorting when being clicked. |
90
|
|
|
*/ |
91
|
|
|
public $enableSorting = true; |
92
|
|
|
/** |
93
|
|
|
* @var array the HTML attributes for the link tag in the header cell |
94
|
|
|
* generated by [[\yii\data\Sort::link]] when sorting is enabled for this column. |
95
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
96
|
|
|
*/ |
97
|
|
|
public $sortLinkOptions = []; |
98
|
|
|
/** |
99
|
|
|
* @var string|array|null|false the HTML code representing a filter input (e.g. a text field, a dropdown list) |
100
|
|
|
* that is used for this data column. This property is effective only when [[GridView::filterModel]] is set. |
101
|
|
|
* |
102
|
|
|
* - If this property is not set, a text field will be generated as the filter input; |
103
|
|
|
* - If this property is an array, a dropdown list will be generated that uses this property value as |
104
|
|
|
* the list options. |
105
|
|
|
* - If you don't want a filter for this data column, set this value to be false. |
106
|
|
|
*/ |
107
|
|
|
public $filter; |
108
|
|
|
/** |
109
|
|
|
* @var array the HTML attributes for the filter input fields. This property is used in combination with |
110
|
|
|
* the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to |
111
|
|
|
* render the HTML attributes for the generated filter input fields. |
112
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
113
|
|
|
*/ |
114
|
|
|
public $filterInputOptions = ['class' => 'form-control', 'id' => null]; |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritdoc |
119
|
|
|
*/ |
120
|
|
|
protected function renderHeaderCellContent() |
121
|
|
|
{ |
122
|
|
|
if ($this->header !== null || $this->label === null && $this->attribute === null) { |
123
|
|
|
return parent::renderHeaderCellContent(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$label = $this->getHeaderCellLabel(); |
127
|
|
|
if ($this->encodeLabel) { |
128
|
|
|
$label = Html::encode($label); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($this->attribute !== null && $this->enableSorting && |
132
|
|
|
($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) { |
133
|
|
|
return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label])); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $label; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritdoc |
141
|
|
|
* @since 2.0.8 |
142
|
|
|
*/ |
143
|
2 |
|
protected function getHeaderCellLabel() |
144
|
|
|
{ |
145
|
2 |
|
$provider = $this->grid->dataProvider; |
146
|
|
|
|
147
|
2 |
|
if ($this->label !== null) { |
148
|
|
|
return $this->label; |
149
|
|
|
} |
150
|
2 |
|
if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) { |
151
|
|
|
/* @var $modelClass Model */ |
152
|
|
|
$modelClass = $provider->query->modelClass; |
|
|
|
|
153
|
|
|
if ($this->grid->filterModel !== null && $this->grid->filterModel instanceof $modelClass) { |
154
|
|
|
return $this->grid->filterModel->getAttributeLabel($this->attribute); |
155
|
|
|
} |
156
|
|
|
/* @var $model ActiveRecord */ |
157
|
|
|
$model = $modelClass::instance(); |
158
|
|
|
|
159
|
|
|
return $model->getAttributeLabel($this->attribute); |
160
|
|
|
} |
161
|
2 |
|
if ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) { |
162
|
|
|
/* @var $modelClass Model */ |
163
|
1 |
|
$modelClass = $provider->modelClass; |
164
|
1 |
|
if ($this->grid->filterModel !== null && $this->grid->filterModel instanceof $modelClass) { |
165
|
|
|
return $this->grid->filterModel->getAttributeLabel($this->attribute); |
166
|
|
|
} |
167
|
|
|
/* @var $model Model */ |
168
|
1 |
|
$model = $modelClass::instance(); |
169
|
|
|
|
170
|
1 |
|
return $model->getAttributeLabel($this->attribute); |
171
|
|
|
} |
172
|
1 |
|
if ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) { |
173
|
1 |
|
return $this->grid->filterModel->getAttributeLabel($this->attribute); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$models = $provider->getModels(); |
177
|
|
|
if (($model = reset($models)) instanceof Model) { |
178
|
|
|
/* @var $model Model */ |
179
|
|
|
return $model->getAttributeLabel($this->attribute); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return Inflector::camel2words($this->attribute); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @inheritdoc |
187
|
|
|
*/ |
188
|
3 |
|
protected function renderFilterCellContent() |
189
|
|
|
{ |
190
|
3 |
|
if (is_string($this->filter)) { |
191
|
1 |
|
return $this->filter; |
192
|
|
|
} |
193
|
|
|
|
194
|
2 |
|
$model = $this->grid->filterModel; |
195
|
|
|
|
196
|
2 |
|
if ($this->filter !== false && $model instanceof Model && $this->attribute !== null && $model->isAttributeActive($this->attribute)) { |
197
|
2 |
|
if ($model->hasErrors($this->attribute)) { |
198
|
|
|
Html::addCssClass($this->filterOptions, 'has-error'); |
199
|
|
|
$error = ' ' . Html::error($model, $this->attribute, $this->grid->filterErrorOptions); |
200
|
|
|
} else { |
201
|
2 |
|
$error = ''; |
202
|
|
|
} |
203
|
2 |
|
if (is_array($this->filter)) { |
204
|
1 |
|
$options = array_merge(['prompt' => ''], $this->filterInputOptions); |
205
|
1 |
|
return Html::activeDropDownList($model, $this->attribute, $this->filter, $options) . $error; |
206
|
1 |
|
} elseif ($this->format === 'boolean') { |
207
|
1 |
|
$options = array_merge(['prompt' => ''], $this->filterInputOptions); |
208
|
1 |
|
return Html::activeDropDownList($model, $this->attribute, [ |
209
|
1 |
|
$this->grid->formatter->booleanFormat[0], |
210
|
1 |
|
$this->grid->formatter->booleanFormat[1], |
211
|
1 |
|
], $options) . $error; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return Html::activeTextInput($model, $this->attribute, $this->filterInputOptions) . $error; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return parent::renderFilterCellContent(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Returns the data cell value. |
222
|
|
|
* @param mixed $model the data model |
223
|
|
|
* @param mixed $key the key associated with the data model |
224
|
|
|
* @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]]. |
225
|
|
|
* @return string the data cell value |
226
|
|
|
*/ |
227
|
|
|
public function getDataCellValue($model, $key, $index) |
228
|
|
|
{ |
229
|
|
|
if ($this->value !== null) { |
230
|
|
|
if (is_string($this->value)) { |
231
|
|
|
return ArrayHelper::getValue($model, $this->value); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return call_user_func($this->value, $model, $key, $index, $this); |
235
|
|
|
} elseif ($this->attribute !== null) { |
236
|
|
|
return ArrayHelper::getValue($model, $this->attribute); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return null; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @inheritdoc |
244
|
|
|
*/ |
245
|
|
|
protected function renderDataCellContent($model, $key, $index) |
246
|
|
|
{ |
247
|
|
|
if ($this->content === null) { |
248
|
|
|
return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return parent::renderDataCellContent($model, $key, $index); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: