ldc-domaine-de-lanvaux/app/Http/Controllers/Bo/WhoareweIconsController.php
2026-10-27 15:12:56 +01:00

151 lines
5.2 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\UpdateChangeOrderRequest;
use App\Http\Requests\StoreWhoareweIconRequest;
use App\Models\WhoareweIcon;
use Illuminate\Support\Facades\DB;
class WhoareweIconsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
{
$whoareweicons = WhoareweIcon::orderBy('order', 'ASC')->get();
return view('back.whoarewe_icons.index', compact('whoareweicons'));
}
/**
* Show the form for creating a new resource.
*
* @param WhoareweIcon $whoareweicon
* @return \Illuminate\Contracts\View\View
* @ignore phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
public function create(WhoareweIcon $whoareweicon): \Illuminate\Contracts\View\View
{
return view('back.whoarewe_icons.create', compact('whoareweicon'));
}
/**
* Store a newly created resource in storage.
*
* @param StoreWhoareweIconRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreWhoareweIconRequest $request): \Illuminate\Http\RedirectResponse
{
return DB::transaction(function () use ($request) {
$whoareweicon = new WhoareweIcon($request->validated());
$whoareweicon->order = $this->getLastOrder();
if ($whoareweicon->saveOrFail()) {
return to_route('bo.whoareweicons.edit', $whoareweicon->id)
->with('success', trans(__('Creation_saved')));
}
return back()->with('success', trans(__('Creation_failed')));
});
}
/**
* Show the form for editing the specified resource.
*
* @param WhoareweIcon $whoareweicon
* @return \Illuminate\Contracts\View\View
* @ignore phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
public function edit(WhoareweIcon $whoareweicon): \Illuminate\Contracts\View\View
{
return view('back.whoarewe_icons.edit', compact('whoareweicon'));
}
/**
* Update the specified resource in storage.
*
* @param StoreWhoareweIconRequest $request
* @param WhoareweIcon $whoareweicon
* @return \Illuminate\Http\RedirectResponse
* @ignore phpcs:disable Generic.Files.LineLength.TooLong
*/
public function update(StoreWhoareweIconRequest $request, WhoareweIcon $whoareweicon): \Illuminate\Http\RedirectResponse
{
return DB::transaction(function () use ($request, $whoareweicon) {
$whoareweicon->fill($request->validated());
if ($whoareweicon->saveOrFail()) {
return to_route('bo.whoareweicons.edit', $whoareweicon->id)
->with('success', trans(__('Changes_saved')));
}
return to_route('bo.whoareweicons.edit', $whoareweicon->id)
->with('error', trans(__('Changes_failed')));
});
}
/**
* Remove the specified resource from storage.
*
* @param WhoareweIcon $whoareweicon
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(WhoareweIcon $whoareweicon): \Illuminate\Http\RedirectResponse
{
if ($whoareweicon->deleteOrFail()) {
return to_route('bo.whoareweicons.index')->with('success', trans(__('Delete_saved')));
}
return to_route('bo.whoareweicons.index')->with('error', trans(__('Delete_failed')));
}
/**
* Remove the specified resource from storage.
*
* @param UpdateChangeOrderRequest $request
* @param WhoareweIcon $whoareweicon
* @return \Illuminate\Http\RedirectResponse
* @ignore phpcs:disable Generic.Files.LineLength.TooLong
*/
public function changeOrder(UpdateChangeOrderRequest $request, WhoareweIcon $whoareweicon): \Illuminate\Http\RedirectResponse
{
if ($request->validated()['action'] === 'up') {
$tmp = WhoareweIcon::where('order', '<', $whoareweicon->order)
->orderBy('order', 'DESC')->first();
$newOrder = $tmp->order;
$tmp->order = $whoareweicon->order;
$whoareweicon->order = $newOrder;
} elseif ($request->validated()['action'] === 'down') {
$tmp = WhoareweIcon::where('order', '>', $whoareweicon->order)
->orderBy('order', 'ASC')->first();
$newOrder = $tmp->order;
$tmp->order = $whoareweicon->order;
$whoareweicon->order = $newOrder;
} else {
return to_route('bo.whoarewe_icons.index');
}
$tmp->save();
$whoareweicon->save();
return back();
}
/**
* Get by order the last element of the list.
*
* @return integer
*/
private function getLastOrder(): int
{
$order = WhoareweIcon::select('order')->orderBy('order', 'DESC')->first();
if ($order === null) {
return 1;
}
return $order->order + 1;
}
}