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 EditHandlerTrait |
11
|
|
|
{ |
12
|
|
|
protected $viewCrudEdit = 'jarboe::crud.edit'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Show edit form page. |
16
|
|
|
* |
17
|
|
|
* @param Request $request |
18
|
|
|
* @param $id |
19
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
20
|
|
|
* @throws PermissionDenied |
21
|
|
|
* @throws UnauthorizedException |
22
|
|
|
*/ |
23
|
4 |
|
public function handleEdit(Request $request, $id) |
|
|
|
|
24
|
|
|
{ |
25
|
4 |
|
$this->beforeInit(); |
26
|
4 |
|
$this->init(); |
27
|
4 |
|
$this->bound(); |
28
|
|
|
|
29
|
4 |
|
$model = $this->crud()->repo()->find($id); |
30
|
4 |
|
if (!$this->crud()->actions()->isAllowed('edit', $model)) { |
31
|
1 |
|
throw new PermissionDenied(); |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
if (!$this->can('edit')) { |
35
|
2 |
|
throw UnauthorizedException::forPermissions(['edit']); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
$this->idEntity = $model->getKey(); |
|
|
|
|
39
|
|
|
|
40
|
1 |
|
return view($this->viewCrudEdit, [ |
41
|
1 |
|
'crud' => $this->crud(), |
42
|
1 |
|
'item' => $model, |
43
|
1 |
|
'viewsAbove' => $this->getEditViewsAbove(), |
44
|
1 |
|
'viewsBelow' => $this->getEditViewsBelow(), |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get array of view's objects, that should be rendered above content of `edit` view. |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
2 |
|
protected function getEditViewsAbove(): array |
54
|
|
|
{ |
55
|
2 |
|
return []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get array of view's objects, that should be rendered below content of `edit` view. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
2 |
|
protected function getEditViewsBelow(): array |
64
|
|
|
{ |
65
|
2 |
|
return []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
abstract protected function beforeInit(); |
69
|
|
|
abstract protected function init(); |
70
|
|
|
abstract protected function bound(); |
71
|
|
|
abstract protected function crud(): CRUD; |
72
|
|
|
abstract protected function can($action): bool; |
73
|
|
|
} |
74
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.