ldc-legaulois/app/Http/Controllers/Bo/OCBContentController.php
2021-04-28 13:57:12 +02:00

151 lines
4.4 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Enums\OCBType;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\OCBContent;
use App\Traits\UpdateModelStatus;
class OCBContentController extends Controller
{
use UpdateModelStatus;
public function index(OcbType $type)
{
$contenu = OCBContent::where('type', $type)->orderBy('order', 'asc')->paginate();
return view('bo.ocb.index')
->with('contenu', $contenu)
->with('type', $type);
}
public function create()
{
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',
]);
$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('uploads/images/ocb/' . date('Y') . '/' . date('m')), $imageName);
$ocb = new OCBContent();
$ocb->picture = sprintf('uploads/images/ocb/%s/%s/%s', date('Y'), date('m'), $imageName);
$ocb->content = $content;
$ocb->status = $status;
$ocb->type = $type;
$ocb->order = $this->getLastOrder($ocb->type);
$ocb->save();
return redirect()->route('bo.ocb.content', $type);
}
public function edit(OCBContent $ocb)
{
return view('bo.ocb.edit')
->with('ocb', $ocb);
}
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) {
if (file_exists(public_path() . '/' . $ocb->picture)) {
unlink(public_path() . '/' . $ocb->picture);
}
$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;
if ($picture !== null) {
$ocb->picture = sprintf('uploads/images/ocb/%s/%s/%s', date('Y'), date('m'), $imageName);
}
$ocb->status = $status;
$ocb->type = $type;
$ocb->save();
return redirect()->route('bo.ocb.edit', $ocb->id)->with('success', 'Enregistré !');
}
public function destroy(OCBContent $ocb)
{
if (file_exists(public_path() . '/' . $ocb->picture)) {
unlink(public_path() . '/' . $ocb->picture);
}
$ocb->delete();
return redirect()->route('bo.ocb.content', $ocb->type);
}
public function changeOrder(OCBContent $current, $action)
{
if ($action === 'up') {
$tmp = OCBContent::where([
['order', '<', $current->order],
['type', $current->type],
])->orderBy('order', 'DESC')->first();
$newOrder = $tmp->order;
$tmp->order = $current->order;
$current->order = $newOrder;
} else {
$tmp = OCBContent::where([
['order', '>', $current->order],
['type', $current->type],
])->orderBy('order', 'ASC')->first();
$newOrder = $tmp->order;
$tmp->order = $current->order;
$current->order = $newOrder;
}
$tmp->save();
$current->save();
return redirect()->route('bo.ocb.content', $current->type);
}
private function getLastOrder(OCBType $type)
{
$order = OCBContent::select('order')->where('type', $type)->orderBy('order', 'DESC')->first();
if ($order === null) {
return 1;
}
return $order->order + 1;
}
}