Completed
Push — master ( cfc511...9c0ae4 )
by Andrii
11:40
created

ActionBox::endBulkActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\widgets;
13
14
use Yii;
15
use yii\base\InvalidConfigException;
16
use yii\bootstrap\ButtonDropdown;
17
use yii\helpers\Html;
18
use yii\helpers\Inflector;
19
use yii\helpers\Json;
20
use yii\helpers\Url;
21
22
class ActionBox extends Box
23
{
24
    public $model;
25
26
    public $dataProvider;
27
28
    public $bulk = true;
29
30
    public $options = [
31
        'class' => 'box-info',
32
    ];
33
34
    public function run()
35
    {
36
        parent::run();
37
        $this->registerClientScript();
38
    }
39
40
    private function registerClientScript()
41
    {
42
        $searchFormId = Json::htmlEncode("#{$this->getBulkFormId()}");
43
        $view = $this->getView();
44
        $view->registerJs(<<<JS
45
        // Checkbox
46
        var checkboxes = $('table input[type="checkbox"]');
47
        var bulkcontainer = $('.box-bulk-actions fieldset');
48
        checkboxes.on('ifChecked ifUnchecked', function(event) {
49
            if (event.type == 'ifChecked' && $('input.icheck').filter(':checked').length > 0) {
50
                bulkcontainer.prop('disabled', false);
51
            } else if ($('input.icheck').filter(':checked').length == 0) {
52
                bulkcontainer.prop('disabled', true);
53
            }
54
        });
55
        // On/Off Actions TODO: reduce scope
56
        $(document).on('click', '.box-bulk-actions a', function (event) {
57
            var link = $(this);
58
            var action = link.data('action');
59
            var form = $($searchFormId);
60
            if (action) {
61
                form.attr({'action': action, method: 'POST'}).submit();
62
            }
63
        });
64
JS
65
        , $view::POS_READY);
66
    }
67
68
    public function beginActions()
69
    {
70
        echo Html::beginTag('div', ['class' => 'row']) . PHP_EOL;
71
        echo Html::beginTag('div', ['class' => 'box-actions col-md-6']) . PHP_EOL;
72
    }
73
74
    public function endActions()
75
    {
76
        echo PHP_EOL . Html::endTag('div');
77
        if ($this->bulk === false) {
78
            echo PHP_EOL . Html::endTag('div');
79
        }
80
    }
81
82
    public function beginBulkActions()
83
    {
84
        if ($this->bulk === false) {
85
            throw new InvalidConfigException("'bulk' property is false, turn this on ('true' statement), if you want use bulk actions.");
86
        }
87
88
        echo Html::beginTag('div', ['class' => 'box-bulk-actions col-md-6 text-right']) . "\n";
89
        echo Html::beginTag('fieldset', ['disabled' => 'disabled']) . "\n";
90
    }
91
92
    public function endBulkActions()
93
    {
94
        echo "\n" . Html::endTag('fieldset');
95
        echo "\n" . Html::endTag('div');
96
        echo "\n" . Html::endTag('div');
97
98
//        print Html::tag('div', '', ['class' => 'clearfix']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
    }
100
101
    public function renderCreateButton($text)
102
    {
103
        return Html::a($text, ['create'], ['class' => 'btn btn-sm btn-success']);
104
    }
105
106
    public function renderSearchButton()
107
    {
108
        return AdvancedSearch::renderButton() . "\n";
109
    }
110
111
    public function beginSearchForm($options = [])
112
    {
113
        return AdvancedSearch::begin(array_merge(['model' => $this->model], $options));
114
    }
115
116
    public function renderSearchForm(array $data = [], $advancedSearchOptions = [])
117
    {
118
        ob_start();
119
        ob_implicit_flush(false);
120
        try {
121
            $search = $this->beginSearchForm($advancedSearchOptions);
122
            foreach (['per_page', 'representation'] as $key) {
123
                echo Html::hiddenInput($key, Yii::$app->request->get($key));
124
            }
125
            echo Yii::$app->view->render('_search', array_merge(compact('search'), $data));
126
            $search->end();
127
        } catch (\Exception $e) {
128
            ob_end_clean();
129
            throw $e;
130
        }
131
132
        return ob_get_clean();
133
    }
134
135 View Code Duplication
    public function renderPerPage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        return ButtonDropdown::widget([
138
            'label' => Yii::t('hipanel', 'Per page') . ': ' . (Yii::$app->request->get('per_page') ?: 25),
139
            'options' => ['class' => 'btn-default btn-sm'],
140
            'dropdown' => [
141
                'items' => [
142
                    ['label' => '25',  'url' => Url::current(['per_page' => null])],
143
                    ['label' => '50',  'url' => Url::current(['per_page' => 50])],
144
                    ['label' => '100', 'url' => Url::current(['per_page' => 100])],
145
                    ['label' => '200', 'url' => Url::current(['per_page' => 200])],
146
                    ['label' => '500', 'url' => Url::current(['per_page' => 500])],
147
                ],
148
            ],
149
        ]);
150
    }
151
152 View Code Duplication
    public function renderSorter(array $options)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        return LinkSorter::widget(array_merge([
155
            'show'  => true,
156
            'sort'  => $this->dataProvider->getSort(),
157
        ], $options));
158
    }
159
160
    public function renderBulkActions(array $options)
161
    {
162
        $this->beginBulkActions();
163
        echo BulkButtons::widget(array_merge([
164
            'model' => $this->model,
165
        ], $options));
166
        $this->endBulkActions();
167
    }
168
169 View Code Duplication
    public function renderBulkButton($text, $action, $color = 'default')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        return Html::submitButton($text, [
172
            'class'         => "btn btn-$color btn-sm",
173
            'form'          => $this->getBulkFormId(),
174
            'formmethod'    => 'POST',
175
            'formaction'    => $action,
176
        ]);
177
    }
178
179
    public function renderDeleteButton($text = null)
180
    {
181
        $text = $text ?: Yii::t('hipanel', 'Delete');
182
        return $this->renderBulkButton($text, 'delete', 'danger');
183
    }
184
185
    public function getBulkFormId()
186
    {
187
        return 'bulk-' . Inflector::camel2id($this->model->formName());
188
    }
189
190
    public function beginBulkForm($action = '')
191
    {
192
        echo Html::beginForm($action, 'POST', ['id' => $this->getBulkFormId()]);
193
    }
194
195
    public function endBulkForm()
196
    {
197
        echo Html::endForm();
198
    }
199
}
200