1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Management; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableCommand; |
8
|
|
|
use Thinktomorrow\Chief\Fields\FieldArrangement; |
9
|
|
|
use Thinktomorrow\Chief\Fields\Fields; |
10
|
|
|
use Thinktomorrow\Chief\Fields\RenderingFields; |
11
|
|
|
use Thinktomorrow\Chief\Fields\SavingFields; |
12
|
|
|
use Thinktomorrow\Chief\Filters\Filters; |
13
|
|
|
use Thinktomorrow\Chief\Management\Assistants\AssistedManager; |
14
|
|
|
use Thinktomorrow\Chief\Management\Details\HasDetails; |
15
|
|
|
use Thinktomorrow\Chief\Management\Details\HasSections; |
16
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\NonExistingRecord; |
17
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute; |
18
|
|
|
|
19
|
|
|
abstract class AbstractManager |
20
|
|
|
{ |
21
|
|
|
use RenderingFields, |
|
|
|
|
22
|
|
|
SavingFields, |
23
|
|
|
HasDetails, |
24
|
|
|
HasSections, |
25
|
|
|
ManagesMedia, |
26
|
|
|
ManagesPagebuilder, |
27
|
|
|
TranslatableCommand, |
28
|
|
|
AssistedManager; |
29
|
|
|
|
30
|
|
|
protected $translation_columns = []; |
31
|
|
|
|
32
|
|
|
protected $model; |
33
|
|
|
|
34
|
|
|
/** @var Register */ |
35
|
|
|
protected $registration; |
36
|
|
|
|
37
|
146 |
|
public function __construct(Registration $registration) |
38
|
|
|
{ |
39
|
146 |
|
$this->registration = $registration; |
|
|
|
|
40
|
|
|
|
41
|
|
|
// Upon instantiation, a general model is set that doesn't point to a persisted record. |
42
|
146 |
|
$this->manage(app($this->registration->model())); |
43
|
|
|
|
44
|
|
|
// Check if key and model are present since the model should be set by the manager itself |
45
|
146 |
|
$this->validateConstraints(); |
46
|
146 |
|
} |
47
|
|
|
|
48
|
146 |
|
public function manage($model): Manager |
49
|
|
|
{ |
50
|
146 |
|
$this->model = $model; |
51
|
|
|
|
52
|
146 |
|
return $this; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
84 |
|
public function findManaged($id): Manager |
56
|
|
|
{ |
57
|
84 |
|
$model = $this->registration->model(); |
|
|
|
|
58
|
|
|
|
59
|
84 |
|
$modelInstance = $model::where('id', $id)->withoutGlobalScopes()->first(); |
60
|
|
|
|
61
|
84 |
|
return (new static($this->registration))->manage($modelInstance); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function findAllManaged($apply_filters = false): Collection |
65
|
|
|
{ |
66
|
1 |
|
$model = $this->registration->model(); |
67
|
|
|
|
68
|
1 |
|
$builder = (new $model)->query(); |
69
|
|
|
|
70
|
1 |
|
if ($apply_filters) { |
71
|
1 |
|
$this->filters()->apply($builder); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
if ($this->isAssistedBy('publish')) { |
75
|
1 |
|
$builder->orderBy('published', 'DESC'); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$builder->orderBy('updated_at', 'DESC'); |
79
|
|
|
|
80
|
|
|
return $builder->get()->map(function ($model) { |
81
|
1 |
|
return (new static($this->registration))->manage($model); |
|
|
|
|
82
|
1 |
|
}); |
83
|
|
|
} |
84
|
|
|
|
85
|
88 |
|
public function model() |
86
|
|
|
{ |
87
|
88 |
|
return $this->model; |
88
|
|
|
} |
89
|
|
|
|
90
|
92 |
|
public function hasExistingModel(): bool |
91
|
|
|
{ |
92
|
92 |
|
return ($this->model && $this->model->exists); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* If the model exists return it otherwise |
97
|
|
|
* throws a nonExistingRecord exception; |
98
|
|
|
* |
99
|
|
|
* @throws NonExistingRecord |
100
|
|
|
*/ |
101
|
92 |
|
protected function existingModel() |
102
|
|
|
{ |
103
|
92 |
|
if (!$this->hasExistingModel()) { |
104
|
1 |
|
throw new NonExistingRecord('Model does not exist yet but is expected.'); |
105
|
|
|
} |
106
|
|
|
|
107
|
91 |
|
return $this->model; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Determine which actions should be available for this |
112
|
|
|
* manager and their respective routed urls. |
113
|
|
|
* |
114
|
|
|
* @param $verb |
115
|
|
|
* @return null|string |
116
|
|
|
* @throws NonExistingRecord |
117
|
|
|
*/ |
118
|
115 |
|
public function route($verb): ?string |
119
|
|
|
{ |
120
|
|
|
$routes = [ |
121
|
115 |
|
'index' => route('chief.back.managers.index', [$this->registration->key()]), |
|
|
|
|
122
|
115 |
|
'create' => route('chief.back.managers.create', [$this->registration->key()]), |
123
|
115 |
|
'store' => route('chief.back.managers.store', [$this->registration->key()]), |
124
|
|
|
]; |
125
|
|
|
|
126
|
115 |
|
if (array_key_exists($verb, $routes)) { |
127
|
66 |
|
return $routes[$verb] ?? null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
//These routes expect the model to be persisted in the database |
131
|
|
|
$modelRoutes = [ |
132
|
92 |
|
'edit' => route('chief.back.managers.edit', [$this->registration->key(), $this->existingModel()->id]), |
133
|
91 |
|
'update' => route('chief.back.managers.update', [$this->registration->key(), $this->existingModel()->id]), |
134
|
91 |
|
'delete' => route('chief.back.managers.delete', [$this->registration->key(), $this->existingModel()->id]), |
135
|
91 |
|
'upload' => route('chief.back.managers.media.upload', [$this->registration->key(), $this->existingModel()->id]), |
136
|
|
|
]; |
137
|
|
|
|
138
|
91 |
|
return $modelRoutes[$verb] ?? null; |
139
|
|
|
} |
140
|
|
|
|
141
|
118 |
|
public function can($verb): bool |
142
|
|
|
{ |
143
|
118 |
|
return !is_null($this->route($verb)); |
144
|
|
|
} |
145
|
|
|
|
146
|
104 |
|
public function guard($verb): Manager |
147
|
|
|
{ |
148
|
104 |
|
if (! $this->can($verb)) { |
149
|
1 |
|
NotAllowedManagerRoute::notAllowedVerb($verb, $this); |
150
|
|
|
} |
151
|
|
|
|
152
|
103 |
|
return $this; |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
81 |
|
public function fields(): Fields |
156
|
|
|
{ |
157
|
81 |
|
return new Fields(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Enrich the manager fields with any of the assistant specified fields |
162
|
|
|
* |
163
|
|
|
* @return Fields |
164
|
|
|
* @throws \Exception |
165
|
|
|
*/ |
166
|
98 |
|
public function fieldsWithAssistantFields(): Fields |
167
|
|
|
{ |
168
|
98 |
|
$fields = $this->fields(); |
169
|
|
|
|
170
|
98 |
|
foreach ($this->assistants() as $assistant) { |
171
|
75 |
|
if (! method_exists($assistant, 'fields')) { |
172
|
73 |
|
continue; |
173
|
|
|
} |
174
|
|
|
|
175
|
75 |
|
$fields = $fields->merge($assistant->fields()); |
176
|
|
|
} |
177
|
|
|
|
178
|
98 |
|
return $fields; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* This determines the arrangement of the manageable fields |
183
|
|
|
* on the create and edit forms. By default, all fields |
184
|
|
|
* are presented in their order of appearance |
185
|
|
|
* |
186
|
|
|
* @param null $key pinpoint to a specific field arrangement e.g. for create page. |
|
|
|
|
187
|
|
|
* @return FieldArrangement |
188
|
|
|
* @throws \Exception |
189
|
|
|
*/ |
190
|
3 |
|
public function fieldArrangement($key = null): FieldArrangement |
191
|
|
|
{ |
192
|
3 |
|
return new FieldArrangement($this->fieldsWithAssistantFields()); |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
public function delete() |
196
|
|
|
{ |
197
|
1 |
|
$this->model->delete(); |
|
|
|
|
198
|
1 |
|
} |
199
|
|
|
|
200
|
|
|
public static function filters(): Filters |
201
|
|
|
{ |
202
|
|
|
return new Filters(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* This method can be used to manipulate the store request payload |
207
|
|
|
* before being passed to the storing / updating the models. |
208
|
|
|
* |
209
|
|
|
* @param Request $request |
210
|
|
|
* @return Request |
211
|
|
|
*/ |
212
|
23 |
|
public function storeRequest(Request $request): Request |
213
|
|
|
{ |
214
|
23 |
|
return $request; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* This method can be used to manipulate the update request payload |
219
|
|
|
* before being passed to the storing / updating the models. |
220
|
|
|
* |
221
|
|
|
* @param Request $request |
222
|
|
|
* @return Request |
223
|
|
|
*/ |
224
|
22 |
|
public function updateRequest(Request $request): Request |
225
|
|
|
{ |
226
|
22 |
|
return $request; |
227
|
|
|
} |
228
|
|
|
|
229
|
75 |
|
protected function requestContainsTranslations(Request $request): bool |
230
|
|
|
{ |
231
|
75 |
|
return $request->has('trans'); |
232
|
|
|
} |
233
|
|
|
|
234
|
146 |
|
protected function validateConstraints() |
235
|
|
|
{ |
236
|
146 |
|
if (!$this->model) { |
237
|
|
|
throw new \DomainException('Model class should be set for this manager. Please set the model property default via the constructor or by extending the setupDefaults method.'); |
238
|
|
|
} |
239
|
146 |
|
} |
240
|
|
|
} |
241
|
|
|
|