1 | <?php |
||
31 | class ModalButton extends Widget |
||
32 | { |
||
33 | /** |
||
34 | * Toggle button will be placed outside of the form. |
||
35 | */ |
||
36 | const BUTTON_OUTSIDE = 1; |
||
37 | |||
38 | /** |
||
39 | * Toggle button will be rendered inside of the form with [[Modal]] widget. |
||
40 | */ |
||
41 | const BUTTON_IN_MODAL = 2; |
||
42 | |||
43 | /** |
||
44 | * Submit with HTML POST request. |
||
45 | */ |
||
46 | const SUBMIT_HTML = 0; |
||
47 | |||
48 | /** |
||
49 | * Submit using PJAX. |
||
50 | */ |
||
51 | const SUBMIT_PJAX = 1; |
||
52 | |||
53 | /** |
||
54 | * Submit using AJAX. |
||
55 | */ |
||
56 | const SUBMIT_AJAX = 2; |
||
57 | |||
58 | /** |
||
59 | * @var ActiveRecord the model. Is required. |
||
60 | */ |
||
61 | public $model; |
||
62 | |||
63 | /** |
||
64 | * @var string Used to generate form action URL and HTML id of modal. |
||
65 | * May be set manually, otherwise will be extracted from the model |
||
66 | */ |
||
67 | public $scenario; |
||
68 | |||
69 | /** |
||
70 | * @var string Model scenario before widget run |
||
71 | */ |
||
72 | protected $_oldScenario; |
||
73 | |||
74 | /** |
||
75 | * @var array|ActiveForm The options for the HTML form. |
||
76 | * The following special options are supported: |
||
77 | * |
||
78 | * - action: string|array, the action, that will be passed as first argument to [[Html::beginForm()]] |
||
79 | * - method: string, the method, that will be passed as second argument to [[Html::beginForm()]] |
||
80 | * |
||
81 | * The rest of the options will be passed to the [[ActiveForm::begin()]] method |
||
82 | * |
||
83 | * If the property was not false, it will contain [[ActiveForm]] instance after [[ModalButton::begin()]]. |
||
84 | */ |
||
85 | public $form = []; |
||
86 | |||
87 | /** |
||
88 | * @var array The options for rendering toggle button |
||
89 | * The toggle button is used to toggle the visibility of the modal window. |
||
90 | * If this property is false, no toggle button will be rendered. |
||
91 | * |
||
92 | * The following special options are supported: |
||
93 | * |
||
94 | * - tag: string, the tag name of the button. Defaults to 'button'. |
||
95 | * - label: string, the label of the button. Defaults to 'Show'. |
||
96 | * |
||
97 | * The rest of the options will be rendered as the HTML attributes of the button tag. |
||
98 | */ |
||
99 | public $button = []; |
||
100 | |||
101 | /** |
||
102 | * @var string|callable The body of modal window. |
||
103 | * If callable - will get the only argument - [[$this->model]] |
||
104 | */ |
||
105 | public $body; |
||
106 | |||
107 | /** |
||
108 | * @var array HTML options that will be passed to the [[Modal]] widget |
||
109 | * When ```footer``` is array, the following special options are supported: |
||
110 | * - tag: string, the tag name of the button. Defaults to 'button'. |
||
111 | * - label: string, the label of the button. Defaults to 'Show'. |
||
112 | * |
||
113 | * The rest of the options will be rendered as the HTML attributes of the button tag. |
||
114 | */ |
||
115 | public $modal = []; |
||
116 | |||
117 | /** |
||
118 | * @var integer determines the way of form submit |
||
119 | * @see [[SUBMIT_HTML]] |
||
120 | * @see [[SUBMIT_PJAX]] |
||
121 | * @see [[SUBMIT_AJAX]] |
||
122 | */ |
||
123 | public $submit = self::SUBMIT_PJAX; |
||
124 | |||
125 | /** |
||
126 | * @var array options that will be passed to ajax submit JS |
||
127 | * @see [[registerAjaxSubmit]] |
||
128 | */ |
||
129 | public $ajaxOptions = []; |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | * @throws InvalidConfigException |
||
134 | */ |
||
135 | public function init() |
||
157 | |||
158 | /** |
||
159 | * Initialization of options. |
||
160 | * @throws InvalidConfigException |
||
161 | */ |
||
162 | protected function initOptions() |
||
231 | |||
232 | /** |
||
233 | * Runs widget. |
||
234 | */ |
||
235 | public function run() |
||
247 | |||
248 | /** |
||
249 | * Renders toggle button. |
||
250 | */ |
||
251 | public function renderButton() |
||
279 | |||
280 | /** |
||
281 | * Constructs model ID, using [[$model]] primary key, or ID of the widget and scenario. |
||
282 | * @return string format: `modal_{id}_{scenario}` |
||
283 | */ |
||
284 | public function getModalId() |
||
294 | |||
295 | /** |
||
296 | * Begins form. |
||
297 | */ |
||
298 | public function beginForm() |
||
305 | |||
306 | /** |
||
307 | * Ends form. |
||
308 | */ |
||
309 | public function endForm() |
||
313 | |||
314 | /** |
||
315 | * Begins modal widget. |
||
316 | */ |
||
317 | public function beginModal() |
||
321 | |||
322 | /** |
||
323 | * Ends modal widget. |
||
324 | */ |
||
325 | public function endModal() |
||
329 | |||
330 | /** |
||
331 | * Registers JavaScript for ajax submit. |
||
332 | */ |
||
333 | public function registerAjaxSubmit() |
||
356 | |||
357 | public function registerFooterButtonScript() |
||
369 | } |
||
370 |
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.