1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Displayers; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
|
7
|
|
|
class Actions extends AbstractDisplayer |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $appends = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $prepends = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Default actions. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $actions = ['view', 'edit', 'delete']; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $resource; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Append a action. |
33
|
|
|
* |
34
|
|
|
* @param $action |
35
|
|
|
* |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function append($action) |
39
|
|
|
{ |
40
|
|
|
array_push($this->appends, $action); |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Prepend a action. |
47
|
|
|
* |
48
|
|
|
* @param $action |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function prepend($action) |
53
|
|
|
{ |
54
|
|
|
array_unshift($this->prepends, $action); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Disable view action. |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function disableView(bool $disable = true) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if ($disable) { |
67
|
|
|
array_delete($this->actions, 'view'); |
68
|
|
|
} elseif (!in_array('view', $this->actions)) { |
69
|
|
|
array_push($this->actions, 'view'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Disable delete. |
77
|
|
|
* |
78
|
|
|
* @return $this. |
|
|
|
|
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function disableDelete(bool $disable = true) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
if ($disable) { |
83
|
|
|
array_delete($this->actions, 'delete'); |
84
|
|
|
} elseif (!in_array('delete', $this->actions)) { |
85
|
|
|
array_push($this->actions, 'delete'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Disable edit. |
93
|
|
|
* |
94
|
|
|
* @return $this. |
|
|
|
|
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function disableEdit(bool $disable = true) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if ($disable) { |
99
|
|
|
array_delete($this->actions, 'edit'); |
100
|
|
|
} elseif (!in_array('edit', $this->actions)) { |
101
|
|
|
array_push($this->actions, 'edit'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set resource of current resource. |
109
|
|
|
* |
110
|
|
|
* @param $resource |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function setResource($resource) |
115
|
|
|
{ |
116
|
|
|
$this->resource = $resource; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get resource of current resource. |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getResource() |
127
|
|
|
{ |
128
|
|
|
return $this->resource ?: parent::getResource(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function display($callback = null) |
135
|
|
|
{ |
136
|
|
|
if ($callback instanceof \Closure) { |
137
|
|
|
$callback->call($this, $this); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$actions = $this->prepends; |
141
|
|
|
|
142
|
|
|
foreach ($this->actions as $action) { |
143
|
|
|
$method = 'render'.ucfirst($action); |
144
|
|
|
array_push($actions, $this->{$method}()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$actions = array_merge($actions, $this->appends); |
148
|
|
|
|
149
|
|
|
return implode('', $actions); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Render view action. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
protected function renderView() |
158
|
|
|
{ |
159
|
|
|
return <<<EOT |
160
|
|
|
<a href="{$this->getResource()}/{$this->getKey()}"> |
161
|
|
|
<i class="fa fa-eye"></i> |
162
|
|
|
</a> |
163
|
|
|
EOT; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Render edit action. |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
protected function renderEdit() |
172
|
|
|
{ |
173
|
|
|
return <<<EOT |
174
|
|
|
<a href="{$this->getResource()}/{$this->getKey()}/edit"> |
175
|
|
|
<i class="fa fa-edit"></i> |
176
|
|
|
</a> |
177
|
|
|
EOT; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Render delete action. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
protected function renderDelete() |
186
|
|
|
{ |
187
|
|
|
$this->setupDeleteScript(); |
188
|
|
|
|
189
|
|
|
return <<<EOT |
190
|
|
|
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="{$this->grid->getGridRowName()}-delete"> |
191
|
|
|
<i class="fa fa-trash"></i> |
192
|
|
|
</a> |
193
|
|
|
EOT; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
protected function setupDeleteScript() |
197
|
|
|
{ |
198
|
|
|
$deleteConfirm = trans('admin.delete_confirm'); |
199
|
|
|
$confirm = trans('admin.confirm'); |
200
|
|
|
$cancel = trans('admin.cancel'); |
201
|
|
|
|
202
|
|
|
$script = <<<SCRIPT |
203
|
|
|
|
204
|
|
|
$('.{$this->grid->getGridRowName()}-delete').unbind('click').click(function() { |
205
|
|
|
|
206
|
|
|
var id = $(this).data('id'); |
207
|
|
|
|
208
|
|
|
swal({ |
209
|
|
|
title: "$deleteConfirm", |
210
|
|
|
type: "warning", |
211
|
|
|
showCancelButton: true, |
212
|
|
|
confirmButtonColor: "#DD6B55", |
213
|
|
|
confirmButtonText: "$confirm", |
214
|
|
|
showLoaderOnConfirm: true, |
215
|
|
|
cancelButtonText: "$cancel", |
216
|
|
|
preConfirm: function() { |
217
|
|
|
return new Promise(function(resolve) { |
218
|
|
|
$.ajax({ |
219
|
|
|
method: 'post', |
220
|
|
|
url: '{$this->getResource()}/' + id, |
221
|
|
|
data: { |
222
|
|
|
_method:'delete', |
223
|
|
|
_token:LA.token, |
224
|
|
|
}, |
225
|
|
|
success: function (data) { |
226
|
|
|
$.pjax.reload('#pjax-container'); |
227
|
|
|
|
228
|
|
|
resolve(data); |
229
|
|
|
} |
230
|
|
|
}); |
231
|
|
|
}); |
232
|
|
|
} |
233
|
|
|
}).then(function(result) { |
234
|
|
|
var data = result.value; |
235
|
|
|
if (typeof data === 'object') { |
236
|
|
|
if (data.status) { |
237
|
|
|
swal(data.message, '', 'success'); |
238
|
|
|
} else { |
239
|
|
|
swal(data.message, '', 'error'); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
}); |
243
|
|
|
}); |
244
|
|
|
|
245
|
|
|
SCRIPT; |
246
|
|
|
|
247
|
|
|
Admin::script($script); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.