HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbRelationalColumn.php
Go to the documentation of this file.
1 <?php
19 
23  public $url;
24 
30  public $cssClass = 'tbrelational-column';
31 
37  public $cacheData = true;
38 
58 
63  public $ajaxErrorMessage = 'Error';
64 
69  public $submitData=array();
70 
74  public function init() {
75 
76  parent::init();
77 
78  if (empty($this->url))
79  $this->url = Yii::app()->getRequest()->requestUri;
80 
81  $this->registerClientScript();
82  }
83 
91  public function renderDataCell($row) {
92 
93  $data = $this->grid->dataProvider->data[$row];
94  $options = $this->htmlOptions;
95 
96  if ($this->cssClassExpression !== null) {
97  $class = $this->evaluateExpression($this->cssClassExpression, array('row' => $row, 'data' => $data));
98  if (isset($options['class'])) {
99  $options['class'] .= ' ' . $class;
100  } else {
101  $options['class'] = $class;
102  }
103  }
104 
105  echo CHtml::openTag('td', $options);
106  echo CHtml::openTag('span', array('class' => $this->cssClass, 'data-rowid' => $this->getPrimaryKey($data)));
107  $this->renderDataCellContent($row, $data);
108  echo CHtml::closeTag('span');
109  echo CHtml::closeTag('td');
110  }
111 
120  protected function getPrimaryKey($data) {
121 
122  if ($this->grid->dataProvider instanceof CActiveDataProvider) {
123  $key = $this->grid->dataProvider->keyAttribute === null ? $data->getPrimaryKey()
124  : $data->{$this->grid->dataProvider->keyAttribute};
125  return is_array($key) ? implode('--', $key) : $key;
126  }
127 
128  if ($this->grid->dataProvider instanceof CArrayDataProvider || $this->grid->dataProvider instanceof CSqlDataProvider) {
129  return is_object($data) ? $data->{$this->grid->dataProvider->keyField}
130  : $data[$this->grid->dataProvider->keyField];
131  }
132 
133  return null;
134  }
135 
139  public function registerClientScript() {
140 
141  Booster::getBooster()->registerAssetCss('bootstrap-relational.css');
142 
144  $cs = Yii::app()->getClientScript();
145  if ($this->afterAjaxUpdate !== null) {
146  if ((!$this->afterAjaxUpdate instanceof CJavaScriptExpression)
147  && (strpos($this->afterAjaxUpdate,'js:') !== 0)
148  ) {
149  $this->afterAjaxUpdate = new CJavaScriptExpression($this->afterAjaxUpdate);
150  }
151  } else {
152  $this->afterAjaxUpdate = 'js:$.noop';
153  }
154 
155  $this->ajaxErrorMessage = CHtml::encode($this->ajaxErrorMessage);
156  $afterAjaxUpdate = CJavaScript::encode($this->afterAjaxUpdate);
157  $span = count($this->grid->columns);
158  $loadingPic = CHtml::image(Booster::getBooster()->getAssetsUrl() . '/img/loading.gif');
159  $cache = $this->cacheData ? 'true' : 'false';
160  $data = !empty($this->submitData) && is_array($this->submitData) ? $this->submitData : 'js:{}';
161  $data = CJavascript::encode($data);
162  list($parentId) = explode('_',$this->id);
163  $js = <<<EOD
164 $(document).on('click','#{$parentId} .{$this->cssClass}', function(){
165  var span = $span;
166  var that = $(this);
167  var status = that.data('status');
168  var rowid = that.data('rowid');
169  var tr = $('#relatedinfo'+rowid);
170  var parent = that.parents('tr').eq(0);
171  var afterAjaxUpdate = {$afterAjaxUpdate};
172 
173  if (status && status=='on'){return}
174  that.data('status','on');
175 
176  if (tr.length && !tr.is(':visible') && {$cache})
177  {
178  tr.slideDown();
179  that.data('status','off');
180  return;
181  }else if (tr.length && tr.is(':visible'))
182  {
183  tr.slideUp();
184  that.data('status','off');
185  return;
186  }
187  if (tr.length)
188  {
189  tr.find('td').html('{$loadingPic}');
190  if (!tr.is(':visible')){
191  tr.slideDown();
192  }
193  }
194  else
195  {
196  var td = $('<td/>').html('{$loadingPic}').attr({'colspan':$span});
197  tr = $('<tr/>').prop({'id':'relatedinfo'+rowid}).append(td);
198  /* we need to maintain zebra styles :) */
199  var fake = $('<tr class="hide"/>').append($('<td/>').attr({'colspan':$span}));
200  parent.after(tr);
201  tr.after(fake);
202  }
203  var data = $.extend({$data}, {id:rowid});
204  $.ajax({
205  url: '{$this->url}',
206  data: data,
207  success: function(data){
208  tr.find('td').html(data);
209  that.data('status','off');
210  if ($.isFunction(afterAjaxUpdate))
211  {
212  afterAjaxUpdate(tr,rowid,data);
213  }
214  },
215  error: function()
216  {
217  tr.find('td').html('{$this->ajaxErrorMessage}');
218  that.data('status','off');
219  }
220  });
221 });
222 EOD;
223  $cs->registerScript(__CLASS__ . '#' . $this->id, $js);
224  }
225 }