1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enomotodev\LaractiveAdmin\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionException; |
7
|
|
|
use ReflectionMethod; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Http\UploadedFile; |
12
|
|
|
use Illuminate\Support\HtmlString; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
|
15
|
|
|
abstract class Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
public $model; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $files = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $validate = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The default layout view. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public static $defaultLayoutView = 'laractive-admin::layout'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The default index view. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public static $defaultIndexView = 'laractive-admin::index'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The default index view. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public static $defaultShowView = 'laractive-admin::show'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The default new view. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
public static $defaultNewView = 'laractive-admin::new'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The default edit view. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public static $defaultEditView = 'laractive-admin::edit'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var \Illuminate\Session\SessionManager |
69
|
|
|
*/ |
70
|
|
|
protected $session; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return \Illuminate\Support\HtmlString |
74
|
|
|
* |
75
|
|
|
* @throws \Throwable |
76
|
|
|
*/ |
77
|
1 |
|
public function index() |
78
|
|
|
{ |
79
|
1 |
|
$model = $this->model::newModelInstance(); |
80
|
1 |
|
$columns = $this->getColumnsFromTable($model); |
81
|
1 |
|
$collection = $this->model::paginate(); |
82
|
|
|
|
83
|
1 |
|
return new HtmlString( |
84
|
1 |
|
view()->make(static::$defaultIndexView, [ |
85
|
1 |
|
'class' => $this->getClassName(), |
86
|
1 |
|
'table' => $this->getTable(), |
87
|
1 |
|
'layoutView' => static::$defaultLayoutView, |
88
|
1 |
|
'columns' => $columns, |
89
|
1 |
|
'collection' => $collection, |
90
|
1 |
|
])->render() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param int $id |
96
|
|
|
* @return \Illuminate\Support\HtmlString |
97
|
|
|
* |
98
|
|
|
* @throws \Throwable |
99
|
|
|
*/ |
100
|
2 |
|
public function show(int $id) |
101
|
|
|
{ |
102
|
2 |
|
$model = $this->model::findOrFail($id); |
103
|
2 |
|
$commentColumns = array_filter($this->getColumnsFromTable($model->comments()->getRelated()), function ($type, $name) use ($model) { |
104
|
2 |
|
return !in_array($name, ['id', 'updated_at', $model->comments()->getForeignKeyName(), $model->comments()->getMorphType()]); |
105
|
2 |
|
}, ARRAY_FILTER_USE_BOTH); |
106
|
|
|
|
107
|
2 |
|
return new HtmlString( |
108
|
2 |
|
view()->make(static::$defaultShowView, [ |
109
|
2 |
|
'class' => $this->getClassName(), |
110
|
2 |
|
'table' => $this->getTable(), |
111
|
2 |
|
'layoutView' => static::$defaultLayoutView, |
112
|
2 |
|
'model' => $model, |
113
|
2 |
|
'commentColumns' => $commentColumns, |
114
|
2 |
|
])->render() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return \Illuminate\Support\HtmlString |
120
|
|
|
* |
121
|
|
|
* @throws \Throwable |
122
|
|
|
*/ |
123
|
1 |
|
public function new() |
124
|
|
|
{ |
125
|
1 |
|
$model = $this->model::newModelInstance(); |
126
|
1 |
|
$columns = $this->getColumnsFromTable($model); |
127
|
1 |
|
$relations = $this->getRelations(); |
128
|
|
|
|
129
|
1 |
|
return new HtmlString( |
130
|
1 |
|
view()->make(static::$defaultNewView, [ |
131
|
1 |
|
'class' => $this->getClassName(), |
132
|
1 |
|
'table' => $this->getTable(), |
133
|
1 |
|
'layoutView' => static::$defaultLayoutView, |
134
|
1 |
|
'columns' => $columns, |
135
|
1 |
|
'model' => $model, |
136
|
1 |
|
'relations' => $relations, |
137
|
1 |
|
'files' => $this->files, |
138
|
1 |
|
])->render() |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param \Illuminate\Http\Request $request |
144
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
145
|
|
|
*/ |
146
|
|
|
public function create(Request $request) |
147
|
|
|
{ |
148
|
1 |
|
$inputs = $this->validate ? $request->validate($this->validate) : array_filter($request->post(), function ($item) { |
149
|
|
|
return $item !== null; |
150
|
1 |
|
}); |
151
|
1 |
|
if (isset($inputs['password'])) { |
152
|
1 |
|
$inputs['password'] = \Hash::make($inputs['password']); |
153
|
|
|
} |
154
|
1 |
|
if (!empty($this->files)) { |
155
|
|
|
$files = array_filter($inputs, function ($item, $key) { |
156
|
|
|
return in_array($key, $this->files) && $item instanceof UploadedFile; |
157
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
158
|
|
|
|
159
|
|
|
foreach ($files as $key => $file) { |
160
|
|
|
$fileName = Str::random(32).".".$request->{$key}->extension(); |
161
|
|
|
$request->{$key}->storePubliclyAs("public/{$this->getTable()}", $fileName); |
162
|
|
|
|
163
|
|
|
$inputs[$key] = $fileName; |
164
|
|
|
} |
165
|
|
|
} |
166
|
1 |
|
$model = $this->model::create($inputs); |
167
|
1 |
|
foreach ($this->getRelations() as $key => $relation) { |
168
|
|
|
if ($relation['type'] !== 'BelongsToMany') { |
169
|
|
|
continue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (!empty($inputs[$key])) { |
173
|
|
|
$model->{$relation['relation_name']}()->sync($inputs[$key]); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
$request->session()->flash('message', 'Create'); |
178
|
|
|
|
179
|
1 |
|
return redirect(route("admin.{$this->getTable()}.show", [$model->id])); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param int $id |
184
|
|
|
* @return \Illuminate\Support\HtmlString |
185
|
|
|
* |
186
|
|
|
* @throws \Throwable |
187
|
|
|
*/ |
188
|
|
|
public function edit(int $id) |
189
|
|
|
{ |
190
|
|
|
$model = $this->model::findOrFail($id); |
191
|
|
|
$columns = $this->getColumnsFromTable($model); |
192
|
|
|
$relations = $this->getRelations(); |
193
|
|
|
|
194
|
|
|
return new HtmlString( |
195
|
|
|
view()->make(static::$defaultEditView, [ |
196
|
|
|
'class' => $this->getClassName(), |
197
|
|
|
'table' => $this->getTable(), |
198
|
|
|
'layoutView' => static::$defaultLayoutView, |
199
|
|
|
'columns' => $columns, |
200
|
|
|
'model' => $model, |
201
|
|
|
'relations' => $relations, |
202
|
|
|
'files' => $this->files, |
203
|
|
|
])->render() |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param \Illuminate\Http\Request $request |
209
|
|
|
* @param int $id |
210
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
211
|
|
|
*/ |
212
|
|
|
public function update(Request $request, int $id) |
213
|
|
|
{ |
214
|
|
|
$model = $this->model::findOrFail($id); |
215
|
|
|
$inputs = $this->validate ? $request->validate($this->validate) : array_filter($request->post(), function ($item) { |
216
|
|
|
return $item !== null; |
217
|
|
|
}); |
218
|
|
|
if (isset($inputs['password'])) { |
219
|
|
|
$inputs['password'] = \Hash::make($inputs['password']); |
220
|
|
|
} |
221
|
|
|
if (!empty($this->files)) { |
222
|
|
|
$files = array_filter($inputs, function ($item, $key) { |
223
|
|
|
return in_array($key, $this->files) && $item instanceof UploadedFile; |
224
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
225
|
|
|
|
226
|
|
|
foreach ($files as $key => $file) { |
227
|
|
|
$fileName = Str::random(32).".".$request->{$key}->extension(); |
228
|
|
|
$request->{$key}->storePubliclyAs("public/{$this->getTable()}", $fileName); |
229
|
|
|
|
230
|
|
|
$inputs[$key] = $fileName; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
$model->update($inputs); |
234
|
|
|
foreach ($this->getRelations() as $key => $relation) { |
235
|
|
|
if ($relation['type'] !== 'BelongsToMany') { |
236
|
|
|
continue; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if (!empty($inputs[$key])) { |
240
|
|
|
$model->{$relation['relation_name']}()->sync($inputs[$key]); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$request->session()->flash('message', 'Update'); |
245
|
|
|
|
246
|
|
|
return redirect(route("admin.{$this->getTable()}.show", [$model->id])); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param \Illuminate\Http\Request $request |
251
|
|
|
* @param int $id |
252
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
253
|
|
|
*/ |
254
|
|
|
public function destroy(Request $request, int $id) |
255
|
|
|
{ |
256
|
|
|
$model = $this->model::findOrFail($id); |
257
|
|
|
$model->delete(); |
258
|
|
|
|
259
|
|
|
$request->session()->flash('message', 'Delete'); |
260
|
|
|
|
261
|
|
|
return redirect(route("admin.{$this->getTable()}.index")); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param \Illuminate\Http\Request $request |
266
|
|
|
* @param int $id |
267
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
268
|
|
|
*/ |
269
|
|
|
public function comments(Request $request, int $id) |
270
|
|
|
{ |
271
|
|
|
$model = $this->model::findOrFail($id); |
272
|
|
|
$inputs = $request->validate([ |
273
|
|
|
'body' => 'required', |
274
|
|
|
]); |
275
|
|
|
$model->comments()->create([ |
276
|
|
|
'body' => $inputs['body'], |
277
|
|
|
]); |
278
|
|
|
|
279
|
|
|
$request->session()->flash('message', 'Create comment'); |
280
|
|
|
|
281
|
|
|
return redirect(route("admin.{$this->getTable()}.show", [$model->id])); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
3 |
|
protected function getTable() |
288
|
|
|
{ |
289
|
3 |
|
return (new $this->model)->getTable(); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return array |
294
|
|
|
*/ |
295
|
1 |
|
protected function getRelations() |
296
|
|
|
{ |
297
|
1 |
|
$model = (new $this->model)->newInstance(); |
298
|
|
|
|
299
|
1 |
|
$relations = []; |
300
|
|
|
|
301
|
|
|
try { |
302
|
1 |
|
$methods = (new ReflectionClass($model))->getMethods(ReflectionMethod::IS_PUBLIC); |
303
|
1 |
|
foreach($methods as $method) { |
304
|
|
|
if ( |
305
|
1 |
|
$method->class != get_class($model) || |
306
|
1 |
|
!empty($method->getParameters()) || |
307
|
1 |
|
$method->getName() == __FUNCTION__ |
308
|
|
|
) { |
309
|
1 |
|
continue; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
try { |
313
|
1 |
|
$return = $method->invoke($model); |
314
|
|
|
|
315
|
1 |
|
if ($return instanceof BelongsTo) { |
316
|
|
|
$relations[$return->getForeignKey()] = [ |
317
|
|
|
'type' => (new ReflectionClass($return))->getShortName(), |
318
|
|
|
'model' => (new ReflectionClass($return->getRelated()))->getName(), |
319
|
|
|
]; |
320
|
1 |
|
} elseif ($return instanceof BelongsToMany) { |
321
|
|
|
$relations[$return->getRelatedPivotKeyName()] = [ |
322
|
|
|
'type' => (new ReflectionClass($return))->getShortName(), |
323
|
|
|
'model' => (new ReflectionClass($return->getRelated()))->getName(), |
324
|
1 |
|
'relation_name' => $return->getRelationName(), |
325
|
|
|
]; |
326
|
|
|
} |
327
|
1 |
|
} catch (ReflectionException $e) { |
328
|
|
|
// Ignore exception |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
} catch (ReflectionException $e) { |
332
|
|
|
// Ignore exception |
333
|
|
|
} |
334
|
|
|
|
335
|
1 |
|
return $relations; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @return string |
340
|
|
|
*/ |
341
|
3 |
|
protected function getClassName() |
342
|
|
|
{ |
343
|
|
|
try { |
344
|
3 |
|
return (new ReflectionClass($this))->getShortName(); |
345
|
|
|
} catch (ReflectionException $e) { |
346
|
|
|
return ''; |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
352
|
|
|
* @return array |
353
|
|
|
* |
354
|
|
|
* @throws \Throwable |
355
|
|
|
*/ |
356
|
3 |
|
protected function getColumnsFromTable($model) |
357
|
|
|
{ |
358
|
3 |
|
$table = $model->getConnection()->getTablePrefix().$model->getTable(); |
359
|
3 |
|
$schema = $model->getConnection()->getDoctrineSchemaManager($table); |
|
|
|
|
360
|
3 |
|
$databasePlatform = $schema->getDatabasePlatform(); |
361
|
3 |
|
$databasePlatform->registerDoctrineTypeMapping('enum', 'string'); |
362
|
|
|
|
363
|
3 |
|
$database = null; |
364
|
3 |
|
if (strpos($table, '.')) { |
365
|
|
|
list($database, $table) = explode('.', $table); |
366
|
|
|
} |
367
|
|
|
|
368
|
3 |
|
$listTableColumns = $schema->listTableColumns($table, $database); |
369
|
|
|
|
370
|
3 |
|
$columns = []; |
371
|
3 |
|
if ($listTableColumns) { |
372
|
3 |
|
foreach ($listTableColumns as $column) { |
373
|
3 |
|
$name = $column->getName(); |
374
|
|
|
|
375
|
3 |
|
switch ($column->getType()->getName()) { |
376
|
3 |
|
case 'string': |
377
|
3 |
|
$type = 'string'; |
378
|
3 |
|
break; |
379
|
3 |
|
case 'text': |
380
|
2 |
|
$type = 'text'; |
381
|
2 |
|
break; |
382
|
3 |
|
case 'date': |
383
|
|
|
$type = 'date'; |
384
|
|
|
break; |
385
|
3 |
|
case 'time': |
386
|
|
|
$type = 'time'; |
387
|
|
|
break; |
388
|
3 |
|
case 'datetimetz': |
389
|
3 |
|
case 'datetime': |
390
|
3 |
|
$type = 'datetime'; |
391
|
3 |
|
break; |
392
|
3 |
|
case 'integer': |
393
|
|
|
case 'bigint': |
394
|
|
|
case 'smallint': |
395
|
3 |
|
$type = 'integer'; |
396
|
3 |
|
break; |
397
|
|
|
case 'boolean': |
398
|
|
|
$type = 'boolean'; |
399
|
|
|
break; |
400
|
|
|
case 'decimal': |
401
|
|
|
case 'float': |
402
|
|
|
$type = 'float'; |
403
|
|
|
break; |
404
|
|
|
default: |
405
|
|
|
$type = 'mixed'; |
406
|
|
|
break; |
407
|
|
|
} |
408
|
|
|
|
409
|
3 |
|
$columns[$name] = $type; |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
3 |
|
return $columns; |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.