141 lines
4.4 KiB
PHP
141 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\ContentOCB;
|
|
|
|
class ContentOCBController extends Controller
|
|
{
|
|
private $typesContenus = ['VOLAILLE' => 'Volaille', 'OEUF' => 'Oeuf', 'LAPIN' => 'Lapin'];
|
|
|
|
public function index($type)
|
|
{
|
|
$contenu = ContentOCB::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 = 'oui-cest-bon-' . strtolower($type) . '-' . date('d-m-Y-h-i-s') .'-le-gaulois' . '.' . $picture->extension();
|
|
$picture->move(public_path('images/ocb/' . date('Y') . '/' . date('m')), $imageName);
|
|
|
|
$ocb = new ContentOCB();
|
|
$ocb->picture = 'images/ocb/' . 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 = ContentOCB::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 = ContentOCB::findOrFail($id);
|
|
|
|
if ($picture !== null) {
|
|
if (file_exists(public_path() . '/' . $ocb->picture))
|
|
unlink(public_path() . '/' . $ocb->picture);
|
|
$imageName = 'oui-cest-bon-' . strtolower($type) . '-' . date('d-m-Y-h-i-s') .'-le-gaulois' . '.' . $picture->extension();
|
|
$picture->move(public_path('images/ocb/' . date('Y') . '/' . date('m')), $imageName);
|
|
}
|
|
|
|
$ocb->type = $type;
|
|
$ocb->content = $content;
|
|
if ($picture !== null)
|
|
$ocb->picture = 'images/ocb/' . 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 = ContentOCB::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 = ContentOCB::findOrFail($id);
|
|
|
|
if ($action === 'up'){
|
|
$tmp = ContentOCB::where([
|
|
['order', '<', $current->order],
|
|
['type', $type],
|
|
])->orderBy('order', 'DESC')->first();
|
|
$newOrder = $tmp->order;
|
|
$tmp->order = $current->order;
|
|
$current->order = $newOrder;
|
|
}else{
|
|
$tmp = ContentOCB::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 = ContentOCB::select('order')->where('type', $type)->orderBy('order', 'DESC')->first();
|
|
if ($order === NULL)
|
|
return 1;
|
|
return $order->order + 1;
|
|
}
|
|
}
|