|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Modules; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Thinktomorrow\Chief\Fields\Fields; |
|
7
|
|
|
use Thinktomorrow\Chief\Fields\Types\HtmlField; |
|
8
|
|
|
use Thinktomorrow\Chief\Fields\Types\InputField; |
|
9
|
|
|
use Thinktomorrow\Chief\Management\AbstractManager; |
|
10
|
|
|
use Thinktomorrow\Chief\Management\Details\Details; |
|
11
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\DeleteAborted; |
|
12
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute; |
|
13
|
|
|
use Thinktomorrow\Chief\Management\Manager; |
|
14
|
|
|
use Thinktomorrow\Chief\Management\Managers; |
|
15
|
|
|
use Thinktomorrow\Chief\Modules\Application\DeleteModule; |
|
16
|
|
|
|
|
17
|
|
|
class ModuleManager extends AbstractManager implements Manager |
|
18
|
|
|
{ |
|
19
|
25 |
|
public function details(): Details |
|
20
|
|
|
{ |
|
21
|
25 |
|
$modelDetails = parent::details(); |
|
22
|
25 |
|
$modelDetails = $modelDetails->set('plural', $this->model->isPageSpecific() ? 'eigen modules' : 'vaste modules'); |
|
|
|
|
|
|
23
|
25 |
|
$modelDetails = $modelDetails->set('title', $this->model->slug); |
|
24
|
|
|
|
|
25
|
25 |
|
return $modelDetails; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
26 |
|
public function route($verb): ?string |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Page specific modules are expected to be found and managed in the context of a certain page. |
|
32
|
|
|
* Therefore the index of these modules is at the modules tab of this page model. |
|
33
|
|
|
*/ |
|
34
|
26 |
|
if ($verb == 'index' && $this->model->isPageSpecific()) { |
|
35
|
|
|
return app(Managers::class)->findByModel($this->model->page)->route('edit').'#eigen-modules'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$routes = [ |
|
39
|
26 |
|
'index' => route('chief.back.modules.index'), |
|
40
|
26 |
|
'create' => route('chief.back.managers.create', [$this->registration->key()]), |
|
|
|
|
|
|
41
|
26 |
|
'store' => route('chief.back.managers.store', [$this->registration->key(), $this->model->id]), |
|
42
|
26 |
|
'edit' => route('chief.back.managers.edit', [$this->registration->key(), $this->model->id]), |
|
43
|
26 |
|
'update' => route('chief.back.managers.update', [$this->registration->key(), $this->model->id]), |
|
44
|
26 |
|
'delete' => route('chief.back.managers.delete', [$this->registration->key(), $this->model->id]), |
|
45
|
26 |
|
'upload' => route('chief.back.managers.media.upload', [$this->registration->key(), $this->model->id]) |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
26 |
|
return $routes[$verb] ?? null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
27 |
|
public function can($verb): bool |
|
52
|
|
|
{ |
|
53
|
|
|
try { |
|
54
|
27 |
|
$this->authorize($verb); |
|
55
|
1 |
|
} catch (NotAllowedManagerRoute $e) { |
|
56
|
1 |
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
26 |
|
return parent::can($verb); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
27 |
|
private function authorize($verb) |
|
63
|
|
|
{ |
|
64
|
27 |
|
$permission = 'update-page'; |
|
65
|
|
|
|
|
66
|
27 |
|
if (in_array($verb, ['index','show'])) { |
|
67
|
17 |
|
$permission = 'view-page'; |
|
68
|
11 |
|
} elseif (in_array($verb, ['create','store'])) { |
|
69
|
3 |
|
$permission = 'create-page'; |
|
70
|
8 |
|
} elseif (in_array($verb, ['delete'])) { |
|
71
|
3 |
|
$permission = 'delete-page'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
27 |
|
if (! auth()->guard('chief')->user()->hasPermissionTo($permission)) { |
|
|
|
|
|
|
75
|
1 |
|
throw NotAllowedManagerRoute::notAllowedPermission($permission, $this); |
|
76
|
|
|
} |
|
77
|
26 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* The set of fields that should be manageable for a certain model. |
|
81
|
|
|
* |
|
82
|
|
|
* Additionally, you should: |
|
83
|
|
|
* 1. Make sure to setup the proper migrations and |
|
84
|
|
|
* 2. For a translatable field you should add this field to the $translatedAttributes property of the model as well. |
|
85
|
|
|
* |
|
86
|
|
|
* @return Fields |
|
87
|
|
|
*/ |
|
88
|
9 |
|
public function fields(): Fields |
|
89
|
|
|
{ |
|
90
|
9 |
|
return new Fields([ |
|
91
|
9 |
|
InputField::make('slug') |
|
92
|
9 |
|
->label('Interne benaming') |
|
|
|
|
|
|
93
|
9 |
|
->validation('required', ['slug' => 'Interne titel is verplicht']), |
|
94
|
9 |
|
InputField::make('title') |
|
95
|
9 |
|
->translatable($this->model->availableLocales()) |
|
|
|
|
|
|
96
|
9 |
|
->label('titel'), |
|
97
|
9 |
|
HtmlField::make('content') |
|
98
|
9 |
|
->translatable($this->model->availableLocales()) |
|
99
|
9 |
|
->label('inhoud'), |
|
|
|
|
|
|
100
|
|
|
]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
6 |
|
public function saveFields(): Manager |
|
104
|
|
|
{ |
|
105
|
|
|
// Store the morph_key upon creation |
|
106
|
6 |
|
if (! $this->model->morph_key) { |
|
107
|
2 |
|
$this->model->morph_key = $this->model->morphKey(); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
6 |
|
return parent::saveFields(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
public function delete() |
|
114
|
|
|
{ |
|
115
|
2 |
|
$this->guard('delete'); |
|
116
|
|
|
|
|
117
|
2 |
|
if (request()->get('deleteconfirmation') !== 'DELETE') { |
|
118
|
|
|
throw new DeleteAborted(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
app(DeleteModule::class)->handle($this->model->id); |
|
122
|
2 |
|
} |
|
123
|
|
|
|
|
124
|
3 |
|
public function storeRequest(Request $request): Request |
|
125
|
|
|
{ |
|
126
|
3 |
|
$trans = []; |
|
127
|
3 |
|
foreach ($request->get('trans', []) as $locale => $translation) { |
|
128
|
|
|
if (is_array_empty($translation)) { |
|
129
|
|
|
continue; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$trans[$locale] = $translation; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
return $request->merge(['trans' => $trans]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
5 |
|
public function updateRequest(Request $request): Request |
|
139
|
|
|
{ |
|
140
|
5 |
|
$trans = []; |
|
141
|
5 |
|
foreach ($request->get('trans', []) as $locale => $translation) { |
|
142
|
5 |
|
if (is_array_empty($translation)) { |
|
143
|
|
|
|
|
144
|
|
|
// Nullify all values |
|
145
|
|
|
$trans[$locale] = array_map(function ($value) { |
|
146
|
|
|
return null; |
|
147
|
|
|
}, $translation); |
|
148
|
|
|
continue; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
5 |
|
$trans[$locale] = $translation; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
5 |
|
return $request->merge(['trans' => $trans]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.