1 | <?php |
||
33 | class Combo extends InputWidget |
||
34 | { |
||
35 | /** |
||
36 | * @var array the url that will be passed to [[Url::to()]] method to create the request URL |
||
37 | */ |
||
38 | public $url; |
||
39 | |||
40 | /** |
||
41 | * @var string the type of the field. |
||
42 | * Usual should be module/comboName. |
||
43 | * For example: client/client, hosting/account, domain/domain. |
||
44 | * In case of the combo overriding with some specific filters, |
||
45 | * the type should represent the filter. |
||
46 | * For example: if the hosting/service combo is extended with filter |
||
47 | * to show only DB services, the type should be hosting/service/db or hosting/dbService. |
||
48 | * The decision of the style depends on overall code style and readability |
||
49 | */ |
||
50 | public $type; |
||
51 | |||
52 | /** |
||
53 | * @var string the name of the representative field in the model. |
||
54 | * Used by [[getPrimaryFilter]] to create the name of the filtering field |
||
55 | * @see getPrimaryFilter() |
||
56 | */ |
||
57 | public $name; |
||
58 | |||
59 | /** |
||
60 | * @var string md5 of the configuration. |
||
61 | * Appears only after the combo registration in [[register]] |
||
62 | * @see register() |
||
63 | */ |
||
64 | public $configId; |
||
65 | |||
66 | /** |
||
67 | * @var array the HTML options for the input element |
||
68 | */ |
||
69 | public $inputOptions = []; |
||
70 | |||
71 | /** |
||
72 | * @var string the outer element selector, that holds all of related Combos |
||
73 | */ |
||
74 | public $formElementSelector = 'form'; |
||
75 | |||
76 | /** |
||
77 | * @var string the language. Default is application language |
||
78 | */ |
||
79 | public $language; |
||
80 | |||
81 | /** |
||
82 | * @var bool allow multiple selection |
||
83 | */ |
||
84 | public $multiple; |
||
85 | |||
86 | /** |
||
87 | * @var bool allow `Select All` button. Only if multiple is true |
||
88 | */ |
||
89 | public $selectAllButton = true; |
||
90 | |||
91 | /** |
||
92 | * @var array |
||
93 | */ |
||
94 | public $current; |
||
95 | |||
96 | /** |
||
97 | * @var mixed returning arguments |
||
98 | * Example: |
||
99 | * |
||
100 | * ``` |
||
101 | * ['id', 'password', 'another_column'] |
||
102 | * ``` |
||
103 | * |
||
104 | * @see getReturn() |
||
105 | * @see setReturn() |
||
106 | */ |
||
107 | protected $_return; |
||
108 | |||
109 | /** |
||
110 | * @var array renamed arguments |
||
111 | * Example: |
||
112 | * |
||
113 | * ``` |
||
114 | * [ |
||
115 | * 'new_col_name' => 'old_col_name', |
||
116 | * 'text' => 'login', |
||
117 | * 'deep' => 'array.subarray.value' // can extract some value from an array |
||
118 | * ] |
||
119 | * ``` |
||
120 | * |
||
121 | * @see getName() |
||
122 | * @see setName() |
||
123 | */ |
||
124 | protected $_rename; |
||
125 | |||
126 | /** |
||
127 | * @var array the static filters |
||
128 | * Example: |
||
129 | * |
||
130 | * ``` |
||
131 | * [ |
||
132 | * 'someStaticValue' => ['format' => 'the_value'], |
||
133 | * 'type' => ['format' => 'seller'], |
||
134 | * 'is_active' => [ |
||
135 | * 'field' => 'server', |
||
136 | * 'format' => new JsExpression('function (id, text, field) { |
||
137 | * if (field.isSet()) { |
||
138 | * return 1; |
||
139 | * } |
||
140 | * }'), |
||
141 | * ] |
||
142 | * ] |
||
143 | * @see setFilter() |
||
144 | * @see getFilter() |
||
145 | */ |
||
146 | protected $_filter = []; |
||
147 | |||
148 | /** |
||
149 | * @var string the name of the primary filter. Default: [[name]]_like |
||
150 | * @see getPrimaryFilter |
||
151 | * @see setPrimaryFilter |
||
152 | */ |
||
153 | protected $_primaryFilter; |
||
154 | |||
155 | /** |
||
156 | * @var boolean|string whether the combo has a primary key |
||
157 | * null (default) - decision will be taken automatically. |
||
158 | * In case when [[attribute]] has the `_id` postfix, this property will be treated as `true` |
||
159 | * false - the combo does not have an id. Meaning the value of the attribute will be used as the ID |
||
160 | * string - the explicit name of the ID attribute |
||
161 | */ |
||
162 | public $_hasId; |
||
163 | |||
164 | /** |
||
165 | * Options that will be passed to the plugin. |
||
166 | * |
||
167 | * @var array |
||
168 | * @see getPluginOptions() |
||
169 | */ |
||
170 | public $_pluginOptions = []; |
||
171 | |||
172 | /** {@inheritdoc} */ |
||
173 | 2 | public function init() |
|
174 | { |
||
175 | 2 | parent::init(); |
|
176 | |||
177 | // Set language |
||
178 | 2 | if ($this->language === null && ($language = Yii::$app->language) !== 'en-US') { |
|
179 | 2 | $this->language = substr($language, 0, 2); |
|
180 | 2 | } |
|
181 | 2 | if (!$this->_return) { |
|
182 | 2 | $this->return = ['id']; |
|
183 | 2 | } |
|
184 | 2 | if (!$this->rename) { |
|
185 | 2 | $this->rename = ['text' => $this->name]; |
|
186 | 2 | } |
|
187 | 2 | if (empty($this->inputOptions['id'])) { |
|
188 | $this->inputOptions['id'] = Html::getInputId($this->model, $this->attribute); |
||
189 | } |
||
190 | 2 | if ($this->multiple) { |
|
191 | $this->inputOptions['multiple'] = true; |
||
192 | } |
||
193 | 2 | if (!empty($this->inputOptions['readonly'])) { |
|
194 | // According to the HTML specification, the `select` element does not support |
||
195 | // property `readonly`. Solution: render `readonly` field as disabled and prepend hidden |
||
196 | // input to submit the attribute value. |
||
197 | $this->inputOptions['disabled'] = true; |
||
198 | } |
||
199 | 2 | if (empty($this->inputOptions['data-combo-field'])) { |
|
200 | 2 | $this->inputOptions['data-combo-field'] = $this->name; |
|
201 | 2 | } |
|
202 | 2 | if (!isset($this->inputOptions['unselect'])) { |
|
203 | 2 | $this->inputOptions['unselect'] = null; |
|
204 | 2 | } |
|
205 | 2 | } |
|
206 | |||
207 | public function run() |
||
213 | |||
214 | /** |
||
215 | * Renders text input that will be used by the plugin. |
||
216 | * Must apply [[inputOptions]] to the HTML element. |
||
217 | * |
||
218 | * @param string $type for compatible reasons. Should not be used |
||
219 | * @return string |
||
220 | */ |
||
221 | protected function renderInput($type = null) |
||
|
|||
222 | { |
||
223 | $html = []; |
||
224 | if (!empty($this->inputOptions['readonly'])) { |
||
225 | // As it was said in comments of `init` method, the `select` element does not support property `readonly`. |
||
226 | // However, disabled select will not be submitted. |
||
227 | // Solution: render hidden input to submit the attribue value. |
||
228 | $html[] = Html::activeHiddenInput($this->model, $this->attribute, [ |
||
229 | 'id' => $this->inputOptions['id'] . '-hidden', |
||
230 | ]); |
||
231 | } |
||
232 | $html[] = Html::activeDropDownList($this->model, $this->attribute, $this->getCurrentOptions(), $this->inputOptions); |
||
233 | |||
234 | return implode('', $html); |
||
235 | } |
||
236 | |||
237 | public function registerClientConfig() |
||
246 | |||
247 | public function registerClientScript() |
||
254 | |||
255 | public function getReturn() |
||
259 | |||
260 | /** |
||
261 | * @return mixed |
||
262 | */ |
||
263 | public function getRename() |
||
267 | |||
268 | /** |
||
269 | * @return mixed |
||
270 | */ |
||
271 | public function getFilter() |
||
275 | |||
276 | /** |
||
277 | * @param mixed $filter |
||
278 | */ |
||
279 | public function setFilter($filter) |
||
283 | |||
284 | /** |
||
285 | * @param mixed $rename |
||
286 | */ |
||
287 | public function setRename($rename) |
||
291 | 2 | ||
292 | /** |
||
293 | * @param mixed $return |
||
294 | */ |
||
295 | public function setReturn($return) |
||
299 | 2 | ||
300 | /** |
||
301 | * @return string |
||
302 | * @see _primaryFilter |
||
303 | */ |
||
304 | public function getPrimaryFilter() |
||
308 | |||
309 | /** |
||
310 | * @param $primaryFilter |
||
311 | * @see _primaryFilter |
||
312 | */ |
||
313 | public function setPrimaryFilter($primaryFilter) |
||
317 | |||
318 | /** |
||
319 | * Returns the config of the Combo, merges with the passed $config. |
||
320 | * |
||
321 | * @param array $options |
||
322 | * @return array |
||
323 | */ |
||
324 | public function getPluginOptions($options = []) |
||
325 | { |
||
326 | $defaultOptions = [ |
||
327 | 'name' => $this->name, |
||
328 | 'type' => $this->type, |
||
329 | 'hasId' => $this->hasId, |
||
330 | 'select2Options' => [ |
||
331 | 'width' => '100%', |
||
332 | 'placeholder' => '----------', |
||
333 | 'minimumInputLength' => '0', |
||
334 | 'ajax' => [ |
||
335 | 'url' => Url::toRoute($this->url ?? ''), |
||
336 | 'type' => 'post', |
||
337 | 'return' => $this->return, |
||
338 | 'rename' => $this->rename, |
||
339 | 'filter' => $this->filter, |
||
340 | 'data' => new JsExpression(" |
||
341 | function (event) { |
||
342 | return $(this).data('field').createFilter($.extend(true, { |
||
343 | '{$this->primaryFilter}': {format: event.term} |
||
344 | }, event.filters || {})); |
||
345 | } |
||
346 | "), |
||
347 | ], |
||
348 | ], |
||
349 | ]; |
||
350 | if ($this->multiple && $this->selectAllButton) { |
||
351 | Select2SelectAllAsset::register($this->view); |
||
352 | $defaultOptions = ArrayHelper::merge($defaultOptions, [ |
||
353 | 'select2Options' => [ |
||
354 | 'tags' => false, |
||
355 | 'tokenSeparators' => [',', ', ', ' '], |
||
356 | 'dropdownAdapter' => new JsExpression('$.fn.select2.amd.require("select2/custom/dropdown-adapter/select-all")'), |
||
357 | ], |
||
358 | ]); |
||
359 | } |
||
360 | |||
361 | return ArrayHelper::merge($defaultOptions, $this->_pluginOptions, $options); |
||
362 | } |
||
363 | |||
364 | public function getFormIsBulk() |
||
368 | |||
369 | /** |
||
370 | * @param array $pluginOptions |
||
371 | */ |
||
372 | public function setPluginOptions($pluginOptions) |
||
376 | |||
377 | /** |
||
378 | * @return bool|string |
||
379 | */ |
||
380 | public function getHasId() |
||
384 | |||
385 | /** |
||
386 | * @param bool|string $hasId |
||
387 | */ |
||
388 | public function setHasId($hasId) |
||
392 | |||
393 | /** |
||
394 | * Method collects list of options that will be rendered inside the `select` tag. |
||
395 | * @return array |
||
396 | */ |
||
397 | protected function getCurrentOptions() |
||
424 | } |
||
425 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.