ldc-domaine-de-lanvaux/app/Http/Controllers/Bo/WhoareweExpertisesController.php
Alexis Gousseau 4c39db12d7 Refonte
2022-08-05 09:38:44 +00:00

147 lines
5.3 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\UpdateChangeOrderRequest;
use App\Http\Requests\StoreWhoareweExpertiseRequest;
use App\Models\WhoareweExpertise;
class WhoareweExpertisesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
{
$whoareweexpertises = WhoareweExpertise::orderBy('order', 'ASC')->get();
return view('bo.whoarewe_expertises.index', compact('whoareweexpertises'));
}
/**
* Show the form for creating a new resource.
*
* @param WhoareweExpertise $whoareweexpertise
* @return \Illuminate\Contracts\View\View
* @ignore phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
public function create(WhoareweExpertise $whoareweexpertise): \Illuminate\Contracts\View\View
{
return view('bo.whoarewe_expertises.create', compact('whoareweexpertise'));
}
/**
* Store a newly created resource in storage.
*
* @param StoreWhoareweExpertiseRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreWhoareweExpertiseRequest $request): \Illuminate\Http\RedirectResponse
{
$whoareweexpertise = new WhoareweExpertise($request->validated());
$whoareweexpertise->order = $this->getLastOrder();
$whoareweexpertise->saveOrFail();
return redirect()->route('bo.whoareweexpertises.edit', $whoareweexpertise->id)
->with('success', 'Pastille créée !');
}
/**
* Show the form for editing the specified resource.
*
* @param WhoareweExpertise $whoareweexpertise
* @return \Illuminate\Contracts\View\View
* @ignore phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
*/
public function edit(WhoareweExpertise $whoareweexpertise): \Illuminate\Contracts\View\View
{
return view('bo.whoarewe_expertises.edit', compact('whoareweexpertise'));
}
/**
* Update the specified resource in storage.
*
* @param StoreWhoareweExpertiseRequest $request
* @param WhoareweExpertise $whoareweexpertise
* @return \Illuminate\Http\RedirectResponse
* @ignore phpcs:disable Generic.Files.LineLength.TooLong
*/
public function update(StoreWhoareweExpertiseRequest $request, WhoareweExpertise $whoareweexpertise): \Illuminate\Http\RedirectResponse
{
$whoareweexpertise->fill($request->validated());
if (!$whoareweexpertise->saveOrFail()) {
return redirect()->route('bo.whoareweexpertises.edit', $whoareweexpertise->id)
->with('error', trans(__('Modification_failed')));
}
return redirect()->route('bo.whoareweexpertises.edit', $whoareweexpertise->id)
->with('success', trans(__('Changes_saved')));
}
/**
* Remove the specified resource from storage.
*
* @param WhoareweExpertise $whoareweexpertise
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(WhoareweExpertise $whoareweexpertise): \Illuminate\Http\RedirectResponse
{
if (!$whoareweexpertise->delete()) {
return redirect()->route('bo.whoareweexpertises.index')->with('error', trans('La suppression a échoué'));
}
return redirect()->route('bo.whoareweexpertises.index')
->with('success', trans('Suppression faite avec succès'));
}
/**
* Remove the specified resource from storage.
*
* @param UpdateChangeOrderRequest $request
* @param WhoareweExpertise $whoareweexpertise
* @return \Illuminate\Http\RedirectResponse
* @ignore phpcs:disable Generic.Files.LineLength.TooLong
*/
public function changeOrder(UpdateChangeOrderRequest $request, WhoareweExpertise $whoareweexpertise): \Illuminate\Http\RedirectResponse
{
if ($request->validated()['action'] === 'up') {
$tmp = WhoareweExpertise::where('order', '<', $whoareweexpertise->order)
->orderBy('order', 'DESC')->first();
$newOrder = $tmp->order;
$tmp->order = $whoareweexpertise->order;
$whoareweexpertise->order = $newOrder;
} elseif ($request->validated()['action'] === 'down') {
$tmp = WhoareweExpertise::where('order', '>', $whoareweexpertise->order)
->orderBy('order', 'ASC')->first();
$newOrder = $tmp->order;
$tmp->order = $whoareweexpertise->order;
$whoareweexpertise->order = $newOrder;
} else {
return redirect()->route('bo.whoarewe_expertises.index');
}
$tmp->save();
$whoareweexpertise->save();
return back();
}
/**
* Get by order the last element of the list.
*
* @return integer
*/
private function getLastOrder(): int
{
$order = WhoareweExpertise::select('order')->orderBy('order', 'DESC')->first();
if ($order === null) {
return 1;
}
return $order->order + 1;
}
}