1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Show; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Illuminate\Contracts\Support\Htmlable; |
7
|
|
|
use Illuminate\Contracts\Support\Renderable; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
class Tools implements Renderable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The panel that holds this tool. |
14
|
|
|
* |
15
|
|
|
* @var Panel |
16
|
|
|
*/ |
17
|
|
|
protected $panel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $resource; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Default tools. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $tools = ['delete', 'edit', 'list']; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Tools should be appends to default tools. |
33
|
|
|
* |
34
|
|
|
* @var Collection |
35
|
|
|
*/ |
36
|
|
|
protected $appends; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Tools should be prepends to default tools. |
40
|
|
|
* |
41
|
|
|
* @var Collection |
42
|
|
|
*/ |
43
|
|
|
protected $prepends; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Tools constructor. |
47
|
|
|
* |
48
|
|
|
* @param Panel $panel |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Panel $panel) |
51
|
|
|
{ |
52
|
|
|
$this->panel = $panel; |
53
|
|
|
|
54
|
|
|
$this->appends = new Collection(); |
55
|
|
|
$this->prepends = new Collection(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Append a tools. |
60
|
|
|
* |
61
|
|
|
* @param mixed $tool |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function append($tool) |
66
|
|
|
{ |
67
|
|
|
$this->appends->push($tool); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Prepend a tool. |
74
|
|
|
* |
75
|
|
|
* @param mixed $tool |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function prepend($tool) |
80
|
|
|
{ |
81
|
|
|
$this->prepends->push($tool); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get resource path. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getResource() |
92
|
|
|
{ |
93
|
|
|
if (is_null($this->resource)) { |
94
|
|
|
$this->resource = $this->panel->getParent()->getResourcePath(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->resource; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Disable `list` tool. |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function disableList() |
106
|
|
|
{ |
107
|
|
|
array_delete($this->tools, 'list'); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Disable `delete` tool. |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function disableDelete() |
118
|
|
|
{ |
119
|
|
|
array_delete($this->tools, 'delete'); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Disable `edit` tool. |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function disableEdit() |
130
|
|
|
{ |
131
|
|
|
array_delete($this->tools, 'edit'); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get request path for resource list. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function getListPath() |
142
|
|
|
{ |
143
|
|
|
return '/'.ltrim($this->getResource(), '/'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get request path for edit. |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
protected function getEditPath() |
152
|
|
|
{ |
153
|
|
|
$key = $this->panel->getParent()->getModel()->getKey(); |
154
|
|
|
|
155
|
|
|
return $this->getListPath().'/'.$key.'/edit'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get request path for delete. |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
protected function getDeletePath() |
164
|
|
|
{ |
165
|
|
|
$key = $this->panel->getParent()->getModel()->getKey(); |
166
|
|
|
|
167
|
|
|
return $this->getListPath().'/'.$key; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Render `list` tool. |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
protected function renderList() |
176
|
|
|
{ |
177
|
|
|
$list = trans('admin.list'); |
178
|
|
|
|
179
|
|
|
return <<<HTML |
180
|
|
|
<div class="btn-group pull-right" style="margin-right: 5px"> |
181
|
|
|
<a href="{$this->getListPath()}" class="btn btn-sm btn-default"> |
182
|
|
|
<i class="fa fa-list"></i> {$list} |
183
|
|
|
</a> |
184
|
|
|
</div> |
185
|
|
|
HTML; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Render `edit` tool. |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
protected function renderEdit() |
194
|
|
|
{ |
195
|
|
|
$edit = trans('admin.edit'); |
196
|
|
|
|
197
|
|
|
return <<<HTML |
198
|
|
|
<div class="btn-group pull-right" style="margin-right: 5px"> |
199
|
|
|
<a href="{$this->getEditPath()}" class="btn btn-sm btn-primary"> |
200
|
|
|
<i class="fa fa-edit"></i> {$edit} |
201
|
|
|
</a> |
202
|
|
|
</div> |
203
|
|
|
HTML; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Render `delete` tool. |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
View Code Duplication |
protected function renderDelete() |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$deleteConfirm = trans('admin.delete_confirm'); |
214
|
|
|
$confirm = trans('admin.confirm'); |
215
|
|
|
$cancel = trans('admin.cancel'); |
216
|
|
|
|
217
|
|
|
$class = uniqid(); |
218
|
|
|
|
219
|
|
|
$script = <<<SCRIPT |
220
|
|
|
|
221
|
|
|
$('.{$class}-delete').unbind('click').click(function() { |
222
|
|
|
|
223
|
|
|
swal({ |
224
|
|
|
title: "$deleteConfirm", |
225
|
|
|
type: "warning", |
226
|
|
|
showCancelButton: true, |
227
|
|
|
confirmButtonColor: "#DD6B55", |
228
|
|
|
confirmButtonText: "$confirm", |
229
|
|
|
closeOnConfirm: false, |
230
|
|
|
cancelButtonText: "$cancel" |
231
|
|
|
}, |
232
|
|
|
function(){ |
233
|
|
|
$.ajax({ |
234
|
|
|
method: 'post', |
235
|
|
|
url: '{$this->getDeletePath()}', |
236
|
|
|
data: { |
237
|
|
|
_method:'delete', |
238
|
|
|
_token:LA.token, |
239
|
|
|
}, |
240
|
|
|
success: function (data) { |
241
|
|
|
$.pjax({container:'#pjax-container', url: '{$this->getListPath()}' }); |
242
|
|
|
|
243
|
|
|
if (typeof data === 'object') { |
244
|
|
|
if (data.status) { |
245
|
|
|
swal(data.message, '', 'success'); |
246
|
|
|
} else { |
247
|
|
|
swal(data.message, '', 'error'); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
}); |
252
|
|
|
}); |
253
|
|
|
}); |
254
|
|
|
|
255
|
|
|
SCRIPT; |
256
|
|
|
|
257
|
|
|
$delete = trans('admin.delete'); |
258
|
|
|
|
259
|
|
|
Admin::script($script); |
260
|
|
|
|
261
|
|
|
return <<<HTML |
262
|
|
|
<div class="btn-group pull-right" style="margin-right: 5px"> |
263
|
|
|
<a href="javascript:void(0);" class="btn btn-sm btn-danger {$class}-delete"> |
264
|
|
|
<i class="fa fa-trash"></i> {$delete} |
265
|
|
|
</a> |
266
|
|
|
</div> |
267
|
|
|
HTML; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Render custom tools. |
272
|
|
|
* |
273
|
|
|
* @param Collection $tools |
274
|
|
|
* @return mixed |
275
|
|
|
*/ |
276
|
|
View Code Duplication |
protected function renderCustomTools($tools) |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
return $tools->map(function ($tool) { |
279
|
|
|
if ($tool instanceof Renderable) { |
280
|
|
|
return $tool->render(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if ($tool instanceof Htmlable) { |
284
|
|
|
return $tool->toHtml(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return (string) $tool; |
288
|
|
|
})->implode(' '); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Render tools. |
293
|
|
|
* |
294
|
|
|
* @return string |
295
|
|
|
*/ |
296
|
|
View Code Duplication |
public function render() |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
$output = $this->renderCustomTools($this->prepends); |
299
|
|
|
|
300
|
|
|
foreach ($this->tools as $tool) { |
301
|
|
|
$renderMethod = 'render'.ucfirst($tool); |
302
|
|
|
$output .= $this->$renderMethod(); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $output.$this->renderCustomTools($this->appends); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
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.