|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ## TbSelect2 class file. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Antonio Ramirez <[email protected]> |
|
7
|
|
|
* @copyright Copyright © Clevertech 2012- |
|
8
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* ## Select2 wrapper widget |
|
13
|
|
|
* |
|
14
|
|
|
* @see http://ivaynberg.github.io/select2/ |
|
15
|
|
|
* |
|
16
|
|
|
* @package booster.widgets.forms.inputs |
|
17
|
|
|
*/ |
|
18
|
|
|
class TbSelect2 extends CInputWidget { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var TbActiveForm when created via TbActiveForm. |
|
22
|
|
|
* This attribute is set to the form that renders the widget |
|
23
|
|
|
* @see TbActionForm->inputRow |
|
24
|
|
|
*/ |
|
25
|
|
|
public $form; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array @param data for generating the list options (value=>display) |
|
29
|
|
|
*/ |
|
30
|
|
|
public $data = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string[] the JavaScript event handlers. |
|
34
|
|
|
*/ |
|
35
|
|
|
public $events = array(); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool whether to display a dropdown select box or use it for tagging |
|
39
|
|
|
*/ |
|
40
|
|
|
public $asDropDownList = true; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string the default value. |
|
44
|
|
|
*/ |
|
45
|
|
|
public $val; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var |
|
49
|
|
|
*/ |
|
50
|
|
|
public $options; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var bool |
|
54
|
|
|
* @since 2.1.0 |
|
55
|
|
|
*/ |
|
56
|
|
|
public $readonly = false; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var bool |
|
60
|
|
|
* @since 2.1.0 |
|
61
|
|
|
*/ |
|
62
|
|
|
public $disabled = false; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* ### .init() |
|
66
|
|
|
* |
|
67
|
|
|
* Initializes the widget. |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function init() { |
|
70
|
1 |
|
$this->normalizeData(); |
|
71
|
|
|
|
|
72
|
1 |
|
$this->normalizeOptions(); |
|
73
|
|
|
|
|
74
|
1 |
|
$this->addEmptyItemIfPlaceholderDefined(); |
|
75
|
|
|
|
|
76
|
1 |
|
$this->setDefaultWidthIfEmpty(); |
|
77
|
|
|
|
|
78
|
|
|
// disabled & readonly |
|
79
|
1 |
|
if (!empty($this->htmlOptions['readonly'])) { |
|
80
|
|
|
$this->readonly = true; |
|
81
|
|
|
} |
|
82
|
1 |
|
if (!empty($this->htmlOptions['disabled'])) { |
|
83
|
|
|
$this->disabled = true; |
|
84
|
|
|
} |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* ### .run() |
|
89
|
|
|
* |
|
90
|
|
|
* Runs the widget. |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function run() { |
|
93
|
1 |
|
list($name, $id) = $this->resolveNameID(); |
|
94
|
|
|
|
|
95
|
1 |
|
if ($this->hasModel()) { |
|
96
|
1 |
|
if ($this->form) { |
|
97
|
|
|
echo $this->asDropDownList ? |
|
98
|
|
|
$this->form->dropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions) : |
|
99
|
|
|
$this->form->hiddenField($this->model, $this->attribute, $this->htmlOptions); |
|
100
|
|
|
} else { |
|
101
|
1 |
|
echo $this->asDropDownList ? |
|
102
|
1 |
|
CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions) : |
|
103
|
|
|
CHtml::activeHiddenField($this->model, $this->attribute, $this->htmlOptions); |
|
104
|
|
|
} |
|
105
|
1 |
|
} else { |
|
106
|
|
|
echo $this->asDropDownList ? |
|
107
|
|
|
CHtml::dropDownList($name, $this->value, $this->data, $this->htmlOptions) : |
|
108
|
|
|
CHtml::hiddenField($name, $this->value, $this->htmlOptions); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
$this->registerClientScript($id); |
|
112
|
1 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* ### .registerClientScript() |
|
116
|
|
|
* |
|
117
|
|
|
* Registers required client script for bootstrap select2. It is not used through bootstrap->registerPlugin |
|
118
|
|
|
* in order to attach events if any |
|
119
|
|
|
* |
|
120
|
|
|
* @param $id |
|
121
|
|
|
* |
|
122
|
|
|
* @throws CException |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function registerClientScript($id) { |
|
125
|
|
|
|
|
126
|
1 |
|
Booster::getBooster()->registerPackage('select2'); |
|
127
|
|
|
|
|
128
|
1 |
|
$options = !empty($this->options) ? CJavaScript::encode($this->options) : ''; |
|
129
|
|
|
|
|
130
|
1 |
|
if (!empty($this->val)) { |
|
131
|
|
|
if (is_array($this->val)) { |
|
132
|
|
|
$data = CJSON::encode($this->val); |
|
133
|
|
|
} else { |
|
134
|
|
|
$data = $this->val; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$defValue = ".select2('val', $data)"; |
|
138
|
|
|
} else |
|
139
|
1 |
|
$defValue = ''; |
|
140
|
|
|
|
|
141
|
1 |
|
if ($this->readonly) { |
|
142
|
|
|
$defValue .= ".select2('readonly', true)"; |
|
143
|
1 |
|
} elseif ($this->disabled) { |
|
144
|
|
|
$defValue .= ".select2('enable', false)"; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
ob_start(); |
|
148
|
1 |
|
echo "jQuery('select#{$id}').select2({$options})"; |
|
149
|
1 |
|
foreach ($this->events as $event => $handler) { |
|
150
|
|
|
echo ".on('{$event}', " . CJavaScript::encode($handler) . ")"; |
|
151
|
1 |
|
} |
|
152
|
1 |
|
echo $defValue; |
|
153
|
|
|
|
|
154
|
1 |
|
Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $this->getId(), ob_get_clean() . ';'); |
|
155
|
1 |
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
private function setDefaultWidthIfEmpty() { |
|
158
|
1 |
|
if (empty($this->options['width'])) { |
|
159
|
1 |
|
$this->options['width'] = 'resolve'; |
|
160
|
1 |
|
} |
|
161
|
1 |
|
} |
|
162
|
|
|
|
|
163
|
1 |
|
private function normalizeData() { |
|
164
|
1 |
|
if (!$this->data) |
|
|
|
|
|
|
165
|
1 |
|
$this->data = array(); |
|
166
|
1 |
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
private function addEmptyItemIfPlaceholderDefined() { |
|
169
|
1 |
|
if (!empty($this->htmlOptions['placeholder'])) |
|
170
|
1 |
|
$this->options['placeholder'] = $this->htmlOptions['placeholder']; |
|
171
|
|
|
|
|
172
|
1 |
|
if (!empty($this->options['placeholder']) && empty($this->htmlOptions['multiple'])) |
|
173
|
1 |
|
$this->prependDataWithEmptyItem(); |
|
174
|
1 |
|
} |
|
175
|
|
|
|
|
176
|
1 |
|
private function normalizeOptions() { |
|
177
|
1 |
|
if (empty($this->options)) { |
|
178
|
1 |
|
$this->options = array(); |
|
179
|
1 |
|
} |
|
180
|
1 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
private function prependDataWithEmptyItem() { |
|
183
|
|
|
$this->data = array('' => '') + $this->data; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.