HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbEditableColumn.php
Go to the documentation of this file.
1 <?php
11 Yii::import('booster.widgets.TbEditable');
12 Yii::import('booster.widgets.TbEditableField');
13 Yii::import('zii.widgets.grid.CDataColumn');
14 
20 class TbEditableColumn extends CDataColumn
21 {
26  public $editable = array();
27 
28  public function init()
29  {
30  if (!$this->name) {
31  throw new CException('You should provide name for TbEditableColumn');
32  }
33 
34  parent::init();
35 
36  //need to attach ajaxUpdate handler to refresh editables on pagination and sort
38  }
39 
40  //protected was removed due to https://github.com/vitalets/x-editable-yii/issues/63
41  public function renderDataCellContent($row, $data)
42  {
43  $isModel = $data instanceOf CModel;
44 
45  if($isModel) {
46  $widgetClass = 'TbEditableField';
47  $options = array(
48  'model' => $data,
49  'attribute' => empty($this->editable['attribute']) ? $this->name : $this->editable['attribute'],
50  );
51 
52  //if value defined in column config --> we should evaluate it
53  //and pass to widget via `text` option: set flag `passText` = true
54  $passText = !empty($this->value);
55  } else {
56  $widgetClass = 'TbEditable';
57  $options = array(
58  'pk' => $data[$this->grid->dataProvider->keyField],
59  'name' => empty($this->editable['name']) ? $this->name : $this->editable['name'],
60  );
61 
62  $passText = true;
63  //if autotext will be applied, do not pass `text` option (pass `value` instead)
64  if(empty($this->value) && TbEditable::isAutotext($this->editable, isset($this->editable['type']) ? $this->editable['type'] : '')) {
65  $options['value'] = $data[$this->name];
66  $passText = false;
67  }
68  }
69 
70  //for live update
71  $options['liveTarget'] = $this->grid->id;
72 
73  $options = CMap::mergeArray($this->editable, $options);
74 
75  //if value defined for column --> use it as element text
76  if($passText) {
77  ob_start();
79  $text = ob_get_clean();
80  $options['text'] = $text;
81  $options['encode'] = false;
82  }
83 
84  //apply may be a string expression, see https://github.com/vitalets/x-editable-yii/issues/33
85  if (isset($options['apply']) && is_string($options['apply'])) {
86  $options['apply'] = $this->evaluateExpression($options['apply'], array('data'=>$data, 'row'=>$row));
87  }
88 
89  //evaluate htmlOptions inside editable config as they can depend on $data
90  //see https://github.com/vitalets/x-editable-yii/issues/40
91  if (isset($options['htmlOptions']) && is_array($options['htmlOptions'])) {
92  foreach($options['htmlOptions'] as $k => $v) {
93  if(is_string($v) && (strpos($v, '$data') !== false || strpos($v, '$row') !== false)) {
94  $options['htmlOptions'][$k] = $this->evaluateExpression($v, array('data'=>$data, 'row'=>$row));
95  }
96  }
97  }
98 
99  $this->grid->controller->widget($widgetClass, $options);
100  }
101 
102  /*
103  Require this overwrite to show bootstrap sort icons
104  */
105  protected function renderHeaderCellContent()
106  {
107  /* TODO
108  if(yii::app()->editable->form != 'bootstrap') {
109  parent::renderHeaderCellContent();
110  return;
111  } */
112 
113  if ($this->grid->enableSorting && $this->sortable && $this->name !== null)
114  {
115  $sort = $this->grid->dataProvider->getSort();
116  $label = isset($this->header) ? $this->header : $sort->resolveLabel($this->name);
117 
118  if ($sort->resolveAttribute($this->name) !== false)
119  $label .= '<span class="caret"></span>';
120 
121  echo $sort->link($this->name, $label, array('class'=>'sort-link'));
122  }
123  else
124  {
125  if ($this->name !== null && $this->header === null)
126  {
127  if ($this->grid->dataProvider instanceof CActiveDataProvider)
128  echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
129  else
130  echo CHtml::encode($this->name);
131  }
132  else
134  }
135  }
136 
137  /*
138  Require this overwrite to show bootstrap filter field
139  */
140  public function renderFilterCell()
141  {
142  /* TODO
143  if(yii::app()->editable->form != 'bootstrap') {
144  parent::renderFilterCell();
145  return;
146  } */
147 
148  echo '<td><div class="filter-container">';
149  $this->renderFilterCellContent();
150  echo '</div></td>';
151  }
152 }