BulkButtons::renderHtml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets;
12
13
use yii\base\Widget;
14
15
class BulkButtons extends Widget
16
{
17
    /**
18
     * @var [[\yii\base\Model]]
19
     */
20
    public $model;
21
22
    public $modelName;
23
24
    public $modelPk;
25
26
    public $items = [];
27
28
    public function run()
29
    {
30
        $this->registerClintScripts();
31
32
        return $this->renderHtml();
33
    }
34
35
    private function registerClintScripts()
36
    {
37
        $view = $this->getView();
38
        $modelFormName = $this->model->formName();
39
        $modelPk = reset($this->model->primaryKey());
0 ignored issues
show
Bug introduced by
$this->model->primaryKey() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
40
41
        $view->registerJs(<<<JS
42
            $( ".bulk-action" ).on( "click", function(event) {
43
                event.preventDefault();
44
                var data = [],
45
                    attribute = $(this).data('attribute'),
46
                    value = $(this).data('value'),
47
                    url = $(this).data('url'),
48
                    keys = $( $('div[role=grid]') ).yiiGridView('getSelectedRows');
49
                jQuery.each(keys, function(k, id) {
50
                    var item = {};
51
                    item['$modelPk'] = id;
52
                    item[attribute] = value;
53
                    data.push(item);
54
                });
55
56
                if ($.support.pjax) {
57
                    var container = $(this).closest('[data-pjax-contaiter]');
58
                    if (container) {
59
                        $.pjax({container: container, data: {'$modelFormName': data}, url: url, type: 'POST'})
60
                    }
61
                } else {
62
                        jQuery.ajax({
63
                        type: 'POST',
64
                        dataType: 'json',
65
                        data: {'$modelFormName': data},
66
                        url: url
67
                    });
68
                }
69
            });
70
JS
71
        );
72
    }
73
74
    protected function renderHtml()
75
    {
76
        return implode('&nbsp;', $this->items);
77
    }
78
}
79