Test Setup Failed
Push — master ( d219f7...aa460f )
by eXeCUT
13:22
created

DetailView::renderSubmitButtons()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 8.5806
cc 4
eloc 17
nc 8
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: execut
5
 * Date: 12/26/17
6
 * Time: 3:37 PM
7
 */
8
9
namespace execut\actions\widgets;
10
11
12
use execut\widgets\LoadingOverlay;
13
use execut\yii\jui\WidgetTrait;
14
use yii\helpers\ArrayHelper;
15
use yii\helpers\Html;
16
use yii\helpers\Url;
17
use kartik\alert\Alert;
18
19
class DetailView extends \kartik\detail\DetailView
20
{
21
    use WidgetTrait;
22
    public $uniqueId = null;
23
    public $action = null;
24
    public $buttonsTemplate = '{save}&nbsp;&nbsp;{apply}&nbsp;&nbsp;{cancel}';
25
    public $saveButton = '<input type="submit" name="save" value="Отправить" class="btn btn-primary" href="" title="Сохранить и вернуться">';
26
    public $applyButton = '<input type="submit" name="apply" value="Применить" class="btn btn-primary" href="" title="Сохранить изменения">';
27
    public $cancelButton = '<a class="btn btn-default" href="{backUrl}">Вернуться к списку</a>';
28
    public $backUrl = null;
29
30
    public function __construct($config = [])
31
    {
32
        $config = ArrayHelper::merge([
33
            'panel'=> false,
34
//            [
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
35
//                'heading'=> '',
36
//                'footer' => '&nbsp;{buttons}',
37
////                'type'=>\kartik\detail\DetailView::TYPE_PRIMARY,
38
//                'headingOptions' => [
39
//                    'template' => ''
40
//                ],
41
//                'footerOptions' => [
42
//                    'style' => 'height: 31px',
43
//                ],
44
//            ],
45
            'buttons1' => '',
46
            'buttonContainer' => [
47
                'class' => 'pull-right',
48
                'style' => 'margin-right: 8px;margin-top: -9px;',
49
            ],
50
            'mainTemplate' => $this->renderAlertBlock() . '{detail}<div style="height:28px;width:0px;display: inline-block"></div>{buttons}',
51
            'bordered' => true,
52
            'striped' => true,
53
            'condensed' => true,
54
            'responsive' => false,
55
            'hideIfEmpty' => true,
56
            'hover' => true,
57
//                        'hAlign'=> true,
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
58
//                        'vAlign'=> true,
59
//                        'fadeDelay'=> 2000,
60
//                        'container' => ['id'=>'kv-demo'],
61
            'formOptions' => [
62
                'enableAjaxValidation' => false,
63
                'validateOnChange' => false,
64
                'validateOnSubmit' => false,
65
                'validateOnBlur' => false,
66
                'options' => [
67
                    'enctype'=>'multipart/form-data',
68
                ],
69
            ],
70
        ], $config);
71
72
        parent::__construct($config);
73
    }
74
75
    public function run() {
76
        echo LoadingOverlay::widget();
77
78
        $this->_registerBundle();
79
        parent::registerPlugin('DetailView');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (registerPlugin() instead of run()). Are you sure this is correct? If so, you might want to change this to $this->registerPlugin().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
80
        echo $this->_beginContainer();
81
        $r = parent::run();
82
        echo $this->_endContainer();
83
84
        return $r;
85
    }
86
87
    public function init()
88
    {
89
        $this->buttons2 = $this->renderSubmitButtons();
90
        $this->attributes = $this->model->getFormFields();
91
        $this->formOptions['action'] = $this->getAction();
92
        $this->deleteOptions = [
93
            'url' => Url::to([
94
                $this->uniqueId . '/delete',
95
                'id' => $this->model->primaryKey,
96
            ]),
97
            'kvdelete' => true
98
        ];
99
        $this->formOptions['validationUrl'] = $this->getAction();
100
101
        parent::init();
102
    }
103
104
    protected function getAction() {
105
        if ($this->action !== null) {
106
            return $this->action;
107
        }
108
109
        return Url::to([
110
            $this->uniqueId . '/update',
111
            'id' => $this->model->primaryKey,
112
        ]);
113
    }
114
115
    /**
116
     * Initializes and renders alert container block
117
     */
118 View Code Duplication
    protected function renderAlertBlock()
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...
119
    {
120
        $session = \Yii::$app->session;
121
        $flashes = $session->getAllFlashes();
122
        $alertContainerOptions = [
123
            'style' => 'max-width:400px'
124
        ];
125
        if (count($flashes) === 0) {
126
            Html::addCssStyle($alertContainerOptions, 'display:none;');
127
        }
128
        $out = Html::beginTag('div', $alertContainerOptions);
129
        foreach ($flashes as $type => $message) {
130
            if (is_array($message)) {
131
                $message = implode('<br>', $message);
132
            }
133
134
            $alertWidgetOptions = [];
135
            $alertWidgetOptions['body'] = $message;
136
            $alertWidgetOptions['options'] = [
137
                'class' => ['alert', 'alert-success'],
138
                'style' => 'padding-left:10px;padding-right:10px;'
139
            ];
140
            $out .= "\n" . Alert::widget($alertWidgetOptions);
141
            $session->removeFlash($type);
142
        }
143
144
        $out .= "\n</div>";
145
146
        return $out;
147
    }
148
149
    /**
150
     * @param $cancelUrl
151
     * @return string
152
     */
153
    public function renderSubmitButtons()
154
    {
155
        $cancelButton = $this->cancelButton;
156
        $backUrl = $this->backUrl;
157
        if ($backUrl === null) {
158
            $backUrlParts = explode('/', $this->uniqueId);
159
            unset($backUrlParts[count($backUrlParts) - 1]);
160
            $backUrl = [implode('/', $backUrlParts)];
161
        }
162
        if (is_array($backUrl)) {
163
            $backUrl = Url::to($backUrl);
164
        }
165
166
        $cancelButton = strtr($cancelButton, [
167
            '{backUrl}' => $backUrl,
168
        ]);
169
170
171
        if (is_callable($this->buttonsTemplate)) {
172
            $this->buttonsTemplate = call_user_func($this->buttonsTemplate, $this->model);
173
        }
174
175
        return strtr($this->buttonsTemplate, [
176
            '{save}' => $this->saveButton,
177
            '{apply}' => $this->applyButton,
178
            '{cancel}' => $cancelButton,
179
        ]);
180
    }
181
}