- npm i sweetalert2 --save-dev - create confirm module - replace all natives confirm with sweet alert
183 lines
6.4 KiB
PHP
183 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Products;
|
|
use App\Models\Ranges;
|
|
use App\Traits\UpdateModelStatus;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
|
|
class RangesController extends Controller
|
|
{
|
|
use UpdateModelStatus;
|
|
|
|
public function index()
|
|
{
|
|
$r = Ranges::orderBy('order', 'asc')->paginate();
|
|
return view('bo.ranges.index', compact('r'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('bo.ranges.create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$name = $request->name;
|
|
$desc = $request->description;
|
|
$picture = $request->picture;
|
|
$picture_r = $request->picture_r;
|
|
$color = $request->color;
|
|
$slug = Str::slug($name);
|
|
|
|
$request->validate([
|
|
'name' => 'required|max:50',
|
|
'description' => 'required|max:255',
|
|
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=640,height=564',
|
|
'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=563',
|
|
'color' => 'required|max:20',
|
|
]);
|
|
|
|
$imageName = Str::slug($name) . '-le-gaulois.' . $picture->extension();
|
|
$imageName_r = Str::slug($name) . '-le-gaulois_r.' . $picture_r->extension();
|
|
$picture->move(public_path('uploads/images/gammes/' . date('Y') . '/' . date('m') . '/'), $imageName);
|
|
$picture_r->move(public_path('uploads/images/gammes/' . date('Y') . '/' . date('m') . '/'), $imageName_r);
|
|
|
|
$r = new Ranges();
|
|
$r->name = $name;
|
|
$r->description = $desc;
|
|
$r->picture = 'uploads/images/gammes/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
|
$r->picture_r = 'uploads/images/gammes/' . date('Y') . '/' . date('m') . '/' . $imageName_r;
|
|
$r->color = $color;
|
|
$r->status = 0;
|
|
$r->order = $this->getLastOrder();
|
|
$r->slug = $slug;
|
|
$r->save();
|
|
|
|
return redirect()->route('bo.ranges.index');
|
|
}
|
|
|
|
public function edit(Ranges $r)
|
|
{
|
|
$products = Products::where('range_id', $r->id)->get();
|
|
return view('bo.ranges.edit', compact('r', 'products'));
|
|
}
|
|
|
|
public function update(Request $request, Ranges $r)
|
|
{
|
|
$name = $request->name;
|
|
$desc = $request->description;
|
|
$picture = $request->picture;
|
|
$picture_r = $request->picture_r;
|
|
$flagshipProduct = $request->flagship_product;
|
|
$color = $request->color;
|
|
$status = $request->status === null ? 0 : 1;
|
|
$slug = Str::slug($name);
|
|
|
|
$request->validate([
|
|
'name' => 'required|max:50',
|
|
'description' => 'required|max:255',
|
|
'color' => 'required|max:20',
|
|
]);
|
|
|
|
if ($picture !== null && $picture_r !== null) {
|
|
$request->validate([
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=640,height=564',
|
|
'picture_r' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=563',
|
|
]);
|
|
} elseif ($picture !== null && $picture_r === null) {
|
|
$request->validate([
|
|
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=640,height=564'
|
|
]);
|
|
} elseif ($picture === null && $picture_r !== null) {
|
|
$request->validate([
|
|
'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=563'
|
|
]);
|
|
}
|
|
|
|
if (empty($flagshipProduct) && $status === 1) {
|
|
return back()->withErrors('Une gamme doit avoir un produit phare pour pouvoir être publiée.');
|
|
}
|
|
|
|
if ($picture !== null) {
|
|
if (file_exists(public_path() . '/' . $r->picture)) {
|
|
unlink(public_path() . '/' . $r->picture);
|
|
}
|
|
$imageName = Str::slug($name) . '.' . $picture->extension();
|
|
$picture->move(public_path('uploads/images/gammes/' . date('Y') . '/' . date('m') . '/'), $imageName);
|
|
}
|
|
if ($picture_r !== null) {
|
|
if (file_exists(public_path() . '/' . $r->picture_r)) {
|
|
unlink(public_path() . '/' . $r->picture_r);
|
|
}
|
|
$imageName_r = Str::slug($name) . '_r.' . $picture_r->extension();
|
|
$picture_r->move(public_path('uploads/images/gammes/' . date('Y') . '/' . date('m') . '/'), $imageName_r);
|
|
}
|
|
|
|
$r->name = $name;
|
|
$r->description = $desc;
|
|
$r->flagship_product = $flagshipProduct;
|
|
if ($picture !== null) {
|
|
$r->picture = 'uploads/images/gammes/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
|
}
|
|
if ($picture_r !== null) {
|
|
$r->picture_r = 'uploads/images/gammes/' . date('Y') . '/' . date('m') . '/' . $imageName_r;
|
|
}
|
|
$r->color = $color;
|
|
$r->status = $status;
|
|
$r->slug = $slug;
|
|
$r->save();
|
|
|
|
return redirect()->route('bo.ranges.edit', ['range' => $r->id])->with('success', 'Enregistré !');
|
|
}
|
|
|
|
public function destroy(Ranges $r)
|
|
{
|
|
if (file_exists(public_path() . '/' . $r->picture)) {
|
|
unlink(public_path() . '/' . $r->picture);
|
|
}
|
|
|
|
if (file_exists(public_path() . '/' . $r->picture_r)) {
|
|
unlink(public_path() . '/' . $r->picture_r);
|
|
}
|
|
|
|
$r->delete();
|
|
|
|
return redirect()->route('bo.ranges.index')
|
|
->with('warning', 'Le contenu à bien été supprimé');
|
|
}
|
|
|
|
public function changeOrder($id, $action)
|
|
{
|
|
$current = Ranges::findOrFail($id);
|
|
if ($action === 'up') {
|
|
$tmp = Ranges::where('order', '<', $current->order)
|
|
->orderBy('order', 'DESC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $current->order;
|
|
$current->order = $newOrder;
|
|
} else {
|
|
$tmp = Ranges::where('order', '>', $current->order)
|
|
->orderBy('order', 'ASC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $current->order;
|
|
$current->order = $newOrder;
|
|
}
|
|
$tmp->save();
|
|
$current->save();
|
|
|
|
return redirect()->route('bo.ranges.index');
|
|
}
|
|
|
|
private function getLastOrder()
|
|
{
|
|
$order = Ranges::select('order')->orderBy('order', 'DESC')->first();
|
|
if ($order === null) {
|
|
return 1;
|
|
}
|
|
return $order->order + 1;
|
|
}
|
|
}
|