|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\backend\widgets; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use yii\base\Widget; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
use yii\helpers\Html; |
|
9
|
|
|
use yii\base\InvalidParamException; |
|
10
|
|
|
use app\modules\shop\models\Category; |
|
11
|
|
|
use kartik\icons\Icon; |
|
12
|
|
|
|
|
13
|
|
|
class CategoryMovementsButtons extends Widget |
|
14
|
|
|
{ |
|
15
|
|
|
const MOVE_ACTION = 'move-selected'; |
|
16
|
|
|
const ADD_ACTION = 'add-selected'; |
|
17
|
|
|
public $url; |
|
18
|
|
|
public $gridSelector; |
|
19
|
|
|
public $wrapperClass = ''; |
|
20
|
|
|
public $addText = ''; |
|
21
|
|
|
public $moveText = ''; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array here you can define buttons classes like this: |
|
24
|
|
|
* [ |
|
25
|
|
|
* 'add-class' => 'btn btn-danger', //class for add button |
|
26
|
|
|
* 'move-class' => 'btn btn-danger', //class for move button |
|
27
|
|
|
* ] |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
public $htmlOptions = []; |
|
31
|
|
|
private $modalSelector = '#mass-categories-actions'; |
|
32
|
|
|
private static $categories = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritdoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function init() |
|
38
|
|
|
{ |
|
39
|
|
|
if (!isset($this->url, $this->gridSelector)) { |
|
40
|
|
|
throw new InvalidParamException('Attribute \'url\' or \'gridSelector\' is not set'); |
|
41
|
|
|
} |
|
42
|
|
|
if (true === empty($this->moveText)) { |
|
43
|
|
|
$this->moveText = Yii::t('app', 'Are you sure you want to move all selected products into'); |
|
44
|
|
|
} |
|
45
|
|
|
if (true === empty($this->addText)) { |
|
46
|
|
|
$this->addText = Yii::t('app', 'Are you sure you want to add all selected products into'); |
|
47
|
|
|
} |
|
48
|
|
|
if (!isset($this->htmlOptions['id'])) { |
|
49
|
|
|
$this->htmlOptions['id'] = ''; |
|
50
|
|
|
} |
|
51
|
|
|
if (true === empty(static::$categories)) { |
|
|
|
|
|
|
52
|
|
|
$cc = Category::find() |
|
53
|
|
|
->select('id, name') |
|
54
|
|
|
->where(['active' => 1]) |
|
55
|
|
|
->asArray(true) |
|
56
|
|
|
->all(); |
|
57
|
|
|
foreach ($cc as $k => $cat) { |
|
58
|
|
|
static::$categories[$cat['id']] = $cat['name']; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function run() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->registerScript(); |
|
69
|
|
|
return $this->renderButtons(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function renderButtons() |
|
76
|
|
|
{ |
|
77
|
|
|
$buttons = [ |
|
78
|
|
|
self::ADD_ACTION => Html::button( |
|
79
|
|
|
Icon::show('plus') . ' ' . |
|
80
|
|
|
Yii::t('app', 'Add selected to:'), |
|
81
|
|
|
[ |
|
82
|
|
|
'class' => isset($this->htmlOptions['add-class']) ? |
|
83
|
|
|
$this->htmlOptions['add-class'] : 'btn btn-default', |
|
84
|
|
|
'disabled' => 'disabled', |
|
85
|
|
|
'data-mc-action' => self::ADD_ACTION |
|
86
|
|
|
]), |
|
87
|
|
|
self::MOVE_ACTION => Html::button( |
|
88
|
|
|
Icon::show('arrows') . ' ' . |
|
89
|
|
|
Yii::t('app', 'Move selected to:'), |
|
90
|
|
|
[ |
|
91
|
|
|
'class' => isset($this->htmlOptions['move-class']) ? |
|
92
|
|
|
$this->htmlOptions['move-class'] : 'btn btn-default', |
|
93
|
|
|
'disabled' => 'disabled', |
|
94
|
|
|
'data-mc-action' => self::MOVE_ACTION |
|
95
|
|
|
]), |
|
96
|
|
|
|
|
97
|
|
|
]; |
|
98
|
|
|
$group = ''; |
|
99
|
|
|
foreach ($buttons as $id => $button) { |
|
100
|
|
|
$group .= Html::tag('div', |
|
101
|
|
|
Html::tag('div', |
|
102
|
|
|
$button . "\n\t" . Html::tag('div', |
|
103
|
|
|
Html::dropDownList(null, null, static::$categories, [ |
|
|
|
|
|
|
104
|
|
|
'prompt' => Yii::t('app', 'Select category'), |
|
105
|
|
|
'class' => 'form-control', |
|
106
|
|
|
'id' => $id, |
|
107
|
|
|
]), [ |
|
108
|
|
|
'class' => 'input-group', |
|
109
|
|
|
]), [ |
|
110
|
|
|
'class' => 'btn-group', |
|
111
|
|
|
]), [ |
|
112
|
|
|
'class' => 'col-xs-12 col-sm-6', |
|
113
|
|
|
]); |
|
114
|
|
|
}; |
|
115
|
|
|
return Html::tag('div', $group, ['class' => 'row m-bottom-10']); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function registerScript() |
|
122
|
|
|
{ |
|
123
|
|
|
$JS = <<<JS |
|
124
|
|
|
$("#%1\$s, #%2\$s").change(function (){ |
|
125
|
|
|
var \$this = $(this), |
|
126
|
|
|
buttonSelector = \$this.attr('id'), |
|
127
|
|
|
\$button = $('button[data-mc-action="' + buttonSelector + '"]'), |
|
128
|
|
|
\$selected = $('option:selected', \$this), |
|
129
|
|
|
categoryId = parseInt(\$selected.val()), |
|
130
|
|
|
categoryName = \$selected.text(), |
|
131
|
|
|
items = $("{$this->gridSelector}").yiiGridView('getSelectedRows'); |
|
132
|
|
|
if (items.length) { |
|
133
|
|
|
if (false === isNaN(categoryId)) { |
|
134
|
|
|
\$button.attr("disabled", false); |
|
135
|
|
|
\$button.data('cat-id', categoryId) |
|
136
|
|
|
.data('cat-name', categoryName) |
|
137
|
|
|
.data('items', items); |
|
138
|
|
|
} else { |
|
139
|
|
|
\$button.attr("disabled", true); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
}); |
|
143
|
|
|
$('button[data-mc-action]').click(function () { |
|
144
|
|
|
var \$this = $(this), |
|
145
|
|
|
\$modal = $("{$this->modalSelector}"), |
|
146
|
|
|
action = \$this.data('mc-action'), |
|
147
|
|
|
\$textContainer = $('#mass-categories-action-modal-text', \$modal), |
|
148
|
|
|
categoryId = parseInt(\$this.data('cat-id')), |
|
149
|
|
|
categoryName = \$this.data('cat-name'), |
|
150
|
|
|
items = \$this.data('items'); |
|
151
|
|
|
\$textContainer.closest('.alert').removeClass('alert-danger').addClass('alert-info'); |
|
152
|
|
|
if (false === isNaN(categoryId)) { |
|
153
|
|
|
switch (action) { |
|
154
|
|
|
case '%1\$s' : |
|
155
|
|
|
\$textContainer.text('{$this->addText} ' + categoryName + '?'); |
|
156
|
|
|
break; |
|
157
|
|
|
case '%2\$s' : |
|
158
|
|
|
\$textContainer.text('{$this->moveText} ' + categoryName + '?'); |
|
159
|
|
|
break; |
|
160
|
|
|
} |
|
161
|
|
|
\$modal.data('url', "{$this->url}") |
|
162
|
|
|
.data('items', items) |
|
163
|
|
|
.data('cat-id', categoryId) |
|
164
|
|
|
.data('mc-action', action) |
|
165
|
|
|
.modal('show'); |
|
166
|
|
|
} |
|
167
|
|
|
return false; |
|
168
|
|
|
}); |
|
169
|
|
|
JS; |
|
170
|
|
|
$this->view->registerJs(sprintf($JS, self::ADD_ACTION, self::MOVE_ACTION)); |
|
171
|
|
|
} |
|
172
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: