1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Management; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableCommand; |
9
|
|
|
use Thinktomorrow\Chief\Fields\FieldArrangement; |
10
|
|
|
use Thinktomorrow\Chief\Fields\Fields; |
11
|
|
|
use Thinktomorrow\Chief\Fields\RenderingFields; |
12
|
|
|
use Thinktomorrow\Chief\Fields\SavingFields; |
13
|
|
|
use Thinktomorrow\Chief\Filters\Filters; |
14
|
|
|
use Thinktomorrow\Chief\Management\Assistants\ManagesAssistants; |
15
|
|
|
use Thinktomorrow\Chief\Management\Details\HasDetails; |
16
|
|
|
use Thinktomorrow\Chief\Management\Details\HasSections; |
17
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\NonExistingRecord; |
18
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute; |
19
|
|
|
use Thinktomorrow\Chief\Relations\ActsAsChild; |
20
|
|
|
use Thinktomorrow\Chief\Relations\ActsAsParent; |
21
|
|
|
|
22
|
|
|
abstract class AbstractManager |
23
|
|
|
{ |
24
|
|
|
use RenderingFields, |
|
|
|
|
25
|
|
|
SavingFields, |
26
|
|
|
HasDetails, |
27
|
|
|
HasSections, |
28
|
|
|
ManagesMedia, |
29
|
|
|
ManagesPagebuilder, |
30
|
|
|
TranslatableCommand, |
31
|
|
|
ManagesAssistants; |
32
|
|
|
|
33
|
|
|
protected $translation_columns = []; |
34
|
|
|
|
35
|
|
|
protected $model; |
36
|
|
|
|
37
|
|
|
/** @var Register */ |
38
|
|
|
protected $registration; |
39
|
|
|
|
40
|
|
|
protected $pageCount = 20; |
41
|
|
|
protected $paginated = true; |
42
|
|
|
protected static $bootedTraitMethods = []; |
43
|
|
|
|
44
|
175 |
|
public function __construct(Registration $registration) |
45
|
|
|
{ |
46
|
175 |
|
$this->registration = $registration; |
|
|
|
|
47
|
|
|
|
48
|
|
|
// Upon instantiation, a general model is set that doesn't point to a persisted record. |
49
|
175 |
|
$this->manage(app($this->registration->model())); |
50
|
|
|
|
51
|
|
|
// Check if key and model are present since the model should be set by the manager itself |
52
|
175 |
|
$this->validateConstraints(); |
53
|
|
|
|
54
|
175 |
|
static::bootTraitMethods(); |
55
|
175 |
|
} |
56
|
|
|
|
57
|
175 |
|
public function managerKey(): string |
58
|
|
|
{ |
59
|
175 |
|
return $this->registration->key(); |
|
|
|
|
60
|
|
|
} |
61
|
175 |
|
|
62
|
|
|
public function manage($model): Manager |
63
|
|
|
{ |
64
|
104 |
|
$this->model = $model; |
65
|
|
|
|
66
|
104 |
|
return $this; |
|
|
|
|
67
|
|
|
} |
68
|
104 |
|
|
69
|
|
|
public function findManaged($id): Manager |
70
|
104 |
|
{ |
71
|
|
|
$modelInstance = $this->modelInstance()::where('id', $id)->withoutGlobalScopes()->first(); |
|
|
|
|
72
|
|
|
|
73
|
6 |
|
return (new static($this->registration))->manage($modelInstance); |
|
|
|
|
74
|
|
|
} |
75
|
6 |
|
|
76
|
|
|
public function indexCollection() |
77
|
6 |
|
{ |
78
|
|
|
$builder = ($this->modelInstance())->query(); |
|
|
|
|
79
|
6 |
|
|
80
|
|
|
$this->filters()->apply($builder); |
81
|
6 |
|
|
82
|
|
|
$builder = $this->indexBuilder($builder); |
83
|
6 |
|
|
84
|
|
|
$builder = $this->indexSorting($builder); |
85
|
6 |
|
|
86
|
6 |
|
if ($this->paginated) { |
87
|
|
|
return $this->indexPagination($builder); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $builder->get()->map(function ($model) { |
91
|
|
|
return (new static($this->registration))->manage($model); |
|
|
|
|
92
|
|
|
}); |
93
|
|
|
} |
94
|
6 |
|
|
95
|
|
|
protected function indexBuilder(Builder $builder): Builder |
96
|
6 |
|
{ |
97
|
|
|
return $builder; |
98
|
|
|
} |
99
|
6 |
|
|
100
|
|
|
protected function indexSorting(Builder $builder): Builder |
101
|
6 |
|
{ |
102
|
3 |
|
if ($this->isAssistedBy('publish')) { |
103
|
|
|
$builder->orderBy('published_at', 'DESC'); |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
|
// if model has no timestamps, updated_at doesn't exist |
107
|
6 |
|
if ($this->modelInstance()->timestamps) { |
|
|
|
|
108
|
|
|
$builder->orderBy('updated_at', 'DESC'); |
109
|
|
|
} |
110
|
6 |
|
|
111
|
|
|
return $builder; |
112
|
|
|
} |
113
|
6 |
|
|
114
|
|
|
protected function indexPagination($builder): Paginator |
115
|
6 |
|
{ |
116
|
|
|
$paginator = $builder->paginate($this->pageCount); |
117
|
|
|
|
118
|
4 |
|
$modifiedCollection = $builder->paginate($this->pageCount)->getCollection()->transform(function ($model) { |
119
|
6 |
|
return (new static($this->registration))->manage($model); |
|
|
|
|
120
|
|
|
}); |
121
|
6 |
|
|
122
|
|
|
return $paginator->setCollection($modifiedCollection); |
123
|
|
|
} |
124
|
104 |
|
|
125
|
|
|
public function modelInstance(): ManagedModel |
126
|
104 |
|
{ |
127
|
|
|
$class = $this->registration->model(); |
|
|
|
|
128
|
|
|
|
129
|
120 |
|
return new $class; |
130
|
|
|
} |
131
|
120 |
|
|
132
|
|
|
public function existingModel(): ManagedModel |
133
|
|
|
{ |
134
|
|
|
if (!$this->hasExistingModel()) { |
135
|
|
|
throw new NonExistingRecord('Model does not exist yet but is expected.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->model; |
139
|
|
|
} |
140
|
120 |
|
|
141
|
|
|
public function hasExistingModel(): bool |
142
|
120 |
|
{ |
143
|
1 |
|
return ($this->model && $this->model->exists); |
144
|
|
|
} |
145
|
|
|
|
146
|
119 |
|
/** |
147
|
|
|
* Determine which actions should be available for this |
148
|
|
|
* manager and their respective routed urls. |
149
|
|
|
* |
150
|
|
|
* @param $verb |
151
|
|
|
* @return null|string |
152
|
|
|
* @throws NonExistingRecord |
153
|
|
|
*/ |
154
|
|
|
public function route($verb): ?string |
155
|
|
|
{ |
156
|
|
|
$routes = [ |
157
|
130 |
|
'index' => route('chief.back.managers.index', [$this->registration->key()]), |
158
|
|
|
'create' => route('chief.back.managers.create', [$this->registration->key()]), |
159
|
|
|
'store' => route('chief.back.managers.store', [$this->registration->key()]), |
160
|
130 |
|
]; |
161
|
130 |
|
|
162
|
130 |
|
if (array_key_exists($verb, $routes)) { |
163
|
|
|
return $routes[$verb] ?? null; |
164
|
|
|
} |
165
|
130 |
|
|
166
|
75 |
|
//These routes expect the model to be persisted in the database |
167
|
|
|
$modelRoutes = [ |
168
|
|
|
'edit' => route('chief.back.managers.edit', [$this->registration->key(), $this->existingModel()->id]), |
|
|
|
|
169
|
|
|
'update' => route('chief.back.managers.update', [$this->registration->key(), $this->existingModel()->id]), |
170
|
|
|
'delete' => route('chief.back.managers.delete', [$this->registration->key(), $this->existingModel()->id]), |
171
|
108 |
|
'upload' => route('chief.back.managers.media.upload', [$this->registration->key(), $this->existingModel()->id]), |
172
|
107 |
|
]; |
173
|
107 |
|
|
174
|
107 |
|
return $modelRoutes[$verb] ?? null; |
175
|
|
|
} |
176
|
|
|
|
177
|
107 |
|
public function can($verb): bool |
178
|
|
|
{ |
179
|
|
|
foreach (static::$bootedTraitMethods['can'] as $method) { |
180
|
141 |
|
if (!method_exists($this, $method)) { |
181
|
|
|
continue; |
182
|
141 |
|
} |
183
|
8 |
|
$this->$method($verb); |
184
|
|
|
} |
185
|
|
|
|
186
|
8 |
|
return !is_null($this->route($verb)); |
187
|
|
|
} |
188
|
|
|
|
189
|
137 |
|
public function guard($verb): Manager |
190
|
|
|
{ |
191
|
|
|
if (! $this->can($verb)) { |
192
|
127 |
|
NotAllowedManagerRoute::notAllowedVerb($verb, $this); |
193
|
|
|
} |
194
|
127 |
|
|
195
|
5 |
|
return $this; |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
122 |
|
public function fields(): Fields |
199
|
|
|
{ |
200
|
|
|
return new Fields(); |
201
|
87 |
|
} |
202
|
|
|
|
203
|
87 |
|
/** |
204
|
|
|
* Enrich the manager fields with any of the assistant specified fields |
205
|
|
|
* |
206
|
|
|
* @return Fields |
207
|
|
|
* @throws \Exception |
208
|
|
|
*/ |
209
|
|
|
public function fieldsWithAssistantFields(): Fields |
210
|
|
|
{ |
211
|
|
|
$fields = $this->fields(); |
212
|
110 |
|
|
213
|
|
|
foreach ($this->assistantsAsClassNames() as $assistantClass) { |
214
|
110 |
|
if (! method_exists($assistantClass, 'fields')) { |
215
|
|
|
continue; |
216
|
110 |
|
} |
217
|
84 |
|
|
218
|
82 |
|
$fields = $fields->merge($this->assistant($assistantClass)->fields()); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
84 |
|
return $fields; |
222
|
|
|
} |
223
|
|
|
|
224
|
110 |
|
/** |
225
|
|
|
* This determines the arrangement of the manageable fields |
226
|
|
|
* on the create and edit forms. By default, all fields |
227
|
|
|
* are presented in their order of appearance |
228
|
|
|
* |
229
|
|
|
* @param null $key pinpoint to a specific field arrangement e.g. for create page. |
|
|
|
|
230
|
|
|
* @return FieldArrangement |
231
|
|
|
* @throws \Exception |
232
|
|
|
*/ |
233
|
|
|
public function fieldArrangement($key = null): FieldArrangement |
234
|
|
|
{ |
235
|
|
|
return new FieldArrangement($this->fieldsWithAssistantFields()); |
236
|
3 |
|
} |
237
|
|
|
|
238
|
3 |
|
public function delete() |
239
|
|
|
{ |
240
|
|
|
$this->guard('delete'); |
241
|
3 |
|
|
242
|
|
|
if ($this->model instanceof ActsAsChild) { |
243
|
3 |
|
$this->model->detachAllParentRelations(); |
244
|
|
|
} |
245
|
2 |
|
|
246
|
2 |
|
if ($this->model instanceof ActsAsParent) { |
247
|
|
|
$this->model->detachAllChildRelations(); |
248
|
|
|
} |
249
|
2 |
|
|
250
|
2 |
|
$this->model->delete(); |
251
|
|
|
} |
252
|
|
|
|
253
|
2 |
|
public static function filters(): Filters |
254
|
2 |
|
{ |
255
|
|
|
return new Filters(); |
256
|
3 |
|
} |
257
|
|
|
|
258
|
3 |
|
/** |
259
|
|
|
* This method can be used to manipulate the store request payload |
260
|
|
|
* before being passed to the storing / updating the models. |
261
|
|
|
* |
262
|
|
|
* @param Request $request |
263
|
|
|
* @return Request |
264
|
|
|
*/ |
265
|
|
|
public function storeRequest(Request $request): Request |
266
|
|
|
{ |
267
|
|
|
return $request; |
268
|
26 |
|
} |
269
|
|
|
|
270
|
26 |
|
/** |
271
|
|
|
* This method can be used to manipulate the update request payload |
272
|
|
|
* before being passed to the storing / updating the models. |
273
|
|
|
* |
274
|
|
|
* @param Request $request |
275
|
|
|
* @return Request |
276
|
|
|
*/ |
277
|
|
|
public function updateRequest(Request $request): Request |
278
|
|
|
{ |
279
|
|
|
return $request; |
280
|
24 |
|
} |
281
|
|
|
|
282
|
24 |
|
protected function requestContainsTranslations(Request $request): bool |
283
|
|
|
{ |
284
|
|
|
return $request->has('trans'); |
285
|
83 |
|
} |
286
|
|
|
|
287
|
83 |
|
protected function validateConstraints() |
288
|
|
|
{ |
289
|
|
|
if (!$this->model) { |
290
|
175 |
|
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.'); |
291
|
|
|
} |
292
|
175 |
|
} |
293
|
|
|
|
294
|
|
|
public static function bootTraitMethods() |
295
|
175 |
|
{ |
296
|
|
|
$class = static::class; |
297
|
175 |
|
|
298
|
|
|
$methods = [ |
299
|
175 |
|
'can' |
300
|
|
|
]; |
301
|
|
|
|
302
|
175 |
|
foreach ($methods as $baseMethod) { |
303
|
|
|
static::$bootedTraitMethods[$baseMethod] = []; |
304
|
|
|
|
305
|
175 |
|
foreach (class_uses_recursive($class) as $trait) { |
306
|
175 |
|
$method = class_basename($trait) . ucfirst($baseMethod); |
307
|
|
|
|
308
|
175 |
|
if (method_exists($class, $method) && ! in_array($method, static::$bootedTraitMethods[$baseMethod])) { |
309
|
175 |
|
static::$bootedTraitMethods[$baseMethod][] = lcfirst($method); |
310
|
|
|
} |
311
|
175 |
|
} |
312
|
175 |
|
} |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|