forked from nineinchnick/edatatables
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEEditableColumn.php
More file actions
208 lines (194 loc) · 6.58 KB
/
EEditableColumn.php
File metadata and controls
208 lines (194 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* EEditableColumn class file.
*
* @license http://www.yiiframework.com/license/
*/
/**
* @todo handle null values
*/
class EEditableColumn extends EDataColumn {
/**
* @var boolean
*/
public $sortable = false;
/**
* @var mixed boolean or string, which is evaluated as an expression which result must be boolean
*/
public $enabled = true;
/**
* @var string If not null, it's evaluated as an expression and result is used instead of type property.
*/
public $dynamicType;
/**
* @var boolean If true, column header will contain a dropdown menu.
* Its contents should be rendered using JavaScript.
*/
public $dropdown;
/**
* Renders the header cell.
*/
public function renderHeaderCell()
{
$this->headerHtmlOptions['id']=$this->id;
echo CHtml::openTag('th',$this->headerHtmlOptions);
if ($this->dropdown) {
echo '<div class="btn-group">
<a class="btn btn-mini dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
<ul class="dropdown-menu"></ul>
</div> ';
}
$this->renderHeaderCellContent();
echo "</th>";
}
/**
* Renders a data cell.
* @param integer $row the row number (zero-based)
*/
public function renderDataCell($row)
{
$data=$this->grid->dataProvider->data[$row];
$options=$this->htmlOptions;
if($this->cssClassExpression!==null)
{
$class=$this->evaluateExpression($this->cssClassExpression,array('row'=>$row,'data'=>$data));
if(isset($options['class']))
$options['class'].=' '.$class;
else
$options['class']=$class;
}
if (isset($options['onclick'])) {
unset($options['onclick']);
}
echo CHtml::openTag('td',$options);
$this->renderDataCellContent($row,$data);
echo '</td>';
}
protected function renderDataCellContent($row,$data)
{
if (is_string($this->visible))
$visible=$this->evaluateExpression($this->visible,array('data'=>$data,'row'=>$row));
else
$visible=$this->visible;
if (!$visible)
return;
if (is_string($this->enabled))
$enabled=$this->evaluateExpression($this->enabled,array('data'=>$data,'row'=>$row));
else
$enabled=$this->enabled;
if (is_string($this->dynamicType))
$this->type=$this->evaluateExpression($this->dynamicType,array('data'=>$data,'row'=>$row));
if (substr($this->type,0,8) === 'readonly') {
$this->type = lcfirst(substr($this->type,8));
echo parent::renderDataCellContent($row,$data);
return;
}
if($this->value!==null)
$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
else if($this->name!==null)
$value=CHtml::value($data,$this->name);
// not using $this->grid->nullDisplay here because it's a by default
$unformattedValue = $value;
$value = $value===null ? '' : $this->grid->getFormatter()->format($value,$this->type);
//! @todo check type, set different controls or attributes for input tag
//! @todo use a different formatter? ex. an image type
/**
* @todo in views we use pre-generated controls, the dev can choose
* from different ones - here we generate them
* on the fly - is this the best way?
* We COULD provide separate view for every attribute, but
* wouldn't it be an overkill?
*/
$id = str_replace('.','_',$this->name).'_row'.$row;
$options = $this->htmlOptions;
if (!$enabled)
$options['disabled']='disabled';
$options['class'] = empty($options['class']) ? 'editable' : $options['class'].' editable';
switch($this->type) {
default:
case 'readonly':
echo parent::renderDataCellContent($row,$data);
break;
case 'text':
echo CHtml::tag('input', array_merge(array('type'=>'text','id'=>$id,'value'=>$value), $options));
break;
case 'password':
echo CHtml::tag('input', array_merge(array('type'=>'password','id'=>$id), $options));
break;
case 'integer':
case 'double':
case 'dec2':
case 'dec3':
case 'dec5':
if ($this->grid->bootstrap) {
$options['class'].= ' span1';
}
echo CHtml::tag('input', array_merge(array('type'=>'text','id'=>$id,'value'=>$value,'size'=>6,'maxlength'=>9), $options));
break;
case 'flags':
case 'set':
case 'object':
$relations = $data->relations();
if (($pos=strpos($this->name,'.'))!==false) {
$pivotRelationName = substr($this->name,0,$pos); // withShops
$pivotRelation = $relations[$pivotRelationName];
$model = $data->$pivotRelationName;
$staticModel = NetActiveRecord::model($pivotRelation[1]);
// change id and name properties to fk column instead of relation name
$id_parts = explode('.', $this->name);
$relationName = array_pop($id_parts);
$pivotRelations = $staticModel->relations();
$relation = $pivotRelations[$relationName];
$id = implode('_',$id_parts).'_'.$relation[2].'_row'.$row;
$name = $relation[2];
} else {
$relation = $relations[$this->name];
$model = $data;
$name = $this->name;
}
$options['data-placeholder'] = Yii::t('EDataTables.edt','Choose').' '.NetActiveRecord::model($relation[1])->label(2).'...';
$controller = lcfirst($relation[1]);
echo $this->grid->owner->widget('ext.select2.ESelect2', array(
'options' => Select2Helper::filterDefaults(CHtml::normalizeUrl('/'.$controller.'/autocomplete'), '', true, array('width'=>'15em')),
'htmlOptions' => array_merge(array(
'id'=>$id,
'name'=>$name,
), $options),
'value'=> $model !== null ?
(isset($relationName) ?
($model->{$relationName} !== null ? $model->{$relationName}->getPrimaryKey() : null)
: ($model->{$this->name} !== null ? $model->{$this->name}->getPrimaryKey() : null))
: null,
),true);
break;
case 'array':
throw new Exception('Editable column not implemented for type '.$this->type.' ('.var_export($value,true).')');
break;
case 'boolean':
echo CHtml::tag('input', array_merge(array('type'=>'checkbox','id'=>$id,'value'=>1), $unformattedValue ? array('checked'=>'checked'):array(), $options));
//echo '<input type="hidden" value="1"/>';
break;
case 'date':
case 'time':
case 'datetime':
$options['class'].=' '.$this->grid->id.'_datepickers';
if ($this->grid->bootstrap) {
$options['class'].= ' span2';
}
echo $this->grid->owner->widget('ext.TbDatepicker.TbDatepicker', array(
'htmlOptions' => array_merge(array(
'id'=>$id,
'name'=>$this->name,
'value'=>$value,
'size'=>8,
'maxlength'=>17,
'style' => 'width: 6em;',
), $options),
), true);
break;
case 'image':
throw new Exception('Editable column not implemented for type '.$this->type.' ('.var_export($value,true).')');
break;
}
}
}