-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEditableButtonColumn.php
201 lines (173 loc) · 6.15 KB
/
EditableButtonColumn.php
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
<?php
/**
* EditableButtonColumn class file.
*
* @author Dyomin Dmitry <[email protected]>
* @link http://size.perm.ru/yii-editable-grid
* @copyright 2014 SiZE
*/
class EditableButtonColumn extends CButtonColumn {
/**
* @var string The label for the remove row button. Defaults to "Delete".
* Note that the label will not be HTML-encoded when rendering.
*/
public $removeRowButtonLabel;
/**
* @var string The image URL for the remove row 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 $removeRowButtonImageUrl;
/**
* @var string a PHP expression that is evaluated for every remove row button and whose result is used
* as the URL for the remove row button. In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the column object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*/
public $removeRowButtonUrl='"#"';
/**
* @var array the HTML options for the remove row button tag.
*/
public $removeRowButtonOptions = array( 'class' => 'removeRow' );
/**
* @var string the confirmation message to be displayed when removeRow button is clicked.
* By setting this property to be false, no confirmation message will be displayed.
* This property is used only if <code>$this->buttons['removeRow']['click']</code> is not set.
*/
public $removeRowConfirmation;
/**
* @var string a javascript function that will be invoked after the removing row.
* This property is used only if <code>$this->buttons['removeRow']['click']</code> is not set.
*/
public $afterRemoveRow;
/**
* @var string The label for the restore row button. Defaults to "Restore".
* Note that the label will not be HTML-encoded when rendering.
*/
public $restoreRowButtonLabel;
/**
* @var string The image URL for the restore row 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 $restoreRowButtonImageUrl;
/**
* @var array the HTML options for the restore row button tag.
*/
public $restoreRowButtonOptions = array( 'class' => 'restoreRow' );
/**
* @var string a javascript function that will be invoked after the restoring row.
*/
public $afterRestoreRow;
/**
* @var string the template that is used to render the content in each data cell.
* (@see CButtonColumn.template)
*/
public $template = '{removeRow}';
/**
* Initializes the default buttons (removeRow).
*/
protected function initDefaultButtons(){
parent::initDefaultButtons();
if ( $this->removeRowButtonLabel === null ) {
$this->removeRowButtonLabel = Yii::t( 'zii', 'Delete' );
}
if ( $this->removeRowConfirmation === null ) {
$this->removeRowConfirmation = Yii::t( 'zii', 'Are you sure you want to delete this item?' );
}
if ( $this->grid->restoreDeletedRows ) {
if ( $this->restoreRowButtonLabel === null ) {
$this->restoreRowButtonLabel = Yii::t( 'editablegrid.editablegrid', 'Restore' );
}
}
$id = 'removeRow';
$button = array(
'label' => $this->{$id.'ButtonLabel'},
'url' => $this->{$id.'ButtonUrl'},
'imageUrl' => $this->{$id.'ButtonImageUrl'},
'options' => $this->{$id.'ButtonOptions'},
);
if ( isset( $this->buttons[ $id ] ) ) {
$this->buttons[ $id ] = array_merge( $button, $this->buttons[ $id ] );
} else {
$this->buttons[ $id ] = $button;
}
if ( !isset( $this->buttons[ $id ][ 'click' ] ) ) {
if ( is_string( $this->{$id.'Confirmation'} ) ) {
$confirmation = "if(!confirm(".CJavaScript::encode( $this->{$id.'Confirmation'} ).")) 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->afterRemoveRow === null ) {
$this->afterRemoveRow = 'function(){}';
}
// Allow to restore deleted rows
if ( $this->grid->restoreDeletedRows ) {
if ( $this->afterRestoreRow === null ) {
$this->afterRestoreRow = 'function(){}';
}
if ( !empty( $this->restoreRowButtonImageUrl ) ) {
$restore_link = CHtml::link( CHtml::image( $this->restoreRowButtonImageUrl, $this->restoreRowButtonLabel ), '#', $this->removeRowButtonOptions );
} else {
$restore_link = CHtml::link( $this->restoreRowButtonLabel, '#', $this->restoreRowButtonOptions );
}
$label = isset( $button[ 'label' ] ) ? $button[ 'label' ] : $id;
$columns = sizeof( $this->grid->columns );
$this->buttons[ $id ][ 'click' ] = <<<EOD
function(){
$confirmation
var th = this,
afterDelete = {$this->afterRemoveRow},
tr = $(this).parents("tr");
tr.find('input,select').each(function(){
$(this).prop('disabled',true);
});
tr.after('<tr><td colspan="{$columns}">{$restore_link}</td></tr>');
tr.hide();
afterDelete(th);
return false;
}
EOD;
Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id.'-restoreRow',"
jQuery(document).on('click','#{$this->grid->id} a.restoreRow',function(){
var th = this,
tr = $(this).parents('tr'),
real_tr = tr.prev('tr'),
afterRestore = {$this->afterRestoreRow};
tr.remove();
real_tr.show();
real_tr.find('input,select').each(function(){
$(this).prop('disabled',false);
});
afterRestore(th);
});
");
} else {
$this->buttons[ $id ][ 'click' ] = <<<EOD
function(){
$confirmation
var th = this,
afterDelete = {$this->afterRemoveRow};
$(this).parents("tr").remove();
afterDelete(th);
return false;
}
EOD;
}
}
}
}