authorizeResource(Picto::class); } public function index(Request $request) { if ($request->ajax() && $request->has('section_id')) { $allowedSorts = ['title', 'description', 'updated_at']; $query = Picto::where('section_id', $request->section_id); $sortCol = in_array($request->sort_col, $allowedSorts) ? $request->sort_col : 'order'; $sortWay = in_array($request->sort_way, ['asc', 'desc']) ? $request->sort_way : 'asc'; $query->orderBy($sortCol, $sortWay); $pictos = $query->get(); return view('bo.pages.pictos.index-list', compact('pictos')); } abort(404); } public function create(Picto $picto) { return view('bo.pages.pictos.create', [ 'pictoModel' => $picto, ]); } /** * Store Picto. * * @param StorePictoRequest $request Request * @param PictoService $pictoService Service for Picto. * @return RedirectResponse * * @throws DdException */ public function store(StorePictoRequest $request, PictoService $pictoService) { $result = $pictoService->store($request->validated()); if (is_string($result)) { return response()->json([ 'success' => false, 'message' => $result, ], 422); } return response()->json([ 'success' => true, 'message' => __( 'crud.messages.has_been_updated', ['model' => Str::ucfirst(trans_choice('models.classes.picto', 1))] ), 'data' => $result, ], 201); } /** * Show the form for editing the specified picto (AJAX returns a partial view). * * @return \Illuminate\Contracts\View\View */ public function edit(Picto $picto, Request $request) { if ($request->ajax()) { return view('bo.pages.pictos.edit', [ 'pictoModel' => $picto, 'section' => $picto->section, ]); } return view('bo.pages.pictos.edit', ['pictoModel' => $picto, 'section' => $picto->section]); } /** * Update the specified picto in storage. * * @return \Illuminate\Http\JsonResponse */ public function update(StorePictoRequest $request, Picto $picto) { $result = $this->pictoService->update($picto, $request->validated()); if (is_string($result)) { return response()->json([ 'success' => false, 'message' => $result, ], 422); } return response()->json([ 'success' => true, 'message' => __( 'crud.messages.has_been_updated', ['model' => Str::ucfirst(trans_choice('models.classes.picto', 1))] ), 'data' => $result, ]); } /** * Save the picture width and height for a picto. */ public function savePictureSize(Request $request): JsonResponse { $request->validate([ 'picture_width' => 'required|integer|min:1', 'picture_height' => 'required|integer|min:1', ]); if ($request->has('picto_id') && $request->picto_id) { $picto = Picto::findOrFail($request->picto_id); $picto->update([ 'picture_width' => $request->picture_width, 'picture_height' => $request->picture_height, ]); } return response()->json(['success' => true]); } /** * Remove the specified picto and return the refreshed picto list. * * @return \Illuminate\Http\JsonResponse */ public function destroy(Picto $picto) { $picto->delete(); $pictos = Picto::with(['section.page'])->get(); return response()->json([ 'success' => true, 'message' => __('crud.messages.has_been_deleted', ['model' => Str::ucfirst(trans_choice('models.classes.picto', 1))]), 'pictos' => $pictos, ]); } public function changeOrder(UpdateChangeOrderRequest $request, Picto $picto): JsonResponse | RedirectResponse { $tmp = Picto::query() ->where('section_id', $picto->section_id) ->where('order', $request->direction ? '<' : '>', $picto->order) ->orderBy('order', $request->direction ? 'DESC' : 'ASC') ->first(); if (! $tmp) { return $request->ajax() ? response()->json(['message' => trans('crud.messages.order_not_changed')], 422) : back()->with('error', trans('crud.messages.order_not_changed')); } DB::transaction(function () use ($picto, $tmp) { $newOrder = $tmp->order; $tmp->update(['order' => $picto->order]); $picto->update(['order' => $newOrder]); }); $msg = trans('crud.messages.order_changed'); return $request->ajax() ? response()->json(['message' => $msg]) : back()->with('success', $msg); } }