HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbToggleColumn.php
Go to the documentation of this file.
1 <?php
18 
19  public $value;
20 
27  public $name;
28 
32  public $htmlOptions = array('class' => 'toggle-column');
33 
37  public $headerHtmlOptions = array('class' => 'toggle-column');
38 
42  public $footerHtmlOptions = array('class' => 'toggle-column');
43 
49 
55 
61 
66  public $checkedIcon = 'ok-circle';
67 
72  public $uncheckedIcon = 'remove-sign';
73 
77  public $emptyIcon = 'question-sign';
78 
82  public $displayText = false;
83 
90  public $sortable = true;
91 
102  public $filter;
103 
108  public $toggleAction = 'toggle';
109 
128  public $afterToggle;
129 
133  public $uniqueClassSuffix = '';
134 
138  protected $button = array();
139 
144  public function init()
145  {
146  if ($this->name === null) {
147  throw new CException(Yii::t(
148  'zii',
149  '"{attribute}" attribute cannot be empty.',
150  array('{attribute}' => "name")
151  ));
152  }
153 
154  $this->initButton();
155  $this->registerClientScript();
156  }
157 
161  protected function initButton()
162  {
163  if ($this->checkedButtonLabel === null) {
164  $this->checkedButtonLabel = Yii::t('zii', 'Uncheck');
165  }
166  if ($this->uncheckedButtonLabel === null) {
167  $this->uncheckedButtonLabel = Yii::t('zii', 'Check');
168  }
169  if ($this->emptyButtonLabel === null) {
170  $this->emptyButtonLabel = Yii::t('zii', 'Not set');
171  }
172 
173  $this->button = array(
174  'url' => 'Yii::app()->controller->createUrl("' . $this->toggleAction . '",array("pk"=>$data->primaryKey,"attribute"=>"' . $this->name . '"))',
175  'htmlOptions' => array('class' => $this->name . '_toggle' . $this->uniqueClassSuffix),
176  );
177 
178  if (Yii::app()->request->enableCsrfValidation) {
179  $csrfTokenName = Yii::app()->request->csrfTokenName;
180  $csrfToken = Yii::app()->request->csrfToken;
181  $csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
182  } else {
183  $csrf = '';
184  }
185 
186  if ($this->afterToggle === null) {
187  $this->afterToggle = 'function(){}';
188  }
189 
190  $this->button['click'] = "js:
191 function() {
192  var th=this;
193  var afterToggle={$this->afterToggle};
194  $.fn.yiiGridView.update('{$this->grid->id}', {
195  type:'POST',
196  url:$(this).attr('href'),{$csrf}
197  success:function(data) {
198  $.fn.yiiGridView.update('{$this->grid->id}');
199  afterToggle(true, data);
200  },
201  error:function(XHR){
202  afterToggle(false,XHR);
203  }
204  });
205  return false;
206 }";
207  }
208 
212  protected function registerClientScript()
213  {
214  $js = array();
215 
216  $function = CJavaScript::encode($this->button['click']);
217  unset($this->button['click']);
218  $class = preg_replace('/\s+/', '.', $this->button['htmlOptions']['class']);
219  $js[] = "$(document).on('click','#{$this->grid->id} a.{$class}',$function);";
220 
221  if ($js !== array()) {
222  Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $this->id, implode("\n", $js));
223  }
224  }
225 
233  protected function renderDataCellContent($row, $data) {
234 
235  $checked = ($this->value === null)
236  ? CHtml::value($data, $this->name)
237  : $this->evaluateExpression($this->value, array('data' => $data, 'row' => $row));
238 
240  $button['icon'] = $checked === null ? $this->emptyIcon : ($checked ? $this->checkedIcon : $this->uncheckedIcon);
241  $button['url'] = isset($button['url']) ? $this->evaluateExpression(
242  $button['url'],
243  array('data' => $data, 'row' => $row)
244  ) : '#';
245 
246  if (!$this->displayText) {
247  $button['htmlOptions']['title'] = $this->getButtonLabel($checked);
248  if (!isset($button['htmlOptions']['data-toggle'])) {
249  $button['htmlOptions']['data-toggle'] = 'tooltip';
250  }
251  echo CHtml::link('<span class="glyphicon glyphicon-' . $button['icon'] . '"></span>', $button['url'], $button['htmlOptions']);
252  } else {
253  $button['label'] = $this->getButtonLabel($checked);
254  $button['class'] = 'booster.widgets.TbButton';
255  $widget = Yii::createComponent($button);
256  $widget->init();
257  $widget->run();
258  }
259  }
260 
261  private function getButtonLabel($value) {
262 
263  return $value === null ? $this->emptyButtonLabel : ($value ? $this->checkedButtonLabel : $this->uncheckedButtonLabel);
264  }
265 }