HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbSelect2.php
Go to the documentation of this file.
1 <?php
17 class TbSelect2 extends CInputWidget {
18 
24  public $form;
28  public $data = array();
29 
33  public $events = array();
34 
38  public $asDropDownList = true;
39 
43  public $val;
44 
48  public $options;
49 
54  public $readonly = false;
55 
60  public $disabled = false;
61 
67  public function init()
68  {
69  $this->normalizeData();
70 
71  $this->normalizeOptions();
72 
74 
75  $this->setDefaultWidthIfEmpty();
76 
77  // disabled & readonly
78  if (!empty($this->htmlOptions['readonly'])) {
79  $this->readonly = true;
80  }
81  if (!empty($this->htmlOptions['disabled'])) {
82  $this->disabled = true;
83  }
84  }
85 
91  public function run()
92  {
93  list($name, $id) = $this->resolveNameID();
94 
95  if ($this->hasModel()) {
96  if ($this->form) {
97  echo $this->asDropDownList
98  ?
99  $this->form->dropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions)
100  :
101  $this->form->hiddenField($this->model, $this->attribute, $this->htmlOptions);
102  } else {
103  echo $this->asDropDownList
104  ?
105  CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions)
106  :
107  CHtml::activeHiddenField($this->model, $this->attribute, $this->htmlOptions);
108  }
109 
110  } else {
111  echo $this->asDropDownList
112  ?
113  CHtml::dropDownList($name, $this->value, $this->data, $this->htmlOptions)
114  :
115  CHtml::hiddenField($name, $this->value, $this->htmlOptions);
116  }
117 
118  $this->registerClientScript($id);
119  }
120 
127  public function registerClientScript($id) {
128 
129  Booster::getBooster()->registerPackage('select2');
130 
131  $options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
132 
133  if(! empty($this->val)) {
134  if(is_array($this->val)) {
135  $data = CJSON::encode($this->val);
136  } else {
137  $data = $this->val;
138  }
139 
140  $defValue = ".select2('val', $data)";
141  }
142  else
143  $defValue = '';
144 
145  if ($this->readonly) {
146  $defValue .= ".select2('readonly', true)";
147  }
148  elseif ($this->disabled) {
149  $defValue .= ".select2('enable', false)";
150  }
151 
152  ob_start();
153  echo "jQuery('#{$id}').select2({$options})";
154  foreach ($this->events as $event => $handler) {
155  echo ".on('{$event}', " . CJavaScript::encode($handler) . ")";
156  }
157  echo $defValue;
158 
159  Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $this->getId(), ob_get_clean() . ';');
160  }
161 
162  private function setDefaultWidthIfEmpty()
163  {
164  if (empty($this->options['width'])) {
165  $this->options['width'] = 'resolve';
166  }
167  }
168 
169  private function normalizeData()
170  {
171  if (!$this->data)
172  $this->data = array();
173  }
174 
176  {
177  if (!empty($this->htmlOptions['placeholder']))
178  $this->options['placeholder'] = $this->htmlOptions['placeholder'];
179 
180  if (!empty($this->options['placeholder']) && empty($this->htmlOptions['multiple']))
181  $this->prependDataWithEmptyItem();
182  }
183 
184  private function normalizeOptions()
185  {
186  if (empty($this->options)) {
187  $this->options = array();
188  }
189  }
190 
191  private function prependDataWithEmptyItem()
192  {
193  $this->data = array('' => '') + $this->data;
194  }
195 }