1 <?php
2 3 4 5 6 7 8 9 10
11
12 Yii::import('bootstrap.helpers.TbArray');
13
14 class WhMultiSelect extends CInputWidget
15 {
16
17 18 19
20 public $data = array();
21
22 23 24
25 public $events = array();
26
27 28 29 30
31 public $pluginOptions;
32
33 34 35
36 public function init()
37 {
38 if (empty($this->data)) {
39 throw new CException(Yii::t('zii', '"data" attribute cannot be blank'));
40 }
41
42 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
43 }
44
45 46 47
48 public function run()
49 {
50 $this->renderField();
51 $this->registerClientScript();
52 }
53
54 55 56
57 public function renderField()
58 {
59 list($name, $id) = $this->resolveNameID();
60
61 TbArray::defaultValue('id', $id, $this->htmlOptions);
62 TbArray::defaultValue('name', $name, $this->htmlOptions);
63
64
65 $this->htmlOptions['multiple'] = 'multiple';
66
67 if ($this->hasModel()) {
68 echo CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
69 } else {
70 echo CHtml::dropDownList($this->name, $this->value, $this->data, $this->htmlOptions);
71 }
72 }
73
74 75 76 77
78 public function registerClientScript()
79 {
80
81 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
82 $assetsUrl = $this->getAssetsUrl($path);
83
84
85 $cs = Yii::app()->getClientScript();
86
87 $cs->registerCssFile($assetsUrl . '/css/bootstrap-multiselect.css');
88 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-multiselect.js');
89
90
91 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
92
93 $this->getApi()->registerPlugin('multiselect', $selector, $this->pluginOptions);
94 $this->getApi()->registerEvents($selector, $this->events);
95 }
96 }