184 lines
6.1 KiB
PHP
184 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\UpdateChangeOrderRequest;
|
|
use App\Http\Requests\StoreGammeRequest;
|
|
use App\Http\Requests\UpdateGammeRequest;
|
|
use App\Lib\ToolboxHelper;
|
|
use App\Models\Gamme;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class GammesController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(): \Illuminate\Contracts\View\View
|
|
{
|
|
$gammes = collect([]);
|
|
$dbGammes = Gamme::orderBy('order')->whereNull('parent_gamme_id')
|
|
->with('childGammes')->get();
|
|
|
|
/** @var \App\Models\Gamme $gamme */
|
|
foreach ($dbGammes as $gamme) {
|
|
$gammes->push($gamme);
|
|
$gamme->childGammes->sortBy('order')
|
|
->map(function (Gamme $childGamme) use (&$gammes) {
|
|
$gammes->push($childGamme);
|
|
});
|
|
}
|
|
|
|
return view('back.gammes.index', compact('gammes'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param Gamme $gamme
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create(Gamme $gamme): \Illuminate\Contracts\View\View
|
|
{
|
|
$parentGammes = Gamme::whereNull('parent_gamme_id')
|
|
->where('id', '!=', $gamme->id)
|
|
->orderBy('created_at', 'DESC')->get();
|
|
|
|
return view('back.gammes.create', compact('gamme', 'parentGammes'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param StoreGammeRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreGammeRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request) {
|
|
$gamme = new Gamme();
|
|
$gamme->fill($request->validated());
|
|
$gamme->order = $this->getLastOrder();
|
|
|
|
if (is_a($request->picture, "SplFileInfo")) {
|
|
$gamme->picture = ToolboxHelper::storeImage($request->picture, $gamme->getTable());
|
|
}
|
|
if (is_a($request->banner, "SplFileInfo")) {
|
|
$gamme->banner = ToolboxHelper::storeImage($request->banner, $gamme->getTable());
|
|
}
|
|
if ($gamme->parent_gamme_id) {
|
|
$gamme->category = null;
|
|
}
|
|
if ($gamme->saveOrFail()) {
|
|
return redirect()->route('bo.gammes.edit', $gamme->id)->with('success', trans(__('Changes_saved')));
|
|
}
|
|
return back()->with('error', trans(__('Changes_failed')));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param Gamme $gamme
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit(Gamme $gamme): \Illuminate\Contracts\View\View
|
|
{
|
|
$parentGammes = Gamme::whereNull('parent_gamme_id')
|
|
->where('id', '!=', $gamme->id)
|
|
->orderBy('created_at', 'DESC')->get();
|
|
|
|
return view('back.gammes.edit', compact('gamme', 'parentGammes'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param Gamme $gamme
|
|
* @param UpdateGammeRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(Gamme $gamme, UpdateGammeRequest $request): \Illuminate\Http\RedirectResponse
|
|
{
|
|
return DB::transaction(function () use ($request, $gamme) {
|
|
$gamme->fill($request->validated());
|
|
|
|
if (is_a($request->picture, "SplFileInfo")) {
|
|
$gamme->picture = ToolboxHelper::storeImage($request->picture, $gamme->getTable());
|
|
}
|
|
if (is_a($request->banner, "SplFileInfo")) {
|
|
$gamme->banner = ToolboxHelper::storeImage($request->banner, $gamme->getTable());
|
|
}
|
|
if ($gamme->parent_gamme_id) {
|
|
$gamme->category = null;
|
|
}
|
|
if ($gamme->saveOrFail()) {
|
|
return redirect()->route('bo.gammes.edit', $gamme->id)
|
|
->with('success', trans(__('Changes_saved')));
|
|
}
|
|
return back()->with('error', trans(__('Changes_failed')));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param Gamme $gamme
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(Gamme $gamme): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if ($gamme->deleteOrFail()) {
|
|
return redirect()->route('bo.gammes.index')->with('success', trans(__('Delete_saved')));
|
|
}
|
|
return back()->with('error', trans(__('Delete_failed')));
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param UpdateChangeOrderRequest $request
|
|
* @param Gamme $gamme
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
* @ignore phpcs:disable Generic.Files.LineLength.TooLong
|
|
*/
|
|
public function changeOrder(UpdateChangeOrderRequest $request, Gamme $gamme): \Illuminate\Http\RedirectResponse
|
|
{
|
|
if ($request->validated()['action'] === 'up') {
|
|
$tmp = Gamme::where('order', '<', $gamme->order)
|
|
->orderBy('order', 'DESC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $gamme->order;
|
|
$gamme->order = $newOrder;
|
|
} elseif ($request->validated()['action'] === 'down') {
|
|
$tmp = Gamme::where('order', '>', $gamme->order)
|
|
->orderBy('order', 'ASC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $gamme->order;
|
|
$gamme->order = $newOrder;
|
|
} else {
|
|
return redirect()->route('bo.homepage_slides.index');
|
|
}
|
|
$tmp->save();
|
|
$gamme->save();
|
|
|
|
return back();
|
|
}
|
|
|
|
/**
|
|
* Get by order the last element of the list.
|
|
*
|
|
* @return integer
|
|
*/
|
|
private function getLastOrder(): int
|
|
{
|
|
$order = Gamme::select('order')->orderBy('order', 'DESC')->first();
|
|
if ($order === null) {
|
|
return 1;
|
|
}
|
|
return $order->order + 1;
|
|
}
|
|
}
|