forked from nineinchnick/edatatables
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEButtonColumn.php
More file actions
193 lines (171 loc) · 6.39 KB
/
EButtonColumn.php
File metadata and controls
193 lines (171 loc) · 6.39 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
<?php
/**
* EButtonColumn class file.
*
* @license http://www.yiiframework.com/license/
*/
Yii::import('zii.widgets.grid.CButtonColumn');
/**
*
*/
class EButtonColumn extends CButtonColumn {
public $sortable = false;
/**
* @var string the label for the history button. Defaults to "history".
* Note that the label will not be HTML-encoded when rendering.
*/
public $historyButtonLabel;
/**
* @var string the image URL for the history button. If not set, an integrated image will be used.
* You may set this property to be false to render a text link instead.
*/
public $historyButtonImageUrl=false;
/**
* @var string a PHP expression that is evaluated for every history button and whose result is used
* as the URL for the history button. In this expression, the variable
* <code>$row</code> the row number (zero-based); <code>$data</code> the data model for the row;
* and <code>$this</code> the column object.
*/
public $historyButtonUrl='Yii::app()->controller->createUrl("history",array("id"=>$data->primaryKey))';
/**
* @var array the HTML options for the history button tag.
*/
public $historyButtonOptions=array('class'=>'history');
public $viewButtonIcon = 'eye-open';
public $updateButtonIcon = 'pencil';
public $deleteButtonIcon = 'trash';
public $historyButtonIcon = 'calendar';
/**
* @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>";
}
/**
* Initializes the default buttons (view, update and delete).
*/
protected function initDefaultButtons()
{
if (!$this->grid->bootstrap) {
$this->viewButtonIcon = 'search';
}
if($this->deleteConfirmation===null)
$this->deleteConfirmation=Yii::t('zii','Are you sure you want to delete this item?');
if(!isset($this->buttons['delete']['click']))
{
if(is_string($this->deleteConfirmation))
$confirmation="if(!confirm(".CJavaScript::encode($this->deleteConfirmation).")) return false;";
else
$confirmation='';
if(Yii::app()->request->enableCsrfValidation)
{
$csrfTokenName = Yii::app()->request->csrfTokenName;
$csrfToken = Yii::app()->request->csrfToken;
$csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
}
else
$csrf = '';
if($this->afterDelete===null)
$this->afterDelete='function(){}';
$this->buttons['delete']['click']=<<<JavaScript
function() {
$confirmation
var th = this,
afterDelete = $this->afterDelete;
$.getJSON($(this).attr('href'), function(data){
jQuery('#{$this->grid->id}').eDataTables('refresh');
afterDelete(th, true, data);
});
return false;
}
JavaScript;
}
parent::initDefaultButtons();
// history button
if($this->historyButtonLabel===null)
$this->historyButtonLabel=Yii::t('EDataTables.edt','History');
if($this->historyButtonImageUrl===null)
$this->historyButtonImageUrl=$this->grid->baseScriptUrl.'/history.png';
$button=array(
'label'=>$this->historyButtonLabel,
'url'=>$this->historyButtonUrl,
'imageUrl'=>$this->historyButtonImageUrl,
'options'=>$this->historyButtonOptions,
);
if(isset($this->buttons['history']))
$this->buttons['history']=array_merge($button,$this->buttons['history']);
else
$this->buttons['history']=$button;
if ($this->viewButtonIcon !== false && !isset($this->buttons['view']['icon']))
$this->buttons['view']['icon'] = $this->viewButtonIcon;
if ($this->updateButtonIcon !== false && !isset($this->buttons['update']['icon']))
$this->buttons['update']['icon'] = $this->updateButtonIcon;
if ($this->deleteButtonIcon !== false && !isset($this->buttons['delete']['icon']))
$this->buttons['delete']['icon'] = $this->deleteButtonIcon;
if ($this->historyButtonIcon !== false && !isset($this->buttons['history']['icon']))
$this->buttons['history']['icon'] = $this->historyButtonIcon;
}
/**
* Renders a link button.
* @param string $id the ID of the button
* @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.
* @param integer $row the row number (zero-based)
* @param mixed $data the data object associated with the row
*/
protected function renderButton($id, $button, $row, $data)
{
if (isset($button['visible']) && !$this->evaluateExpression($button['visible'], array('row'=>$row, 'data'=>$data)))
return;
$label = isset($button['label']) ? $button['label'] : $id;
$url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data'=>$data, 'row'=>$row)) : '#';
$options = isset($button['options']) ? $button['options'] : array();
if (!isset($options['title']))
$options['title'] = $label;
if ($this->grid->bootstrap) {
if (!isset($options['rel']))
$options['rel'] = 'tooltip';
}
if (isset($button['icon']))
{
if ($this->grid->bootstrap) {
if (strpos($button['icon'], 'icon') === false)
$button['icon'] = 'icon-'.implode(' icon-', explode(' ', $button['icon']));
echo CHtml::link('<i class="'.$button['icon'].'"></i>', $url, $options);
} else {
if (strpos($button['icon'], 'icon') === false)
$button['icon'] = 'ui-icon-'.implode(' ui-icon-', explode(' ', $button['icon']));
if (!isset($options['class']))
$options['class']='';
$options['class'] .=' view left ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary ui-button-icon-primary ui-icon '.$button['icon'];
if (isset($button['imageUrl']) && is_string($button['imageUrl']))
echo CHtml::link(CHtml::image($button['imageUrl'], $label), $url, $options);
else
echo CHtml::link($label, $url, $options);
}
} else if (isset($button['imageUrl']) && is_string($button['imageUrl'])) {
echo CHtml::link(CHtml::image($button['imageUrl'], $label), $url, $options);
} else {
echo CHtml::link($label, $url, $options);
}
}
public function getDataCellContent($row,$data) {
ob_start();
$this->renderDataCellContent($row,$data);
return ob_get_clean();
}
}