-
Notifications
You must be signed in to change notification settings - Fork 1
/
ActionColumn.php
150 lines (140 loc) · 5.05 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
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
<?php
namespace carono\components;
use carono\yii2rbac\RoleManager;
use Yii;
use yii\helpers\Inflector;
class ActionColumn extends \yii\grid\ActionColumn
{
public $text;
public $checkUrlAccess = true;
public $visibleButton;
public function init()
{
parent::init();
$methods = get_class_methods($this);
preg_match_all('/button(\w+)/', join(" ", $methods), $m);
foreach ($m[1] as $button) {
$this->buttons[lcfirst($button)] = function ($url, $model, $key) use ($button) {
$result = call_user_func_array([$this, "button" . $button], [$url, $model, $key]);
if ($result instanceof ButtonColumn) {
return $result->asLink();
} else {
return $result;
}
};
}
}
public function buttonUpload($url, $model, $key)
{
$button = new ButtonColumn();
$button->icon = "glyphicon glyphicon-upload";
$button->title = "Загрузить";
$button->url = $url;
$button->options = $this->buttonOptions;
return $button;
}
protected function renderDataCellContent($model, $key, $index)
{
return preg_replace_callback(
'/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
$action = $matches[1];
$name = lcfirst(Inflector::camelize(strtr($matches[1], ['/' => '_', '\\' => '_'])));
if (isset($this->buttons[$name]) && $this->buttonIsVisible($name, $model, $key, $index)) {
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;
}
$url = $this->createUrl($action, $model, $key, $index);
if ($isVisible && (!$this->checkUrlAccess || RoleManager::checkAccessByUrl($url))) {
return call_user_func($this->buttons[$name], $url, $model, $key);
} else {
return '';
}
} else {
return '';
}
}, $this->template
);
}
public function buttonIsVisible($name, $model, $key, $index)
{
if ($this->visibleButton) {
return call_user_func($this->visibleButton, $name, $model, $key, $index);
} else {
return true;
}
}
public function createUrl($action, $model, $key, $index)
{
if (method_exists($model, 'getUrl') && ($url = $model->getUrl($action)) && !$this->urlCreator) {
return $url;
} else {
return parent::createUrl($action, $model, $key, $index);
}
}
protected static function registerModifyScript($id, $send, $revert, $ok = "glyphicon-ok",
$error = 'glyphicon-remove', $wait = "glyphicon-time")
{
$send = strpos($send, 'fa-') !== false ? "fa " . $send : "glyphicon " . $send;
$revert = strpos($revert, 'fa-') !== false ? "fa " . $revert : "glyphicon " . $revert;
$ok = strpos($ok, 'fa-') !== false ? "fa " . $ok : "glyphicon " . $ok;
$error = strpos($error, 'fa-') !== false ? "fa " . $error : "glyphicon " . $error;
$wait = strpos($wait, 'fa-') !== false ? "fa " . $wait : "glyphicon " . $wait;
$script
= <<<EOT
$('[ajax-link=$id]').on('click', function () {
var elem=$(this);
if (!elem.attr('revert-url')){
elem.attr('revert-url',elem.attr('href'));
}
$.ajax({
type: "POST",
url: $(this).attr('href'),
dataType: 'json',
async: true,
success: function (data) {
if (elem.attr('revert')){
elem.removeAttr('revert');
}else{
elem.attr('revert', 1);
}
if (elem.attr('revert')){
elem.find('.glyphicon, .fa').attr('class','').addClass('$revert');
elem.attr('href', elem.attr('revert-url'));
}else{
elem.find('.glyphicon, .fa').attr('class','').addClass('$send');
elem.attr('href', elem.attr('send-url'));
}
if (data['message']) {
$(elem).parent().tooltip({
title: data['message'],
placement: 'right',
trigger:'hover',
delay: {show: 500, hide: 500}
}).tooltip('show').on('hidden.bs.tooltip',function(){ $(this).tooltip('destroy') });
}
},
beforeSend: function (){
elem.find('.glyphicon, .fa').attr('class','').addClass('$wait');
},
error: function(jqXHR, textStatus, errorThrown){
elem.find('.glyphicon, .fa').attr('class','').addClass('$error');
if (jqXHR.responseJSON['message']) {
$(elem).parent().tooltip({
title:jqXHR.responseJSON['message'],
placement : 'right',
trigger:'hover',
delay: {show: 500, hide: 500}
}).tooltip('show').on('hidden.bs.tooltip',function(){ $(this).tooltip('destroy') });
}
}
});
return false;
})
EOT;
Yii::$app->getView()->registerJs($script);
}
}