|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace integready\bulkactionscheckboxcolumn; |
| 4 | + |
| 5 | +use yii\bootstrap\ButtonDropdown as BBDropdown; |
| 6 | +use yii\base\Widget; |
| 7 | +use yii\helpers\Html; |
| 8 | +use yii\helpers\Json; |
| 9 | +use yii\web\View; |
| 10 | + |
| 11 | +class ButtonDropdown extends Widget |
| 12 | +{ |
| 13 | + |
| 14 | + /** |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + public $label; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + public $field; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + public $selectorName = 'updateStatusIds'; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + public $gridId = 'gridview-index'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Items list |
| 36 | + * ``` |
| 37 | + * [ |
| 38 | + * Model::STATUS_ACTIVE => 'Status active', |
| 39 | + * Model::STATUS_INACTIVE => 'Status inactive', |
| 40 | + * ] |
| 41 | + * ``` |
| 42 | + * @var array |
| 43 | + */ |
| 44 | + public $items; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var string |
| 48 | + */ |
| 49 | + public $float = 'left'; |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritdoc |
| 53 | + */ |
| 54 | + public function init() |
| 55 | + { |
| 56 | + parent::init(); |
| 57 | + $this->registerAssets(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return bool|string |
| 62 | + */ |
| 63 | + public function run() |
| 64 | + { |
| 65 | + if (!empty($this->items) && is_array($this->items) && count($this->items) >= 1) { |
| 66 | + $items = []; |
| 67 | + foreach ($this->items as $key => $item) { |
| 68 | + $items[] = [ |
| 69 | + 'label' => $item, |
| 70 | + 'url' => '#', |
| 71 | + 'linkOptions' => [ |
| 72 | + 'data-status' => $key, |
| 73 | + 'data-field' => $this->field, |
| 74 | + 'class' => $this->selectorName, |
| 75 | + ], |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + $button = BBDropdown::widget([ |
| 80 | + 'label' => $this->label, |
| 81 | + 'dropdown' => [ |
| 82 | + 'items' => $items, |
| 83 | + ], |
| 84 | + ]); |
| 85 | + |
| 86 | + $field = Html::input('hidden', $this->field, ''); |
| 87 | + |
| 88 | + return $button . $field; |
| 89 | + } |
| 90 | + |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Registers the needed assets |
| 96 | + */ |
| 97 | + public function registerAssets() |
| 98 | + { |
| 99 | + |
| 100 | + $view = $this->getView(); |
| 101 | + ButtonDropdownAssets::register($view); |
| 102 | + $options = [ |
| 103 | + 'selectorName' => $this->selectorName, |
| 104 | + ]; |
| 105 | + |
| 106 | + $view->registerJs( |
| 107 | + 'if (window.yiiOptions === undefined) { yiiOptions = []; yiiOptions.push(' . Json::htmlEncode($options) . ');} else {yiiOptions.push(' . Json::htmlEncode($options) . ');}', |
| 108 | + View::POS_HEAD, |
| 109 | + 'yiiOptions_' . $this->gridId |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments