1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiPanel core package |
4
|
|
|
* |
5
|
|
|
* @link https://hipanel.com/ |
6
|
|
|
* @package hipanel-core |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\widgets; |
12
|
|
|
|
13
|
|
|
use hipanel\base\Model; |
14
|
|
|
use hipanel\helpers\ArrayHelper; |
15
|
|
|
use hipanel\helpers\FontIcon; |
16
|
|
|
use Yii; |
17
|
|
|
use yii\base\Event; |
18
|
|
|
use yii\base\Widget; |
19
|
|
|
use yii\bootstrap\Modal; |
20
|
|
|
use yii\helpers\Html; |
21
|
|
|
use yii\helpers\Url; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class BlockModalButton |
25
|
|
|
* Used for special render of object blocking modal window with activating button. |
26
|
|
|
*/ |
27
|
|
|
class BlockModalButton extends Widget |
28
|
|
|
{ |
29
|
|
|
const ACTION_ENABLE = 'enable'; |
30
|
|
|
const ACTION_DISABLE = 'disable'; |
31
|
|
|
|
32
|
|
|
const SCENARIO_ENABLE = 'enable-block'; |
33
|
|
|
const SCENARIO_DISABLE = 'disable-block'; |
34
|
|
|
|
35
|
|
|
const EVENT_BEFORE_BODY = 'beforeBody'; |
36
|
|
|
const EVENT_AFTER_BODY = 'afterBody'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var integer ID of action |
40
|
|
|
* |
41
|
|
|
* @see ACTION_DISABLE |
42
|
|
|
* @SEE ACTION_ENABLE |
43
|
|
|
*/ |
44
|
|
|
public $action; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Model |
48
|
|
|
*/ |
49
|
|
|
public $model; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public $scenario; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array|Modal stores the overriding options for [[ModalButton]]. |
58
|
|
|
* After Modal creating, stores the object. |
59
|
|
|
*/ |
60
|
|
|
public $modal = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string the validation URL, will be passed to [[ModalButton::form]] |
64
|
|
|
* Default: `validate-form?scenario={$this->scenario}` |
65
|
|
|
*/ |
66
|
|
|
public $validationUrl; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array block reasons. Should be a key-value array, will be passed to [[Html::dropDown]] |
70
|
|
|
* Default: `Yii::$app->controller->getBlockReasons()` |
71
|
|
|
*/ |
72
|
|
|
public $blockReasons; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var array Options for trigger button. Will be passed to [[ModalButton::button]] |
76
|
|
|
*/ |
77
|
|
|
public $button = []; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var array Options to render the header of modal. |
81
|
|
|
* Keys with special behaviour: |
82
|
|
|
* - tag - html tag, used to wrap the header label (default: h4) |
83
|
|
|
* - label - the value of tag |
84
|
|
|
* |
85
|
|
|
* All other options will be passed as third argument options to [[Html::tag]] |
86
|
|
|
*/ |
87
|
|
|
public $header = []; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var array Options to render the warning message inside of the modal. |
91
|
|
|
* Keys with special behaviour: |
92
|
|
|
* - tag - html tag, used to wrap the label (default: h4) |
93
|
|
|
* - label - the value of tag |
94
|
|
|
* |
95
|
|
|
* All other options will be passed as third argument options to [[Html::tag]] |
96
|
|
|
*/ |
97
|
|
|
public $warning = []; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var array |
101
|
|
|
*/ |
102
|
|
|
public $footer = []; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function init() |
108
|
|
|
{ |
109
|
|
|
parent::init(); |
110
|
|
|
$this->initOptions(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Configures options of widget. |
115
|
|
|
*/ |
116
|
|
|
public function initOptions() |
117
|
|
|
{ |
118
|
|
|
$model = $this->model; |
119
|
|
|
if ($this->action === null) { |
120
|
|
|
$this->action = $model->state === $model::STATE_BLOCKED ? self::ACTION_DISABLE : self::ACTION_ENABLE; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($this->scenario === null) { |
124
|
|
|
$this->scenario = $this->action === static::ACTION_ENABLE ? static::SCENARIO_ENABLE : static::SCENARIO_DISABLE; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($this->validationUrl === null) { |
128
|
|
|
$this->validationUrl = Url::toRoute(['validate-form', 'scenario' => $this->scenario]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($this->blockReasons === null) { |
132
|
|
|
$this->blockReasons = Yii::$app->controller->getBlockReasons(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (is_array($this->button)) { |
136
|
|
|
$this->button = ArrayHelper::merge([ |
137
|
|
|
static::ACTION_ENABLE => [ |
138
|
|
|
'label' => FontIcon::i('fa-lock fa-fw') . Yii::t('hipanel', 'Enable block'), |
139
|
|
|
'position' => ModalButton::BUTTON_OUTSIDE, |
140
|
|
|
], |
141
|
|
|
static::ACTION_DISABLE => [ |
142
|
|
|
'label' => FontIcon::i('fa-unlock fa-fw') . Yii::t('hipanel', 'Disable block'), |
143
|
|
|
'position' => ModalButton::BUTTON_OUTSIDE, |
144
|
|
|
], |
145
|
|
|
], $this->button); |
146
|
|
|
$this->button = $this->button[$this->action]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (is_array($this->header)) { |
150
|
|
|
$this->header = ArrayHelper::merge([ |
151
|
|
|
static::ACTION_ENABLE => [ |
152
|
|
|
'label' => Yii::t('hipanel', 'Are you sure you want to block this object'), |
153
|
|
|
'class' => 'label-danger', |
154
|
|
|
], |
155
|
|
|
static::ACTION_DISABLE => [ |
156
|
|
|
'label' => Yii::t('hipanel', 'Are you sure you want to unblock this object'), |
157
|
|
|
'class' => 'label-info', |
158
|
|
|
], |
159
|
|
|
], $this->header); |
160
|
|
|
|
161
|
|
|
$this->header = $this->header[$this->action]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (is_array($this->warning)) { |
165
|
|
|
$this->warning = ArrayHelper::merge([ |
166
|
|
|
static::ACTION_ENABLE => false, |
167
|
|
|
static::ACTION_DISABLE => [ |
168
|
|
|
'label' => Yii::t('hipanel', 'Check whether all of the violations were eliminated'), |
169
|
|
|
], |
170
|
|
|
], $this->warning); |
171
|
|
|
|
172
|
|
|
$this->warning = $this->warning[$this->action]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (is_array($this->footer)) { |
176
|
|
|
$this->footer = ArrayHelper::merge([ |
177
|
|
|
static::ACTION_ENABLE => [ |
178
|
|
|
'label' => Yii::t('hipanel', 'Enable block'), |
179
|
|
|
'data-loading-text' => Yii::t('hipanel', 'loading...'), |
180
|
|
|
'class' => 'btn btn-danger', |
181
|
|
|
], |
182
|
|
|
static::ACTION_DISABLE => [ |
183
|
|
|
'label' => Yii::t('hipanel', 'Disable block'), |
184
|
|
|
'data-loading-text' => Yii::t('hipanel', 'loading...'), |
185
|
|
|
'class' => 'btn btn-info', |
186
|
|
|
], |
187
|
|
|
], $this->footer); |
188
|
|
|
|
189
|
|
|
$this->footer = $this->footer[$this->action]; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Begins modal. |
195
|
|
|
* @throws \yii\base\InvalidConfigException |
196
|
|
|
*/ |
197
|
|
|
protected function modalBegin() |
198
|
|
|
{ |
199
|
|
|
$config = ArrayHelper::merge([ |
200
|
|
|
'class' => ModalButton::class, |
201
|
|
|
'model' => $this->model, |
202
|
|
|
'scenario' => $this->scenario, |
203
|
|
|
'button' => $this->button, |
204
|
|
|
'form' => [ |
205
|
|
|
'enableAjaxValidation' => true, |
206
|
|
|
'validationUrl' => $this->validationUrl, |
207
|
|
|
], |
208
|
|
|
'modal' => [ |
209
|
|
|
'header' => Html::tag(ArrayHelper::remove($this->header, 'tag', 'h4'), |
210
|
|
|
ArrayHelper::remove($this->header, 'label')), |
211
|
|
|
'headerOptions' => $this->header, |
212
|
|
|
'footer' => $this->footer, |
213
|
|
|
], |
214
|
|
|
], $this->modal); |
215
|
|
|
|
216
|
|
|
$this->modal = call_user_func([ArrayHelper::remove($config, 'class'), 'begin'], $config); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Ends modal. |
221
|
|
|
*/ |
222
|
|
|
protected function modalEnd() |
223
|
|
|
{ |
224
|
|
|
$this->modal->end(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Renders the body of the Modal. |
229
|
|
|
* Triggers [[EVENT_BEFORE_BODY]] and [[EVENT_AFTER_BODY]] |
230
|
|
|
* Set `$event->handled = true` to prevent default body render. |
231
|
|
|
*/ |
232
|
|
|
protected function renderBody() |
233
|
|
|
{ |
234
|
|
|
$event = new Event(); |
235
|
|
|
$this->trigger(static::EVENT_BEFORE_BODY, $event); |
236
|
|
|
if ($event->handled) { |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if ($this->warning) { |
|
|
|
|
241
|
|
|
echo Html::tag('div', |
242
|
|
|
Html::tag( |
243
|
|
|
ArrayHelper::remove($this->warning, 'tag', 'h4'), |
244
|
|
|
ArrayHelper::remove($this->warning, 'label'), |
245
|
|
|
$this->warning |
246
|
|
|
), |
247
|
|
|
['class' => 'callout callout-warning'] |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if ($this->blockReasons) { |
|
|
|
|
252
|
|
|
echo $this->modal->form->field($this->model, 'type')->dropDownList($this->blockReasons); |
253
|
|
|
} |
254
|
|
|
echo $this->modal->form->field($this->model, 'comment'); |
255
|
|
|
|
256
|
|
|
$this->trigger(static::EVENT_AFTER_BODY); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Renders widget. |
261
|
|
|
*/ |
262
|
|
|
public function run() |
263
|
|
|
{ |
264
|
|
|
$this->modalBegin(); |
265
|
|
|
$this->renderBody(); |
266
|
|
|
$this->modalEnd(); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
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@property
annotation 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.