forked from pgyf/yii2-contextmenu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SerialColumnTrait.php
263 lines (225 loc) · 9.27 KB
/
SerialColumnTrait.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace liyunfang\contextmenu;
use Yii;
use Closure;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
/**
* Description of SerialColumnTrait
*
* @author liyunfang <381296986@qq.com>
* @date 2015-08-27
*/
trait SerialColumnTrait {
/**
* Use contextMenu
*/
public $contextMenu = true;
private $_isContextMenu = false;
private $_contextMenuId = '';
/**
* The prefix of contextMenu ID
*/
public $contextMenuPrefix = "context-menu";
/**
* contextMenu ID is generated dynamically by the property
*/
public $contextMenuAttribute = false;
//if extends \kartik\grid\ActionColumn,
public $viewOptions = [ 'data' => ['pjax' => '0' , 'toggle' => 'tooltip']];
public $updateOptions = [ 'data' => ['pjax' => '0' , 'toggle' => 'tooltip']];
public $deleteOptions = [ 'data' => ['pjax' => '0' , 'toggle' => 'tooltip' , 'method' => 'post' ]];
public function init() {
$this->_isContextMenu = ($this->grid->bootstrap && $this->contextMenu);
parent::init();
if($this->_isContextMenu){
$this->initDefaultButtons();
$this->registerAssets();
}
}
protected function initDefaultButtons() {
if(!$this->_isContextMenu){
parent::initDefaultButtons();
}
else{
if (!isset($this->buttons['view'])) {
$this->buttons['view'] = function ($url, $model) {
$options = $this->viewOptions;
$title = Yii::t('yii', 'View');
$icon = '<span class="glyphicon glyphicon-eye-open"></span>';
$label = ArrayHelper::remove($options, 'label', $icon . ' ' . $title );
$options = ArrayHelper::merge(['title' => $title], $options);
$options['tabindex'] = '-1';
return '<li>' . Html::a($label, $url, $options) . '</li>' . PHP_EOL;
};
}
if (!isset($this->buttons['update'])) {
$this->buttons['update'] = function ($url, $model) {
$options = $this->updateOptions;
$title = Yii::t('yii', 'Update');
$icon = '<span class="glyphicon glyphicon-pencil"></span>';
$label = ArrayHelper::remove($options, 'label', $icon . ' ' . $title);
$options = ArrayHelper::merge(['title' => $title], $options);
$options['tabindex'] = '-1';
return '<li>' . Html::a($label, $url, $options) . '</li>' . PHP_EOL;
};
}
if (!isset($this->buttons['delete'])) {
$this->buttons['delete'] = function ($url, $model) {
$options = $this->deleteOptions;
$title = Yii::t('yii', 'Delete');
$icon = '<span class="glyphicon glyphicon-trash"></span>';
$label = ArrayHelper::remove($options, 'label', $icon . ' ' . $title);
$options = ArrayHelper::merge([ 'title' => $title, 'data' => ['confirm' => Yii::t('yii', 'Are you sure you want to delete this item?') ] ],$options);
$options['tabindex'] = '-1';
return '<li>' . Html::a($label, $url, $options) . '</li>' . PHP_EOL;
};
}
}
}
protected function renderDataCellContent($model, $key, $index) {
$pageNumContent = parent::renderDataCellContent($model, $key, $index);
if(!$this->_isContextMenu){
return $pageNumContent;
}
else{
$content = $this->actionRenderDataCellContent($model, $key, $index);
$contextMenuId = '';
if($this->contextMenuPrefix){
$contextMenuId = $this->contextMenuPrefix.'-';
}
if(!$this->contextMenuAttribute){
$contextMenuId .= $this->grid->getId().'-'.$index;
}
else{
$contextMenuId .= $model->{$this->contextMenuAttribute};
}
$this->_contextMenuId = $contextMenuId;
$dropdown = Html::tag('ul', $content, ['class' => 'dropdown-menu' , 'role' => 'menu']);
return $pageNumContent.PHP_EOL . Html::tag('div', $dropdown, [ 'id' => $contextMenuId , 'style' => 'display:block;' ]);
}
}
/**
* @inheritdoc
*/
public function renderDataCell($model, $key, $index)
{
if(!$this->_isContextMenu){
return parent::renderDataCell($model, $key, $index);
}
else{
if ($this->contentOptions instanceof \Closure) {
$options = call_user_func($this->contentOptions, $model, $key, $index, $this);
} else {
$options = $this->contentOptions;
}
return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
}
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$view = $this->grid->getView();
ContextmenuAsset::register($view);
}
public function getContextMenuId(){
return $this->_contextMenuId;
}
/**
* @var string the ID of the controller that should handle the actions specified here.
* If not set, it will use the currently active controller. This property is mainly used by
* [[urlCreator]] to create URLs for different actions. The value of this property will be prefixed
* to each action name to form the route of the action.
*/
public $controller;
/**
* @var string the template used for composing each cell in the action column.
* Tokens enclosed within curly brackets are treated as controller action IDs (also called *button names*
* in the context of action column). They will be replaced by the corresponding button rendering callbacks
* specified in [[buttons]]. For example, the token `{view}` will be replaced by the result of
* the callback `buttons['view']`. If a callback cannot be found, the token will be replaced with an empty string.
*
* As an example, to only have the view, and update button you can add the ActionColumn to your GridView columns as follows:
*
* ```
* ['class' => 'yii\grid\ActionColumn', 'template' => '{view} {update}'],
* ```
*
* @see buttons
*/
public $template = '{view} {update} {delete}';
/**
* @var array button rendering callbacks. The array keys are the button names (without curly brackets),
* and the values are the corresponding button rendering callbacks. The callbacks should use the following
* signature:
*
* ```php
* function ($url, $model, $key) {
* // return the button HTML code
* }
* ```
*
* where `$url` is the URL that the column creates for the button, `$model` is the model object
* being rendered for the current row, and `$key` is the key of the model in the data provider array.
*
* You can add further conditions to the button, for example only display it, when the model is
* editable (here assuming you have a status field that indicates that):
*
* ```php
* [
* 'update' => function ($url, $model, $key) {
* return $model->status === 'editable' ? Html::a('Update', $url) : '';
* },
* ],
* ```
*/
public $buttons = [];
/**
* @var callable a callback that creates a button URL using the specified model information.
* The signature of the callback should be the same as that of [[createUrl()]].
* If this property is not set, button URLs will be created using [[createUrl()]].
*/
public $urlCreator;
/**
* @var array html options to be applied to the [[initDefaultButtons()|default buttons]].
* @since 2.0.4
*/
public $buttonOptions = [];
/**
* Creates a URL for the given action and model.
* This method is called for each button and each row.
* @param string $action the button name (or action ID)
* @param \yii\db\ActiveRecord $model the data model
* @param mixed $key the key associated with the data model
* @param integer $index the current row index
* @return string the created URL
*/
public function createUrl($action, $model, $key, $index)
{
if ($this->urlCreator instanceof Closure) {
return call_user_func($this->urlCreator, $action, $model, $key, $index);
} else {
$params = is_array($key) ? $key : ['id' => (string) $key];
$params[0] = $this->controller ? $this->controller . '/' . $action : $action;
return Url::toRoute($params);
}
}
/**
* @inheritdoc
*/
protected function actionRenderDataCellContent($model, $key, $index)
{
return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
$name = $matches[1];
if (isset($this->buttons[$name])) {
$url = $this->createUrl($name, $model, $key, $index);
return call_user_func($this->buttons[$name], $url, $model, $key);
} else {
return '';
}
}, $this->template);
}
}