|
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
|
|
|
/** |
|
13
|
|
|
* Show edit form page. |
|
14
|
|
|
* |
|
15
|
|
|
* @param Request $request |
|
16
|
|
|
* @param $id |
|
17
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
18
|
|
|
* @throws PermissionDenied |
|
19
|
|
|
* @throws UnauthorizedException |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public function handleEdit(Request $request, $id) |
|
22
|
|
|
{ |
|
23
|
1 |
|
$this->init(); |
|
24
|
1 |
|
$this->bound(); |
|
25
|
|
|
|
|
26
|
1 |
|
$model = $this->crud()->repo()->find($id); |
|
27
|
1 |
|
if (!$this->crud()->actions()->isAllowed('edit', $model)) { |
|
28
|
|
|
throw new PermissionDenied(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
if (!$this->can('edit')) { |
|
32
|
|
|
throw UnauthorizedException::forPermissions(['edit']); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
$this->idEntity = $model->getKey(); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
1 |
|
return view($this->viewCrudEdit, [ |
|
|
|
|
|
|
38
|
1 |
|
'crud' => $this->crud(), |
|
39
|
1 |
|
'item' => $model, |
|
40
|
1 |
|
'viewsAbove' => $this->getEditViewsAbove(), |
|
41
|
1 |
|
'viewsBelow' => $this->getEditViewsBelow(), |
|
42
|
|
|
]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get array of view's objects, that should be rendered above content of `edit` view. |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
2 |
|
protected function getEditViewsAbove(): array |
|
51
|
|
|
{ |
|
52
|
2 |
|
return []; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get array of view's objects, that should be rendered below content of `edit` view. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
2 |
|
protected function getEditViewsBelow(): array |
|
61
|
|
|
{ |
|
62
|
2 |
|
return []; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
abstract protected function init(); |
|
|
|
|
|
|
66
|
|
|
abstract protected function bound(); |
|
|
|
|
|
|
67
|
|
|
abstract protected function crud(): CRUD; |
|
68
|
|
|
abstract protected function can($action): bool; |
|
69
|
|
|
} |
|
70
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: