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