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\Admin\Widgets\DefaultWidget; |
8
|
|
|
use LaravelFlare\Flare\Exceptions\ModelAdminException; |
9
|
|
|
use LaravelFlare\Flare\Traits\ModelAdmin\ModelWriting; |
10
|
|
|
use LaravelFlare\Flare\Traits\ModelAdmin\ModelCloning; |
11
|
|
|
use LaravelFlare\Flare\Traits\ModelAdmin\ModelQuerying; |
12
|
|
|
use LaravelFlare\Flare\Traits\Attributes\AttributeAccess; |
13
|
|
|
use LaravelFlare\Flare\Traits\ModelAdmin\ModelValidating; |
14
|
|
|
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelWriteable; |
15
|
|
|
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelQueryable; |
16
|
|
|
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelCloneable; |
17
|
|
|
use LaravelFlare\Flare\Contracts\ModelAdmin\ModelValidatable; |
18
|
|
|
use LaravelFlare\Flare\Contracts\ModelAdmin\AttributesAccessable; |
19
|
|
|
|
20
|
|
|
class ModelAdmin extends Admin implements AttributesAccessable, ModelWriteable, ModelQueryable, ModelValidatable, ModelCloneable |
21
|
|
|
{ |
22
|
|
|
use AttributeAccess, ModelWriting, ModelQuerying, ModelValidating, ModelCloning; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class of Model to Manage. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected static $managedModel; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ModelAdmin Icon. |
33
|
|
|
* |
34
|
|
|
* Font Awesome Defined Icon, eg 'user' = 'fa-user' |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected static $icon = 'user'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Validation Rules for onCreate, onEdit actions. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $rules = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Columns for Model. |
49
|
|
|
* |
50
|
|
|
* Defines which fields to show in the listing tables output. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $columns = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Columns for Model are Sortable. |
58
|
|
|
* |
59
|
|
|
* @var boolean |
60
|
|
|
*/ |
61
|
|
|
protected $sortable = true; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The Controller to be used by the Model Admin. |
65
|
|
|
* |
66
|
|
|
* This defaults to parent::getController() |
67
|
|
|
* if it has been left undefined. |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected static $controller = '\LaravelFlare\Flare\Admin\Models\ModelAdminController'; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The Policy used for the Model Authorization logic. |
75
|
|
|
* |
76
|
|
|
* This class should implement the ModelAdminPoliceable which |
77
|
|
|
* includes authorization checks for the create, view, edit and delete actions. |
78
|
|
|
* |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
protected static $policy = '\LaravelFlare\Flare\Permissions\ModelAdminPolicy'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The current model to be managed. |
85
|
|
|
* |
86
|
|
|
* @var Model |
87
|
|
|
*/ |
88
|
|
|
public $model; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* __construct. |
92
|
|
|
*/ |
93
|
|
|
public function __construct() |
94
|
|
|
{ |
95
|
|
|
if (!isset(static::$managedModel) || static::$managedModel === null) { |
96
|
|
|
throw new ModelAdminException('You have a ModelAdmin which does not have a model assigned to it. ModelAdmins must include a model to manage.', 1); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->model = $this->model(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns a Model Instance. |
104
|
|
|
* |
105
|
|
|
* Note: We should revisit this as really we shouldn't |
106
|
|
|
* be returning a new instance of the object on every |
107
|
|
|
* request. |
108
|
|
|
* |
109
|
|
|
* @return Model |
110
|
|
|
*/ |
111
|
|
|
public function model() |
112
|
|
|
{ |
113
|
|
|
return new static::$managedModel(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the Route Paramets. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function routeParameters() |
122
|
|
|
{ |
123
|
|
|
return array_merge(parent::routeParameters(), [ |
124
|
|
|
'model' => $this->managedModel, |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Formats and returns the Columns. |
130
|
|
|
* |
131
|
|
|
* This is really gross, I'm removing it soon. |
132
|
|
|
* |
133
|
|
|
* @return |
134
|
|
|
*/ |
135
|
|
|
public function getColumns() |
136
|
|
|
{ |
137
|
|
|
$columns = []; |
138
|
|
|
|
139
|
|
|
foreach ($this->columns as $field => $fieldTitle) { |
140
|
|
|
if (in_array($field, $this->model->getFillable())) { |
141
|
|
|
if (!$field) { |
142
|
|
|
$field = $fieldTitle; |
143
|
|
|
$fieldTitle = Str::title($fieldTitle); |
144
|
|
|
} |
145
|
|
|
$columns[$field] = $fieldTitle; |
146
|
|
|
continue; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (($methodBreaker = strpos($field, '.')) !== false) { |
150
|
|
|
$method = substr($field, 0, $methodBreaker); |
151
|
|
|
if (method_exists($this->model, $method)) { |
152
|
|
|
if (method_exists($this->model->$method(), $submethod = str_replace($method.'.', '', $field))) { |
153
|
|
|
$this->model->$method()->$submethod(); |
154
|
|
|
|
155
|
|
|
$columns[$field] = $fieldTitle; |
156
|
|
|
continue; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (is_numeric($field)) { |
162
|
|
|
$field = $fieldTitle; |
163
|
|
|
$fieldTitle = Str::title($fieldTitle); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$columns[$field] = $fieldTitle; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (count($columns)) { |
170
|
|
|
return $columns; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return [$this->model->getKeyName() => $this->model->getKeyName()]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Gets an Attribute by the provided key |
178
|
|
|
* on either the current model or a provided model instance. |
179
|
|
|
* |
180
|
|
|
* @param string $key |
181
|
|
|
* @param mixed $model |
182
|
|
|
* |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
|
|
public function getAttribute($key, $model = false) |
186
|
|
|
{ |
187
|
|
|
if (!$model) { |
188
|
|
|
$model = $this->model; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ($this->hasGetAccessor($key)) { |
192
|
|
|
$method = 'get'.Str::studly($key).'Attribute'; |
193
|
|
|
|
194
|
|
|
return $this->{$method}($model); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ($this->hasRelatedKey($key, $model)) { |
198
|
|
|
return $this->relatedKey($key, $model); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $model->getAttribute($key); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Determine if a get accessor exists for an attribute. |
206
|
|
|
* |
207
|
|
|
* @param string $key |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
public function hasGetAccessor($key) |
212
|
|
|
{ |
213
|
|
|
return method_exists($this, 'get'.Str::studly($key).'Attribute'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Determines if a key resolved a related Model. |
218
|
|
|
* |
219
|
|
|
* @param string $key |
220
|
|
|
* @param mixed $model |
221
|
|
|
* |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
public function hasRelatedKey($key, $model = false) |
225
|
|
|
{ |
226
|
|
|
if (!$model) { |
227
|
|
|
$model = $this->model; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (($methodBreaker = strpos($key, '.')) !== false) { |
231
|
|
|
$method = substr($key, 0, $methodBreaker); |
232
|
|
|
if (method_exists($model, $method)) { |
233
|
|
|
return true; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return false; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Resolves a relation based on the key provided, |
242
|
|
|
* either on the current model or a provided model instance. |
243
|
|
|
* |
244
|
|
|
* @param string $key |
245
|
|
|
* @param mixed $model |
246
|
|
|
* |
247
|
|
|
* @return mixed |
248
|
|
|
*/ |
249
|
|
|
public function relatedKey($key, $model = false) |
250
|
|
|
{ |
251
|
|
|
if (!$model) { |
252
|
|
|
$model = $this->model; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
if (($methodBreaker = strpos($key, '.')) !== false) { |
256
|
|
|
$method = substr($key, 0, $methodBreaker); |
257
|
|
|
if (method_exists($model, $method)) { |
258
|
|
|
if (method_exists($model->$method, $submethod = str_replace($method.'.', '', $key))) { |
259
|
|
|
return $model->$method->$submethod(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
if (isset($model->$method->$submethod)) { |
263
|
|
|
return $model->$method->$submethod; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $model->getRelationValue($method); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return false; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Set a given attribute on the model. |
275
|
|
|
* |
276
|
|
|
* @param string $key |
277
|
|
|
* @param mixed $value |
278
|
|
|
*/ |
279
|
|
|
public function setAttribute($key, $value) |
280
|
|
|
{ |
281
|
|
|
if ($this->hasSetMutator($key)) { |
282
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
283
|
|
|
|
284
|
|
|
return $this->{$method}($value); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$this->model->attributes[$key] = $value; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Determine if a set mutator exists for an attribute. |
292
|
|
|
* |
293
|
|
|
* @param string $key |
294
|
|
|
* |
295
|
|
|
* @return bool |
296
|
|
|
*/ |
297
|
|
|
public function hasSetMutator($key) |
298
|
|
|
{ |
299
|
|
|
return method_exists($this, 'set'.Str::studly($key).'Attribute'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Determine if a get mutator exists for an attribute. |
304
|
|
|
* |
305
|
|
|
* @param string $key |
306
|
|
|
* |
307
|
|
|
* @return bool |
308
|
|
|
*/ |
309
|
|
|
public function hasGetMutator($key) |
310
|
|
|
{ |
311
|
|
|
return method_exists($this, 'get'.Str::studly($key).'Attribute'); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Determine if the Model Admin is sortable in it's list view. |
316
|
|
|
* |
317
|
|
|
* @param string $key |
|
|
|
|
318
|
|
|
* |
319
|
|
|
* @return bool |
320
|
|
|
*/ |
321
|
|
|
public function isSortable() |
322
|
|
|
{ |
323
|
|
|
return isset($this->sortable) && $this->sortable ? true : false; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Returns a DefaultWidget instance based on the currently ManagedModel. |
328
|
|
|
* |
329
|
|
|
* @return DefaultWidget |
330
|
|
|
*/ |
331
|
|
|
public function defaultWidget() |
332
|
|
|
{ |
333
|
|
|
return new DefaultWidget($this); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Determine if the Managed Model is using the SoftDeletes Trait. |
338
|
|
|
* |
339
|
|
|
* @return bool |
340
|
|
|
*/ |
341
|
|
|
public function hasSoftDeletes() |
342
|
|
|
{ |
343
|
|
|
return in_array( |
344
|
|
|
\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive(get_class(static::$managedModel())) |
|
|
|
|
345
|
|
|
); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.