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\loadingOverlay\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
|
|
|
const DEFAULT_BUTTONS_TEMPLATE = '{save} {apply} {cancel}'; |
23
|
|
|
const All_BUTTONS_TEMPLATE = '{check} {save} {apply} {cancel}'; |
24
|
|
|
public $uniqueId = null; |
25
|
|
|
public $action = null; |
26
|
|
|
public $buttonsTemplate = self::DEFAULT_BUTTONS_TEMPLATE; |
27
|
|
|
public $saveButton = '<input type="submit" name="save" value="Отправить" class="btn btn-primary" href="" title="Сохранить и вернуться">'; |
28
|
|
|
public $checkButton = '<input type="submit" name="check" value="Проверить" class="btn btn-info" href="" title="Проверить">'; |
29
|
|
|
public $applyButton = '<input type="submit" name="apply" value="Применить" class="btn btn-primary" href="" title="Сохранить изменения">'; |
30
|
|
|
public $cancelButton = '<a class="btn btn-default" href="{backUrl}">Вернуться к списку</a>'; |
31
|
|
|
public $backUrl = null; |
32
|
|
|
public $alertBlockAddon = null; |
33
|
|
|
|
34
|
|
|
public function __construct($config = []) |
35
|
|
|
{ |
36
|
|
|
$config = ArrayHelper::merge([ |
37
|
|
|
'panel'=> false, |
38
|
|
|
// [ |
|
|
|
|
39
|
|
|
// 'heading'=> '', |
40
|
|
|
// 'footer' => ' {buttons}', |
41
|
|
|
//// 'type'=>\kartik\detail\DetailView::TYPE_PRIMARY, |
42
|
|
|
// 'headingOptions' => [ |
43
|
|
|
// 'template' => '' |
44
|
|
|
// ], |
45
|
|
|
// 'footerOptions' => [ |
46
|
|
|
// 'style' => 'height: 31px', |
47
|
|
|
// ], |
48
|
|
|
// ], |
49
|
|
|
'buttons1' => '', |
50
|
|
|
'buttonContainer' => [ |
51
|
|
|
'class' => 'buttons-container', |
52
|
|
|
], |
53
|
|
|
'mainTemplate' => $this->renderAlertBlock() . '{detail}{buttons}', |
54
|
|
|
'bordered' => true, |
55
|
|
|
'striped' => true, |
56
|
|
|
'condensed' => true, |
57
|
|
|
'responsive' => false, |
58
|
|
|
'hideIfEmpty' => true, |
59
|
|
|
'hover' => true, |
60
|
|
|
// 'hAlign'=> true, |
|
|
|
|
61
|
|
|
// 'vAlign'=> true, |
62
|
|
|
// 'fadeDelay'=> 2000, |
63
|
|
|
// 'container' => ['id'=>'kv-demo'], |
64
|
|
|
'formOptions' => [ |
65
|
|
|
'enableAjaxValidation' => false, |
66
|
|
|
'validateOnChange' => false, |
67
|
|
|
'validateOnSubmit' => false, |
68
|
|
|
'validateOnBlur' => false, |
69
|
|
|
'options' => [ |
70
|
|
|
'enctype'=>'multipart/form-data', |
71
|
|
|
], |
72
|
|
|
], |
73
|
|
|
], $config); |
74
|
|
|
|
75
|
|
|
parent::__construct($config); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function run() { |
79
|
|
|
echo LoadingOverlay::widget(); |
80
|
|
|
|
81
|
|
|
$this->_registerBundle(); |
82
|
|
|
parent::registerPlugin('DetailView'); |
|
|
|
|
83
|
|
|
echo $this->_beginContainer(); |
84
|
|
|
$r = parent::run(); |
85
|
|
|
echo $this->_endContainer(); |
86
|
|
|
|
87
|
|
|
return $r; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function init() |
91
|
|
|
{ |
92
|
|
|
$this->attributes = $this->model->getFormFields(); |
93
|
|
|
$this->formOptions['action'] = $this->getAction(); |
94
|
|
|
$this->deleteOptions = [ |
95
|
|
|
'url' => Url::to([ |
96
|
|
|
$this->uniqueId . '/delete', |
97
|
|
|
'id' => $this->model->primaryKey, |
98
|
|
|
]), |
99
|
|
|
'kvdelete' => true |
100
|
|
|
]; |
101
|
|
|
$this->formOptions['validationUrl'] = $this->getAction(); |
102
|
|
|
|
103
|
|
|
parent::init(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getAction() { |
107
|
|
|
if ($this->action !== null) { |
108
|
|
|
return $this->action; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return Url::to([ |
112
|
|
|
$this->uniqueId . '/update', |
113
|
|
|
'id' => $this->model->primaryKey, |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function runWidget() |
118
|
|
|
{ |
119
|
|
|
$this->buttons2 = $this->renderSubmitButtons(); |
120
|
|
|
parent::runWidget(); // TODO: Change the autogenerated stub |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Initializes and renders alert container block |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
protected function renderAlertBlock() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$session = \Yii::$app->session; |
129
|
|
|
$flashes = $session->getAllFlashes(); |
130
|
|
|
$alertContainerOptions = [ |
131
|
|
|
'style' => 'max-width:400px' |
132
|
|
|
]; |
133
|
|
|
if (count($flashes) === 0) { |
134
|
|
|
Html::addCssStyle($alertContainerOptions, 'display:none;'); |
135
|
|
|
} |
136
|
|
|
$out = Html::beginTag('div', $alertContainerOptions); |
137
|
|
|
foreach ($flashes as $type => $message) { |
138
|
|
|
if (is_array($message)) { |
139
|
|
|
$message = implode('<br>', $message); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$alertWidgetOptions = []; |
143
|
|
|
$alertWidgetOptions['body'] = $message; |
144
|
|
|
$alertWidgetOptions['options'] = [ |
145
|
|
|
'class' => ['alert', 'alert-success'], |
146
|
|
|
'style' => 'padding-left:10px;padding-right:10px;' |
147
|
|
|
]; |
148
|
|
|
$out .= "\n" . Alert::widget($alertWidgetOptions); |
149
|
|
|
$session->removeFlash($type); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$out .= "\n</div>"; |
153
|
|
|
|
154
|
|
|
return $this->alertBlockAddon . $out; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $cancelUrl |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function renderSubmitButtons() |
162
|
|
|
{ |
163
|
|
|
$cancelButton = $this->cancelButton; |
164
|
|
|
$backUrl = $this->backUrl; |
165
|
|
|
if ($backUrl === null) { |
166
|
|
|
$backUrlParts = explode('/', $this->uniqueId); |
167
|
|
|
unset($backUrlParts[count($backUrlParts) - 1]); |
168
|
|
|
$backUrl = [implode('/', $backUrlParts)]; |
169
|
|
|
} |
170
|
|
|
if (is_array($backUrl)) { |
171
|
|
|
$backUrl = Url::to($backUrl); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$cancelButton = strtr($cancelButton, [ |
175
|
|
|
'{backUrl}' => $backUrl, |
176
|
|
|
]); |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
if (is_callable($this->buttonsTemplate)) { |
180
|
|
|
$this->buttonsTemplate = call_user_func($this->buttonsTemplate, $this->model); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return strtr($this->buttonsTemplate, [ |
184
|
|
|
'{check}' => $this->checkButton, |
185
|
|
|
'{save}' => $this->saveButton, |
186
|
|
|
'{apply}' => $this->applyButton, |
187
|
|
|
'{cancel}' => $cancelButton, |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
} |
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.