ActionBox   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 24
lcom 2
cbo 12
dl 0
loc 179
ccs 0
cts 144
cp 0
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 5 1
A registerClientScript() 0 27 1
A beginActions() 0 5 1
A endActions() 0 7 2
A beginBulkActions() 0 9 2
A endBulkActions() 0 8 1
A renderCreateButton() 0 4 1
A renderSearchButton() 0 4 1
A beginSearchForm() 0 4 1
A renderSearchForm() 0 18 3
A renderPerPage() 0 16 2
A renderSorter() 0 7 1
A renderBulkActions() 0 8 1
A renderBulkButton() 0 9 1
A renderDeleteButton() 0 6 2
A getBulkFormId() 0 4 1
A beginBulkForm() 0 4 1
A endBulkForm() 0 4 1
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;
14
use yii\base\InvalidConfigException;
15
use yii\bootstrap\ButtonDropdown;
16
use yii\helpers\Html;
17
use yii\helpers\Inflector;
18
use yii\helpers\Json;
19
use yii\helpers\Url;
20
21
class ActionBox extends Box
22
{
23
    public $model;
24
25
    public $dataProvider;
26
27
    public $bulk = true;
28
29
    public $options = [
30
        'class' => 'box-info',
31
    ];
32
33
    public function run()
34
    {
35
        parent::run();
36
        $this->registerClientScript();
37
    }
38
39
    private function registerClientScript()
40
    {
41
        $searchFormId = Json::htmlEncode("#{$this->getBulkFormId()}");
42
        $view = $this->getView();
43
        $view->registerJs(<<<JS
44
        // Checkbox
45
        var checkboxes = $('table input[type="checkbox"]');
46
        var bulkcontainer = $('.box-bulk-actions fieldset');
47
        checkboxes.on('ifChecked ifUnchecked', function(event) {
48
            if (event.type == 'ifChecked' && $('input.icheck').filter(':checked').length > 0) {
49
                bulkcontainer.prop('disabled', false);
50
            } else if ($('input.icheck').filter(':checked').length == 0) {
51
                bulkcontainer.prop('disabled', true);
52
            }
53
        });
54
        // On/Off Actions TODO: reduce scope
55
        $(document).on('click', '.box-bulk-actions a', function (event) {
56
            var link = $(this);
57
            var action = link.data('action');
58
            var form = $($searchFormId);
59
            if (action) {
60
                form.attr({'action': action, method: 'POST'}).submit();
61
            }
62
        });
63
JS
64
        , $view::POS_READY);
65
    }
66
67
    public function beginActions()
68
    {
69
        echo Html::beginTag('div', ['class' => 'row']) . PHP_EOL;
70
        echo Html::beginTag('div', ['class' => 'box-actions col-md-6']) . PHP_EOL;
71
    }
72
73
    public function endActions()
74
    {
75
        echo PHP_EOL . Html::endTag('div');
76
        if ($this->bulk === false) {
77
            echo PHP_EOL . Html::endTag('div');
78
        }
79
    }
80
81
    public function beginBulkActions()
82
    {
83
        if ($this->bulk === false) {
84
            throw new InvalidConfigException("'bulk' property is false, turn this on ('true' statement), if you want use bulk actions.");
85
        }
86
87
        echo Html::beginTag('div', ['class' => 'box-bulk-actions col-md-6 text-right']) . "\n";
88
        echo Html::beginTag('fieldset', ['disabled' => 'disabled']) . "\n";
89
    }
90
91
    public function endBulkActions()
92
    {
93
        echo "\n" . Html::endTag('fieldset');
94
        echo "\n" . Html::endTag('div');
95
        echo "\n" . Html::endTag('div');
96
97
//        print Html::tag('div', '', ['class' => 'clearfix']);
98
    }
99
100
    public function renderCreateButton($text)
101
    {
102
        return Html::a($text, ['create'], ['class' => 'btn btn-sm btn-success']);
103
    }
104
105
    public function renderSearchButton()
106
    {
107
        return AdvancedSearch::renderButton() . "\n";
108
    }
109
110
    public function beginSearchForm($options = [])
111
    {
112
        return AdvancedSearch::begin(array_merge(['model' => $this->model], $options));
113
    }
114
115
    public function renderSearchForm(array $data = [], $advancedSearchOptions = [])
116
    {
117
        ob_start();
118
        ob_implicit_flush(false);
119
        try {
120
            $search = $this->beginSearchForm($advancedSearchOptions);
121
            foreach (['per_page', 'representation'] as $key) {
122
                echo Html::hiddenInput($key, Yii::$app->request->get($key));
123
            }
124
            echo Yii::$app->view->render('_search', array_merge(compact('search'), $data));
125
            $search->end();
126
        } catch (\Exception $e) {
127
            ob_end_clean();
128
            throw $e;
129
        }
130
131
        return ob_get_clean();
132
    }
133
134
    public function renderPerPage()
135
    {
136
        return ButtonDropdown::widget([
137
            'label' => Yii::t('hipanel', 'Per page') . ': ' . (Yii::$app->request->get('per_page') ?: 25),
138
            'options' => ['class' => 'btn-default btn-sm'],
139
            'dropdown' => [
140
                'items' => [
141
                    ['label' => '25',  'url' => Url::current(['per_page' => null])],
142
                    ['label' => '50',  'url' => Url::current(['per_page' => 50])],
143
                    ['label' => '100', 'url' => Url::current(['per_page' => 100])],
144
                    ['label' => '200', 'url' => Url::current(['per_page' => 200])],
145
                    ['label' => '500', 'url' => Url::current(['per_page' => 500])],
146
                ],
147
            ],
148
        ]);
149
    }
150
151
    public function renderSorter(array $options)
152
    {
153
        return LinkSorter::widget(array_merge([
154
            'show'  => true,
155
            'sort'  => $this->dataProvider->getSort(),
156
        ], $options));
157
    }
158
159
    public function renderBulkActions(array $options)
160
    {
161
        $this->beginBulkActions();
162
        echo BulkButtons::widget(array_merge([
163
            'model' => $this->model,
164
        ], $options));
165
        $this->endBulkActions();
166
    }
167
168
    public function renderBulkButton($text, $action, $color = 'default')
169
    {
170
        return Html::submitButton($text, [
171
            'class'         => "btn btn-$color btn-sm",
172
            'form'          => $this->getBulkFormId(),
173
            'formmethod'    => 'POST',
174
            'formaction'    => $action,
175
        ]);
176
    }
177
178
    public function renderDeleteButton($text = null)
179
    {
180
        $text = $text ?: Yii::t('hipanel', 'Delete');
181
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