ldc-domaine-de-lanvaux/app/Http/Controllers/Bo/WhoareweExpertisesController.php

153 lines
5.5 KiB
PHP
Raw Permalink Normal View History

2022-08-05 09:38:44 +00:00
<?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;
2022-11-16 10:51:43 +01:00
use Illuminate\Support\Facades\DB;
2022-08-05 09:38:44 +00:00
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('back.whoarewe_expertises.index', compact('whoareweexpertises'));
2022-08-05 09:38:44 +00:00
}
/**
* 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('back.whoarewe_expertises.create', compact('whoareweexpertise'));
2022-08-05 09:38:44 +00:00
}
/**
* Store a newly created resource in storage.
*
* @param StoreWhoareweExpertiseRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreWhoareweExpertiseRequest $request): \Illuminate\Http\RedirectResponse
{
2022-11-16 10:51:43 +01:00
return DB::transaction(function () use ($request) {
$whoareweexpertise = new WhoareweExpertise($request->validated());
$whoareweexpertise->order = $this->getLastOrder();
if ($whoareweexpertise->saveOrFail()) {
2026-10-27 15:12:56 +01:00
return to_route('bo.whoareweexpertises.edit', $whoareweexpertise->id)
2022-11-16 10:51:43 +01:00
->with('success', trans(__('Creation_saved')));
};
return back()->with('error', trans(__('Creation_failed')));
});
2022-08-05 09:38:44 +00:00
}
/**
* 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('back.whoarewe_expertises.edit', compact('whoareweexpertise'));
2022-08-05 09:38:44 +00:00
}
/**
* 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
{
2022-11-16 10:51:43 +01:00
return DB::transaction(function () use ($request, $whoareweexpertise) {
$whoareweexpertise->fill($request->validated());
2022-08-05 09:38:44 +00:00
2022-11-16 10:51:43 +01:00
if ($whoareweexpertise->saveOrFail()) {
2026-10-27 15:12:56 +01:00
return to_route('bo.whoareweexpertises.edit', $whoareweexpertise->id)
2022-11-16 10:51:43 +01:00
->with('success', trans(__('Changes_saved')));
}
2022-08-05 09:38:44 +00:00
2026-10-27 15:12:56 +01:00
return to_route('bo.whoareweexpertises.edit', $whoareweexpertise->id)
2022-11-16 10:51:43 +01:00
->with('error', trans(__('Changes_failed')));
});
2022-08-05 09:38:44 +00:00
}
/**
* Remove the specified resource from storage.
*
* @param WhoareweExpertise $whoareweexpertise
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(WhoareweExpertise $whoareweexpertise): \Illuminate\Http\RedirectResponse
{
2022-11-16 10:51:43 +01:00
if ($whoareweexpertise->deleteOrFail()) {
2026-10-27 15:12:56 +01:00
return to_route('bo.whoareweexpertises.index')
2022-11-16 10:51:43 +01:00
->with('success', trans(__('Delete_saved')));
2022-08-05 09:38:44 +00:00
}
2026-10-27 15:12:56 +01:00
return to_route('bo.whoareweexpertises.index')->with('error', trans(__('Delete_failed')));
2022-08-05 09:38:44 +00:00
}
/**
* 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 {
2026-10-27 15:12:56 +01:00
return to_route('bo.whoarewe_expertises.index');
2022-08-05 09:38:44 +00:00
}
$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;
}
}