1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 Yii::import('yiiwheels.widgets.editable.WhEditableField');
19 Yii::import('zii.widgets.grid.CDataColumn');
20
21 class WhEditableColumn extends CDataColumn
22 {
23 24 25 26
27 public $editable = array();
28
29 public function init()
30 {
31 if (!$this->name) {
32 throw new CException('You should provide name for EditableColumn');
33 }
34
35 parent::init();
36
37
38 WhEditable::attachAjaxUpdateEvent($this->grid);
39 }
40
41 42 43 44 45
46 protected function renderDataCellContent($row, $data)
47 {
48 $isModel = $data instanceOf CModel;
49
50 if ($isModel) {
51 $widgetClass = 'EditableField';
52 $options = array(
53 'model' => $data,
54 'attribute' => empty($this->editable['attribute']) ? $this->name : $this->editable['attribute'],
55 );
56
57
58
59 $passText = !empty($this->value);
60 } else {
61 $widgetClass = 'WhEditable';
62 $options = array(
63 'pk' => $data[$this->grid->dataProvider->keyField],
64 'name' => empty($this->editable['name']) ? $this->name : $this->editable['name'],
65 );
66
67 $passText = true;
68
69 if (empty($this->value) && WhEditable::isAutotext($this->editable, isset($this->editable['type']) ? $this->editable['type'] : '')) {
70 $options['value'] = $data[$this->name];
71 $passText = false;
72 }
73 }
74
75
76 $options['liveTarget'] = $this->grid->id;
77
78 $options = CMap::mergeArray($this->editable, $options);
79
80
81 if ($passText) {
82 ob_start();
83 parent::renderDataCellContent($row, $data);
84 $text = ob_get_clean();
85 $options['text'] = $text;
86 $options['encode'] = false;
87 }
88
89
90 if (isset($options['apply']) && is_string($options['apply'])) {
91 $options['apply'] = $this->evaluateExpression($options['apply'], array('data' => $data, 'row' => $row));
92 }
93
94
95
96 if (isset($options['htmlOptions']) && is_array($options['htmlOptions'])) {
97 foreach ($options['htmlOptions'] as $k => $v) {
98 if (is_string($v) && (strpos($v, '$data') !== false || strpos($v, '$row') !== false)) {
99 $options['htmlOptions'][$k] = $this->evaluateExpression($v, array('data' => $data, 'row' => $row));
100 }
101 }
102 }
103
104 $this->grid->controller->widget($widgetClass, $options);
105 }
106
107 108 109
110 protected function ()
111 {
112 if (yii::app()->editable->form != EditableConfig::FORM_BOOTSTRAP) {
113 parent::renderHeaderCellContent();
114 return;
115 }
116
117 if ($this->grid->enableSorting && $this->sortable && $this->name !== null) {
118 $sort = $this->grid->dataProvider->getSort();
119 $label = isset($this->header) ? $this->header : $sort->resolveLabel($this->name);
120
121 if ($sort->resolveAttribute($this->name) !== false)
122 $label .= '<span class="caret"></span>';
123
124 echo $sort->link($this->name, $label, array('class' => 'sort-link'));
125 } else {
126 if ($this->name !== null && $this->header === null) {
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 } else
132 parent::renderHeaderCellContent();
133 }
134 }
135
136 137 138
139 public function renderFilterCell()
140 {
141 if (yii::app()->editable->form != EditableConfig::FORM_BOOTSTRAP) {
142 parent::renderFilterCell();
143 return;
144 }
145
146 echo '<td><div class="filter-container">';
147 $this->renderFilterCellContent();
148 echo '</div></td>';
149 }
150 }
151