Completed
Push — master ( f7fa95...7f6c7b )
by Andrii
13:26 queued 13s
created

FeaturedColumnTrait::init()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 9.4555
c 0
b 0
f 0
cc 5
nc 4
nop 0
crap 30
1
<?php
2
/**
3
 * Advanced Grid for Yii2.
4
 *
5
 * @link      https://github.com/hiqdev/yii2-higrid
6
 * @package   yii2-higrid
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\higrid;
12
13
use Closure;
14
use Yii;
15
use yii\helpers\ArrayHelper;
16
use yii\helpers\Json;
17
18
/**
19
 * Trait FeaturedColumnTrait.
20
 *
21
 * Gives the next features:
22
 * - popover text shown at header cell
23
 * - filterAttribute: to specify name of attribute used for filtering distinct from attribute
24
 * - resizable columns
25
 */
26
trait FeaturedColumnTrait
27
{
28
    /**
29
     * @var string Popover text
30
     */
31
    public $popover;
32
33
    /**
34
     * @var array Options to popover()
35
     */
36
    public $popoverOptions = [
37
        'placement' => 'bottom',
38
        'selector' => 'a',
39
    ];
40
41
    /**
42
     * Like a `DataColumn::value` for change the value of the column for export to CSV, TSV etc.
43
     *
44
     * @var string|Closure an anonymous function or a string that is used to determine the value to display in the export column.
45
     *
46
     * If this is an anonymous function, it will be called for each row and the return value will be used as the value to
47
     * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`.
48
     * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
49
     * and `$column` is a reference to the [[DataColumn]] object.
50
     */
51
    public $exportedValue;
52
53
    /**
54
     * A list of column names from the current grid that should be inserted instead of the current one during export
55
     */
56
    public array $exportedColumns = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function init()
62
    {
63
        parent::init();
64
65
        if ($this->grid->resizableColumns !== false && !isset($this->headerOptions['data-resizable-column-id'])) {
66
            $this->headerOptions['data-resizable-column-id'] = $this->attribute;
67
        }
68
69
        if ($this->hasProperty('defaultOptions')) {
70
            foreach ($this->defaultOptions as $k => $v) {
71
                $this->{$k} = ArrayHelper::merge($v, $this->{$k});
72
            }
73
        }
74
        $this->registerClientScript();
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function registerClientScript()
81
    {
82
        $view = Yii::$app->getView();
83
        $ops = Json::encode($this->popoverOptions);
84
        if (property_exists($this, 'grid')) {
85
            $view->registerJs("$('#{$this->grid->id} thead th[data-attribute=\"{$this->attribute}\"]').popover($ops);", \yii\web\View::POS_READY);
86
        } else {
87
            $view->registerJs("$('table[role=\"grid\"] thead th[data-attribute=\"{$this->attribute}\"]').popover($ops);", \yii\web\View::POS_READY);
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function renderHeaderCellContent()
95
    {
96
        /// XXX better change yii
97
        if ($this->hasProperty('attribute')) {
98
            $save = $this->attribute;
99
            $this->attribute = $this->getSortAttribute();
100
        }
101
102
        if ($this->popover) {
103
            $this->headerOptions = ArrayHelper::merge($this->headerOptions, [
104
                'data' => [
105
                    'toggle' => 'popover',
106
                    'trigger' => 'hover',
107
                    'content' => $this->popover,
108
                    'attribute' => $this->attribute,
109
                ],
110
            ]);
111
        }
112
113
        $res = parent::renderHeaderCellContent();
114
        if ($this->hasProperty('attribute')) {
115
            $this->attribute = $save;
116
        }
117
118
        return $res;
119
    }
120
121
    /**
122
     * @var string name for filter input
123
     */
124
    public $sortAttribute;
125
126
    /**
127
     * Getter for sortAttribute.
128
     */
129
    public function getSortAttribute()
130
    {
131
        if ($this->sortAttribute === false) {
132
            return false;
133
        }
134
135
        return $this->sortAttribute ?: $this->attribute;
136
    }
137
138
    /**
139
     * @var string name for filter input
140
     */
141
    public $filterAttribute;
142
143
    /**
144
     * Getter for filterAttribute.
145
     */
146
    public function getFilterAttribute()
147
    {
148
        return $this->filterAttribute ?: $this->attribute;
149
    }
150
151
    /**
152
     * @var string|array|boolean|callable the HTML code representing a filter input (e.g. a text field, a dropdown list)
153
     * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set.
154
     *
155
     * - If this property is not set, a text field will be generated as the filter input;
156
     * - If this property is an array, a dropdown list will be generated that uses this property value as
157
     *   the list options.
158
     * - If this property is a closure, it's output will be used as a filter value.
159
     * Example determination of closure: `function ($dataColumn, $model, $attribute) { ... }`
160
     * - If you don't want a filter for this data column, set this value to be false.
161
     */
162
    public $filter;
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    protected function renderFilterCellContent()
168
    {
169
        if ($this->filter instanceof Closure) {
170
            return call_user_func($this->filter, $this, $this->grid->filterModel, $this->attribute);
171
        }
172
        /// XXX better change yii
173
        if ($this->hasProperty('attribute')) {
174
            $save = $this->attribute;
175
            $this->attribute = $this->getFilterAttribute();
176
        }
177
        $out = parent::renderFilterCellContent();
178
        if ($this->hasProperty('attribute')) {
179
            $this->attribute = $save;
180
        }
181
182
        return $out;
183
    }
184
}
185