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