1 | <?php |
||
34 | class Combo extends Widget |
||
35 | { |
||
36 | /** |
||
37 | * @var Model |
||
38 | */ |
||
39 | public $model; |
||
40 | |||
41 | /** |
||
42 | * @var string the attribute name |
||
43 | */ |
||
44 | public $attribute; |
||
45 | |||
46 | /** |
||
47 | * @var array the url that will be passed to [[Url::to()]] method to create the request URL |
||
48 | */ |
||
49 | public $url; |
||
50 | |||
51 | /** |
||
52 | * @var string the type of the field. |
||
53 | * Usual should be module/comboName. |
||
54 | * For example: client/client, hosting/account, domain/domain. |
||
55 | * In case of the combo overriding with some specific filters, |
||
56 | * the type should represent the filter. |
||
57 | * For example: if the hosting/service combo is extended with filter |
||
58 | * to show only DB services, the type should be hosting/service/db or hosting/dbService. |
||
59 | * The decision of the style depends on overall code style and readability |
||
60 | */ |
||
61 | public $type; |
||
62 | |||
63 | /** |
||
64 | * @var string the name of the representative field in the model. |
||
65 | * Used by [[getPrimaryFilter]] to create the name of the filtering field |
||
66 | * @see getPrimaryFilter() |
||
67 | */ |
||
68 | public $name; |
||
69 | |||
70 | /** |
||
71 | * @var string md5 of the configuration. |
||
72 | * Appears only after the combo registration in [[register]] |
||
73 | * @see register() |
||
74 | */ |
||
75 | public $configId; |
||
76 | |||
77 | /** |
||
78 | * @var array the HTML options for the input element |
||
79 | */ |
||
80 | public $inputOptions = []; |
||
81 | |||
82 | /** |
||
83 | * @var string the outer element selector, that holds all of related Combos |
||
84 | */ |
||
85 | public $formElementSelector = 'form'; |
||
86 | |||
87 | /** |
||
88 | * @var string the language. Default is application language |
||
89 | */ |
||
90 | public $language; |
||
91 | |||
92 | /** |
||
93 | * @var bool allow multiple selection |
||
94 | */ |
||
95 | public $multiple; |
||
96 | |||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | public $current; |
||
101 | |||
102 | /** |
||
103 | * @var mixed returning arguments |
||
104 | * Example: |
||
105 | * |
||
106 | * ``` |
||
107 | * ['id', 'password', 'another_column'] |
||
108 | * ``` |
||
109 | * |
||
110 | * @see getReturn() |
||
111 | * @see setReturn() |
||
112 | */ |
||
113 | protected $_return; |
||
114 | |||
115 | /** |
||
116 | * @var array renamed arguments |
||
117 | * Example: |
||
118 | * |
||
119 | * ``` |
||
120 | * [ |
||
121 | * 'new_col_name' => 'old_col_name', |
||
122 | * 'text' => 'login', |
||
123 | * 'deep' => 'array.subarray.value' // can extract some value from an array |
||
124 | * ] |
||
125 | * ``` |
||
126 | * |
||
127 | * @see getName() |
||
128 | * @see setName() |
||
129 | */ |
||
130 | protected $_rename; |
||
131 | |||
132 | /** |
||
133 | * @var array the static filters |
||
134 | * Example: |
||
135 | * |
||
136 | * ``` |
||
137 | * [ |
||
138 | * 'someStaticValue' => ['format' => 'the_value'], |
||
139 | * 'type' => ['format' => 'seller'], |
||
140 | * 'is_active' => [ |
||
141 | * 'field' => 'server', |
||
142 | * 'format' => new JsExpression('function (id, text, field) { |
||
143 | * if (field.isSet()) { |
||
144 | * return 1; |
||
145 | * } |
||
146 | * }'), |
||
147 | * ] |
||
148 | * ] |
||
149 | * @see setFilter() |
||
150 | * @see getFilter() |
||
151 | */ |
||
152 | protected $_filter = []; |
||
153 | |||
154 | /** |
||
155 | * @var string the name of the primary filter. Default: [[name]]_like |
||
156 | * @see getPrimaryFilter |
||
157 | * @see setPrimaryFilter |
||
158 | */ |
||
159 | protected $_primaryFilter; |
||
160 | |||
161 | /** |
||
162 | * @var boolean|string whether the combo has a primary key |
||
163 | * null (default) - decision will be taken automatically. |
||
164 | * In case when [[attribute]] has the `_id` postfix, this property will be treated as `true` |
||
165 | * false - the combo does not have an id. Meaning the value of the attribute will be used as the ID |
||
166 | * string - the explicit name of the ID attribute |
||
167 | */ |
||
168 | public $_hasId; |
||
169 | |||
170 | /** |
||
171 | * Options that will be passed to the plugin. |
||
172 | * |
||
173 | * @var array |
||
174 | * @see getPluginOptions() |
||
175 | */ |
||
176 | public $_pluginOptions = []; |
||
177 | |||
178 | /** {@inheritdoc} */ |
||
179 | 2 | public function init() |
|
180 | { |
||
181 | 2 | parent::init(); |
|
182 | |||
183 | // Set language |
||
184 | 2 | if ($this->language === null && ($language = Yii::$app->language) !== 'en-US') { |
|
185 | 2 | $this->language = substr($language, 0, 2); |
|
186 | 2 | } |
|
187 | 2 | if (!$this->_return) { |
|
188 | 2 | $this->return = ['id']; |
|
189 | 2 | } |
|
190 | 2 | if (!$this->rename) { |
|
191 | 2 | $this->rename = ['text' => $this->name]; |
|
192 | 2 | } |
|
193 | 2 | if (!$this->inputOptions['id']) { |
|
194 | $this->inputOptions['id'] = Html::getInputId($this->model, $this->attribute); |
||
195 | } |
||
196 | 2 | if ($this->multiple) { |
|
197 | $this->inputOptions['multiple'] = true; |
||
198 | } |
||
199 | 2 | if ($this->inputOptions['readonly']) { |
|
200 | // According to the HTML specification, the `select` element does not support |
||
201 | // property `readonly`. Solution: render `readonly` field as disabled and prepend hidden |
||
202 | // input to submit the attribute value. |
||
203 | $this->inputOptions['disabled'] = true; |
||
204 | } |
||
205 | 2 | if (!$this->inputOptions['data-combo-field']) { |
|
206 | 2 | $this->inputOptions['data-combo-field'] = $this->name; |
|
207 | 2 | } |
|
208 | 2 | if (!isset($this->inputOptions['unselect'])) { |
|
209 | 2 | $this->inputOptions['unselect'] = null; |
|
210 | 2 | } |
|
211 | 2 | } |
|
212 | |||
213 | public function run() |
||
219 | |||
220 | /** |
||
221 | * Renders text input that will be used by the plugin. |
||
222 | * Must apply [[inputOptions]] to the HTML element. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | protected function renderInput() |
||
227 | { |
||
228 | $html = []; |
||
229 | if ($this->inputOptions['readonly']) { |
||
230 | // As it was said in comments of `init` method, the `select` element does not support property `readonly`. |
||
231 | // However, disabled select will not be submitted. |
||
232 | // Solution: render hidden input to submit the attribue value. |
||
233 | $html[] = Html::activeHiddenInput($this->model, $this->attribute, [ |
||
234 | 'id' => $this->inputOptions['id'] . '-hidden', |
||
235 | ]); |
||
236 | } |
||
237 | $html[] = Html::activeDropDownList($this->model, $this->attribute, $this->getCurrentOptions(), $this->inputOptions); |
||
238 | |||
239 | return implode('', $html); |
||
240 | } |
||
241 | |||
242 | public function registerClientConfig() |
||
251 | |||
252 | public function registerClientScript() |
||
259 | |||
260 | public function getReturn() |
||
264 | |||
265 | /** |
||
266 | * @return mixed |
||
267 | */ |
||
268 | 2 | public function getRename() |
|
272 | |||
273 | /** |
||
274 | * @return mixed |
||
275 | */ |
||
276 | public function getFilter() |
||
280 | |||
281 | /** |
||
282 | * @param mixed $filter |
||
283 | */ |
||
284 | public function setFilter($filter) |
||
288 | |||
289 | /** |
||
290 | * @param mixed $rename |
||
291 | */ |
||
292 | 2 | public function setRename($rename) |
|
296 | |||
297 | /** |
||
298 | * @param mixed $return |
||
299 | */ |
||
300 | 2 | public function setReturn($return) |
|
304 | |||
305 | /** |
||
306 | * @return string |
||
307 | * @see _primaryFilter |
||
308 | */ |
||
309 | public function getPrimaryFilter() |
||
313 | |||
314 | /** |
||
315 | * @param $primaryFilter |
||
316 | * @see _primaryFilter |
||
317 | */ |
||
318 | public function setPrimaryFilter($primaryFilter) |
||
322 | |||
323 | /** |
||
324 | * Returns the config of the Combo, merges with the passed $config. |
||
325 | * |
||
326 | * @param array $options |
||
327 | * @return array |
||
328 | */ |
||
329 | public function getPluginOptions($options = []) |
||
330 | { |
||
331 | $defaultOptions = [ |
||
332 | 'name' => $this->name, |
||
333 | 'type' => $this->type, |
||
334 | 'hasId' => $this->hasId, |
||
335 | 'select2Options' => [ |
||
336 | 'width' => '100%', |
||
337 | 'placeholder' => '----------', |
||
338 | 'minimumInputLength' => '0', |
||
339 | 'ajax' => [ |
||
340 | 'url' => Url::toRoute($this->url), |
||
341 | 'type' => 'post', |
||
342 | 'return' => $this->return, |
||
343 | 'rename' => $this->rename, |
||
344 | 'filter' => $this->filter, |
||
345 | 'data' => new JsExpression(" |
||
346 | function (event) { |
||
347 | return $(this).data('field').createFilter($.extend(true, { |
||
348 | '{$this->primaryFilter}': {format: event.term} |
||
349 | }, event.filters || {})); |
||
350 | } |
||
351 | "), |
||
352 | ], |
||
353 | ], |
||
354 | ]; |
||
355 | |||
356 | return ArrayHelper::merge($defaultOptions, $this->_pluginOptions, $options); |
||
357 | } |
||
358 | |||
359 | public function getFormIsBulk() |
||
363 | |||
364 | /** |
||
365 | * @param array $pluginOptions |
||
366 | */ |
||
367 | public function setPluginOptions($pluginOptions) |
||
371 | |||
372 | /** |
||
373 | * @return bool|string |
||
374 | */ |
||
375 | public function getHasId() |
||
379 | |||
380 | /** |
||
381 | * @param bool|string $hasId |
||
382 | */ |
||
383 | public function setHasId($hasId) |
||
387 | |||
388 | /** |
||
389 | * Method collects list of options that will be rendered inside the `select` tag |
||
390 | * @return array |
||
391 | */ |
||
392 | protected function getCurrentOptions() |
||
419 | } |
||
420 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..