Test Setup Failed
Push — master ( 5056ce...d219f7 )
by eXeCUT
10:46
created

GridView::getUpdateUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: execut
5
 * Date: 2/21/18
6
 * Time: 2:44 PM
7
 */
8
9
namespace execut\actions\widgets;
10
11
12
use execut\yii\jui\WidgetTrait;
13
use kartik\alert\Alert;
14
use yii\helpers\ArrayHelper;
15
use yii\helpers\Html;
16
use yii\helpers\Inflector;
17
use yii\helpers\Json;
18
use yii\helpers\Url;
19
use kartik\base\Config;
20
21
class GridView extends \kartik\grid\GridView
22
{
23
    use WidgetTrait;
24
    public $addButtonUrl = null;
25
    public $updateUrl = null;
26
    public $title = null;
27
    public $isAjaxCrud = false;
28
    public $formModel = null;
29
    public $uniqueId = null;
30
    public $hover = true;
31
    public function init()
32
    {
33
        $this->toolbar = $this->getToolbarConfig();
34
        $this->rowOptions = function ($row) {
35
            return [
36
                'data-id' => $row->primaryKey,
37
                'attributes' => Json::encode($row->attributes),
38
            ];
39
        };
40
41
        return parent::init();
42
    }
43
44
    protected function getUpdateUrl() {
45
        if ($this->updateUrl !== null) {
46
            return $this->updateUrl;
47
        }
48
49
        return $this->addButtonUrl;
50
    }
51
52
    /**
53
     * Registers a specific jQuery UI widget options
54
     * @param string $name the name of the jQuery UI widget
55
     * @param string $id the ID of the widget
56
     */
57
    protected function registerWidget($name = null, $id = null)
58
    {
59
60
        if ($name === null) {
61
            $name = $this->getDefaultJsWidgetName();
62
        }
63
64
        $this->_registerBundle();
65
66
        if (!$this->isAjaxCrud) {
67
            if ($id === null) {
68
                $id = $this->options['id'];
69
            }
70
71
            $options = empty($this->clientOptions) ? '' : Json::htmlEncode([
72
                'updateUrl' => Url::to($this->getUpdateUrl())
73
            ]);
74
            $js = "jQuery('#$id').$name($options);";
75
            $this->getView()->registerJs($js);
76
        }
77
    }
78
79
    public function run()
80
    {
81
        $this->registerWidget();
82
        $this->initToggleData();
83
        $this->initExport();
84
        if ($this->export !== false && isset($this->exportConfig[self::PDF])) {
85
            Config::checkDependency(
86
                'mpdf\Pdf',
87
                'yii2-mpdf',
88
                'for PDF export functionality. To include PDF export, follow the install steps below. If you do not ' .
89
                "need PDF export functionality, do not include 'PDF' as a format in the 'export' property. You can " .
90
                "otherwise set 'export' to 'false' to disable all export functionality"
91
            );
92
        }
93
        $this->initHeader();
94
        $this->initBootstrapStyle();
95
        $this->containerOptions['id'] = $this->options['id'] . '-container';
96
        Html::addCssClass($this->containerOptions, 'kv-grid-container');
97
        $this->registerAssets();
98
        $this->renderPanel();
99
        $this->beginPjax();
100
        $this->initLayout();
101
        parent::run();
102
        $this->endPjax();
103
    }
104
105
    /**
106
     * @param $refreshUrlParams
107
     * @return array
108
     */
109
    public function getToolbarConfig(): array
110
    {
111
        if ($this->toolbar === false) {
112
            return [];
113
        }
114
//        $refreshUrlParams = [
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
115
//            $this->adapter->uniqueId,
116
//        ];
117
//
118
//        foreach ($this->refreshAttributes as $key) {
119
//            if (!empty($this->adapter->actionParams->get[$key])) {
120
//                $refreshUrlParams[$key] = $this->adapter->actionParams->get[$key];
121
//            }
122
//        }
123
        if (!is_array($this->toolbar)) {
124
            if (empty($this->toolbar)) {
125
                $this->toolbar = [];
126
            } else {
127
                $this->toolbar = [$this->toolbar];
128
            }
129
        }
130
131
        return ArrayHelper::merge($this->toolbar, [
132
//            'massEdit' => ['content' => $this->renderMassEditButton()],
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
133
//            'massVisible' => ['content' => $this->renderVisibleButtons()],
134
            'add' => ['content' => $this->renderAddButton()],
135
            'alert' => ['content' => $this->renderAlertBlock()],
136
//            'refresh' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
137
//                'content' => Html::a('<i class="glyphicon glyphicon-repeat"></i>', $refreshUrlParams, ['data-pjax' => 0, 'class' => 'btn btn-default', 'title' => 'Reset Grid']),
138
//            ],
139
//            'dynaParams' => ['content' => '{dynagridFilter}{dynagridSort}{dynagrid}'],
140
//            'toggleData' => '{toggleData}',
141
//            'export' => '{export}',
142
        ]);
143
    }
144
145
    public function beginPjax() {
146
        parent::beginPjax();
147
        if ($this->isAjaxCrud) {
148
            $model = $this->formModel;
149
            if (is_callable($model)) {
150
                $model = $model();
151
            }
152
153
            if ($this->pjax) {
154
                $gridId = $this->id . '-pjax';
155
            } else {
156
                $gridId = null;
157
            }
158
159
            echo EditDialog::widget([
160
                'id' => $this->id . '-edit',
161
                'model' => $model,
162
                'uniqueId' => $this->uniqueId,
163
                'alertId' => $this->id . '-alert',
164
                'clientOptions' => [
165
                    'inputsPrefix' => str_replace('-', '', Inflector::camel2id($model->formName())),
166
                    'editButtons' => 'tr[data-key]',
167
                    'gridId' => $gridId,
168
                ],
169
                'toggleButtonOptions' => false,
170
            ]);
171
        }
172
    }
173
174
    public function renderAlertBlock() {
175
        $alertWidgetOptions = [
176
            'id' => $this->id . '-alert',
177
        ];
178
179
        $alertWidgetOptions['body'] = '<span></span>';
180
        $alertWidgetOptions['options'] = [
181
            'class' => ['alert', 'alert-success'],
182
            'style' => 'padding-left:10px;padding-right:10px;display:none;'
183
        ];
184
185
        return Alert::widget($alertWidgetOptions);
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    protected function renderAddButton()
192
    {
193
        $title = \yii::t('execut.actions', 'Add') . ' ' . $this->title;
194
        return Html::a($title, Url::to($this->addButtonUrl), [
195
                'id' => $this->id . '-edit-add-button',
196
                'type' => 'button',
197
                'data-pjax' => 0,
198
                'title' => $title,
199
                'class' => 'btn btn-success'
200
            ]);
201
    }
202
}