1 | <?php |
||||
2 | |||||
3 | namespace Terranet\Administrator\Controllers; |
||||
4 | |||||
5 | use Illuminate\Contracts\Support\Renderable; |
||||
6 | use Illuminate\Http\RedirectResponse; |
||||
7 | use Illuminate\Http\Request; |
||||
8 | use Illuminate\Support\Facades\URL; |
||||
9 | use Spatie\MediaLibrary\Models\Media; |
||||
10 | use Symfony\Component\HttpFoundation\Response; |
||||
11 | use Terranet\Administrator\Contracts\Module; |
||||
12 | use Terranet\Administrator\Requests\UpdateRequest; |
||||
13 | use Terranet\Administrator\Scaffolding; |
||||
14 | use Terranet\Administrator\Services\MediaLibraryProvider; |
||||
15 | |||||
16 | class ScaffoldController extends AdminController |
||||
17 | { |
||||
18 | /** |
||||
19 | * @param $page |
||||
20 | * @param Module $resource |
||||
21 | * |
||||
22 | * @return \Illuminate\View\View |
||||
23 | */ |
||||
24 | public function index($page, Module $resource) |
||||
25 | { |
||||
26 | $this->authorize('index', $resource->model()); |
||||
27 | |||||
28 | $items = $resource->finderInstance()->fetchAll(); |
||||
29 | |||||
30 | return view(app('scaffold.template')->index('index'), ['items' => $items]); |
||||
31 | } |
||||
32 | |||||
33 | /** |
||||
34 | * View resource. |
||||
35 | * |
||||
36 | * @param $page |
||||
37 | * @param $id |
||||
38 | * |
||||
39 | * @return \Illuminate\View\View |
||||
40 | */ |
||||
41 | public function view($page, $id) |
||||
42 | { |
||||
43 | $this->authorize('view', $eloquent = app('scaffold.model')); |
||||
44 | |||||
45 | return view(app('scaffold.template')->view('index'), [ |
||||
46 | 'item' => $eloquent, |
||||
47 | ]); |
||||
48 | } |
||||
49 | |||||
50 | /** |
||||
51 | * Edit resource. |
||||
52 | * |
||||
53 | * @param $page |
||||
54 | * @param $id |
||||
55 | * |
||||
56 | * @return \Illuminate\View\View |
||||
57 | */ |
||||
58 | public function edit($page, $id) |
||||
59 | { |
||||
60 | $this->authorize('update', $eloquent = app('scaffold.model')); |
||||
61 | |||||
62 | return view(app('scaffold.template')->edit('index'), [ |
||||
63 | 'item' => $eloquent, |
||||
64 | ]); |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * @param $page |
||||
69 | * @param $id |
||||
70 | * @param null|UpdateRequest $request |
||||
71 | * |
||||
72 | * @return RedirectResponse |
||||
73 | */ |
||||
74 | public function update($page, $id, UpdateRequest $request) |
||||
75 | { |
||||
76 | /** @var Scaffolding $resource */ |
||||
77 | $resource = app('scaffold.module'); |
||||
78 | |||||
79 | $this->authorize('update', $eloquent = app('scaffold.model')); |
||||
80 | |||||
81 | try { |
||||
82 | $resource->actionsManager()->exec('save', [$eloquent, $request]); |
||||
83 | } catch (\Exception $e) { |
||||
84 | return back()->withErrors([$e->getMessage()]); |
||||
85 | } |
||||
86 | |||||
87 | return $this->redirectTo($page, $id, $request)->with( |
||||
88 | 'messages', |
||||
89 | [trans('administrator::messages.update_success')] |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
90 | ); |
||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * Create new item. |
||||
95 | * |
||||
96 | * @return \Illuminate\View\View |
||||
97 | */ |
||||
98 | public function create() |
||||
99 | { |
||||
100 | $this->authorize('create', $eloquent = app('scaffold.module')->model()); |
||||
101 | |||||
102 | return view(app('scaffold.template')->edit('index'), ['item' => $eloquent]); |
||||
103 | } |
||||
104 | |||||
105 | /** |
||||
106 | * Store new item. |
||||
107 | * |
||||
108 | * @param $page |
||||
109 | * @param null|UpdateRequest $request |
||||
110 | * |
||||
111 | * @return \Illuminate\Http\RedirectResponse |
||||
112 | */ |
||||
113 | public function store($page, UpdateRequest $request) |
||||
114 | { |
||||
115 | /** @var Scaffolding $resource */ |
||||
116 | $resource = app('scaffold.module'); |
||||
117 | |||||
118 | $this->authorize('create', $eloquent = $resource->model()); |
||||
119 | |||||
120 | try { |
||||
121 | $eloquent = $resource->actionsManager()->exec('save', [$eloquent, $request]); |
||||
122 | } catch (\Exception $e) { |
||||
123 | return back()->withErrors([$e->getMessage()]); |
||||
124 | } |
||||
125 | |||||
126 | return $this->redirectTo($page, $eloquent->id, $request)->with( |
||||
127 | 'messages', |
||||
128 | [trans('administrator::messages.create_success')] |
||||
0 ignored issues
–
show
The function
trans was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
129 | ); |
||||
130 | } |
||||
131 | |||||
132 | /** |
||||
133 | * Destroy item. |
||||
134 | * |
||||
135 | * @param Module $module |
||||
136 | * |
||||
137 | * @return \Illuminate\Http\RedirectResponse |
||||
138 | */ |
||||
139 | public function delete(Module $module) |
||||
140 | { |
||||
141 | $this->authorize('delete', $eloquent = app('scaffold.model')); |
||||
142 | |||||
143 | $id = $eloquent->id; |
||||
144 | |||||
145 | try { |
||||
146 | $module->actionsManager()->exec('delete', [$eloquent]); |
||||
147 | } catch (\Exception $e) { |
||||
148 | return back()->withErrors([$e->getMessage()]); |
||||
149 | } |
||||
150 | |||||
151 | $message = trans('administrator::messages.remove_success'); |
||||
0 ignored issues
–
show
The function
trans was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
152 | |||||
153 | if (URL::previous() === route('scaffold.view', ['module' => $module, 'id' => $id])) { |
||||
154 | return back()->with('messages', [$message]); |
||||
155 | } |
||||
156 | |||||
157 | return redirect()->to(route('scaffold.index', ['module' => $module]))->with('messages', [$message]); |
||||
158 | } |
||||
159 | |||||
160 | /** |
||||
161 | * Destroy attachment. |
||||
162 | * |
||||
163 | * @param $page |
||||
164 | * @param $id |
||||
165 | * @param $attachment |
||||
166 | * |
||||
167 | * @return \Illuminate\Http\RedirectResponse |
||||
168 | */ |
||||
169 | public function deleteAttachment($page, $id, $attachment) |
||||
170 | { |
||||
171 | /** @var Module $resource */ |
||||
172 | $resource = app('scaffold.module'); |
||||
173 | |||||
174 | $this->authorize('update', $eloquent = app('scaffold.model')); |
||||
175 | |||||
176 | try { |
||||
177 | $resource->actionsManager()->exec('detachFile', [$eloquent, $attachment]); |
||||
178 | } catch (\Exception $e) { |
||||
179 | return back()->withErrors([$e->getMessage()]); |
||||
180 | } |
||||
181 | |||||
182 | |||||
183 | return back()->with('messages', [trans('administrator::messages.remove_success')]); |
||||
0 ignored issues
–
show
The function
trans was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
184 | } |
||||
185 | |||||
186 | /** |
||||
187 | * @param $module |
||||
188 | * @param $id |
||||
189 | * @param Request $request |
||||
190 | */ |
||||
191 | public function fetchMedia($module, $id, Request $request) |
||||
192 | { |
||||
193 | /** @var Module $resource */ |
||||
194 | $resource = app('scaffold.module'); |
||||
195 | |||||
196 | $this->authorize('view', $eloquent = app('scaffold.model')); |
||||
197 | |||||
198 | $media = MediaLibraryProvider::forModel($eloquent)->fetch( |
||||
199 | $request->get('collection', 'default'), |
||||
200 | 20 |
||||
201 | ); |
||||
202 | |||||
203 | $items = collect($media->items())->map([MediaLibraryProvider::class, 'toJson']); |
||||
204 | |||||
205 | return response()->json(array_merge( |
||||
206 | $media->toArray(), |
||||
207 | ['data' => $items->toArray()] |
||||
208 | )); |
||||
209 | } |
||||
210 | |||||
211 | /** |
||||
212 | * @param $page |
||||
213 | * @param $id |
||||
214 | * @param string $conversion |
||||
215 | * @param Request $request |
||||
216 | * @return RedirectResponse |
||||
217 | */ |
||||
218 | public function attachMedia($page, $id, string $collection, Request $request) |
||||
219 | { |
||||
220 | /** @var Module $resource */ |
||||
221 | $resource = app('scaffold.module'); |
||||
222 | |||||
223 | $this->authorize('update', $eloquent = app('scaffold.model')); |
||||
224 | |||||
225 | $file = $request->file('_media_')[$collection]; |
||||
226 | |||||
227 | $mediaItem = MediaLibraryProvider::forModel($eloquent)->attach($file, $collection); |
||||
228 | |||||
229 | return response()->json(MediaLibraryProvider::toJson($mediaItem)); |
||||
230 | } |
||||
231 | |||||
232 | /** |
||||
233 | * @param $page |
||||
234 | * @param $id |
||||
235 | * @param $mediaId |
||||
236 | */ |
||||
237 | public function detachMedia($page, $id, $mediaId) |
||||
238 | { |
||||
239 | /** @var Module $resource */ |
||||
240 | $resource = app('scaffold.module'); |
||||
241 | |||||
242 | $this->authorize('update', $eloquent = app('scaffold.model')); |
||||
243 | |||||
244 | MediaLibraryProvider::forModel($eloquent)->detach($mediaId); |
||||
245 | |||||
246 | return response()->json([], \Illuminate\Http\Response::HTTP_NO_CONTENT); |
||||
247 | } |
||||
248 | |||||
249 | /** |
||||
250 | * Search for a model(s). |
||||
251 | * |
||||
252 | * @param Request $request |
||||
253 | * |
||||
254 | * @return \Illuminate\Http\JsonResponse |
||||
255 | */ |
||||
256 | public function search(Request $request): \Illuminate\Http\JsonResponse |
||||
257 | { |
||||
258 | $eloquent = $request->get('searchable'); |
||||
259 | $column = $request->get('field'); |
||||
260 | $term = $request->get('query'); |
||||
261 | |||||
262 | $items = []; |
||||
263 | |||||
264 | if ($eloquent && $column) { |
||||
265 | $eloquent = new $eloquent(); |
||||
266 | $searchByKey = is_numeric($term); |
||||
267 | $searchableKey = $searchByKey ? $eloquent->getKeyName() : $column; |
||||
268 | |||||
269 | $instance = $eloquent |
||||
270 | ->when($searchByKey, function ($query) use ($searchableKey, $term) { |
||||
271 | return $query->where($searchableKey, (int)$term); |
||||
272 | }) |
||||
273 | ->when(!$searchByKey, function ($query) use ($searchableKey, $term) { |
||||
274 | return $query->orWhere($searchableKey, 'LIKE', "%{$term}%"); |
||||
275 | }) |
||||
276 | ->get(["{$eloquent->getKeyName()} as id", "{$column} as name"]); |
||||
277 | |||||
278 | $items = $instance->toArray(); |
||||
279 | } |
||||
280 | |||||
281 | return response()->json(['items' => $items]); |
||||
282 | } |
||||
283 | |||||
284 | /** |
||||
285 | * Custom action related to item. |
||||
286 | * |
||||
287 | * @param $page |
||||
288 | * @param $id |
||||
289 | * @param $action |
||||
290 | * |
||||
291 | * @return \Illuminate\Http\RedirectResponse |
||||
292 | */ |
||||
293 | public function action($page, $id, $action) |
||||
294 | { |
||||
295 | /** @var Module $resource */ |
||||
296 | $resource = app('scaffold.module'); |
||||
297 | |||||
298 | $this->authorize($action, $eloquent = app('scaffold.model')); |
||||
299 | |||||
300 | $response = $resource->actionsManager()->exec('action::'.$action, [$eloquent]); |
||||
301 | |||||
302 | if ($response instanceof Response || $response instanceof Renderable) { |
||||
303 | return $response; |
||||
304 | } |
||||
305 | |||||
306 | return back()->with( |
||||
0 ignored issues
–
show
The function
back was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
307 | 'messages', |
||||
308 | [trans('administrator::messages.action_success')] |
||||
0 ignored issues
–
show
The function
trans was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
309 | ); |
||||
310 | } |
||||
311 | } |
||||
312 |