1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Spatie\Permission\Exceptions\UnauthorizedException; |
7
|
|
|
use Yaro\Jarboe\Exceptions\PermissionDenied; |
8
|
|
|
use Yaro\Jarboe\Table\CRUD; |
9
|
|
|
|
10
|
|
|
trait DeleteHandlerTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Handle delete action. |
14
|
|
|
* |
15
|
|
|
* @param Request $request |
16
|
|
|
* @param $id |
17
|
|
|
* @return \Illuminate\Http\JsonResponse |
18
|
|
|
* @throws PermissionDenied |
19
|
|
|
* @throws UnauthorizedException |
20
|
|
|
*/ |
21
|
4 |
|
public function handleDelete(Request $request, $id) |
|
|
|
|
22
|
|
|
{ |
23
|
4 |
|
$this->beforeInit(); |
24
|
4 |
|
$this->init(); |
25
|
4 |
|
$this->bound(); |
26
|
|
|
|
27
|
4 |
|
$model = $this->crud()->repo()->find($id); |
28
|
4 |
|
if (!$this->crud()->actions()->isAllowed('delete', $model)) { |
29
|
1 |
|
throw new PermissionDenied(); |
30
|
|
|
} |
31
|
|
|
|
32
|
3 |
|
if (!$this->can('delete')) { |
33
|
2 |
|
throw UnauthorizedException::forPermissions(['delete']); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
$this->idEntity = $model->getKey(); |
|
|
|
|
37
|
|
|
|
38
|
1 |
|
if ($this->crud()->repo()->delete($id)) { |
39
|
1 |
|
$type = 'hidden'; |
40
|
|
|
try { |
41
|
1 |
|
$this->crud()->repo()->find($id); |
42
|
|
|
} catch (\Exception $e) { |
43
|
|
|
$type = 'removed'; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
return response()->json([ |
47
|
1 |
|
'type' => $type, |
48
|
1 |
|
'message' => __('jarboe::common.list.delete_success_message', ['id' => $id]), |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return response()->json([ |
53
|
|
|
'message' => __('jarboe::common.list.delete_failed_message', ['id' => $id]), |
54
|
|
|
], 422); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
abstract protected function beforeInit(); |
58
|
|
|
abstract protected function init(); |
59
|
|
|
abstract protected function bound(); |
60
|
|
|
abstract protected function crud(): CRUD; |
61
|
|
|
abstract protected function can($action): bool; |
62
|
|
|
} |
63
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.