156 lines
4.9 KiB
PHP
156 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Enums\DansLesPresType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Bo\StoreDansLesPresContentRequest;
|
|
use App\Http\Requests\Bo\UpdateDansLesPresContentRequest;
|
|
use App\Models\DansLesPresContent;
|
|
use App\Traits\UpdateModelStatus;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DansLesPresController extends Controller
|
|
{
|
|
use UpdateModelStatus;
|
|
|
|
/**
|
|
* Viewany danslespres
|
|
*
|
|
* @param \App\Enums\DansLesPresType $type
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function index(DansLesPresType $type)
|
|
{
|
|
$contenu = DansLesPresContent::where('type', $type)
|
|
->orderBy('order', 'asc')
|
|
->paginate();
|
|
return view('bo.danslespres.index')
|
|
->with('contenu', $contenu)
|
|
->with('type', $type);
|
|
}
|
|
|
|
/**
|
|
* Create danslespres
|
|
*
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('bo.danslespres.create');
|
|
}
|
|
|
|
/**
|
|
* Store danslespres
|
|
*
|
|
* @param \App\Http\Requests\Bo\StoreDansLesPresContentRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(StoreDansLesPresContentRequest $request)
|
|
{
|
|
$danslesprescontent = new DansLesPresContent($request->validated());
|
|
$danslesprescontent->order = $this->getLastOrder($danslesprescontent->type);
|
|
$this->storeUploadedImage($request, $danslesprescontent, 'picture');
|
|
$danslesprescontent->saveOrFail();
|
|
|
|
return redirect()->route('bo.dans_les_pres.content', $danslesprescontent->type);
|
|
}
|
|
|
|
/**
|
|
* Edit page
|
|
*
|
|
* @param \App\Models\DansLesPresContent $danslesprescontent
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
*/
|
|
public function edit(DansLesPresContent $danslesprescontent)
|
|
{
|
|
return view('bo.danslespres.edit')
|
|
->with('danslesprescontent', $danslesprescontent);
|
|
}
|
|
|
|
/**
|
|
* Update ocb
|
|
*
|
|
* @param \App\Http\Requests\Bo\UpdateDansLesPresContentRequest $request
|
|
* @param \App\Models\DansLesPresContent $danslesprescontent
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(UpdateDansLesPresContentRequest $request, DansLesPresContent $danslesprescontent)
|
|
{
|
|
$danslesprescontent->fill($request->validated());
|
|
if ($request->picture !== null) {
|
|
$this->removeUploadedImage($danslesprescontent, 'picture');
|
|
$this->storeUploadedImage($request, $danslesprescontent, 'picture');
|
|
}
|
|
$danslesprescontent->saveOrFail();
|
|
|
|
return redirect()->route('bo.dans_les_pres.edit', $danslesprescontent->id)
|
|
->with('success', trans('Enregistré !'));
|
|
}
|
|
|
|
/**
|
|
* Destroy ocb content
|
|
*
|
|
* @param \App\Models\DansLesPresContent $danslesprescontent
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy(DansLesPresContent $danslesprescontent)
|
|
{
|
|
$this->removeUploadedImage($danslesprescontent, 'picture');
|
|
$danslesprescontent->delete();
|
|
|
|
return redirect()->route('bo.dans_les_pres.content', $danslesprescontent->type)
|
|
->with('warning', trans('Le contenu à bien été supprimé'));
|
|
}
|
|
|
|
/**
|
|
* Change ocb order
|
|
*
|
|
* @param \App\Models\DansLesPresContent $current
|
|
* @param mixed $action
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function changeOrder(DansLesPresContent $current, $action)
|
|
{
|
|
if ($action === 'up') {
|
|
$tmp = DansLesPresContent::where([
|
|
['order', '<', $current->order],
|
|
['type', $current->type],
|
|
])->orderBy('order', 'DESC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $current->order;
|
|
$current->order = $newOrder;
|
|
} else {
|
|
$tmp = DansLesPresContent::where([
|
|
['order', '>', $current->order],
|
|
['type', $current->type],
|
|
])->orderBy('order', 'ASC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $current->order;
|
|
$current->order = $newOrder;
|
|
}
|
|
DB::transaction(function () use ($tmp, $current) {
|
|
$tmp->saveOrFail();
|
|
$current->saveOrFail();
|
|
});
|
|
|
|
return redirect()->back();
|
|
}
|
|
|
|
/**
|
|
* Get Last order
|
|
*
|
|
* @param \App\Enums\DansLesPresType $type
|
|
* @return integer
|
|
*/
|
|
private function getLastOrder(DansLesPresType $type): int
|
|
{
|
|
$order = DansLesPresContent::select('order')
|
|
->where('type', $type)
|
|
->orderBy('order', 'DESC')->first();
|
|
if ($order === null) {
|
|
return 1;
|
|
}
|
|
return $order->order + 1;
|
|
}
|
|
}
|