152 lines
5.5 KiB
PHP
152 lines
5.5 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;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
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'));
|
|
}
|
|
|
|
/**
|
|
* 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'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param StoreWhoareweExpertiseRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreWhoareweExpertiseRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$whoareweexpertise = new WhoareweExpertise($request->validated());
|
|
$whoareweexpertise->order = $this->getLastOrder();
|
|
|
|
if ($whoareweexpertise->saveOrFail()) {
|
|
return to_route('bo.whoareweexpertises.edit', $whoareweexpertise->id)
|
|
->with('success', trans(__('Creation_saved')));
|
|
};
|
|
return back()->with('error', trans(__('Creation_failed')));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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'));
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
{
|
|
return DB::transaction(function () use ($request, $whoareweexpertise) {
|
|
$whoareweexpertise->fill($request->validated());
|
|
|
|
if ($whoareweexpertise->saveOrFail()) {
|
|
return to_route('bo.whoareweexpertises.edit', $whoareweexpertise->id)
|
|
->with('success', trans(__('Changes_saved')));
|
|
}
|
|
|
|
return to_route('bo.whoareweexpertises.edit', $whoareweexpertise->id)
|
|
->with('error', trans(__('Changes_failed')));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param WhoareweExpertise $whoareweexpertise
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(WhoareweExpertise $whoareweexpertise): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if ($whoareweexpertise->deleteOrFail()) {
|
|
return to_route('bo.whoareweexpertises.index')
|
|
->with('success', trans(__('Delete_saved')));
|
|
}
|
|
|
|
return to_route('bo.whoareweexpertises.index')->with('error', trans(__('Delete_failed')));
|
|
}
|
|
|
|
/**
|
|
* 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 to_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;
|
|
}
|
|
}
|