ldc-legaulois/app/Http/Controllers/Bo/OCBContentController.php

152 lines
4.4 KiB
PHP
Raw Normal View History

<?php
2021-03-20 14:38:15 +01:00
namespace App\Http\Controllers\Bo;
2021-04-26 15:44:00 +02:00
use App\Enums\OCBType;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
2021-04-08 15:20:48 +02:00
use App\Models\OCBContent;
2021-04-28 13:57:12 +02:00
use App\Traits\UpdateModelStatus;
2021-04-08 15:20:48 +02:00
class OCBContentController extends Controller
{
2021-04-28 13:57:12 +02:00
use UpdateModelStatus;
2021-03-20 14:38:15 +01:00
2021-04-26 15:44:00 +02:00
public function index(OcbType $type)
{
2021-04-26 15:44:00 +02:00
$contenu = OCBContent::where('type', $type)->orderBy('order', 'asc')->paginate();
return view('bo.ocb.index')
->with('contenu', $contenu)
2021-03-20 14:38:15 +01:00
->with('type', $type);
}
public function create()
{
2021-04-26 15:44:00 +02:00
return view('bo.ocb.create');
}
public function store(Request $request)
{
$picture = $request->picture;
$content = $request->content;
$status = 0;
$type = $request->type;
$request->validate([
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=553',
]);
2021-03-21 22:51:19 +01:00
$imageName = sprintf(
'oui-cest-bon-%s-%s-le-gaulois.%s',
strtolower($type),
date('d-m-Y-h-i-s'),
$picture->extension()
);
2021-02-16 17:32:51 +01:00
$picture->move(public_path('uploads/images/ocb/' . date('Y') . '/' . date('m')), $imageName);
2021-04-08 15:20:48 +02:00
$ocb = new OCBContent();
2021-03-21 22:51:19 +01:00
$ocb->picture = sprintf('uploads/images/ocb/%s/%s/%s', date('Y'), date('m'), $imageName);
$ocb->content = $content;
$ocb->status = $status;
$ocb->type = $type;
2021-04-26 15:44:00 +02:00
$ocb->order = $this->getLastOrder($ocb->type);
$ocb->save();
2021-04-26 15:44:00 +02:00
return redirect()->route('bo.ocb.content', $type);
}
2021-04-26 15:44:00 +02:00
public function edit(OCBContent $ocb)
{
return view('bo.ocb.edit')
2021-04-26 15:44:00 +02:00
->with('ocb', $ocb);
}
2021-04-26 15:44:00 +02:00
public function update(Request $request, OCBContent $ocb)
{
$picture = $request->picture;
$content = $request->content;
$status = $request->status === null ? 0 : 1;
$type = $request->type;
if ($picture !== null) {
$request->validate([
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1366,height=553',
]);
}
if ($picture !== null) {
2021-03-20 14:38:15 +01:00
if (file_exists(public_path() . '/' . $ocb->picture)) {
unlink(public_path() . '/' . $ocb->picture);
2021-03-20 14:38:15 +01:00
}
2021-03-21 22:51:19 +01:00
$imageName = sprintf(
'oui-cest-bon-%s-%s-le-gaulois.%s',
strtolower($type),
date('d-m-Y-h-i-s'),
$picture->extension()
);
$picture->move(public_path(sprintf(
'uploads/images/ocb/%s/%s/%s',
date('Y'),
date('m'),
$imageName
)));
}
$ocb->type = $type;
$ocb->content = $content;
2021-03-20 14:38:15 +01:00
if ($picture !== null) {
2021-03-21 22:51:19 +01:00
$ocb->picture = sprintf('uploads/images/ocb/%s/%s/%s', date('Y'), date('m'), $imageName);
2021-03-20 14:38:15 +01:00
}
$ocb->status = $status;
$ocb->type = $type;
$ocb->save();
return redirect()->route('bo.ocb.edit', $ocb->id)->with('success', 'Enregistré !');
}
2021-04-26 15:44:00 +02:00
public function destroy(OCBContent $ocb)
{
2021-03-20 14:38:15 +01:00
if (file_exists(public_path() . '/' . $ocb->picture)) {
unlink(public_path() . '/' . $ocb->picture);
2021-03-20 14:38:15 +01:00
}
$ocb->delete();
2021-04-26 15:44:00 +02:00
return redirect()->route('bo.ocb.content', $ocb->type);
}
2021-04-26 15:44:00 +02:00
public function changeOrder(OCBContent $current, $action)
{
2021-03-20 14:38:15 +01:00
if ($action === 'up') {
2021-04-08 15:20:48 +02:00
$tmp = OCBContent::where([
['order', '<', $current->order],
2021-04-26 15:44:00 +02:00
['type', $current->type],
])->orderBy('order', 'DESC')->first();
$newOrder = $tmp->order;
$tmp->order = $current->order;
$current->order = $newOrder;
2021-03-20 14:38:15 +01:00
} else {
2021-04-08 15:20:48 +02:00
$tmp = OCBContent::where([
['order', '>', $current->order],
2021-04-26 15:44:00 +02:00
['type', $current->type],
])->orderBy('order', 'ASC')->first();
$newOrder = $tmp->order;
$tmp->order = $current->order;
$current->order = $newOrder;
}
$tmp->save();
$current->save();
2021-04-26 15:44:00 +02:00
return redirect()->route('bo.ocb.content', $current->type);
}
2021-04-26 15:44:00 +02:00
private function getLastOrder(OCBType $type)
{
2021-04-08 15:20:48 +02:00
$order = OCBContent::select('order')->where('type', $type)->orderBy('order', 'DESC')->first();
2021-03-20 14:38:15 +01:00
if ($order === null) {
return 1;
2021-03-20 14:38:15 +01:00
}
return $order->order + 1;
}
}