Completed
Pull Request — 2.1 (#12704)
by Robert
08:59
created

DataColumn   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 37.93%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 10
dl 0
loc 195
ccs 22
cts 58
cp 0.3793
rs 9.6
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataCellValue() 0 13 4
A renderDataCellContent() 0 8 2
B renderHeaderCellContent() 0 18 9
D getHeaderCellLabel() 0 30 9
C renderFilterCellContent() 0 25 8
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
 * @author Qiang Xue <[email protected]>
34
 * @since 2.0
35
 */
36
class DataColumn extends Column
37
{
38
    /**
39
     * @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
40
     * is specified, the value of the specified attribute will be retrieved from each data model and displayed.
41
     *
42
     * Also, if [[label]] is not specified, the label associated with the attribute will be displayed.
43
     */
44
    public $attribute;
45
    /**
46
     * @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting
47
     * link label when sorting is enabled for this column.
48
     * If it is not set and the models provided by the GridViews data provider are instances
49
     * of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]].
50
     * Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label.
51
     */
52
    public $label;
53
    /**
54
     * @var boolean whether the header label should be HTML-encoded.
55
     * @see label
56
     * @since 2.0.1
57
     */
58
    public $encodeLabel = true;
59
    /**
60
     * @var string|\Closure an anonymous function or a string that is used to determine the value to display in the current column.
61
     *
62
     * If this is an anonymous function, it will be called for each row and the return value will be used as the value to
63
     * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`.
64
     * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
65
     * and `$column` is a reference to the [[DataColumn]] object.
66
     *
67
     * You may also set this property to a string representing the attribute name to be displayed in this column.
68
     * This can be used when the attribute to be displayed is different from the [[attribute]] that is used for
69
     * sorting and filtering.
70
     *
71
     * If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]].
72
     */
73
    public $value;
74
    /**
75
     * @var string|array in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`,
76
     * `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by
77
     * the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when
78
     * [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView.
79
     */
80
    public $format = 'text';
81
    /**
82
     * @var boolean whether to allow sorting by this column. If true and [[attribute]] is found in
83
     * the sort definition of [[GridView::dataProvider]], then the header cell of this column
84
     * will contain a link that may trigger the sorting when being clicked.
85
     */
86
    public $enableSorting = true;
87
    /**
88
     * @var array the HTML attributes for the link tag in the header cell
89
     * generated by [[\yii\data\Sort::link]] when sorting is enabled for this column.
90
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
91
     */
92
    public $sortLinkOptions = [];
93
    /**
94
     * @var string|array|null|false the HTML code representing a filter input (e.g. a text field, a dropdown list)
95
     * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set.
96
     *
97
     * - If this property is not set, a text field will be generated as the filter input;
98
     * - If this property is an array, a dropdown list will be generated that uses this property value as
99
     *   the list options.
100
     * - If you don't want a filter for this data column, set this value to be false.
101
     */
102
    public $filter;
103
    /**
104
     * @var array the HTML attributes for the filter input fields. This property is used in combination with
105
     * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to
106
     * render the HTML attributes for the generated filter input fields.
107
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
108
     */
109
    public $filterInputOptions = ['class' => 'form-control', 'id' => null];
110
111
112
    /**
113
     * @inheritdoc
114
     */
115
    protected function renderHeaderCellContent()
116
    {
117
        if ($this->header !== null || $this->label === null && $this->attribute === null) {
118
            return parent::renderHeaderCellContent();
119
        }
120
121
        $label = $this->getHeaderCellLabel();
122
        if ($this->encodeLabel) {
123
            $label = Html::encode($label);
124
        }
125
126
        if ($this->attribute !== null && $this->enableSorting &&
127
            ($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
128
            return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label]));
129
        } else {
130
            return $label;
131
        }
132
    }
133
134
    /**
135
     * @inheritdoc
136
     * @since 2.0.8
137
     */
138 2
    protected function getHeaderCellLabel()
139
    {
140 2
        $provider = $this->grid->dataProvider;
141
142 2
        if ($this->label === null) {
143 2
            if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
144
                /* @var $model Model */
145
                $model = new $provider->query->modelClass;
0 ignored issues
show
Bug introduced by
Accessing modelClass on the interface yii\db\ActiveQueryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
146
                $label = $model->getAttributeLabel($this->attribute);
147 2
            } elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
148
                /* @var $model Model */
149 1
                $model = new $provider->modelClass;
150 1
                $label = $model->getAttributeLabel($this->attribute);
151 2
            } elseif ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) {
152 1
                $label = $this->grid->filterModel->getAttributeLabel($this->attribute);
153 1
            } else {
154
                $models = $provider->getModels();
155
                if (($model = reset($models)) instanceof Model) {
156
                    /* @var $model Model */
157
                    $label = $model->getAttributeLabel($this->attribute);
158
                } else {
159
                    $label = Inflector::camel2words($this->attribute);
160
                }
161
            }
162 2
        } else {
163
            $label = $this->label;
164
        }
165
166 2
        return $label;
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172 2
    protected function renderFilterCellContent()
173
    {
174 2
        if (is_string($this->filter)) {
175 1
            return $this->filter;
176
        }
177
178 1
        $model = $this->grid->filterModel;
179
180 1
        if ($this->filter !== false && $model instanceof Model && $this->attribute !== null && $model->isAttributeActive($this->attribute)) {
181 1
            if ($model->hasErrors($this->attribute)) {
182
                Html::addCssClass($this->filterOptions, 'has-error');
183
                $error = ' ' . Html::error($model, $this->attribute, $this->grid->filterErrorOptions);
184
            } else {
185 1
                $error = '';
186
            }
187 1
            if (is_array($this->filter)) {
188 1
                $options = array_merge(['prompt' => ''], $this->filterInputOptions);
189 1
                return Html::activeDropDownList($model, $this->attribute, $this->filter, $options) . $error;
190
            } else {
191
                return Html::activeTextInput($model, $this->attribute, $this->filterInputOptions) . $error;
192
            }
193
        } else {
194
            return parent::renderFilterCellContent();
195
        }
196
    }
197
198
    /**
199
     * Returns the data cell value.
200
     * @param mixed $model the data model
201
     * @param mixed $key the key associated with the data model
202
     * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
203
     * @return string the data cell value
204
     */
205
    public function getDataCellValue($model, $key, $index)
206
    {
207
        if ($this->value !== null) {
208
            if (is_string($this->value)) {
209
                return ArrayHelper::getValue($model, $this->value);
210
            } else {
211
                return call_user_func($this->value, $model, $key, $index, $this);
212
            }
213
        } elseif ($this->attribute !== null) {
214
            return ArrayHelper::getValue($model, $this->attribute);
215
        }
216
        return null;
217
    }
218
219
    /**
220
     * @inheritdoc
221
     */
222
    protected function renderDataCellContent($model, $key, $index)
223
    {
224
        if ($this->content === null) {
225
            return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
226
        } else {
227
            return parent::renderDataCellContent($model, $key, $index);
228
        }
229
    }
230
}
231