1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use LaravelFlare\Flare\Admin\Admin; |
7
|
|
|
use LaravelFlare\Flare\Exceptions\ModelAdminException; |
8
|
|
|
use LaravelFlare\Flare\Traits\ModelAdmin\ModelQuerying; |
9
|
|
|
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelQueryable; |
10
|
|
|
|
11
|
|
|
class ModelAdmin extends Admin implements ModelQueryable |
12
|
|
|
{ |
13
|
|
|
use ModelQuerying; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class of Model to Manage. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $managedModel; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ModelAdmin Icon. |
24
|
|
|
* |
25
|
|
|
* Font Awesome Defined Icon, eg 'user' = 'fa-user' |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $icon = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Validation Rules for onCreate, onEdit actions. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $rules = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Columns for Model. |
40
|
|
|
* |
41
|
|
|
* Defines which fields to show in the listing tables output. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $columns = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Columns for Model are Sortable. |
49
|
|
|
* |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
protected $sortable = true; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The Controller to be used by the Model Admin. |
56
|
|
|
* |
57
|
|
|
* This defaults to parent::getController() |
58
|
|
|
* if it has been left undefined. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $controller = '\LaravelFlare\Flare\Admin\Models\ModelAdminController'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The Policy used for the Model Authorization logic. |
66
|
|
|
* |
67
|
|
|
* This class should implement the ModelAdminPoliceable which |
68
|
|
|
* includes authorization checks for the create, view, edit and delete actions. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $policy = '\LaravelFlare\Flare\Permissions\ModelAdminPolicy'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The current model to be managed. |
76
|
|
|
* |
77
|
|
|
* @var Model |
78
|
|
|
*/ |
79
|
|
|
public $model; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* __construct. |
83
|
|
|
*/ |
84
|
|
|
public function __construct() |
85
|
|
|
{ |
86
|
|
|
$this->getManagedModel(); |
87
|
|
|
|
88
|
|
|
$this->model = $this->model(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns a Model Instance. |
93
|
|
|
* |
94
|
|
|
* @return Model |
95
|
|
|
*/ |
96
|
|
|
public function model() |
97
|
|
|
{ |
98
|
|
|
if (!$this->model) { |
99
|
|
|
$class = $this->getManagedModel(); |
100
|
|
|
|
101
|
|
|
return $this->model = new $class(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->model; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns a New Model Instance. |
109
|
|
|
* |
110
|
|
|
* @return Model |
111
|
|
|
*/ |
112
|
|
|
public function newModel() |
113
|
|
|
{ |
114
|
|
|
$class = self::getManagedModel(); |
115
|
|
|
|
116
|
|
|
return new $class(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the Managed Model Class. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getManagedModel() |
125
|
|
|
{ |
126
|
|
|
if (!isset($this->managedModel) || $this->managedModel === null) { |
127
|
|
|
throw new ModelAdminException('You have a ModelAdmin which does not have a model assigned to it. ModelAdmins must include a model to manage.', 1); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->managedModel; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the Managed Model Class. |
135
|
|
|
* |
136
|
|
|
* @param string $managedModel |
137
|
|
|
*/ |
138
|
|
|
public function setManagedModel($managedModel = null) |
139
|
|
|
{ |
140
|
|
|
$this->managedModel = $managedModel; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns the Route Paramets. |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function routeParameters() |
149
|
|
|
{ |
150
|
|
|
return array_merge(parent::routeParameters(), [ |
151
|
|
|
'model' => $this->managedModel, |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Formats and returns the Columns. |
157
|
|
|
* |
158
|
|
|
* This is really gross, I'm removing it soon. |
159
|
|
|
* |
160
|
|
|
* @return |
161
|
|
|
*/ |
162
|
|
|
public function getColumns() |
163
|
|
|
{ |
164
|
|
|
$columns = []; |
165
|
|
|
|
166
|
|
|
foreach ($this->columns as $field => $fieldTitle) { |
167
|
|
|
if (in_array($field, $this->model->getFillable())) { |
168
|
|
|
if (!$field) { |
169
|
|
|
$field = $fieldTitle; |
170
|
|
|
$fieldTitle = Str::title($fieldTitle); |
171
|
|
|
} |
172
|
|
|
$columns[$field] = $fieldTitle; |
173
|
|
|
continue; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// We can replace this with data_get() I believe. |
177
|
|
|
if (($methodBreaker = strpos($field, '.')) !== false) { |
178
|
|
|
$method = substr($field, 0, $methodBreaker); |
179
|
|
|
if (method_exists($this->model, $method)) { |
180
|
|
|
if (method_exists($this->model->$method(), $submethod = str_replace($method.'.', '', $field))) { |
181
|
|
|
$this->model->$method()->$submethod(); |
182
|
|
|
|
183
|
|
|
$columns[$field] = $fieldTitle; |
184
|
|
|
continue; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (is_numeric($field)) { |
190
|
|
|
$field = $fieldTitle; |
191
|
|
|
$fieldTitle = Str::title($fieldTitle); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$columns[$field] = $fieldTitle; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if (count($columns)) { |
198
|
|
|
return $columns; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return [$this->model->getKeyName() => $this->model->getKeyName()]; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Gets an Attribute by the provided key |
206
|
|
|
* on either the current model or a provided model instance. |
207
|
|
|
* |
208
|
|
|
* @param string $key |
209
|
|
|
* @param mixed $model |
210
|
|
|
* |
211
|
|
|
* @return mixed |
212
|
|
|
*/ |
213
|
|
|
public function getAttribute($key, $model = false) |
214
|
|
|
{ |
215
|
|
|
if (!$model) { |
216
|
|
|
$model = $this->model; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ($this->hasGetAccessor($key)) { |
220
|
|
|
$method = 'get'.Str::studly($key).'Attribute'; |
221
|
|
|
|
222
|
|
|
return $this->{$method}($model); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if ($this->hasRelatedKey($key, $model)) { |
226
|
|
|
return $this->relatedKey($key, $model); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $model->getAttribute($key); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Determine if a get accessor exists for an attribute. |
234
|
|
|
* |
235
|
|
|
* @param string $key |
236
|
|
|
* |
237
|
|
|
* @return bool |
238
|
|
|
*/ |
239
|
|
|
public function hasGetAccessor($key) |
240
|
|
|
{ |
241
|
|
|
return method_exists($this, 'get'.Str::studly($key).'Attribute'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Determines if a key resolved a related Model. |
246
|
|
|
* |
247
|
|
|
* @param string $key |
248
|
|
|
* @param mixed $model |
249
|
|
|
* |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
|
|
public function hasRelatedKey($key, $model = false) |
253
|
|
|
{ |
254
|
|
|
if (!$model) { |
255
|
|
|
$model = $this->model; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
if (($methodBreaker = strpos($key, '.')) !== false) { |
259
|
|
|
$method = substr($key, 0, $methodBreaker); |
260
|
|
|
if (method_exists($model, $method)) { |
261
|
|
|
return true; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return false; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Resolves a relation based on the key provided, |
270
|
|
|
* either on the current model or a provided model instance. |
271
|
|
|
* |
272
|
|
|
* @param string $key |
273
|
|
|
* @param mixed $model |
274
|
|
|
* |
275
|
|
|
* @return mixed |
276
|
|
|
*/ |
277
|
|
|
public function relatedKey($key, $model = false) |
278
|
|
|
{ |
279
|
|
|
if (!$model) { |
280
|
|
|
$model = $this->model; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if (($methodBreaker = strpos($key, '.')) !== false) { |
284
|
|
|
$method = substr($key, 0, $methodBreaker); |
285
|
|
|
if (method_exists($model, $method)) { |
286
|
|
|
if (method_exists($model->$method, $submethod = str_replace($method.'.', '', $key))) { |
287
|
|
|
return $model->$method->$submethod(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
if (isset($model->$method->$submethod)) { |
291
|
|
|
return $model->$method->$submethod; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $model->getRelationValue($method); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return false; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Set a given attribute on the model. |
303
|
|
|
* |
304
|
|
|
* @param string $key |
305
|
|
|
* @param mixed $value |
306
|
|
|
*/ |
307
|
|
|
public function setAttribute($key, $value) |
308
|
|
|
{ |
309
|
|
|
if ($this->hasSetMutator($key)) { |
310
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
311
|
|
|
|
312
|
|
|
return $this->{$method}($value); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$this->model->attributes[$key] = $value; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Determine if a set mutator exists for an attribute. |
320
|
|
|
* |
321
|
|
|
* @param string $key |
322
|
|
|
* |
323
|
|
|
* @return bool |
324
|
|
|
*/ |
325
|
|
|
public function hasSetMutator($key) |
326
|
|
|
{ |
327
|
|
|
return method_exists($this, 'set'.Str::studly($key).'Attribute'); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Determine if a get mutator exists for an attribute. |
332
|
|
|
* |
333
|
|
|
* @param string $key |
334
|
|
|
* |
335
|
|
|
* @return bool |
336
|
|
|
*/ |
337
|
|
|
public function hasGetMutator($key) |
338
|
|
|
{ |
339
|
|
|
return method_exists($this, 'get'.Str::studly($key).'Attribute'); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns an array of Attribute Fields ready for output. |
344
|
|
|
* |
345
|
|
|
* @param string $type |
346
|
|
|
* |
347
|
|
|
* @return array |
348
|
|
|
*/ |
349
|
|
|
public function outputFields($type = 'view') |
|
|
|
|
350
|
|
|
{ |
351
|
|
|
return $this->getFields(); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Gets the Managed Model Mapping. |
356
|
|
|
* |
357
|
|
|
* @return array |
358
|
|
|
*/ |
359
|
|
|
public function getFields() |
360
|
|
|
{ |
361
|
|
|
return $this->fields; |
|
|
|
|
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Gets the Managed Model Mapping. |
366
|
|
|
* |
367
|
|
|
* @param array $fields |
368
|
|
|
*/ |
369
|
|
|
public function setFields($fields = []) |
370
|
|
|
{ |
371
|
|
|
$this->fields = $fields; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Determine if the Model Admin is sortable in it's list view. |
376
|
|
|
* |
377
|
|
|
* @return bool |
378
|
|
|
*/ |
379
|
|
|
public function isSortable() |
380
|
|
|
{ |
381
|
|
|
return isset($this->sortable) && $this->sortable ? true : false; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Determine if the Model Admin has Viewing Capabilities. |
386
|
|
|
* |
387
|
|
|
* @return bool |
388
|
|
|
*/ |
389
|
|
|
public function hasViewing() |
390
|
|
|
{ |
391
|
|
|
return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelViewing::class); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Determine if the Model Admin has Creating Capabilities. |
396
|
|
|
* |
397
|
|
|
* @return bool |
398
|
|
|
*/ |
399
|
|
|
public function hasCreating() |
400
|
|
|
{ |
401
|
|
|
return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelCreating::class); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Determine if the Model Admin has Cloning Capabilities. |
406
|
|
|
* |
407
|
|
|
* @return bool |
408
|
|
|
*/ |
409
|
|
|
public function hasCloning() |
410
|
|
|
{ |
411
|
|
|
return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelCloning::class); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Determine if the Model Admin has Editting Capabilities. |
416
|
|
|
* |
417
|
|
|
* @return bool |
418
|
|
|
*/ |
419
|
|
|
public function hasEditting() |
420
|
|
|
{ |
421
|
|
|
return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelEditting::class); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Determine if the Model Admin has Deleting Capabilities. |
426
|
|
|
* |
427
|
|
|
* @return bool |
428
|
|
|
*/ |
429
|
|
|
public function hasDeleting() |
430
|
|
|
{ |
431
|
|
|
return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelDeleting::class); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* Determine if the Managed Model is using the SoftDeletes Trait. |
436
|
|
|
* |
437
|
|
|
* This is guarded by hasDeleting, since we shouldn't allow SoftDeleting |
438
|
|
|
* without the deleting trait (even though it isn't really required). |
439
|
|
|
* |
440
|
|
|
* @return bool |
441
|
|
|
*/ |
442
|
|
|
public function hasSoftDeletes() |
443
|
|
|
{ |
444
|
|
|
if (!$this->hasDeleting()) { |
|
|
|
|
445
|
|
|
return false; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
$managedModelClass = $this->getManagedModel(); |
449
|
|
|
|
450
|
|
|
return in_array( |
451
|
|
|
\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive(get_class(new $managedModelClass())) |
452
|
|
|
) && in_array( |
453
|
|
|
\LaravelFlare\Flare\Traits\ModelAdmin\ModelSoftDeleting::class, class_uses_recursive(get_class($this)) |
454
|
|
|
); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Determine if the Model Admin has Validating Capabilities. |
459
|
|
|
* |
460
|
|
|
* @return bool |
461
|
|
|
*/ |
462
|
|
|
public function hasValidating() |
463
|
|
|
{ |
464
|
|
|
return $this->hasTrait(\LaravelFlare\Flare\Traits\ModelValidating::class); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Determine if the Managed Model has a Trait and Contract |
469
|
|
|
* |
470
|
|
|
* @return bool |
471
|
|
|
*/ |
472
|
|
|
public function hasTraitAndContract($trait = null, $contract = null) |
473
|
|
|
{ |
474
|
|
|
return ($this->hasTrait($trait) && $this->hasContract($contract)); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Returns whether the current ModelAdmin has a given trait. |
479
|
|
|
* |
480
|
|
|
* @param string $trait |
481
|
|
|
* |
482
|
|
|
* @return boolean |
483
|
|
|
*/ |
484
|
|
|
public function hasTrait($trait = null) |
485
|
|
|
{ |
486
|
|
|
if (!$trait) { |
|
|
|
|
487
|
|
|
return; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
$managedModelClass = $this->getManagedModel(); |
491
|
|
|
|
492
|
|
|
return in_array($trait, class_uses_recursive(get_class(new $managedModelClass()))); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Returns whether the current ModelAdmin has a given contract. |
497
|
|
|
* |
498
|
|
|
* @param string $contract |
499
|
|
|
* |
500
|
|
|
* @return boolean |
501
|
|
|
*/ |
502
|
|
|
public function hasContract($contract = null) |
|
|
|
|
503
|
|
|
{ |
504
|
|
|
if (!$trait) { |
|
|
|
|
505
|
|
|
return; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
$managedModelClass = $this->getManagedModel(); |
|
|
|
|
509
|
|
|
} |
510
|
|
|
} |
511
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.