Test Setup Failed
Push — master ( d4a333...594b61 )
by eXeCUT
15:26
created

GridView::getFormModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
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 $responsiveWrap = false;
32
    public function init()
33
    {
34
        $this->toolbar = $this->getToolbarConfig();
35
        $this->rowOptions = function ($row) {
36
            return [
37
                'class' => 'link-row',
38
                'data-id' => $row->primaryKey,
39
                'attributes' => Json::encode($row->attributes),
40
            ];
41
        };
42
43
        return parent::init();
44
    }
45
46
    protected function getUpdateUrl() {
47
        if ($this->updateUrl !== null) {
48
            return $this->updateUrl;
49
        }
50
51
        return $this->addButtonUrl;
52
    }
53
54
    /**
55
     * Registers a specific jQuery UI widget options
56
     * @param string $name the name of the jQuery UI widget
57
     * @param string $id the ID of the widget
58
     */
59
    protected function registerWidget($name = null, $id = null)
60
    {
61
        if ($name === null) {
62
            $name = $this->getDefaultJsWidgetName();
63
        }
64
65
        $this->_registerBundle();
66
67
        if (!$this->isAjaxCrud && $this->getUpdateUrl()) {
68
            if ($id === null) {
69
                $id = $this->options['id'];
70
            }
71
72
            $options = empty($this->clientOptions) ? '' : Json::htmlEncode([
73
                'updateUrl' => Url::to($this->getUpdateUrl())
74
            ]);
75
            $js = "jQuery('#$id').$name($options);";
76
            $this->getView()->registerJs($js);
77
        }
78
    }
79
80
    public function run()
81
    {
82
        $this->registerWidget();
83
        $this->initToggleData();
84
        $this->initExport();
85
        if ($this->export !== false && isset($this->exportConfig[self::PDF])) {
86
            Config::checkDependency(
87
                'mpdf\Pdf',
88
                'yii2-mpdf',
89
                'for PDF export functionality. To include PDF export, follow the install steps below. If you do not ' .
90
                "need PDF export functionality, do not include 'PDF' as a format in the 'export' property. You can " .
91
                "otherwise set 'export' to 'false' to disable all export functionality"
92
            );
93
        }
94
        $this->initHeader();
95
        $this->initBootstrapStyle();
96
        $this->containerOptions['id'] = $this->options['id'] . '-container';
97
        Html::addCssClass($this->containerOptions, 'kv-grid-container');
98
        $this->registerAssets();
99
        $this->renderPanel();
100
        $this->beginPjax();
101
        $this->initLayout();
102
        parent::run();
103
        $this->endPjax();
104
    }
105
106
    /**
107
     * @param $refreshUrlParams
108
     * @return array
109
     */
110
    public function getToolbarConfig(): array
111
    {
112
        if ($this->toolbar === false) {
113
            return [];
114
        }
115
//        $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...
116
//            $this->adapter->uniqueId,
117
//        ];
118
//
119
//        foreach ($this->refreshAttributes as $key) {
120
//            if (!empty($this->adapter->actionParams->get[$key])) {
121
//                $refreshUrlParams[$key] = $this->adapter->actionParams->get[$key];
122
//            }
123
//        }
124
        if (!is_array($this->toolbar)) {
125
            if (empty($this->toolbar)) {
126
                $this->toolbar = [];
127
            } else {
128
                $this->toolbar = [$this->toolbar];
129
            }
130
        }
131
132
        return ArrayHelper::merge([
133
//            '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...
134
//            'massVisible' => ['content' => $this->renderVisibleButtons()],
135
            'add' => ['content' => $this->renderAddButton()],
136
            'alert' => ['content' => $this->renderAlertBlock()],
137
//            '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...
138
//                'content' => Html::a('<i class="glyphicon glyphicon-repeat"></i>', $refreshUrlParams, ['data-pjax' => 0, 'class' => 'btn btn-default', 'title' => 'Reset Grid']),
139
//            ],
140
//            'dynaParams' => ['content' => '{dynagridFilter}{dynagridSort}{dynagrid}'],
141
//            'toggleData' => '{toggleData}',
142
//            'export' => '{export}',
143
        ], $this->toolbar);
144
    }
145
146
    public function beginPjax() {
147
        parent::beginPjax();
148
        if ($this->isAjaxCrud) {
149
            $model = $this->getFormModel();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as $this->getFormModel() (which targets execut\actions\widgets\GridView::getFormModel()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
150
151
            if ($this->pjax) {
152
                $gridId = $this->id . '-pjax';
153
            } else {
154
                $gridId = null;
155
            }
156
157
            echo EditDialog::widget([
158
                'id' => $this->id . '-edit',
159
                'model' => $model,
160
                'uniqueId' => $this->uniqueId,
161
                'alertId' => $this->id . '-alert',
162
                'clientOptions' => [
163
                    'inputsPrefix' => str_replace('-', '', Inflector::camel2id($model->formName())),
0 ignored issues
show
Bug introduced by
The method formName cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
164
                    'editButtons' => 'tr[data-key]',
165
                    'gridId' => $gridId,
166
                ],
167
                'toggleButtonOptions' => false,
168
            ]);
169
        }
170
    }
171
172
    public function renderAlertBlock() {
173
        $alertWidgetOptions = [
174
            'id' => $this->id . '-alert',
175
        ];
176
177
        $alertWidgetOptions['body'] = '<span></span>';
178
        $alertWidgetOptions['options'] = [
179
            'class' => ['alert', 'alert-success'],
180
            'style' => 'padding-left:10px;padding-right:10px;display:none;'
181
        ];
182
183
        return Alert::widget($alertWidgetOptions);
184
    }
185
186
    /**
187
     * @return string
188
     */
189
    protected function renderAddButton()
190
    {
191
        $title = \yii::t('execut.actions', 'Add') . ' ' . $this->title;
192
        $url = $this->addButtonUrl;
193
        if (!is_array($url)) {
194
            $url = [$url];
195
        }
196
197
198
        return Html::a($title, Url::to($url), [
199
                'id' => $this->id . '-edit-add-button',
200
                'type' => 'button',
201
                'data-pjax' => 0,
202
                'title' => $title,
203
                'class' => 'btn btn-success'
204
            ]);
205
    }
206
207
    /**
208
     * @return null
209
     */
210
    protected function getFormModel()
211
    {
212
        $model = $this->formModel;
213
        if (is_callable($model)) {
214
            $model = $model();
215
        }
216
        return $model;
217
    }
218
}