2020-09-21 11:03:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2021-09-23 15:14:14 +02:00
|
|
|
use App\Http\Requests\StoreRange;
|
|
|
|
|
use App\Http\Requests\UpdateRange;
|
2020-10-07 16:46:58 +02:00
|
|
|
use App\Models\Products;
|
2021-09-21 09:26:21 +02:00
|
|
|
use App\Models\Range;
|
2021-09-24 00:18:02 +02:00
|
|
|
use App\Models\SubRange;
|
2021-04-28 13:57:12 +02:00
|
|
|
use App\Traits\UpdateModelStatus;
|
2020-09-21 11:03:50 +02:00
|
|
|
|
2021-09-21 09:26:21 +02:00
|
|
|
class RangeController extends Controller
|
2020-09-21 11:03:50 +02:00
|
|
|
{
|
2021-04-28 13:57:12 +02:00
|
|
|
use UpdateModelStatus;
|
|
|
|
|
|
2020-09-21 11:03:50 +02:00
|
|
|
public function index()
|
|
|
|
|
{
|
2021-09-21 09:26:21 +02:00
|
|
|
$r = Range::orderBy('order', 'asc')->paginate();
|
2020-09-21 11:03:50 +02:00
|
|
|
return view('bo.ranges.index', compact('r'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
|
|
|
|
return view('bo.ranges.create');
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 15:14:14 +02:00
|
|
|
public function store(StoreRange $request)
|
2020-09-21 11:03:50 +02:00
|
|
|
{
|
2021-09-23 15:14:14 +02:00
|
|
|
$range = new Range($request->validated());
|
|
|
|
|
$this->storeUploadedImage($request, $range, 'picture');
|
|
|
|
|
$this->storeUploadedImage($request, $range, 'picture_r');
|
|
|
|
|
$range->order = $this->getLastOrder();
|
|
|
|
|
$range->save();
|
|
|
|
|
return redirect()->route('bo.ranges.edit', $range)
|
|
|
|
|
->with('success', 'Gamme enregistrée');
|
2020-09-21 11:03:50 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-23 15:14:14 +02:00
|
|
|
public function edit(Range $range)
|
2020-09-21 11:03:50 +02:00
|
|
|
{
|
2021-09-24 00:18:02 +02:00
|
|
|
$productSubRanges = $range->subRanges()->orderBy('order')->get();
|
|
|
|
|
$subranges = SubRange::whereNotIn('id', $productSubRanges->pluck('id'))->get();
|
2021-09-23 15:14:14 +02:00
|
|
|
$products = Products::where('range_id', $range->id)->get();
|
2021-09-24 00:18:02 +02:00
|
|
|
return view('bo.ranges.edit', compact('range', 'products', 'subranges', 'productSubRanges'));
|
2020-09-21 11:03:50 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-23 15:14:14 +02:00
|
|
|
public function update(UpdateRange $request, Range $range)
|
2020-09-21 11:03:50 +02:00
|
|
|
{
|
2021-09-23 15:14:14 +02:00
|
|
|
$range->fill($request->validated());
|
|
|
|
|
$this->storeUploadedImage($request, $range, 'picture');
|
|
|
|
|
$this->storeUploadedImage($request, $range, 'picture_r');
|
2020-09-21 11:03:50 +02:00
|
|
|
|
2021-09-23 15:14:14 +02:00
|
|
|
if (empty($request->flagship_product) && $request->status == true) {
|
2021-01-22 11:43:17 +01:00
|
|
|
return back()->withErrors('Une gamme doit avoir un produit phare pour pouvoir être publiée.');
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2021-01-22 11:43:17 +01:00
|
|
|
|
2021-09-23 15:14:14 +02:00
|
|
|
$range->saveOrFail();
|
2020-09-21 11:03:50 +02:00
|
|
|
|
2021-09-24 00:18:02 +02:00
|
|
|
// subranges handle
|
|
|
|
|
$i = 0;
|
|
|
|
|
$range->subRanges()->sync(collect($request->subranges)->map(function ($v) {
|
|
|
|
|
return \intval($v);
|
|
|
|
|
})->flip()
|
|
|
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
|
|
|
->mapWithKeys(function ($value, $key) use (&$i) {
|
|
|
|
|
return [
|
|
|
|
|
$key => [
|
|
|
|
|
'order' => $i++
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}));
|
|
|
|
|
|
2021-09-23 15:14:14 +02:00
|
|
|
return redirect()->route('bo.ranges.edit', $range)
|
|
|
|
|
->with('success', 'Gamme enregistrée');
|
2020-09-21 11:03:50 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 09:26:21 +02:00
|
|
|
public function destroy(Range $r)
|
2020-09-21 11:03:50 +02:00
|
|
|
{
|
2021-05-04 12:56:35 +02:00
|
|
|
if (file_exists(public_path() . '/' . $r->picture)) {
|
|
|
|
|
unlink(public_path() . '/' . $r->picture);
|
|
|
|
|
}
|
2020-10-07 16:46:58 +02:00
|
|
|
|
2021-05-04 12:56:35 +02:00
|
|
|
if (file_exists(public_path() . '/' . $r->picture_r)) {
|
|
|
|
|
unlink(public_path() . '/' . $r->picture_r);
|
2020-10-07 16:46:58 +02:00
|
|
|
}
|
2020-09-21 11:03:50 +02:00
|
|
|
|
2021-05-04 12:56:35 +02:00
|
|
|
$r->delete();
|
|
|
|
|
|
2021-09-18 11:13:57 +02:00
|
|
|
return redirect()->route('bo.ranges.index')
|
|
|
|
|
->with('warning', 'Le contenu à bien été supprimé');
|
2020-09-21 11:03:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function changeOrder($id, $action)
|
|
|
|
|
{
|
2021-09-21 09:26:21 +02:00
|
|
|
$current = Range::findOrFail($id);
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($action === 'up') {
|
2021-09-21 09:26:21 +02:00
|
|
|
$tmp = Range::where('order', '<', $current->order)
|
2021-05-04 12:56:35 +02:00
|
|
|
->orderBy('order', 'DESC')->first();
|
2020-10-07 16:46:58 +02:00
|
|
|
$newOrder = $tmp->order;
|
|
|
|
|
$tmp->order = $current->order;
|
|
|
|
|
$current->order = $newOrder;
|
2021-03-20 14:38:15 +01:00
|
|
|
} else {
|
2021-09-21 09:26:21 +02:00
|
|
|
$tmp = Range::where('order', '>', $current->order)
|
2021-05-04 12:56:35 +02:00
|
|
|
->orderBy('order', 'ASC')->first();
|
2020-10-07 16:46:58 +02:00
|
|
|
$newOrder = $tmp->order;
|
|
|
|
|
$tmp->order = $current->order;
|
|
|
|
|
$current->order = $newOrder;
|
2020-09-21 11:03:50 +02:00
|
|
|
}
|
|
|
|
|
$tmp->save();
|
|
|
|
|
$current->save();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.ranges.index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getLastOrder()
|
|
|
|
|
{
|
2021-09-21 09:26:21 +02:00
|
|
|
$order = Range::select('order')->orderBy('order', 'DESC')->first();
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($order === null) {
|
2020-09-21 11:03:50 +02:00
|
|
|
return 1;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-09-21 11:03:50 +02:00
|
|
|
return $order->order + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|