137 lines
4.5 KiB
PHP
137 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
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('title')->whereNull('parent_gamme_id')
|
|
->with('childGammes')->get();
|
|
|
|
/** @var \App\Models\Gamme $gamme */
|
|
foreach ($dbGammes as $gamme) {
|
|
$gammes->push($gamme);
|
|
$gamme->childGammes->sortBy('title')
|
|
->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());
|
|
|
|
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')));
|
|
}
|
|
}
|