1 | <?php |
||
32 | class ModalButton extends Widget |
||
33 | { |
||
34 | /** |
||
35 | * Toggle button will be placed outside of the form. |
||
36 | */ |
||
37 | const BUTTON_OUTSIDE = 1; |
||
38 | |||
39 | /** |
||
40 | * Toggle button will be rendered inside of the form with [[Modal]] widget. |
||
41 | */ |
||
42 | const BUTTON_IN_MODAL = 2; |
||
43 | |||
44 | /** |
||
45 | * Submit with HTML POST request. |
||
46 | */ |
||
47 | const SUBMIT_HTML = 0; |
||
48 | |||
49 | /** |
||
50 | * Submit using PJAX. |
||
51 | */ |
||
52 | const SUBMIT_PJAX = 1; |
||
53 | |||
54 | /** |
||
55 | * Submit using AJAX. |
||
56 | */ |
||
57 | const SUBMIT_AJAX = 2; |
||
58 | |||
59 | /** |
||
60 | * @var ActiveRecord the model. Is required. |
||
61 | */ |
||
62 | public $model; |
||
63 | |||
64 | /** |
||
65 | * @var string Used to generate form action URL and HTML id of modal. |
||
66 | * May be set manually, otherwise will be extracted from the model |
||
67 | */ |
||
68 | public $scenario; |
||
69 | |||
70 | /** |
||
71 | * @var string Model scenario before widget run |
||
72 | */ |
||
73 | protected $_oldScenario; |
||
74 | |||
75 | /** |
||
76 | * @var array|ActiveForm The options for the HTML form. |
||
77 | * The following special options are supported: |
||
78 | * |
||
79 | * - action: string|array, the action, that will be passed as first argument to [[Html::beginForm()]] |
||
80 | * - method: string, the method, that will be passed as second argument to [[Html::beginForm()]] |
||
81 | * |
||
82 | * The rest of the options will be passed to the [[ActiveForm::begin()]] method |
||
83 | * |
||
84 | * If the property was not false, it will contain [[ActiveForm]] instance after [[ModalButton::begin()]]. |
||
85 | */ |
||
86 | public $form = []; |
||
87 | |||
88 | /** |
||
89 | * @var array The options for rendering toggle button |
||
90 | * The toggle button is used to toggle the visibility of the modal window. |
||
91 | * If this property is false, no toggle button will be rendered. |
||
92 | * |
||
93 | * The following special options are supported: |
||
94 | * |
||
95 | * - tag: string, the tag name of the button. Defaults to 'button'. |
||
96 | * - label: string, the label of the button. Defaults to 'Show'. |
||
97 | * |
||
98 | * The rest of the options will be rendered as the HTML attributes of the button tag. |
||
99 | */ |
||
100 | public $button = []; |
||
101 | |||
102 | /** |
||
103 | * @var string|callable The body of modal window. |
||
104 | * If callable - will get the only argument - [[$this->model]] |
||
105 | */ |
||
106 | public $body; |
||
107 | |||
108 | /** |
||
109 | * @var array HTML options that will be passed to the [[Modal]] widget |
||
110 | * When ```footer``` is array, the following special options are supported: |
||
111 | * - tag: string, the tag name of the button. Defaults to 'button'. |
||
112 | * - label: string, the label of the button. Defaults to 'Show'. |
||
113 | * |
||
114 | * The rest of the options will be rendered as the HTML attributes of the button tag. |
||
115 | */ |
||
116 | public $modal = []; |
||
117 | |||
118 | /** |
||
119 | * @var integer determines the way of form submit. |
||
120 | * @see [[SUBMIT_HTML]] |
||
121 | * @see [[SUBMIT_PJAX]] |
||
122 | * @see [[SUBMIT_AJAX]] |
||
123 | */ |
||
124 | public $submit = self::SUBMIT_PJAX; |
||
125 | |||
126 | /** |
||
127 | * @var array options that will be passed to ajax submit JS |
||
128 | * @see [[registerAjaxSubmit]] |
||
129 | */ |
||
130 | public $ajaxOptions = []; |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | * @throws InvalidConfigException |
||
135 | */ |
||
136 | public function init() |
||
137 | { |
||
138 | $this->initOptions(); |
||
139 | |||
140 | if ($this->button['position'] === static::BUTTON_OUTSIDE && isset($this->button['label'])) { |
||
141 | $this->renderButton(); |
||
142 | } |
||
143 | |||
144 | if ($this->form !== false) { |
||
145 | $this->beginForm(); |
||
146 | } |
||
147 | |||
148 | $this->beginModal(); |
||
149 | |||
150 | if (isset($this->body)) { |
||
151 | if ($this->body instanceof \Closure) { |
||
152 | echo call_user_func($this->body, $this->model); |
||
153 | } else { |
||
154 | echo $this->body; |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Initialization of options. |
||
161 | * @throws InvalidConfigException |
||
162 | */ |
||
163 | protected function initOptions() |
||
164 | { |
||
165 | if (!($this->model instanceof Model)) { |
||
166 | throw new InvalidConfigException('Model is required'); |
||
167 | } |
||
168 | |||
169 | if (!($this->model->getPrimaryKey())) { |
||
170 | throw new InvalidConfigException('Model has empty primary key'); |
||
171 | } |
||
172 | |||
173 | if ($this->button !== false) { |
||
174 | $this->button['position'] = isset($this->button['position']) ? $this->button['position'] : static::BUTTON_OUTSIDE; |
||
175 | } |
||
176 | |||
177 | if (empty($this->scenario)) { |
||
178 | $this->scenario = $this->model->scenario; |
||
179 | } else { |
||
180 | $this->_oldScenario = $this->model->scenario; |
||
181 | $this->model->scenario = $this->scenario; |
||
182 | } |
||
183 | |||
184 | if ($this->form !== false) { |
||
185 | $formConfig = [ |
||
186 | 'method' => 'POST', |
||
187 | 'action' => $this->scenario, |
||
188 | 'options' => [ |
||
189 | 'class' => 'inline', |
||
190 | 'data' => [ |
||
191 | 'modal-form' => true, |
||
192 | ], |
||
193 | ], |
||
194 | ]; |
||
195 | if ($this->submit === static::SUBMIT_PJAX) { |
||
196 | $formConfig['options'] = ArrayHelper::merge($formConfig['options'], [ |
||
197 | 'data' => ['pjax' => 1, 'pjax-push' => 0], |
||
198 | ]); |
||
199 | } elseif ($this->submit === static::SUBMIT_AJAX) { |
||
200 | $formConfig['options'] = ArrayHelper::merge($formConfig['options'], [ |
||
201 | 'data' => ['ajax-submit' => 1], |
||
202 | ]); |
||
203 | |||
204 | $this->registerAjaxSubmit(); |
||
205 | } |
||
206 | |||
207 | $this->form = ArrayHelper::merge($formConfig, $this->form); |
||
208 | } |
||
209 | |||
210 | if (is_array($footer = $this->modal['footer'])) { |
||
211 | $tag = ArrayHelper::remove($footer, 'tag', 'input'); |
||
212 | $label = ArrayHelper::remove($footer, 'label', 'OK'); |
||
213 | $footer = ArrayHelper::merge([ |
||
214 | 'data-modal-submit' => true, |
||
215 | 'data-loading-text' => '<i class="fa fa-circle-o-notch fa-spin"></i> ' . Yii::t('hipanel', 'loading'), |
||
216 | ], $footer); |
||
217 | |||
218 | if ($tag === 'input') { |
||
219 | $footer['type'] = 'submit'; |
||
220 | $footer['value'] = $label; |
||
221 | } |
||
222 | |||
223 | $this->modal['footer'] = Html::tag($tag, $label, $footer); |
||
224 | $this->registerFooterButtonScript(); |
||
225 | } |
||
226 | |||
227 | $this->modal = ArrayHelper::merge([ |
||
228 | 'id' => $this->getModalId(), |
||
229 | 'toggleButton' => ($this->button['position'] === static::BUTTON_IN_MODAL) ? $this->button : false, |
||
230 | ], $this->modal); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Runs widget. |
||
235 | */ |
||
236 | public function run() |
||
248 | |||
249 | /** |
||
250 | * Renders toggle button. |
||
251 | */ |
||
252 | public function renderButton() |
||
280 | |||
281 | /** |
||
282 | * Constructs model ID, using [[$model]] primary key, or ID of the widget and scenario. |
||
283 | * @return string format: ```modal_{id}_{scenario}``` |
||
284 | */ |
||
285 | public function getModalId() |
||
291 | |||
292 | /** |
||
293 | * Begins form. |
||
294 | */ |
||
295 | public function beginForm() |
||
300 | |||
301 | /** |
||
302 | * Ends form. |
||
303 | */ |
||
304 | public function endForm() |
||
308 | |||
309 | /** |
||
310 | * Begins modal widget. |
||
311 | */ |
||
312 | public function beginModal() |
||
316 | |||
317 | /** |
||
318 | * Ends modal widget. |
||
319 | */ |
||
320 | public function endModal() |
||
324 | |||
325 | /** |
||
326 | * Registers JavaScript for ajax submit. |
||
327 | * @return void |
||
328 | */ |
||
329 | public function registerAjaxSubmit() |
||
352 | |||
353 | public function registerFooterButtonScript() |
||
364 | } |
||
365 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.