Skip to content

Commit 7efb344

Browse files
committed
Init
1 parent f3f0495 commit 7efb344

File tree

8 files changed

+446
-0
lines changed

8 files changed

+446
-0
lines changed

BulkCheckboxAction.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace integready\bulkactionscheckboxcolumn;
4+
5+
use Yii;
6+
use yii\helpers\Url;
7+
use yii\base\Action;
8+
use yii\helpers\Json;
9+
use yii\helpers\ArrayHelper;
10+
11+
/**
12+
* Class BulkCheckboxAction
13+
*/
14+
class BulkCheckboxAction extends Action
15+
{
16+
/**
17+
* @var \yii\db\ActiveRecord
18+
*/
19+
public $modelClass;
20+
21+
/**
22+
* @var string
23+
*/
24+
public $gridId;
25+
26+
/**
27+
* @var string
28+
*/
29+
public $statusField;
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function run()
35+
{
36+
$postUrl = Yii::$app->request->get();
37+
$data = empty(Yii::$app->request->get($this->gridId . '_' . $this->statusField)) ? false : Json::decode(Yii::$app->request->get($this->gridId . '_' . $this->statusField), true);
38+
if ($data && count($data) >= 1) {
39+
$this->modelClass::updateAll([$this->statusField => $data['status']], [$this->modelClass::primaryKey()[0] => $data['ids']]);
40+
}
41+
42+
if (!empty($postUrl[$this->gridId . '_' . $this->statusField])) {
43+
unset($postUrl[$this->gridId . '_' . $this->statusField]);
44+
$urlParams = ArrayHelper::merge(['did/index'], $postUrl);
45+
Yii::$app->response->redirect(Url::to($urlParams));
46+
}
47+
}
48+
}

BulkCheckboxColumn.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace integready\bulkactionscheckboxcolumn;
4+
5+
use kartik\grid\CheckboxColumn;
6+
7+
/**
8+
* Class BulkCheckboxColumn
9+
*/
10+
class BulkCheckboxColumn extends CheckboxColumn
11+
{
12+
/**
13+
* @var array
14+
*/
15+
public $elements;
16+
17+
/**
18+
* @var string
19+
*/
20+
protected $fieldFilterSelector = '';
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function init()
26+
{
27+
parent::init();
28+
$grid = $this->grid;
29+
30+
$buttons = '';
31+
if (!empty($this->elements) && count($this->elements) >= 1) {
32+
$comma = false;
33+
foreach ($this->elements as $key => $element) {
34+
$element['field'] = $grid->getId() . '_' . $element['field'];
35+
if ($comma) {
36+
$this->fieldFilterSelector .= ', input[name=\'' . $element['field'] . '\']';
37+
} else {
38+
$this->fieldFilterSelector .= 'input[name=\'' . $element['field'] . '\']';
39+
$comma = true;
40+
}
41+
42+
$buttons .= ButtonDropdown::widget([
43+
'label' => $element['label'],
44+
'field' => $element['field'],
45+
'selectorName' => $grid->id . '-ids',
46+
'gridId' => $grid->id,
47+
'items' => $element['items'],
48+
]);
49+
}
50+
}
51+
52+
$grid->toolbar = [
53+
'content' => $buttons,
54+
'{export}',
55+
'{toggleData}'
56+
];
57+
58+
if (!empty($this->fieldFilterSelector)) {
59+
if (!empty($grid->filterSelector)) {
60+
$grid->filterSelector .= ', ' . $this->fieldFilterSelector;
61+
} else {
62+
$grid->filterSelector = $this->fieldFilterSelector;
63+
}
64+
}
65+
}
66+
}

ButtonDropdown.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

ButtonDropdownAssets.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace integready\bulkactionscheckboxcolumn;
4+
5+
use yii\web\AssetBundle;
6+
7+
/**
8+
* Class ButtonDropdownAssets
9+
*/
10+
class ButtonDropdownAssets extends AssetBundle
11+
{
12+
/**
13+
* @var string
14+
*/
15+
public $sourcePath = '@integready/bulkactionscheckboxcolumn/assets';
16+
17+
/**
18+
* @var array
19+
*/
20+
public $css = [];
21+
22+
/**
23+
* @var array
24+
*/
25+
public $js = [
26+
'js/script.js',
27+
];
28+
29+
/**
30+
* @var array
31+
*/
32+
public $depends = [
33+
'yii\web\YiiAsset',
34+
'yii\bootstrap\BootstrapAsset',
35+
];
36+
}

Module.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace integready\bulkactionscheckboxcolumn;
4+
5+
use yii\base\Widget;
6+
7+
/**
8+
* Class Module
9+
*/
10+
class Module extends Widget { }

0 commit comments

Comments
 (0)