-
Notifications
You must be signed in to change notification settings - Fork 4
/
GridBulkActions.php
174 lines (146 loc) · 3.4 KB
/
GridBulkActions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace webvimark\extensions\GridBulkActions;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Url;
use Yii;
class GridBulkActions extends Widget
{
/**
* @var array
*/
public $actions;
/**
* @var string
*/
public $gridId;
/**
* Default - $this->gridId . '-pjax'
*
* @var string
*/
public $pjaxId;
/**
* @var string
*/
public $okButtonClass = 'btn btn-sm btn-default';
/**
* @var string
*/
public $dropDownClass = 'form-control input-sm';
/**
* @var string
*/
public $wrapperClass = 'form-inline';
/**
* @var string
*/
public $promptText;
/**
* @var string
*/
public $confirmationText;
/**
* Multilingual support
*/
public function init()
{
parent::init();
$this->promptText = $this->promptText ? $this->promptText : GridBulkActions::t('app', '--- With selected ---');
$this->confirmationText = $this->confirmationText ? $this->confirmationText : GridBulkActions::t('app', 'Delete elements?');
}
/**
* @param string $category
* @param string $message
* @param array $params
* @param null $language
*
* @return string
*/
public static function t($category, $message, $params = [], $language = null)
{
if ( !isset(Yii::$app->i18n->translations['widgets/GridBulkActions/*']) )
{
Yii::$app->i18n->translations['widgets/GridBulkActions/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => __DIR__ . '/messages',
'fileMap' => [
'widgets/GridBulkActions/app' => 'app.php',
],
];
}
return Yii::t('widgets/GridBulkActions/' . $category, $message, $params, $language);
}
/**
* @throws \yii\base\InvalidConfigException
* @return string
*/
public function run()
{
if ( ! $this->gridId )
{
throw new InvalidConfigException('Missing gridId param');
}
$this->setDefaultOptions();
$this->view->registerJs($this->js());
return $this->render('index');
}
/**
* Set default options
*/
protected function setDefaultOptions()
{
if ( ! $this->actions )
{
$this->actions = [
Url::to(['bulk-activate'])=>GridBulkActions::t('app', 'Activate'),
Url::to(['bulk-deactivate'])=>GridBulkActions::t('app', 'Deactivate'),
'----'=>[
Url::to(['bulk-delete'])=>GridBulkActions::t('app', 'Delete'),
],
];
}
if ( ! $this->pjaxId )
{
$this->pjaxId = $this->gridId . '-pjax';
}
$this->gridId = ltrim($this->gridId, '#');
$this->pjaxId = ltrim($this->pjaxId, '#');
}
/**
* @return string
*/
protected function js()
{
$js = <<<JS
// Select values in bulk actions list
$(document).off('change', '[name="grid-bulk-actions"]').on('change', '[name="grid-bulk-actions"]', function () {
var _t = $(this);
var okButton = $(_t.data('ok-button'));
if (_t.val()) {
okButton.removeClass('disabled');
}
else {
okButton.addClass('disabled');
}
});
// Clicking OK button
$(document).off('click', '.grid-bulk-ok-button').on('click', '.grid-bulk-ok-button', function () {
var _t = $(this);
var list = $(_t.data('list'));
if (list.val().indexOf('bulk-delete') >= 0) {
if ( ! confirm('$this->confirmationText') )
return false;
}
$.post(list.val(), $(_t.data('grid') + ' [name="selection[]"]').serialize() )
.done(function(){
_t.addClass('disabled');
list.val('');
$.pjax.reload({container: _t.data('pjax')});
});
});
JS;
return $js;
}
}