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

149 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\StoreWhoareweBrandRequest;
use App\Models\WhoareweBrand;
use Illuminate\Support\Facades\DB;
class WhoareweBrandsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
{
$whoarewebrands = WhoareweBrand::orderBy('order', 'ASC')->get();
return view('back.whoarewe_brands.index', compact('whoarewebrands'));
}
/**
* Show the form for creating a new resource.
*
* @param WhoareweBrand $whoarewebrand
* @return \Illuminate\Contracts\View\View
* @ignore phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
public function create(WhoareweBrand $whoarewebrand): \Illuminate\Contracts\View\View
{
return view('back.whoarewe_brands.create', compact('whoarewebrand'));
}
/**
* Store a newly created resource in storage.
*
* @param StoreWhoareweBrandRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreWhoareweBrandRequest $request): \Illuminate\Http\RedirectResponse
{
return DB::transaction(function () use ($request) {
$whoarewebrand = new WhoareweBrand($request->validated());
$whoarewebrand->order = $this->getLastOrder();
if ($whoarewebrand->saveOrFail()) {
return to_route('bo.whoarewebrands.edit', $whoarewebrand->id)
->with('success', trans(_('Creation_saved')));
}
return back()->with('error', trans(_('Creation_failed')));
});
}
/**
* Show the form for editing the specified resource.
*
* @param WhoareweBrand $whoarewebrand
* @return \Illuminate\Contracts\View\View
*/
public function edit(WhoareweBrand $whoarewebrand): \Illuminate\Contracts\View\View
{
return view('back.whoarewe_brands.edit', compact('whoarewebrand'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\StoreWhoareweBrandRequest $request
* @param WhoareweBrand $whoarewebrand
* @return \Illuminate\Http\RedirectResponse
* @ignore phpcs:disable Generic.Files.LineLength.TooLong
*/
public function update(StoreWhoareweBrandRequest $request, WhoareweBrand $whoarewebrand): \Illuminate\Http\RedirectResponse
{
return DB::transaction(function () use ($request, $whoarewebrand) {
$whoarewebrand->fill($request->validated());
if ($whoarewebrand->saveOrFail()) {
return to_route('bo.whoarewebrands.edit', $whoarewebrand->id)
->with('success', trans(__('Changes_saved')));
}
return to_route('bo.whoarewebrands.edit', $whoarewebrand->id)
->with('error', trans(__('Changes_failed')));
});
}
/**
* Remove the specified resource from storage.
*
* @param WhoareweBrand $whoarewebrand
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(WhoareweBrand $whoarewebrand): \Illuminate\Http\RedirectResponse
{
if ($whoarewebrand->deleteOrFail()) {
return to_route('bo.whoarewebrands.index')->with('success', trans('Suppression faite avec succès'));
}
return to_route('bo.whoarewebrands.index')->with('error', trans('La suppression a échoué'));
}
/**
* Remove the specified resource from storage.
*
* @param UpdateChangeOrderRequest $request
* @param WhoareweBrand $whoarewebrand
* @return \Illuminate\Http\RedirectResponse
*/
public function changeOrder(UpdateChangeOrderRequest $request, WhoareweBrand $whoarewebrand): \Illuminate\Http\RedirectResponse
{
if ($request->validated()['action'] === 'up') {
$tmp = WhoareweBrand::where('order', '<', $whoarewebrand->order)
->orderBy('order', 'DESC')->first();
$newOrder = $tmp->order;
$tmp->order = $whoarewebrand->order;
$whoarewebrand->order = $newOrder;
} elseif ($request->validated()['action'] === 'down') {
$tmp = WhoareweBrand::where('order', '>', $whoarewebrand->order)
->orderBy('order', 'ASC')->first();
$newOrder = $tmp->order;
$tmp->order = $whoarewebrand->order;
$whoarewebrand->order = $newOrder;
} else {
return to_route('bo.whoarewe_brands.index');
}
$tmp->save();
$whoarewebrand->save();
return back();
}
/**
* Remove the specified resource from storage.
*
* @return integer
*/
private function getLastOrder(): int
{
$order = WhoareweBrand::select('order')->orderBy('order', 'DESC')->first();
if ($order === null) {
return 1;
}
return $order->order + 1;
}
}