|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* HiPanel core package |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://hipanel.com/ |
|
7
|
|
|
* @package hipanel-core |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hipanel\widgets; |
|
13
|
|
|
|
|
14
|
|
|
use Yii; |
|
15
|
|
|
use yii\base\InvalidConfigException; |
|
16
|
|
|
use yii\helpers\Html; |
|
17
|
|
|
use yii\helpers\Json; |
|
18
|
|
|
use yii\helpers\Url; |
|
19
|
|
|
use yii\web\JsExpression; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class AjaxModal. |
|
23
|
|
|
* |
|
24
|
|
|
* @property $actionUrl string URL |
|
25
|
|
|
* @property $modalFormId string ID for modal form |
|
26
|
|
|
* @property $errorText string The error text for PNotify generation. On set will be processed through [[Yii::t()]] |
|
27
|
|
|
* @property $successText string The success text for PNotify generation. On set will be processed through [[Yii::t()]] |
|
28
|
|
|
* @property $loadingText string The text that will be displayed on submit button after press. On set will be processed through [[Yii::t()]] |
|
29
|
|
|
* @property $loadingHtml string The HTML to be appended to the modal body |
|
30
|
|
|
*/ |
|
31
|
|
|
class AjaxModal extends \yii\bootstrap\Modal |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
public $bulkPage = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var bool |
|
40
|
|
|
*/ |
|
41
|
|
|
public $usePost = false; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
public $scenario; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string URL |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $_actionUrl; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string ID for modal form |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $_modalFormId; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string The error text for PNotify generation. |
|
60
|
|
|
* On set will be processed through [[Yii::t()]] |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $_errorText; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string The success text for PNotify generation. |
|
66
|
|
|
* On set will be processed through [[Yii::t()]] |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $_successText; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var string The text that will be displayed on submit button after press. |
|
72
|
|
|
* On set will be processed through [[Yii::t()]] |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $_loadingText; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var boolean whether to handle the submit of the form in the modal with special AJAX script |
|
78
|
|
|
* @see registerClientScript |
|
79
|
|
|
*/ |
|
80
|
|
|
public $handleSubmit = true; |
|
81
|
|
|
|
|
82
|
|
|
public function getActionUrl() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->_actionUrl ? Url::to($this->_actionUrl) : Url::to($this->scenario); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function setActionUrl($url) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->_actionUrl = $url; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getModalFormId() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->_modalFormId ?: $this->scenario . '-form'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function setModalFormId($id) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->_modalFormId = $id; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getErrorText() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->_errorText ?: Yii::t('app', 'An error occurred. Try again later.'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function setErrorText($text) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->_errorText = Yii::t('app', $text); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getSuccessText() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->_successText ?: Yii::t('app', 'Settings saved'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function setSuccessText($text) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->_successText = Yii::t('app', $text); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getLoadingText() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->_loadingText ?: Yii::t('app', 'loading') . '...'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function setLoadingText($text) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->_loadingText = Yii::t('app', $text); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function init() |
|
133
|
|
|
{ |
|
134
|
|
|
if (!$this->scenario) { |
|
135
|
|
|
throw new InvalidConfigException("Attribute 'scenario' is required"); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
Html::addCssClass($this->options['class'], 'text-left'); |
|
139
|
|
|
|
|
140
|
|
|
$this->initAdditionalOptions(); |
|
141
|
|
|
if ($this->handleSubmit !== false) { |
|
142
|
|
|
$this->registerClientScript(); |
|
143
|
|
|
} |
|
144
|
|
|
parent::init(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
protected function initAdditionalOptions() |
|
148
|
|
|
{ |
|
149
|
|
|
$quotedHtml = Json::htmlEncode($this->loadingHtml); |
|
|
|
|
|
|
150
|
|
|
if (!isset($this->clientEvents['show.bs.modal'])) { |
|
151
|
|
|
if ($this->bulkPage) { |
|
152
|
|
View Code Duplication |
if ($this->usePost) { |
|
|
|
|
|
|
153
|
|
|
$this->clientEvents['show.bs.modal'] = new JsExpression("function() { |
|
154
|
|
|
var selection = jQuery('div[role=\"grid\"]').yiiGridView('getSelectedRows'); |
|
155
|
|
|
$.post('{$this->actionUrl}', {selection: selection}).done(function (data) { |
|
|
|
|
|
|
156
|
|
|
$('#{$this->id} .modal-body').html(data); |
|
157
|
|
|
}); |
|
158
|
|
|
}"); |
|
159
|
|
|
} else { |
|
160
|
|
|
$this->clientEvents['show.bs.modal'] = new JsExpression("function() { |
|
161
|
|
|
var selection = jQuery('div[role=\"grid\"]').yiiGridView('getSelectedRows'); |
|
162
|
|
|
$.get('{$this->actionUrl}', {selection: selection}).done(function (data) { |
|
|
|
|
|
|
163
|
|
|
$('#{$this->id} .modal-body').html(data); |
|
164
|
|
|
}); |
|
165
|
|
|
}"); |
|
166
|
|
|
} |
|
167
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
168
|
|
|
if ($this->usePost) { |
|
169
|
|
|
$this->clientEvents['show.bs.modal'] = new JsExpression("function() { |
|
170
|
|
|
$.post('{$this->actionUrl}').done(function (data) { |
|
|
|
|
|
|
171
|
|
|
$('#{$this->id} .modal-body').html(data); |
|
172
|
|
|
}); |
|
173
|
|
|
}"); |
|
174
|
|
|
} else { |
|
175
|
|
|
$this->clientEvents['show.bs.modal'] = new JsExpression("function() { |
|
176
|
|
|
$.get('{$this->actionUrl}').done(function (data) { |
|
|
|
|
|
|
177
|
|
|
$('#{$this->id} .modal-body').html(data); |
|
178
|
|
|
}); |
|
179
|
|
|
}"); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
if (!isset($this->clientEvents['hidden.bs.modal'])) { |
|
184
|
|
|
$this->clientEvents['hidden.bs.modal'] = new JsExpression("function() { |
|
185
|
|
|
jQuery('#{$this->id} .modal-body').html({$quotedHtml}); |
|
186
|
|
|
}"); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
protected function registerClientScript() |
|
191
|
|
|
{ |
|
192
|
|
|
$url = is_string($this->handleSubmit) ? $this->handleSubmit : $this->actionUrl; |
|
|
|
|
|
|
193
|
|
|
Yii::$app->view->registerJs(<<<JS |
|
194
|
|
|
jQuery(document).on('submit', '#{$this->modalFormId}', function(event) { |
|
|
|
|
|
|
195
|
|
|
event.preventDefault(); |
|
196
|
|
|
var form = jQuery(this); |
|
197
|
|
|
var btn = jQuery('form[data-pjax] button').button('{$this->loadingText}'); |
|
|
|
|
|
|
198
|
|
|
jQuery.ajax({ |
|
199
|
|
|
url: '$url', |
|
200
|
|
|
type: 'POST', |
|
201
|
|
|
timeout: 0, |
|
202
|
|
|
data: form.serialize(), |
|
203
|
|
|
error: function() { |
|
204
|
|
|
new PNotify({ |
|
205
|
|
|
text: "{$this->errorText}", |
|
|
|
|
|
|
206
|
|
|
type: 'error', |
|
207
|
|
|
buttons: { |
|
208
|
|
|
sticker: false |
|
209
|
|
|
}, |
|
210
|
|
|
icon: false |
|
211
|
|
|
}); |
|
212
|
|
|
}, |
|
213
|
|
|
success: function() { |
|
214
|
|
|
jQuery('#$this->id').modal('hide'); |
|
215
|
|
|
new PNotify({ |
|
216
|
|
|
text: "{$this->successText}", |
|
|
|
|
|
|
217
|
|
|
type: 'info', |
|
218
|
|
|
buttons: { |
|
219
|
|
|
sticker: false |
|
220
|
|
|
}, |
|
221
|
|
|
icon: false |
|
222
|
|
|
}); |
|
223
|
|
|
btn.button('reset'); |
|
224
|
|
|
} |
|
225
|
|
|
}); |
|
226
|
|
|
}); |
|
227
|
|
|
JS |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function getLoadingHtml() |
|
232
|
|
|
{ |
|
233
|
|
|
return <<<HTML |
|
234
|
|
|
<div class="progress"> |
|
235
|
|
|
<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"> |
|
236
|
|
|
<span class="sr-only">{$this->loadingText}</span> |
|
|
|
|
|
|
237
|
|
|
</div> |
|
238
|
|
|
</div> |
|
239
|
|
|
HTML; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* {@inheritdoc} |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function renderBodyBegin() |
|
246
|
|
|
{ |
|
247
|
|
|
return Html::beginTag('div', ['class' => 'modal-body']) . $this->loadingHtml; |
|
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* {@inheritdoc} |
|
252
|
|
|
*/ |
|
253
|
|
|
protected function registerClientEvents() |
|
254
|
|
|
{ |
|
255
|
|
|
if (!empty($this->clientEvents)) { |
|
256
|
|
|
foreach ($this->clientEvents as $event => $handler) { |
|
257
|
|
|
if ($handler instanceof \Closure) { |
|
258
|
|
|
$this->clientEvents[$event] = call_user_func($handler, $this); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
parent::registerClientEvents(); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.