ldc-legaulois/app/Http/Controllers/Bo/OCBContentController.php
2021-04-08 15:20:48 +02:00

160 lines
4.7 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\OCBContent;
class OCBContentController extends Controller
{
// TODO: use enum OCBType
private $typesContenus = ['VOLAILLE' => 'Volaille', 'OEUF' => 'Oeuf', 'LAPIN' => 'Lapin'];
public function index($type)
{
$contenu = OCBContent::where('type', strtoupper($type))->orderBy('order', 'asc')->get();
return view('bo.ocb.index')
->with('contenu', $contenu)
->with('type', $type);
}
public function create()
{
return view('bo.ocb.create')->with('types', $this->typesContenus);
}
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($type);
$ocb->save();
return redirect()->route('bo.ocb.content', strtolower($type));
}
public function edit($id)
{
$ocb = OCBContent::findOrFail($id);
return view('bo.ocb.edit')
->with('ocb', $ocb)
->with('types', $this->typesContenus);
}
public function update(Request $request, $id)
{
$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',
]);
}
$ocb = OCBContent::findOrFail($id);
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($id)
{
$ocb = OCBContent::findOrFail($id);
if (file_exists(public_path() . '/' . $ocb->picture)) {
unlink(public_path() . '/' . $ocb->picture);
}
$ocb->delete();
return redirect()->route('bo.ocb.content', 'volaille');
}
public function changeOrder($id, $type, $action)
{
$current = OCBContent::findOrFail($id);
if ($action === 'up') {
$tmp = OCBContent::where([
['order', '<', $current->order],
['type', $type],
])->orderBy('order', 'DESC')->first();
$newOrder = $tmp->order;
$tmp->order = $current->order;
$current->order = $newOrder;
} else {
$tmp = OCBContent::where([
['order', '>', $current->order],
['type', $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', strtolower($type));
}
private function getLastOrder($type)
{
$order = OCBContent::select('order')->where('type', $type)->orderBy('order', 'DESC')->first();
if ($order === null) {
return 1;
}
return $order->order + 1;
}
}