-
Notifications
You must be signed in to change notification settings - Fork 7
/
ActionColumn.php
82 lines (75 loc) · 2.71 KB
/
ActionColumn.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
<?php
namespace mdm\widgets;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* Description of ActionColumn
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class ActionColumn extends \yii\grid\ActionColumn
{
public $iconTemplate = '<span class="glyphicon glyphicon-{icon}"></span>';
/**
* Initializes the default button rendering callbacks.
*/
protected function initDefaultButtons()
{
$buttons = [
'view' => [
'title' => Yii::t('yii', 'View'),
'aria-label' => Yii::t('yii', 'View'),
'data-pjax' => '0',
'icon' => 'eye-open',
],
'update' => [
'title' => Yii::t('yii', 'Update'),
'aria-label' => Yii::t('yii', 'Update'),
'data-pjax' => '0',
'icon' => 'pencil'
],
'delete' => [
'title' => Yii::t('yii', 'Delete'),
'aria-label' => Yii::t('yii', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
'data-pjax' => '0',
'icon' => 'trash'
]
];
foreach ($buttons as $name => $button) {
$this->buttons[$name] = array_merge($button, $this->buttonOptions, $this->buttons[$name]);
}
}
/**
* @inheritdoc
*/
protected function renderDataCellContent($model, $key, $index)
{
return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
$name = $matches[1];
if (isset($this->visibleButtons[$name])) {
$isVisible = $this->visibleButtons[$name] instanceof \Closure ? call_user_func($this->visibleButtons[$name], $model, $key, $index)
: $this->visibleButtons[$name];
} else {
$isVisible = true;
}
if ($isVisible) {
$button = isset($this->buttons[$name]) ? $this->buttons[$name] : [
'label' => ucfirst($name),
'aria-label' => ucfirst($name),
'data-pjax' => '0',
];
$url = $this->createUrl($name, $model, $key, $index);
if ($button instanceof \Closure) {
return call_user_func($button, $url, $model, $key);
} else {
$icon = str_replace('{icon}', ArrayHelper::remove($button, 'icon', ''), $this->iconTemplate);
return Html::a($icon, $url, $button);
}
}
return '';
}, $this->template);
}
}