2020-11-09 11:03:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\OldSlug;
|
2021-03-17 09:28:02 +01:00
|
|
|
use App\Models\Product;
|
|
|
|
|
use App\Models\Range;
|
2020-11-09 11:03:13 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class RangesController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
2021-03-17 09:28:02 +01:00
|
|
|
$ranges = Range::all();
|
2020-11-09 11:03:13 +01:00
|
|
|
return view('bo.ranges.index', compact('ranges'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
|
|
|
|
return view('bo.ranges.create');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
2022-08-25 12:26:43 +02:00
|
|
|
$name = $request->name;
|
|
|
|
|
$slug = Str::slug($name);
|
2020-11-09 11:03:13 +01:00
|
|
|
$description = $request->description;
|
2022-08-25 12:26:43 +02:00
|
|
|
$picture = $request->picture;
|
|
|
|
|
$status = $request->status === null ? 0 : 1;
|
2020-11-09 11:03:13 +01:00
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
|
'description' => 'required',
|
2021-04-20 11:40:17 +02:00
|
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=' .
|
|
|
|
|
config('theme.range_image_maxwidth') . ',max-height=' .
|
|
|
|
|
config('theme.range_image_maxheight'),
|
2020-11-09 11:03:13 +01:00
|
|
|
]);
|
|
|
|
|
|
2022-08-25 12:26:43 +02:00
|
|
|
$range = new Range();
|
|
|
|
|
$range->name = $name;
|
|
|
|
|
$range->slug = $slug;
|
2020-11-09 11:03:13 +01:00
|
|
|
$range->description = $description;
|
2022-08-25 12:26:43 +02:00
|
|
|
$range->status = $status;
|
2020-11-09 11:03:13 +01:00
|
|
|
$range->save();
|
|
|
|
|
|
|
|
|
|
if ($picture !== null) {
|
2021-04-16 09:00:36 +02:00
|
|
|
$imageName = $range->id . '_' . $slug . '_' . Str::random(5) . '.' . $picture->extension();
|
2020-11-09 11:03:13 +01:00
|
|
|
$picture->move(public_path('images/gammes'), $imageName);
|
|
|
|
|
$range->picture = $imageName;
|
|
|
|
|
$range->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.ranges.index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit($id)
|
|
|
|
|
{
|
2021-03-17 09:28:02 +01:00
|
|
|
$range = Range::findOrFail($id);
|
2020-11-09 11:03:13 +01:00
|
|
|
return view('bo.ranges.edit', compact('range'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
|
{
|
2022-08-25 12:26:43 +02:00
|
|
|
$name = $request->name;
|
|
|
|
|
$slug = Str::slug($name);
|
2020-11-09 11:03:13 +01:00
|
|
|
$description = $request->description;
|
2022-08-25 12:26:43 +02:00
|
|
|
$picture = $request->picture;
|
|
|
|
|
$status = $request->status === null ? 0 : 1;
|
2020-11-09 11:03:13 +01:00
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
|
'description' => 'required',
|
2021-04-20 11:40:17 +02:00
|
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=' .
|
|
|
|
|
config('theme.range_image_maxwidth') . ',max-height=' .
|
|
|
|
|
config('theme.range_image_maxheight'),
|
2020-11-09 11:03:13 +01:00
|
|
|
]);
|
|
|
|
|
|
2021-03-17 09:28:02 +01:00
|
|
|
$range = Range::findOrFail($id);
|
2020-11-09 11:03:13 +01:00
|
|
|
|
|
|
|
|
if ($picture !== null) {
|
2022-08-25 12:26:43 +02:00
|
|
|
if ($range->picture !== null && file_exists(public_path('images/gammes/') . $range->picture)) {
|
2020-11-09 11:03:13 +01:00
|
|
|
unlink(public_path('images/gammes/') . $range->picture);
|
2022-08-25 12:26:43 +02:00
|
|
|
}
|
2021-04-16 09:00:36 +02:00
|
|
|
$imageName = $range->id . '_' . $slug . '_' . Str::random(5) . '.' . $picture->extension();
|
2020-11-09 11:03:13 +01:00
|
|
|
$picture->move(public_path('images/gammes'), $imageName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$range->name = $name;
|
|
|
|
|
|
|
|
|
|
if ($range->slug != $slug) {
|
|
|
|
|
$exist = OldSlug::where('slug', $range->slug)->first();
|
|
|
|
|
if ($exist === null) {
|
2022-08-25 12:26:43 +02:00
|
|
|
$oldSlug = new OldSlug();
|
2020-11-09 11:03:13 +01:00
|
|
|
$oldSlug->range_id = $range->id;
|
2022-08-25 12:26:43 +02:00
|
|
|
$oldSlug->slug = $range->slug;
|
2020-11-09 11:03:13 +01:00
|
|
|
$oldSlug->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 12:26:43 +02:00
|
|
|
$range->slug = $slug;
|
2020-11-09 11:03:13 +01:00
|
|
|
$range->description = $description;
|
2022-08-25 12:26:43 +02:00
|
|
|
if ($picture !== null) {
|
2020-11-09 11:03:13 +01:00
|
|
|
$range->picture = $imageName;
|
2022-08-25 12:26:43 +02:00
|
|
|
}
|
2020-11-09 11:03:13 +01:00
|
|
|
$range->status = $status;
|
|
|
|
|
$range->save();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.ranges.edit', $range->id)->with('success', 'Modifications enregistrés !');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy($id)
|
|
|
|
|
{
|
2021-03-17 09:28:02 +01:00
|
|
|
$range = Range::findOrFail($id);
|
2020-11-09 11:03:13 +01:00
|
|
|
|
2021-03-17 09:28:02 +01:00
|
|
|
$product = Product::where('range_id', $range->id)->count();
|
2021-04-22 15:21:09 +02:00
|
|
|
if ($product !== 0) {
|
2020-11-09 11:03:13 +01:00
|
|
|
return redirect()->back()->withErrors("Suppression impossible : \"" . $range->name . "\" contient encore des produits.");
|
2021-04-22 15:21:09 +02:00
|
|
|
}
|
2020-11-09 11:03:13 +01:00
|
|
|
|
2021-04-22 15:21:09 +02:00
|
|
|
if ($range->picture !== null) {
|
|
|
|
|
if (file_exists(public_path('images/gammes/') . $range->picture)) {
|
2020-11-09 11:03:13 +01:00
|
|
|
unlink(public_path('images/gammes/') . $range->picture);
|
2021-04-22 15:21:09 +02:00
|
|
|
}
|
2020-11-09 11:03:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OldSlug::where('range_id', $range->id)->delete();
|
|
|
|
|
|
|
|
|
|
$range->delete();
|
|
|
|
|
return redirect()->route('bo.ranges.index');
|
|
|
|
|
}
|
|
|
|
|
}
|