|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* HiPanel core package |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://hipanel.com/ |
|
7
|
|
|
* @package hipanel-core |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hipanel\grid; |
|
13
|
|
|
|
|
14
|
|
|
use hiqdev\higrid\FeaturedColumnTrait; |
|
15
|
|
|
use Closure; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
use yii\helpers\Html; |
|
18
|
|
|
|
|
19
|
|
|
class ActionColumn extends \yii\grid\ActionColumn |
|
20
|
|
|
{ |
|
21
|
|
|
use FeaturedColumnTrait { |
|
22
|
|
|
init as traitInit; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public $attribute; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var |
|
29
|
|
|
*/ |
|
30
|
|
|
public $buttonOptions = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var integer count of visible buttons that will be shown without spoiler |
|
34
|
|
|
*/ |
|
35
|
|
|
public $visibleButtonsCount = 1; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function init() |
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->header === null) { |
|
43
|
|
|
$this->header = Yii::t('hipanel', 'Actions'); |
|
44
|
|
|
} |
|
45
|
|
|
$this->traitInit(); |
|
46
|
|
|
$this->registerBtnGroupDirectionFix(); |
|
47
|
|
|
$this->getCountButtons(); |
|
48
|
|
|
$this->template = ($this->getCountButtons() > $this->visibleButtonsCount) ? '<div class="btn-group btn-group-fix">' . $this->template . '</ul></div>' : '<div class="btn-group btn-group-fix">' . $this->template . '</div>'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getCountButtons() |
|
52
|
|
|
{ |
|
53
|
|
|
return preg_match_all('/\\{([\w\-\/]+)\\}/', $this->template); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function registerBtnGroupDirectionFix() |
|
57
|
|
|
{ |
|
58
|
|
|
Yii::$app->view->registerJs(' |
|
59
|
|
|
$(function () { |
|
60
|
|
|
var $gridRows = $( ".btn-group.btn-group-fix" ); |
|
61
|
|
|
if ($gridRows.length > 2) { |
|
62
|
|
|
$gridRows.slice(-2).addClass("dropup"); |
|
63
|
|
|
} else { |
|
64
|
|
|
$gridRows.parents("table").eq(0).css({"height": "150pt"}); |
|
65
|
|
|
} |
|
66
|
|
|
}); |
|
67
|
|
|
'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function renderFirstButton($item, $index) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
return ($this->getCountButtons() > $this->visibleButtonsCount) ? $item . '<button type="button" class="btn btn-default dropdown-toggle btn-xs" data-toggle="dropdown" aria-expanded="false"> |
|
73
|
|
|
<span class="caret"></span> |
|
74
|
|
|
<span class="sr-only">Toggle Dropdown</span> |
|
75
|
|
|
</button> |
|
76
|
|
|
<ul class="dropdown-menu pull-right" role="menu">' : $item; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function renderOtherButton($item) |
|
80
|
|
|
{ |
|
81
|
|
|
return '<li>' . $item . '</li>'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Initializes the default button rendering callbacks. |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function initDefaultButtons() |
|
88
|
|
|
{ |
|
89
|
|
View Code Duplication |
if (!isset($this->buttons['view'])) { |
|
|
|
|
|
|
90
|
|
|
$this->buttons['view'] = function ($url, $model, $key) { |
|
|
|
|
|
|
91
|
|
|
$options = array_merge([ |
|
92
|
|
|
'title' => Yii::t('app', 'Details'), |
|
93
|
|
|
'aria-label' => Yii::t('app', 'Details'), |
|
94
|
|
|
'data-pjax' => '0', |
|
95
|
|
|
'class' => 'btn btn-default btn-xs', |
|
96
|
|
|
], $this->buttonOptions); |
|
97
|
|
|
|
|
98
|
|
|
return Html::a('<i class="fa fa-bars"></i> ' . Yii::t('app', 'Details'), $url, $options); |
|
99
|
|
|
}; |
|
100
|
|
|
} |
|
101
|
|
View Code Duplication |
if (!isset($this->buttons['update'])) { |
|
|
|
|
|
|
102
|
|
|
$this->buttons['update'] = function ($url, $model, $key) { |
|
|
|
|
|
|
103
|
|
|
$options = array_merge([ |
|
104
|
|
|
'title' => Yii::t('app', 'Update'), |
|
105
|
|
|
'aria-label' => Yii::t('app', 'Update'), |
|
106
|
|
|
'data-pjax' => '0', |
|
107
|
|
|
], $this->buttonOptions); |
|
108
|
|
|
|
|
109
|
|
|
return Html::a('<i class="fa fa-pencil"></i> ' . Yii::t('app', 'Update'), $url, $options); |
|
110
|
|
|
}; |
|
111
|
|
|
} |
|
112
|
|
|
if (!isset($this->buttons['delete'])) { |
|
113
|
|
|
$this->buttons['delete'] = function ($url, $model, $key) { |
|
|
|
|
|
|
114
|
|
|
$options = array_merge([ |
|
115
|
|
|
'title' => Yii::t('app', 'Delete'), |
|
116
|
|
|
'aria-label' => Yii::t('app', 'Delete'), |
|
117
|
|
|
'data' => [ |
|
118
|
|
|
'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), |
|
119
|
|
|
'method' => 'POST', |
|
120
|
|
|
'data-pjax' => '0', |
|
121
|
|
|
], |
|
122
|
|
|
|
|
123
|
|
|
], $this->buttonOptions); |
|
124
|
|
|
|
|
125
|
|
|
return Html::a('<i class="fa fa-trash-o"></i> ' . Yii::t('app', 'Delete'), $url, $options); |
|
126
|
|
|
}; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function renderDataCellContent($model, $key, $index) |
|
134
|
|
|
{ |
|
135
|
|
|
return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) { |
|
136
|
|
|
static $renderedCount = 0; |
|
137
|
|
|
$name = $matches[1]; |
|
138
|
|
|
|
|
139
|
|
|
if (isset($this->visibleButtons[$name])) { |
|
140
|
|
|
$isVisible = $this->visibleButtons[$name] instanceof Closure |
|
141
|
|
|
? call_user_func($this->visibleButtons[$name], $model, $key, $index) |
|
142
|
|
|
: $this->visibleButtons[$name]; |
|
143
|
|
|
} else { |
|
144
|
|
|
$isVisible = true; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($isVisible && isset($this->buttons[$name])) { |
|
148
|
|
|
$url = $this->createUrl($name, $model, $key, $index); |
|
149
|
|
|
$renderedItem = call_user_func($this->buttons[$name], $url, $model, $key); |
|
150
|
|
|
$result = ($renderedCount < $this->visibleButtonsCount) ? $this->renderFirstButton($renderedItem, $index) : $this->renderOtherButton($renderedItem); |
|
151
|
|
|
++$renderedCount; |
|
152
|
|
|
|
|
153
|
|
|
return $result; |
|
154
|
|
|
} else { |
|
155
|
|
|
return ''; |
|
156
|
|
|
} |
|
157
|
|
|
}, $this->template); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.