246 lines
7.6 KiB
PHP
246 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Slide;
|
|
use App\Models\Slider;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
|
|
class SlidesController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$slides = Slide::orderBy('order', 'ASC')->get();
|
|
return view('bo.slides.index', compact('slides'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$sliders = Slider::all();
|
|
return view('bo.slides.create', compact('sliders'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @todo : validation des dates
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$title = $request->title;
|
|
$picture = $request->picture;
|
|
$text = $request->text;
|
|
$linkLabel = $request->link_label;
|
|
$link = $request->link;
|
|
$newTab = $request->link_new_tab === null ? 0 : 1;
|
|
$sliderId = $request->slider_id;
|
|
|
|
if (empty($request->date_start_date)) {
|
|
$date_start = null;
|
|
} else {
|
|
if (empty($request->date_start_time)) {
|
|
$date_start = $request->date_start_date . " 00:00";
|
|
} else {
|
|
$date_start = $request->date_start_date . " " . $request->date_start_time;
|
|
}
|
|
}
|
|
|
|
if (empty($request->date_end_date)) {
|
|
$date_end = null;
|
|
} else {
|
|
if (empty($request->date_end_time)) {
|
|
$date_end = $request->date_end_date . " 23:59";
|
|
} else {
|
|
$date_end = $request->date_end_date . " " . $request->date_end_time;
|
|
}
|
|
}
|
|
|
|
$request->validate([
|
|
'title' => 'required|max:255',
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=' .
|
|
config('theme.slide_image_maxwidth') . ',max-height=' .
|
|
config('theme.slide_image_maxheight'),
|
|
'link_label' => 'sometimes|max:255',
|
|
'link' => 'sometimes|max:255',
|
|
]);
|
|
|
|
$slide = new Slide();
|
|
$slide->title = $title;
|
|
$slide->text = $text;
|
|
$slide->link_label = $linkLabel;
|
|
$slide->link = $link;
|
|
$slide->link_new_tab = $newTab;
|
|
$slide->date_start = $date_start;
|
|
$slide->date_end = $date_end;
|
|
$slide->slider_id = $sliderId;
|
|
$slide->order = $this->getLastOrder();
|
|
$slide->save();
|
|
|
|
if ($picture !== null) {
|
|
$imageName = $slide->id . '_' . Str::random(5) . '.' . $picture->extension();
|
|
$picture->move(public_path('images/slides'), $imageName);
|
|
$slide->picture = $imageName;
|
|
$slide->save();
|
|
}
|
|
|
|
return redirect()->route('bo.slides.edit', $slide->id)->with('success', 'Slide créé !');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$slide = Slide::findOrFail($id);
|
|
$sliders = Slider::all();
|
|
return view('bo.slides.edit', compact('slide', 'sliders'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$title = $request->title;
|
|
$picture = $request->picture;
|
|
$text = $request->text;
|
|
$linkLabel = $request->link_label;
|
|
$link = $request->link;
|
|
$newTab = $request->link_new_tab === null ? 0 : 1;
|
|
$sliderId = $request->slider_id;
|
|
|
|
if (empty($request->date_start_date)) {
|
|
$date_start = null;
|
|
} else {
|
|
if (empty($request->date_start_time)) {
|
|
$date_start = $request->date_start_date . " 00:00";
|
|
} else {
|
|
$date_start = $request->date_start_date . " " . $request->date_start_time;
|
|
}
|
|
}
|
|
|
|
if (empty($request->date_end_date)) {
|
|
$date_end = null;
|
|
} else {
|
|
if (empty($request->date_end_time)) {
|
|
$date_end = $request->date_end_date . " 23:59";
|
|
} else {
|
|
$date_end = $request->date_end_date . " " . $request->date_end_time;
|
|
}
|
|
}
|
|
|
|
$request->validate([
|
|
'title' => 'required|max:255',
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=' .
|
|
config('theme.slide_image_maxwidth') . ',max-height=' .
|
|
config('theme.slide_image_maxheight'),
|
|
'link_label' => 'sometimes|max:255',
|
|
'link' => 'sometimes|max:255',
|
|
]);
|
|
|
|
$slide = Slide::findOrFail($id);
|
|
$slide->title = $title;
|
|
$slide->text = $text;
|
|
$slide->link_label = $linkLabel;
|
|
$slide->link = $link;
|
|
$slide->link_new_tab = $newTab;
|
|
$slide->date_start = $date_start;
|
|
$slide->date_end = $date_end;
|
|
$slide->slider_id = $sliderId;
|
|
$slide->save();
|
|
|
|
if ($picture !== null) {
|
|
if (file_exists(public_path() . 'images/slides/' . $slide->picture)) {
|
|
unlink(public_path() . 'images/slides/' . $slide->picture);
|
|
}
|
|
$imageName = $slide->id . '_' . Str::random(5) . '.' . $picture->extension();
|
|
$picture->move(public_path('images/slides'), $imageName);
|
|
$slide->picture = $imageName;
|
|
$slide->save();
|
|
}
|
|
|
|
return redirect()->route('bo.slides.edit', $slide->id)->with('success', 'Modifications enregistrées !');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param integer $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$s = Slide::findOrFail($id);
|
|
|
|
$s->delete();
|
|
|
|
return redirect()->route('bo.slides.index');
|
|
}
|
|
|
|
|
|
public function changeOrder($id, $action)
|
|
{
|
|
$current = Slide::findOrFail($id);
|
|
|
|
if ($action === 'up') {
|
|
$tmp = Slide::where('order', '<', $current->order)->orderBy('order', 'DESC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $current->order;
|
|
$current->order = $newOrder;
|
|
} else {
|
|
$tmp = Slide::where('order', '>', $current->order)->orderBy('order', 'ASC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $current->order;
|
|
$current->order = $newOrder;
|
|
}
|
|
$tmp->save();
|
|
$current->save();
|
|
|
|
return back();
|
|
// return redirect()->route('bo.slides.index');
|
|
}
|
|
|
|
/**
|
|
* retreive order position to put a new slide to
|
|
*/
|
|
private function getLastOrder()
|
|
{
|
|
$order = Slide::select('order')->orderBy('order', 'DESC')->first();
|
|
if ($order === null) {
|
|
return 1;
|
|
}
|
|
return $order->order + 1;
|
|
}
|
|
}
|