|
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 = ''; |
|
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 bool |
|
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
|
|
|
// We can replace this with data_get() I believe. |
|
150
|
|
|
if (($methodBreaker = strpos($field, '.')) !== false) { |
|
151
|
|
|
$method = substr($field, 0, $methodBreaker); |
|
152
|
|
|
if (method_exists($this->model, $method)) { |
|
153
|
|
|
if (method_exists($this->model->$method(), $submethod = str_replace($method.'.', '', $field))) { |
|
154
|
|
|
$this->model->$method()->$submethod(); |
|
155
|
|
|
|
|
156
|
|
|
$columns[$field] = $fieldTitle; |
|
157
|
|
|
continue; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
if (is_numeric($field)) { |
|
163
|
|
|
$field = $fieldTitle; |
|
164
|
|
|
$fieldTitle = Str::title($fieldTitle); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$columns[$field] = $fieldTitle; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if (count($columns)) { |
|
171
|
|
|
return $columns; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return [$this->model->getKeyName() => $this->model->getKeyName()]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Gets an Attribute by the provided key |
|
179
|
|
|
* on either the current model or a provided model instance. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $key |
|
182
|
|
|
* @param mixed $model |
|
183
|
|
|
* |
|
184
|
|
|
* @return mixed |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getAttribute($key, $model = false) |
|
187
|
|
|
{ |
|
188
|
|
|
if (!$model) { |
|
189
|
|
|
$model = $this->model; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if ($this->hasGetAccessor($key)) { |
|
193
|
|
|
$method = 'get'.Str::studly($key).'Attribute'; |
|
194
|
|
|
|
|
195
|
|
|
return $this->{$method}($model); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
if ($this->hasRelatedKey($key, $model)) { |
|
199
|
|
|
return $this->relatedKey($key, $model); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $model->getAttribute($key); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Determine if a get accessor exists for an attribute. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $key |
|
209
|
|
|
* |
|
210
|
|
|
* @return bool |
|
211
|
|
|
*/ |
|
212
|
|
|
public function hasGetAccessor($key) |
|
213
|
|
|
{ |
|
214
|
|
|
return method_exists($this, 'get'.Str::studly($key).'Attribute'); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Determines if a key resolved a related Model. |
|
219
|
|
|
* |
|
220
|
|
|
* @param string $key |
|
221
|
|
|
* @param mixed $model |
|
222
|
|
|
* |
|
223
|
|
|
* @return bool |
|
224
|
|
|
*/ |
|
225
|
|
|
public function hasRelatedKey($key, $model = false) |
|
226
|
|
|
{ |
|
227
|
|
|
if (!$model) { |
|
228
|
|
|
$model = $this->model; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
if (($methodBreaker = strpos($key, '.')) !== false) { |
|
232
|
|
|
$method = substr($key, 0, $methodBreaker); |
|
233
|
|
|
if (method_exists($model, $method)) { |
|
234
|
|
|
return true; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return false; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Resolves a relation based on the key provided, |
|
243
|
|
|
* either on the current model or a provided model instance. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $key |
|
246
|
|
|
* @param mixed $model |
|
247
|
|
|
* |
|
248
|
|
|
* @return mixed |
|
249
|
|
|
*/ |
|
250
|
|
|
public function relatedKey($key, $model = false) |
|
251
|
|
|
{ |
|
252
|
|
|
if (!$model) { |
|
253
|
|
|
$model = $this->model; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if (($methodBreaker = strpos($key, '.')) !== false) { |
|
257
|
|
|
$method = substr($key, 0, $methodBreaker); |
|
258
|
|
|
if (method_exists($model, $method)) { |
|
259
|
|
|
if (method_exists($model->$method, $submethod = str_replace($method.'.', '', $key))) { |
|
260
|
|
|
return $model->$method->$submethod(); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
if (isset($model->$method->$submethod)) { |
|
264
|
|
|
return $model->$method->$submethod; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
return $model->getRelationValue($method); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
return false; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Set a given attribute on the model. |
|
276
|
|
|
* |
|
277
|
|
|
* @param string $key |
|
278
|
|
|
* @param mixed $value |
|
279
|
|
|
*/ |
|
280
|
|
|
public function setAttribute($key, $value) |
|
281
|
|
|
{ |
|
282
|
|
|
if ($this->hasSetMutator($key)) { |
|
283
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
|
284
|
|
|
|
|
285
|
|
|
return $this->{$method}($value); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
$this->model->attributes[$key] = $value; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Determine if a set mutator exists for an attribute. |
|
293
|
|
|
* |
|
294
|
|
|
* @param string $key |
|
295
|
|
|
* |
|
296
|
|
|
* @return bool |
|
297
|
|
|
*/ |
|
298
|
|
|
public function hasSetMutator($key) |
|
299
|
|
|
{ |
|
300
|
|
|
return method_exists($this, 'set'.Str::studly($key).'Attribute'); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Determine if a get mutator exists for an attribute. |
|
305
|
|
|
* |
|
306
|
|
|
* @param string $key |
|
307
|
|
|
* |
|
308
|
|
|
* @return bool |
|
309
|
|
|
*/ |
|
310
|
|
|
public function hasGetMutator($key) |
|
311
|
|
|
{ |
|
312
|
|
|
return method_exists($this, 'get'.Str::studly($key).'Attribute'); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Determine if the Model Admin is sortable in it's list view. |
|
317
|
|
|
* |
|
318
|
|
|
* @param string $key |
|
|
|
|
|
|
319
|
|
|
* |
|
320
|
|
|
* @return bool |
|
321
|
|
|
*/ |
|
322
|
|
|
public function isSortable() |
|
323
|
|
|
{ |
|
324
|
|
|
return isset($this->sortable) && $this->sortable ? true : false; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Determine if the Managed Model is using the SoftDeletes Trait. |
|
329
|
|
|
* |
|
330
|
|
|
* @return bool |
|
331
|
|
|
*/ |
|
332
|
|
|
public function hasSoftDeletes() |
|
333
|
|
|
{ |
|
334
|
|
|
return in_array( |
|
335
|
|
|
\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive(get_class(new static::$managedModel())) |
|
336
|
|
|
) && in_array( |
|
337
|
|
|
\LaravelFlare\Flare\Traits\ModelAdmin\ModelSoftDeleting::class, class_uses_recursive(get_class($this)) |
|
338
|
|
|
); |
|
339
|
|
|
} |
|
340
|
|
|
} |
|
341
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.