126 lines
3.7 KiB
PHP
126 lines
3.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Bo;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\OldSlug;
|
||
|
|
use App\Models\Products;
|
||
|
|
use App\Models\Ranges;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
class RangesController extends Controller
|
||
|
|
{
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
$ranges = Ranges::all();
|
||
|
|
return view('bo.ranges.index', compact('ranges'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create()
|
||
|
|
{
|
||
|
|
return view('bo.ranges.create');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(Request $request)
|
||
|
|
{
|
||
|
|
$name = $request->name;
|
||
|
|
$slug = Str::slug($name);
|
||
|
|
$description = $request->description;
|
||
|
|
$picture = $request->picture;
|
||
|
|
$status = $request->status === null ? 0 : 1;
|
||
|
|
|
||
|
|
$request->validate([
|
||
|
|
'name' => 'required|string|max:255',
|
||
|
|
'description' => 'required',
|
||
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$range = new Ranges();
|
||
|
|
$range->name = $name;
|
||
|
|
$range->slug = $slug;
|
||
|
|
$range->description = $description;
|
||
|
|
$range->status = $status;
|
||
|
|
$range->save();
|
||
|
|
|
||
|
|
if ($picture !== null) {
|
||
|
|
$imageName = $range->id . '.' . $picture->extension();
|
||
|
|
$picture->move(public_path('images/gammes'), $imageName);
|
||
|
|
$range->picture = $imageName;
|
||
|
|
$range->save();
|
||
|
|
}
|
||
|
|
|
||
|
|
return redirect()->route('bo.ranges.index');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function edit($id)
|
||
|
|
{
|
||
|
|
$range = Ranges::findOrFail($id);
|
||
|
|
return view('bo.ranges.edit', compact('range'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(Request $request, $id)
|
||
|
|
{
|
||
|
|
$name = $request->name;
|
||
|
|
$slug = Str::slug($name);
|
||
|
|
$description = $request->description;
|
||
|
|
$picture = $request->picture;
|
||
|
|
$status = $request->status === null ? 0 : 1;
|
||
|
|
|
||
|
|
$request->validate([
|
||
|
|
'name' => 'required|string|max:255',
|
||
|
|
'description' => 'required',
|
||
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$range = Ranges::findOrFail($id);
|
||
|
|
|
||
|
|
if ($picture !== null) {
|
||
|
|
if ($range->picture !== null && file_exists(public_path('images/gammes/') . $range->picture))
|
||
|
|
unlink(public_path('images/gammes/') . $range->picture);
|
||
|
|
$imageName = $range->id . '.' . $picture->extension();
|
||
|
|
$picture->move(public_path('images/gammes'), $imageName);
|
||
|
|
}
|
||
|
|
|
||
|
|
$range->name = $name;
|
||
|
|
|
||
|
|
if ($range->slug != $slug) {
|
||
|
|
$exist = OldSlug::where('slug', $range->slug)->first();
|
||
|
|
if ($exist === null) {
|
||
|
|
$oldSlug = new OldSlug();
|
||
|
|
$oldSlug->range_id = $range->id;
|
||
|
|
$oldSlug->slug = $range->slug;
|
||
|
|
$oldSlug->save();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$range->slug = $slug;
|
||
|
|
$range->description = $description;
|
||
|
|
if ($picture !== null)
|
||
|
|
$range->picture = $imageName;
|
||
|
|
$range->status = $status;
|
||
|
|
$range->save();
|
||
|
|
|
||
|
|
return redirect()->route('bo.ranges.edit', $range->id)->with('success', 'Modifications enregistrés !');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy($id)
|
||
|
|
{
|
||
|
|
$range = Ranges::findOrFail($id);
|
||
|
|
|
||
|
|
$product = Products::where('range_id', $range->id)->count();
|
||
|
|
if ($product !== 0)
|
||
|
|
return redirect()->back()->withErrors("Suppression impossible : \"" . $range->name . "\" contient encore des produits.");
|
||
|
|
|
||
|
|
if($range->picture !== null) {
|
||
|
|
if (file_exists(public_path('images/gammes/') . $range->picture))
|
||
|
|
unlink(public_path('images/gammes/') . $range->picture);
|
||
|
|
}
|
||
|
|
|
||
|
|
OldSlug::where('range_id', $range->id)->delete();
|
||
|
|
|
||
|
|
$range->delete();
|
||
|
|
return redirect()->route('bo.ranges.index');
|
||
|
|
}
|
||
|
|
}
|