HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbTimePicker.php
Go to the documentation of this file.
1 <?php
16 Yii::import('booster.widgets.TbBaseInputWidget');
17 
19 
23  public $form;
24 
51  public $options = array();
52 
59  public $events = array();
60 
64  public $wrapperHtmlOptions = array();
65 
70  public $noAppend = false;
71 
75  public function run() {
76 
77  list($name, $id) = $this->resolveNameID();
78 
79  // TODO: what is this?
80  // Add a class of no-user-select to widget
81  $this->htmlOptions['class'] = empty($this->htmlOptions['class'])
82  ? 'no-user-select'
83  : 'no-user-select ' . $this->htmlOptions['class'];
84 
85  // We are overriding the result of $this->resolveNameID() here, because $id which it emits is not unique through the page.
86  if (empty($this->htmlOptions['id'])) {
87  $this->htmlOptions['id'] = $this->id;
88  }
89 
90  // Adding essential class for timepicker to work.
91  $this->wrapperHtmlOptions = $this->injectClass($this->wrapperHtmlOptions, 'bootstrap-timepicker');
92 
93  if (!$this->noAppend)
94  $this->wrapperHtmlOptions = $this->injectClass($this->wrapperHtmlOptions, 'input-group');
95 
96 
97  echo CHtml::openTag('div', $this->wrapperHtmlOptions);
98  if ($this->hasModel()) {
99  if ($this->form) {
100  echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
101  } else {
102  echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
103  }
104  } else {
105  echo CHtml::textField($name, $this->value, $this->htmlOptions);
106  }
107  if (!$this->noAppend)
108  $this->echoAppend();
109  echo CHtml::closeTag('div');
110 
111  $this->registerClientScript($this->id);
112  }
113 
119  public function registerClientScript($id) {
120 
121  Booster::getBooster()->cs->registerPackage('timepicker');
122 
123  $options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
124 
125  ob_start();
126 
127  echo "jQuery('#{$id}').timepicker({$options})";
128  foreach ($this->events as $event => $handler) {
129  echo ".on('{$event}', " . CJavaScript::encode($handler) . ")";
130  }
131 
132  Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $id, ob_get_clean() . ';');
133  }
134 
141  private function injectClass($valueset, $className) {
142 
143  if (array_key_exists('class', $valueset) and is_string($valueset['class'])) {
144  $valueset['class'] = implode(
145  ' ',
146  array_merge(
147  explode(
148  ' ',
149  $valueset['class']
150  ),
151  array($className)
152  )
153  );
154  } else {
155  $valueset['class'] = $className;
156  }
157 
158  return $valueset;
159  }
160 
161  private function echoAppend() {
162 
163  echo CHtml::tag('span', array('class' => 'input-group-addon'), CHtml::tag('i', array('class' => 'glyphicon glyphicon-time'), ''));
164  }
165 }