1 <?php
2 3 4 5 6 7 8 9 10 11
12 Yii::import('bootstrap.helpers.TbArray');
13 Yii::import('bootstrap.helpers.TbHtml');
14
15 class WhSelectBox extends CInputWidget
16 {
17
18 19 20
21 public $data = array();
22
23 24 25 26 27 28 29 30 31 32
33 public $size = 'input-medium';
34
35 36 37
38 public $displayFilter = true;
39
40 41 42
43 public $wrapperOptions = array();
44
45 46 47 48
49 public function init()
50 {
51 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
52 }
53
54 55 56
57 public function run()
58 {
59 $this->renderField();
60 $this->registerClientScript();
61 }
62
63 64 65
66 public function renderField()
67 {
68 list($name, $id) = $this->resolveNameID();
69
70 TbArray::defaultValue('id', $id, $this->htmlOptions);
71 TbArray::defaultValue('name', $name, $this->htmlOptions);
72
73 TbHtml::addCssClass('bfh-selectbox', $this->wrapperOptions);
74 echo CHtml::openTag('div', $this->wrapperOptions);
75 if ($this->hasModel()) {
76 echo CHtml::activeHiddenField($this->model, $this->attribute, $this->htmlOptions);
77 $value = $this->model->{$this->attribute};
78 $valueText = $value && isset($this->data[$value]) ? $this->data[$value] : ' ';
79 } else {
80 echo CHtml::hiddenField($name, $this->value, $this->htmlOptions);
81 $value = $this->value;
82 $valueText = $value && isset($this->data[$value]) ? $this->data[$value] : ' ';
83 }
84
85 echo CHtml::openTag('a', array('class' => 'bfh-selectbox-toggle', 'role' => 'button', 'data-toggle' => 'bfh-selectbox', 'href' => '#'));
86 echo CHtml::tag('span', array('class' => 'bfh-selectbox-option ' . $this->size, 'data-option' => $value), $valueText);
87 echo CHtml::tag('b', array('class' => 'caret'), ' ');
88 echo CHtml::closeTag('a');
89
90 echo CHtml::openTag('div', array('class' => 'bfh-selectbox-options'));
91 if($this->displayFilter) {
92 echo '<input type="text" class="bfh-selectbox-filter">';
93 }
94 $items = array();
95 foreach($this->data as $key=>$item) {
96 $items[] = CHtml::tag('a', array('tabindex' => '-1', 'href' => '#', 'data-option' => $key), $item);
97 }
98 echo CHtml::tag('ul', array('role'=>'options'), '<li>' . implode('</li><li>', $items) . '</i>');
99 echo CHtml::closeTag('div');
100
101 echo CHtml::closeTag('div');
102 }
103
104 105 106
107 public function registerClientScript()
108 {
109
110 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
111 $assetsUrl = $this->getAssetsUrl($path);
112
113
114 $cs = Yii::app()->getClientScript();
115
116 $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers.css');
117 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-selectbox.js');
118
119 }
120 }