Completed
Push — 2.1 ( 75349f...bf116e )
by Alexander
29:27
created

DataColumn   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 75.81%

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 10
dl 0
loc 211
ccs 47
cts 62
cp 0.7581
rs 9.3999
c 0
b 0
f 0

5 Methods

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