ActiveField::multiSelect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
/**
3
 * This file is part of the fangface/yii2-concord package
4
 *
5
 * For the full copyright and license information, please view
6
 * the file LICENSE.md that was distributed with this source code.
7
 *
8
 * @package fangface/yii2-concord
9
 * @author Fangface <[email protected]>
10
 * @copyright Copyright (c) 2014 Fangface <[email protected]>
11
 * @license https://github.com/fangface/yii2-concord/blob/master/LICENSE.md MIT License
12
 *
13
 */
14
15
namespace fangface\forms;
16
17
use fangface\base\traits\InputAddonTrait;
18
use fangface\helpers\Html;
19
use fangface\widgets\BootstrapColorPicker;
20
use fangface\widgets\BootstrapSelect;
21
use fangface\widgets\BootstrapSelectSplitter;
22
use fangface\widgets\CKEditor;
23
use fangface\widgets\DatePicker;
24
use fangface\widgets\DateTimePicker;
25
use fangface\widgets\MiniColors;
26
use fangface\widgets\MultiSelect;
27
use fangface\widgets\Select2;
28
use fangface\widgets\TimePicker;
29
use yii\helpers\ArrayHelper;
30
31
32
class ActiveField extends \yii\widgets\ActiveField
33
{
34
35
    use InputAddonTrait;
36
37
    /**
38
     * @var ActiveForm the form that this field is associated with.
39
     */
40
    public $form;
41
42
43
    public function render($content = null)
44
    {
45
        if (!isset($this->parts['{hint}']) || $this->parts['{hint}'] === null) {
46
            $this->template = str_replace("\n{hint}" , '', $this->template);
47
        }
48
        $this->prepareTemplate();
49
        return parent::render($content);
50
    }
51
52
53
    /**
54
     * Prepare the template
55
     */
56
    public function prepareTemplate()
57
    {
58
        $this->template = strtr($this->template, [
59
            '{input}' => $this->generateAddon(),
60
        ]);
61
    }
62
63
64
    /**
65
     * Renders the closing tag of the field container.
66
     *
67
     * @return string the rendering result.
68
     */
69
    public function end()
70
    {
71
        return Html::endTag(isset($this->options['tag']) ? $this->options['tag'] : 'div') . "\n";
72
    }
73
74
75
    /**
76
     * Generates a icon for input.
77
     *
78
     * @param array|string|boolean $iconOptions icon options.
79
     * @param array|string|boolean $tooltipOptions tooltip options.
80
     * @return static the field object itself
81
     */
82
    public function icon($iconOptions = [], $tooltipOptions = [])
83
    {
84
        if (($iconOptions !== null && $iconOptions !== null) || ($tooltipOptions !== null && $tooltipOptions !== false)) {
85
86
            if (is_bool($iconOptions)) {
87
                $icon = 'fa fa-cogs';
88
                $iconOptions = [];
89
            } elseif (is_string($iconOptions)) {
90
                $icon = $iconOptions;
91
                $iconOptions = [];
92
            } else {
93
                $icon = ArrayHelper::remove($iconOptions, 'icon', 'fa fa-cogs');
94
            }
95
96
            if ($tooltipOptions !== null) {
97
                if (is_string($tooltipOptions)) {
98
                    $tooltipOptions = ['title' => $tooltipOptions];
99
                }
100
                $iconOptions['data-container'] = ArrayHelper::getValue($tooltipOptions, 'container', 'body');
0 ignored issues
show
Bug introduced by
It seems like $tooltipOptions defined by parameter $tooltipOptions on line 82 can also be of type boolean; however, yii\helpers\BaseArrayHelper::getValue() does only seem to accept array|object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
101
                $iconOptions['data-placement'] = ArrayHelper::getValue($tooltipOptions, 'placement', 'top');
0 ignored issues
show
Bug introduced by
It seems like $tooltipOptions defined by parameter $tooltipOptions on line 82 can also be of type boolean; however, yii\helpers\BaseArrayHelper::getValue() does only seem to accept array|object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
102
                $iconOptions['data-original-title'] = ArrayHelper::getValue($tooltipOptions, 'title', '');
0 ignored issues
show
Bug introduced by
It seems like $tooltipOptions defined by parameter $tooltipOptions on line 82 can also be of type boolean; however, yii\helpers\BaseArrayHelper::getValue() does only seem to accept array|object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103
            }
104
105
106
            if ($icon) {
107
                $position = ArrayHelper::remove($iconOptions, 'position', InputField::ICON_POSITION_LEFT);
108
                $tooltip = false;
109
                if (isset($iconOptions['data-original-title'])) {
110
                    $tooltip = true;
111
                }
112
                Html::addCssClass($iconOptions, $icon . ($tooltip ? ' tooltips' : ''));
113
                $this->addIconAddon($iconOptions, $position);
0 ignored issues
show
Documentation introduced by
$iconOptions is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
            }
115
        }
116
        return $this;
117
    }
118
119
120
    /**
121
     * If supported set the focus to this field once the form is loaded
122
     *
123
     * @param boolean $select should contents of field also be selected
124
     * @return static the field object itself
125
     */
126
    public function setFocus($select = false)
127
    {
128
        Html::addCssClass($this->inputOptions, 'default-field' . ($select ? '-select' : ''));
129
        return $this;
130
    }
131
132
133
    /**
134
     * Add a clear input option
135
     *
136
     * @param string $clearType
137
     * @param string $clearValue [optional]
138
     * @param string $groupSize [optional] default input group size
139
     */
140
    public function addClearAddOn($clearType = 'input', $clearValue = '', $groupSize = '')
141
    {
142
        $iconData = [
143
            'class' => 'glyphicon glyphicon-remove clickable',
144
            'title' => 'Click to reset',
145
            'data-remove-input' => $clearType,
146
        ];
147
        if ($clearValue) {
148
            $iconData['data-remove-value'] = $clearValue;
149
        }
150
151
        $addon = Html::tag('span', Html::tag('i', '', $iconData),
152
            ['class' => 'input-group-addon addon-after']
153
        );
154
155
        $this->addAddon(['content' => $addon, 'raw' => true], 'append');
156
        $this->setGroupSize($groupSize);
157
    }
158
159
160
    /**
161
     * Override to allow for not including the uncheck hidden element when not wanted
162
     * e.g. when the checkbox is disabled
163
     *
164
     * {@inheritDoc}
165
     * @see \yii\widgets\ActiveField::checkbox($options, $enclosedByLabel)
166
     */
167
    public function checkbox($options = [], $enclosedByLabel = true)
168
    {
169
        if ($enclosedByLabel) {
170
            $this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
171
            $this->parts['{label}'] = '';
172
        } else {
173
            if (isset($options['label']) && !isset($this->parts['{label}'])) {
174
                $this->parts['{label}'] = $options['label'];
175
                if (!empty($options['labelOptions'])) {
176
                    $this->labelOptions = $options['labelOptions'];
177
                }
178
            }
179
            unset($options['labelOptions']);
180
            $options['label'] = null;
181
            $this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
182
        }
183
        $this->adjustLabelFor($options);
184
185
        return $this;
186
    }
187
188
189
    /**
190
     * Generates a tag that contains error.
191
     *
192
     * @param $error string the error to use.
193
     * @param array $options the tag options in terms of name-value pairs.
194
     *                       It will be merged with [[errorOptions]].
195
     * @return static the field object itself
196
     */
197
    public function staticError($error, $options = [])
198
    {
199
        $options = array_merge($this->errorOptions, $options);
200
        $tag = isset($options['tag']) ? $options['tag'] : 'div';
201
        unset($options['tag']);
202
        $this->parts['{error}'] = Html::tag($tag, $error, $options);
203
        return $this;
204
    }
205
206
207
    /**
208
     * Renders a static input
209
     *
210
     * @param array $options the tag options
211
     * @return $this
212
     */
213
    public function staticInput($options = [])
214
    {
215
        if (isset($options['value'])) {
216
            $content = $options['value'];
217
            unset($options['value']);
218
        } else {
219
            $content = Html::getAttributeValue($this->model, $this->attribute);
220
        }
221
        Html::addCssClass($options, 'form-control-static');
222
        $this->parts['{input}'] = Html::tag('p', $content, $options);
223
        $this->template = str_replace("\n{hint}", '', $this->template);
224
        $this->template = str_replace("\n{error}", '', $this->template);
225
        return $this;
226
    }
227
228
229
    /**
230
     * Generates datePicker component [[DatePicker]].
231
     *
232
     * @param array $options
233
     *        datePicker options
234
     * @return $this
235
     */
236
    public function datePicker($options = [])
237
    {
238
        $this->parts['{input}'] = DatePicker::widget(array_merge($options, [
239
            'model' => $this->model,
240
            'attribute' => $this->attribute,
241
            'form' => $this->form,
242
            'type' => $this->type,
243
            'addon' => $this->addon, // hand addon over to widget
244
        ]));
245
        $this->addon = []; // addon already processed by widget
246
        return $this;
247
    }
248
249
250
    /**
251
     * Generates dateTimePicker component [[DateTimePicker]].
252
     *
253
     * @param array $options
254
     *        datePicker options
255
     * @return $this
256
     */
257
    public function dateTimePicker($options = [])
258
    {
259
        $this->parts['{input}'] = DateTimePicker::widget(array_merge($options, [
260
            'model' => $this->model,
261
            'attribute' => $this->attribute,
262
            'form' => $this->form,
263
            'type' => $this->type,
264
            'addon' => $this->addon, // hand addon over to widget
265
        ]));
266
        $this->addon = []; // addon already processed by widget
267
        return $this;
268
    }
269
270
271
    /**
272
     * Generates timePicker component [[TimePicker]].
273
     *
274
     * @param array $options
275
     *        datePicker options
276
     * @return $this
277
     */
278
    public function timePicker($options = [])
279
    {
280
        $this->parts['{input}'] = TimePicker::widget(array_merge($options, [
281
            'model' => $this->model,
282
            'attribute' => $this->attribute,
283
            'form' => $this->form,
284
            'type' => $this->type,
285
            'addon' => $this->addon, // hand addon over to widget
286
        ]));
287
        $this->addon = []; // addon already processed by widget
288
        return $this;
289
    }
290
291
292
    /**
293
     * Generates dateRangePicker component [[DateRangePicker]].
294
     *
295
     * @param array $options
296
     *        dateRangePicker options
297
     * @return $this
298
     */
299
    public function dateRangePicker($options = [])
300
    {
301
        if ($this->form->type == ActiveForm::TYPE_VERTICAL) {
302
            //$options = array_merge($options, ['options' => ['style' => 'display:table-cell;']]);
303
            $options = array_merge($options, [
304
                'options' => [
305
                    'class' => 'show'
306
                ]
307
            ]);
308
        }
309
        $this->parts['{input}'] = DateRangePicker::widget(array_merge($options, [
310
            'model' => $this->model,
311
            'attribute' => $this->attribute,
312
            'form' => $this->form,
313
            'type' => $this->type,
314
            'addon' => $this->addon, // hand addon over to widget
315
        ]));
316
        $this->addon = []; // addon already processed by widget
317
        return $this;
318
    }
319
320
321
    /**
322
     * Generates colorPicker component [[BootstrapColorPicker]].
323
     *
324
     * @param array $options colorPicker options
325
     * @return $this
326
     */
327
    public function bsColorPicker($options = [])
328
    {
329
        $this->parts['{input}'] = BootstrapColorPicker::widget(array_merge($options, [
330
            'model' => $this->model,
331
            'attribute' => $this->attribute,
332
            'form' => $this->form,
333
            'type' => $this->type,
334
            'addon' => $this->addon, // hand addon over to widget
335
        ]));
336
        $this->addon = []; // addon already processed by widget
337
        return $this;
338
    }
339
340
341
    /**
342
     * Generates mini color widget [[MiniColor]].
343
     *
344
     * @param array $options colorPicker options
345
     * @return $this
346
     */
347
    public function miniColors($options = [])
348
    {
349
        $this->parts['{input}'] = MiniColors::widget(array_merge($options, [
350
            'model' => $this->model,
351
            'attribute' => $this->attribute,
352
            'form' => $this->form,
353
            'type' => $this->type,
354
            'addon' => $this->addon, // hand addon over to widget
355
        ]));
356
        $this->addon = []; // addon already processed by widget
357
        return $this;
358
    }
359
360
361
    /**
362
     * Generates select2 component [[Select2]].
363
     *
364
     * @param array $items
365
     * @param array $options select2 options
366
     * @return $this
367
     */
368
    public function select2($items, $options = [])
369
    {
370
        $this->parts['{input}'] = Select2::widget(array_merge($options, [
371
            'model' => $this->model,
372
            'attribute' => $this->attribute,
373
            'items' => $items,
374
            'form' => $this->form,
375
            'type' => $this->type,
376
            'addon' => $this->addon, // hand addon over to widget
377
        ]));
378
        $this->addon = []; // addon already processed by widget
379
        return $this;
380
    }
381
382
383
    /**
384
     * Generates select2 component [[Select2]] for multiple selection.
385
     *
386
     * @param array $items
387
     * @param array $options select2 options
388
     * @return $this
389
     */
390
    public function select2Multi($items, $options = [])
391
    {
392
        return $this->select2($items, $options);
393
    }
394
395
396
    /**
397
     * Generates bootstrap select picker component [[BootstrapSelect]].
398
     *
399
     * @param array $items
400
     * @param array $options bootstrap select options
401
     * @return $this
402
     */
403
    public function bsSelectPicker($items, $options = [])
404
    {
405
        $this->parts['{input}'] = BootstrapSelect::widget(array_merge($options, [
406
            'model' => $this->model,
407
            'attribute' => $this->attribute,
408
            'items' => $items,
409
            'form' => $this->form,
410
            'type' => $this->type,
411
            'addon' => $this->addon, // hand addon over to widget
412
        ]));
413
        $this->addon = []; // addon already processed by widget
414
        return $this;
415
    }
416
417
418
    /**
419
     * Generates bootstrap select picker component [[BootstrapSelect]] for multiple selection.
420
     *
421
     * @param array $items
422
     * @param array $options bootstrap select options
423
     * @return $this
424
     */
425
    public function bsSelectPickerMulti($items, $options = [])
426
    {
427
        return $this->bsSelectPicker($items, $options);
428
    }
429
430
431
    /**
432
     * Generates bootstrap select picker component [[BootstrapSelect]].
433
     *
434
     * @param array $items
435
     * @param array $options bootstrap select options
436
     * @return $this
437
     */
438
    public function bsSelectSplitter($items, $options = [])
439
    {
440
        $this->parts['{input}'] = BootstrapSelectSplitter::widget(array_merge($options, [
441
            'model' => $this->model,
442
            'attribute' => $this->attribute,
443
            'items' => $items,
444
            'form' => $this->form,
445
            'type' => $this->type,
446
            'addon' => $this->addon, // hand addon over to widget
447
        ]));
448
        $this->addon = []; // addon already processed by widget
449
        return $this;
450
    }
451
452
453
    /**
454
     * Generates select2 component [[Select2]] for tag selection.
455
     *
456
     * @param array $items
457
     * @param array $options select2 options
458
     * @return $this
459
     */
460
    public function select2Tags($items, $options = [])
461
    {
462
        return $this->select2($items, $options);
463
    }
464
465
466
    /**
467
     * Generates multiSelect component [[MultiSelect]].
468
     *
469
     * @param array $items
470
     * @param array $options multiSelect options
471
     * @return $this
472
     */
473
    public function multiSelect($items, $options = [])
474
    {
475
        $this->parts['{input}'] = MultiSelect::widget(array_merge($options, [
476
            'model' => $this->model,
477
            'attribute' => $this->attribute,
478
            'items' => $items,
479
            'form' => $this->form,
480
            'type' => $this->type,
481
            'addon' => $this->addon, // hand addon over to widget
482
        ]));
483
        $this->addon = []; // addon already processed by widget
484
        return $this;
485
    }
486
487
488
    /**
489
     * Generates range component.
490
     *
491
     * @param array $options range options
492
     * @return $this
493
     */
494
    public function range($options = [])
495
    {
496
        $this->parts['{input}'] = IonRangeSlider::widget(array_merge($options, [
497
            'model' => $this->model,
498
            'attribute' => $this->attribute,
499
            'form' => $this->form,
500
            'type' => $this->type,
501
            'addon' => $this->addon, // hand addon over to widget
502
        ]));
503
        $this->addon = []; // addon already processed by widget
504
        return $this;
505
    }
506
507
508
    /**
509
     * Generates spinner component.
510
     *
511
     * @param array $options spinner options
512
     * @return $this
513
     */
514
    public function spinner($options = [])
515
    {
516
        $this->parts['{input}'] = Spinner::widget(array_merge($options, [
517
            'model' => $this->model,
518
            'attribute' => $this->attribute,
519
            'form' => $this->form,
520
            'type' => $this->type,
521
            'addon' => $this->addon, // hand addon over to widget
522
        ]));
523
        $this->addon = []; // addon already processed by widget
524
        return $this;
525
    }
526
527
528
    /**
529
     * Renders a text input for an integer input.
530
     * @return $this the field object itself
531
     */
532
    public function textInputInteger($options = [])
533
    {
534
        return $this->textInput($options);
535
    }
536
537
538
    /**
539
     * Renders a text input for a decimal input.
540
     * @return $this the field object itself
541
     */
542
    public function textInputDecimal($options = [])
543
    {
544
        return $this->textInput($options);
545
    }
546
547
548
    /**
549
     * Renders a text input for a year input.
550
     * @return $this the field object itself
551
     */
552
    public function textInputYear($options = [])
553
    {
554
        return $this->textInput($options);
555
    }
556
557
558
    /**
559
     * Renders a text input for a time input.
560
     * @return $this the field object itself
561
     */
562
    public function textInputTime($options = [])
563
    {
564
        return $this->textInput($options);
565
    }
566
567
568
    /**
569
     * Renders a text input for a password strength input.
570
     * @return $this the field object itself
571
     */
572
    public function passwordStrength($options = [])
573
    {
574
        return $this->passwordInput($options);
575
    }
576
577
578
    /**
579
     * Generates CKEditor component [[CKEditor]].
580
     *
581
     * @param array $options CKEditor options
582
     * @return $this
583
     */
584
    public function editor_CK($options = [])
585
    {
586
        $this->parts['{input}'] = CKEditor::widget(array_merge($options, [
587
            'model' => $this->model,
588
            'attribute' => $this->attribute,
589
            'form' => $this->form,
590
            'type' => $this->type,
591
            'addon' => $this->addon, // hand addon over to widget
592
        ]));
593
        $this->addon = []; // addon already processed by widget
594
        return $this;
595
    }
596
597
}