Completed
Pull Request — master (#25)
by
unknown
11:53
created

MultiSelect::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
cc 2
nc 2
nop 0
rs 10
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-multiselect-widget project.
5
 * (c) 2amigOS! <http://2amigos.us/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace dosamigos\multiselect;
11
12
use Yii;
13
use yii\base\InvalidConfigException;
14
use yii\base\InvalidParamException;
15
use yii\helpers\Html;
16
use yii\helpers\Json;
17
use yii\widgets\InputWidget;
18
19
/**
20
 * MultiSelect renders a [David Stutz Multiselect widget](http://davidstutz.github.io/bootstrap-multiselect/)
21
 *
22
 * @see http://davidstutz.github.io/bootstrap-multiselect/
23
 * @author Antonio Ramirez <[email protected]>
24
 * @link http://www.ramirezcobos.com/
25
 * @link http://www.2amigos.us/
26
 * @package dosamigos\widgets
27
 */
28
class MultiSelect extends InputWidget
29
{
30
    /**
31
     * @var array data for generating the list options (value=>display)
32
     */
33
    public $data = [];
34
    /**
35
     * @var array the options for the Bootstrap Multiselect JS plugin.
36
     *            Please refer to the Bootstrap Multiselect plugin Web page for possible options.
37
     * @see http://davidstutz.github.io/bootstrap-multiselect/#options
38
     */
39
    public $clientOptions = [];
40
41
    /**
42
     * @inheritdoc
43
     * @throws InvalidParamException
44
     */
45
    public function run()
46
    {
47
        if ($this->hasModel()) {
48
            echo Html::activeDropDownList($this->model, $this->attribute, $this->data, $this->options);
49
        } else {
50
            echo Html::dropDownList($this->name, $this->value, $this->data, $this->options);
51
        }
52
        $this->registerPlugin();
53
    }
54
55
    /**
56
     * Registers MultiSelect Bootstrap plugin and the related events
57
     * @throws InvalidParamException
58
     */
59
    protected function registerPlugin()
60
    {
61
        $view = $this->getView();
62
63
        MultiSelectAsset::register($view);
64
65
        $id = $this->options['id'];
66
67
        $options = $this->clientOptions !== false && !empty($this->clientOptions)
68
            ? Json::encode($this->clientOptions)
69
            : '';
70
71
        $js = "jQuery('#$id').multiselect($options);";
72
        $view->registerJs($js);
73
    }
74
}
75