1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
6
|
|
|
use Illuminate\Http\RedirectResponse; |
7
|
|
|
use Illuminate\View\View; |
8
|
|
|
use Microboard\Http\Requests\Setting\StoreFormRequest; |
9
|
|
|
use Microboard\Http\Requests\Setting\UpdateFormRequest; |
10
|
|
|
use Microboard\Models\Setting; |
11
|
|
|
use Spatie\MediaLibrary\MediaCollections\Exceptions\FileDoesNotExist; |
12
|
|
|
use Spatie\MediaLibrary\MediaCollections\Exceptions\FileIsTooBig; |
13
|
|
|
|
14
|
|
|
class SettingController extends ResourceController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Display a listing of the resource. |
18
|
|
|
* |
19
|
|
|
* @return View |
20
|
|
|
* @throws AuthorizationException |
21
|
|
|
*/ |
22
|
|
|
public function index() |
23
|
|
|
{ |
24
|
|
|
$this->authorize('viewAny', new Setting); |
25
|
|
|
return view('microboard::settings.index', [ |
26
|
|
|
'groups' => cache()->remember('settings', 25000, function () { |
27
|
|
|
return Setting::all()->groupBy('group'); |
28
|
|
|
}) |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Update the specified resource in storage. |
34
|
|
|
* |
35
|
|
|
* @param UpdateFormRequest $request |
|
|
|
|
36
|
|
|
* @param Setting $setting |
|
|
|
|
37
|
|
|
* @return RedirectResponse |
38
|
|
|
* @throws AuthorizationException |
39
|
|
|
*/ |
40
|
|
|
public function update() |
41
|
|
|
{ |
42
|
|
|
$request = resolve($this->getUpdateFormRequest()); |
43
|
|
|
$this->authorize('update', new Setting); |
44
|
|
|
|
45
|
|
|
foreach ($request->get('value') as $id => $value) { |
46
|
|
|
$setting = tap(Setting::find($id))->update(compact('value')); |
47
|
|
|
|
48
|
|
|
if (in_array($setting->type, ['avatar', 'files'])) { |
49
|
|
|
if ($setting->type === 'avatar') { |
50
|
|
|
$setting->getMedia()->each->delete(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
addMediaTo($setting, 'default', "value.{$setting->id}"); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
cache()->forget('settings'); |
58
|
|
|
|
59
|
|
|
return redirect()->back(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param StoreFormRequest $request |
64
|
|
|
* @param Setting $model |
65
|
|
|
* @return RedirectResponse |
66
|
|
|
* @throws FileDoesNotExist |
67
|
|
|
* @throws FileIsTooBig |
68
|
|
|
*/ |
69
|
|
|
protected function created($request, $model) |
70
|
|
|
{ |
71
|
|
|
addMediaTo($model, 'default', "value.{$model->id}"); |
72
|
|
|
|
73
|
|
|
return redirect()->back(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function getUpdateFormRequest(): string |
80
|
|
|
{ |
81
|
|
|
return UpdateFormRequest::class; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function getModel(): string |
88
|
|
|
{ |
89
|
|
|
return Setting::class; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getStoreFormRequest(): string |
96
|
|
|
{ |
97
|
|
|
return StoreFormRequest::class; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.