|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\backend\widgets; |
|
4
|
|
|
|
|
5
|
|
|
use yii\base\InvalidParamException; |
|
6
|
|
|
use yii\base\Widget; |
|
7
|
|
|
use yii\helpers\Html; |
|
8
|
|
|
use kartik\icons\Icon; |
|
9
|
|
|
use Yii; |
|
10
|
|
|
|
|
11
|
|
|
class PublishSwitchButtons extends Widget |
|
12
|
|
|
{ |
|
13
|
|
|
const MASS_PUBLISH = 'publish'; |
|
14
|
|
|
const MASS_UNPUBLISH = 'unpublish'; |
|
15
|
|
|
|
|
16
|
|
|
public $url; |
|
17
|
|
|
public $gridSelector; |
|
18
|
|
|
public $wrapperClass = ''; |
|
19
|
|
|
public $publishText = ''; |
|
20
|
|
|
public $unpublishText = ''; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array here you can define buttons classes like this: |
|
23
|
|
|
* [ |
|
24
|
|
|
* 'on-class' => 'btn btn-danger', //class for publish all button |
|
25
|
|
|
* 'off-class' => 'btn btn-danger', //class for unpublish all button |
|
26
|
|
|
* ] |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
public $htmlOptions = []; |
|
30
|
|
|
public $modalSelector = '#mass-publish-confirmation'; |
|
31
|
|
|
|
|
32
|
|
|
public function init() |
|
33
|
|
|
{ |
|
34
|
|
|
if (!isset($this->url, $this->gridSelector)) { |
|
35
|
|
|
throw new InvalidParamException('Attribute \'url\' or \'gridSelector\' is not set'); |
|
36
|
|
|
} |
|
37
|
|
|
if (true === empty($this->publishText)) { |
|
38
|
|
|
$this->publishText = Yii::t('app', 'Are you sure you want to publish all selected items?'); |
|
39
|
|
|
} |
|
40
|
|
|
if (true === empty($this->unpublishText)) { |
|
41
|
|
|
$this->unpublishText = Yii::t('app', 'Are you sure you want to unpublish all selected items?'); |
|
42
|
|
|
} |
|
43
|
|
|
if (!isset($this->htmlOptions['id'])) { |
|
44
|
|
|
$this->htmlOptions['id'] = 'publishSwitch'; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function run() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$this->registerScript(); |
|
51
|
|
|
return $this->renderButtons(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function renderButtons() |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
$buttons = Html::button( |
|
57
|
|
|
Icon::show('eye') . ' ' . |
|
58
|
|
|
\Yii::t('app', 'Publish All'), |
|
59
|
|
|
[ |
|
60
|
|
|
'class' => isset($this->htmlOptions['on-class']) ? |
|
61
|
|
|
$this->htmlOptions['on-class'] : 'btn btn-default', |
|
62
|
|
|
'data-action' => self::MASS_PUBLISH |
|
63
|
|
|
] |
|
64
|
|
|
|
|
65
|
|
|
) |
|
66
|
|
|
. Html::button( |
|
67
|
|
|
Icon::show('eye-slash') . ' ' . |
|
68
|
|
|
\Yii::t('app', 'Unpublish All'), |
|
69
|
|
|
[ |
|
70
|
|
|
'class' => isset($this->htmlOptions['on-class']) |
|
71
|
|
|
? $this->htmlOptions['on-class'] : 'btn btn-default', |
|
72
|
|
|
'data-action' => self::MASS_UNPUBLISH |
|
73
|
|
|
] |
|
74
|
|
|
); |
|
75
|
|
|
return Html::tag('div', $buttons, [ |
|
76
|
|
|
'id' => $this->htmlOptions['id'], |
|
77
|
|
|
'class' => 'btn-group ' . $this->wrapperClass, |
|
78
|
|
|
'role' => 'group', |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function registerScript() |
|
83
|
|
|
{ |
|
84
|
|
|
$JS = <<<JS |
|
85
|
|
|
$("#{$this->htmlOptions['id']} button").on('click', function() { |
|
86
|
|
|
var items = $("{$this->gridSelector}").yiiGridView('getSelectedRows'); |
|
87
|
|
|
if (items.length) { |
|
88
|
|
|
var \$modal = $("{$this->modalSelector}"), |
|
89
|
|
|
action = $(this).data('action'), |
|
90
|
|
|
\$textContainer = $('#mass-publish-modal-text', \$modal); |
|
91
|
|
|
\$textContainer.closest('.alert').removeClass('alert-danger').addClass('alert-info'); |
|
92
|
|
|
switch (action) { |
|
93
|
|
|
case '%s' : |
|
94
|
|
|
\$textContainer.text('{$this->publishText}'); |
|
95
|
|
|
break; |
|
96
|
|
|
case '%s' : |
|
97
|
|
|
\$textContainer.text('{$this->unpublishText}'); |
|
98
|
|
|
break; |
|
99
|
|
|
} |
|
100
|
|
|
\$modal.data('url', "{$this->url}") |
|
101
|
|
|
.data('items', items) |
|
102
|
|
|
.data('switch-action', action) |
|
103
|
|
|
.modal('show'); |
|
104
|
|
|
} |
|
105
|
|
|
return false; |
|
106
|
|
|
}); |
|
107
|
|
|
JS; |
|
108
|
|
|
$this->view->registerJs(sprintf($JS, self::MASS_PUBLISH, self::MASS_UNPUBLISH)); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.