HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbInput.php
Go to the documentation of this file.
1 <?php
17 abstract class TbInput extends CInputWidget
18 {
19  // The different input types.
20  const TYPE_CHECKBOX = 'checkbox';
21  const TYPE_CHECKBOXLIST = 'checkboxlist';
22  const TYPE_CHECKBOXLIST_INLINE = 'checkboxlist_inline';
23  const TYPE_CHECKBOXGROUPSLIST = 'checkboxgroupslist';
24  const TYPE_DROPDOWN = 'dropdownlist';
25  const TYPE_FILE = 'filefield';
26  const TYPE_PASSWORD = 'password';
27  const TYPE_PASSFIELD = 'passfield';
28  const TYPE_RADIO = 'radiobutton';
29  const TYPE_RADIOLIST = 'radiobuttonlist';
30  const TYPE_RADIOLIST_INLINE = 'radiobuttonlist_inline';
31  const TYPE_RADIOBUTTONGROUPSLIST = 'radiobuttongroupslist';
32  const TYPE_TEXTAREA = 'textarea';
33  const TYPE_TEXT = 'text';
34  const TYPE_MASKEDTEXT = 'maskedtextfield';
35  const TYPE_CAPTCHA = 'captcha';
36  const TYPE_UNEDITABLE = 'uneditable';
37  const TYPE_DATEPICKER = 'datepicker';
38  const TYPE_DATETIMEPICKER = 'datetimepicker';
39  const TYPE_REDACTOR = 'redactor';
40  const TYPE_MARKDOWNEDITOR = 'markdowneditor';
41  const TYPE_HTML5EDITOR = 'wysihtml5';
42  const TYPE_DATERANGEPICKER = 'daterangepicker';
43  const TYPE_TOGGLEBUTTON = 'togglebutton';
44  const TYPE_COLORPICKER = 'colorpicker';
45  const TYPE_CKEDITOR = 'ckeditor';
46  const TYPE_TIMEPICKER = 'timepicker';
47  const TYPE_SELECT2 = 'select2';
48  const TYPE_TYPEAHEAD = 'typeahead';
49  const TYPE_NUMBER = 'numberfield';
50  const TYPE_CUSTOM = 'custom';
51 
55  public $form;
56 
60  public $label;
61 
68  public $type;
69 
73  public $data = array();
74 
78  public $prependText;
79 
83  public $appendText;
84 
88  public $hintText;
89 
93  public $labelOptions = array();
94 
98  public $prependOptions = array();
99 
103  public $appendOptions = array();
104 
108  public $hintOptions = array();
109 
113  public $errorOptions = array();
114 
118  public $captchaOptions = array();
119 
125  public $enableAjaxValidation = true;
126 
132  public $enableClientValidation = true;
133 
141  public function init()
142  {
143  if (!isset($this->form)) {
144  throw new CException(__CLASS__ . ': Failed to initialize widget! Form is not set.');
145  }
146 
147  if (!isset($this->model)) {
148  throw new CException(__CLASS__ . ': Failed to initialize widget! Model is not set.');
149  }
150 
151  if (!isset($this->type)) {
152  throw new CException(__CLASS__ . ': Failed to initialize widget! Input type is not set.');
153  }
154 
155  // todo: move this logic elsewhere, it doesn't belong here ...
156  if ($this->type === self::TYPE_UNEDITABLE) {
157  if (isset($this->htmlOptions['class'])) {
158  $this->htmlOptions['class'] .= ' uneditable-input';
159  } else {
160  $this->htmlOptions['class'] = 'uneditable-input';
161  }
162  }
163 
164  $this->processHtmlOptions();
165  }
166 
172  protected function processHtmlOptions()
173  {
174  if (isset($this->htmlOptions['label'])) {
175  $this->label = $this->htmlOptions['label'];
176  unset($this->htmlOptions['label']);
177  }
178 
179  if (isset($this->htmlOptions['prepend'])) {
180  $this->prependText = $this->htmlOptions['prepend'];
181  unset($this->htmlOptions['prepend']);
182  }
183 
184  if (isset($this->htmlOptions['append'])) {
185  $this->appendText = $this->htmlOptions['append'];
186  unset($this->htmlOptions['append']);
187  }
188 
189  if (isset($this->htmlOptions['hint'])) {
190  $this->hintText = $this->htmlOptions['hint'];
191  unset($this->htmlOptions['hint']);
192  }
193 
194  if (isset($this->htmlOptions['labelOptions'])) {
195  $this->labelOptions = $this->htmlOptions['labelOptions'];
196  unset($this->htmlOptions['labelOptions']);
197  }
198 
199  if (isset($this->htmlOptions['prependOptions'])) {
200  $this->prependOptions = $this->htmlOptions['prependOptions'];
201  unset($this->htmlOptions['prependOptions']);
202  }
203 
204  if (isset($this->htmlOptions['appendOptions'])) {
205  $this->appendOptions = $this->htmlOptions['appendOptions'];
206  unset($this->htmlOptions['appendOptions']);
207  }
208 
209  if (isset($this->htmlOptions['hintOptions'])) {
210  $this->hintOptions = $this->htmlOptions['hintOptions'];
211  unset($this->htmlOptions['hintOptions']);
212  }
213 
214  if (isset($this->htmlOptions['errorOptions'])) {
215  $this->errorOptions = $this->htmlOptions['errorOptions'];
216  if (isset($this->htmlOptions['errorOptions']['enableAjaxValidation'])) {
217  $this->enableAjaxValidation = (boolean)$this->htmlOptions['errorOptions']['enableAjaxValidation'];
218  }
219 
220  if (isset($this->htmlOptions['errorOptions']['enableClientValidation'])) {
221  $this->enableClientValidation = (boolean)$this->htmlOptions['errorOptions']['enableClientValidation'];
222  }
223  unset($this->htmlOptions['errorOptions']);
224  }
225 
226  if (isset($this->htmlOptions['captchaOptions'])) {
227  $this->captchaOptions = $this->htmlOptions['captchaOptions'];
228  unset($this->htmlOptions['captchaOptions']);
229  }
230  }
231 
239  public function run()
240  {
241  switch ($this->type) {
242  case self::TYPE_CHECKBOX:
243  $this->checkBox();
244  break;
245 
246  case self::TYPE_CHECKBOXLIST:
247  $this->checkBoxList();
248  break;
249 
250  case self::TYPE_CHECKBOXLIST_INLINE:
251  $this->checkBoxListInline();
252  break;
253 
254  case self::TYPE_CHECKBOXGROUPSLIST:
255  $this->checkBoxGroupsList();
256  break;
257 
258  case self::TYPE_DROPDOWN:
259  $this->dropDownList();
260  break;
261 
262  case self::TYPE_FILE:
263  $this->fileField();
264  break;
265 
266  case self::TYPE_PASSWORD:
267  $this->passwordField();
268  break;
269 
270  case self::TYPE_PASSFIELD:
271  $this->passfieldField();
272  break;
273 
274  case self::TYPE_RADIO:
275  $this->radioButton();
276  break;
277 
278  case self::TYPE_RADIOLIST:
279  $this->radioButtonList();
280  break;
281 
282  case self::TYPE_RADIOLIST_INLINE:
283  $this->radioButtonListInline();
284  break;
285 
286  case self::TYPE_RADIOBUTTONGROUPSLIST:
287  $this->radioButtonGroupsList();
288  break;
289 
290  case self::TYPE_TEXTAREA:
291  $this->textArea();
292  break;
293 
294  case 'textfield': // backwards compatibility
295  case self::TYPE_TEXT:
296  $this->textField();
297  break;
298 
299  case self::TYPE_MASKEDTEXT:
300  $this->maskedTextField();
301  break;
302 
303  case self::TYPE_CAPTCHA:
304  $this->captcha();
305  break;
306 
307  case self::TYPE_UNEDITABLE:
308  $this->uneditableField();
309  break;
310 
311  case self::TYPE_DATEPICKER:
312  $this->datepickerField();
313  break;
314 
315  case self::TYPE_DATETIMEPICKER:
316  $this->datetimepickerField();
317  break;
318 
319  case self::TYPE_REDACTOR:
320  $this->redactorJs();
321  break;
322 
323  case self::TYPE_MARKDOWNEDITOR:
324  $this->markdownEditorJs();
325  break;
326 
327  case self::TYPE_HTML5EDITOR:
328  $this->html5Editor();
329  break;
330 
331  case self::TYPE_DATERANGEPICKER:
332  $this->dateRangeField();
333  break;
334 
335  case self::TYPE_TOGGLEBUTTON:
336  $this->toggleButton();
337  break;
338 
339  case self::TYPE_COLORPICKER:
340  $this->colorpickerField();
341  break;
342 
343  case self::TYPE_CKEDITOR:
344  $this->ckEditor();
345  break;
346 
347  case self::TYPE_TIMEPICKER:
348  $this->timepickerField();
349  break;
350 
351  case self::TYPE_SELECT2:
352  $this->select2Field();
353  break;
354 
355  case self::TYPE_TYPEAHEAD:
356  $this->typeAheadField();
357  break;
358 
359  case self::TYPE_NUMBER:
360  $this->numberField();
361  break;
362 
363  case self::TYPE_CUSTOM:
364  $this->customField();
365  break;
366 
367  default:
368  throw new CException(__CLASS__ . ': Failed to run widget! Type is invalid.');
369  }
370  }
371 
379  protected function getLabel()
380  {
381  if ($this->label !== false && !in_array($this->type, array('checkbox', 'radio')) && $this->hasModel()) {
382  return $this->form->labelEx($this->model, $this->attribute, $this->labelOptions);
383  } else if ($this->label !== null) {
384  return $this->label;
385  } else {
386  return '';
387  }
388  }
389 
397  protected function getPrepend()
398  {
399  if ($this->hasAddOn()) {
400  $htmlOptions = $this->prependOptions;
401 
402  if (isset($htmlOptions['class'])) {
403  $htmlOptions['class'] .= ' add-on';
404  } else {
405  $htmlOptions['class'] = 'add-on';
406  }
407 
408  ob_start();
409  echo '<div class="' . $this->getAddonCssClass() . '">';
410  if (isset($this->prependText)) {
411  if (isset($htmlOptions['isRaw']) && $htmlOptions['isRaw']) {
412  echo $this->prependText;
413  } else {
414  echo CHtml::tag('span', $htmlOptions, $this->prependText);
415  }
416  }
417 
418  return ob_get_clean();
419  } else {
420  return '';
421  }
422  }
423 
431  protected function getAppend()
432  {
433  if ($this->hasAddOn()) {
434  $htmlOptions = $this->appendOptions;
435 
436  if (isset($htmlOptions['class'])) {
437  $htmlOptions['class'] .= ' add-on';
438  } else {
439  $htmlOptions['class'] = 'add-on';
440  }
441 
442  ob_start();
443  if (isset($this->appendText)) {
444  if (isset($htmlOptions['isRaw']) && $htmlOptions['isRaw']) {
445  echo $this->appendText;
446  } else {
447  echo CHtml::tag('span', $htmlOptions, $this->appendText);
448  }
449  }
450 
451  echo '</div>';
452  return ob_get_clean();
453  } else {
454  return '';
455  }
456  }
457 
467  protected function getAttributeId($attribute)
468  {
469  return isset($this->htmlOptions['id'])
470  ? $this->htmlOptions['id']
471  : CHtml::getIdByName(CHtml::resolveName($this->model, $attribute));
472  }
473 
481  protected function getError()
482  {
483  return $this->form->error(
484  $this->model,
485  $this->attribute,
486  $this->errorOptions,
487  $this->enableAjaxValidation,
488  $this->enableClientValidation
489  );
490  }
491 
499  protected function getHint()
500  {
501  if (isset($this->hintText)) {
502  $htmlOptions = $this->hintOptions;
503 
504  if (isset($htmlOptions['class'])) {
505  $htmlOptions['class'] .= ' help-block';
506  } else {
507  $htmlOptions['class'] = 'help-block';
508  }
509 
510  return CHtml::tag('p', $htmlOptions, $this->hintText);
511  } else {
512  return '';
513  }
514  }
515 
523  protected function getContainerCssClass()
524  {
525  return $this->model->hasErrors($this->attribute) ? CHtml::$errorCss : '';
526  }
527 
535  protected function getAddonCssClass()
536  {
537  $classes = array();
538  if (isset($this->prependText)) {
539  $classes[] = 'input-prepend';
540  }
541  if (isset($this->appendText)) {
542  $classes[] = 'input-append';
543  }
544 
545  return implode(' ', $classes);
546  }
547 
555  protected function hasAddOn()
556  {
557  return isset($this->prependText) || isset($this->appendText);
558  }
559 
568  abstract protected function checkBox();
569 
578  abstract protected function toggleButton();
579 
588  abstract protected function checkBoxList();
589 
598  abstract protected function checkBoxListInline();
599 
608  abstract protected function checkBoxGroupsList();
609 
618  abstract protected function dropDownList();
619 
628  abstract protected function fileField();
629 
638  abstract protected function passwordField();
639 
648  abstract protected function passfieldField();
649 
658  abstract protected function radioButton();
659 
668  abstract protected function radioButtonList();
669 
678  abstract protected function radioButtonListInline();
679 
688  abstract protected function radioButtonGroupsList();
689 
698  abstract protected function textArea();
699 
708  abstract protected function textField();
709 
718  abstract protected function maskedTextField();
719 
728  abstract protected function captcha();
729 
738  abstract protected function uneditableField();
739 
748  abstract protected function datepickerField();
749 
758  abstract protected function datetimepickerField();
759 
768  abstract protected function redactorJs();
769 
778  abstract protected function markdownEditorJs();
779 
788  abstract protected function ckEditor();
789 
798  abstract protected function html5Editor();
799 
808  abstract protected function dateRangeField();
809 
818  abstract protected function colorpickerField();
819 
828  abstract protected function timepickerField();
829 
837  abstract protected function select2Field();
838 
843  abstract protected function typeAheadField();
844 
853  abstract protected function numberField();
854 
863  abstract protected function customField();
864 
874  protected function getSeparatedSelectableInput()
875  {
876  switch ($this->type)
877  {
878  case self::TYPE_CHECKBOX:
879  $method = 'checkBox';
880  break;
881  case self::TYPE_RADIO:
882  $method = 'radioButton';
883  break;
884  default:
885  throw new CException('This method can be used with only selectable control', E_USER_ERROR);
886  }
887 
888  $control = $this->form->{$method}($this->model, $this->attribute, $this->htmlOptions);
889  $hidden = '';
890 
891  $hasHiddenField = (array_key_exists('uncheckValue', $this->htmlOptions) && $this->htmlOptions['uncheckValue'] === null)
892  ? false
893  : true;
894 
895  if ($hasHiddenField && preg_match('/<input .*?type="hidden".*?\/>/', $control, $matches))
896  {
897  $hidden = $matches[0];
898  $control = str_replace($hidden, '', $control);
899  }
900 
901  return array($hidden, $control);
902  }
903 }