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