ldc-legaulois/app/Http/Controllers/Bo/ArticlesController.php
2021-03-31 12:13:58 +02:00

222 lines
7.9 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Enums\NotificationType;
use App\Events\NewNotification;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\Articles;
use App\Models\ArticlesCategories;
use Illuminate\Support\Facades\Cache;
class ArticlesController extends Controller
{
public function index()
{
$a = Articles::with('category')->get();
return view('bo.articles.index', compact('a'));
}
public function create()
{
$ac = ArticlesCategories::all();
return view('bo.articles.create', compact('ac'));
}
public function store(Request $request)
{
$request->validate([
'title' => 'required|max:255',
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=340,height=340',
'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=780,height=508',
'content' => 'required',
]);
$title = $request->title;
$slug = Str::slug($title);
$picture = $request->picture;
$picture_r = $request->picture_r;
$content = $request->content;
$categoryId = $request->category;
$status = 0;
$notification = $request->notification === null ? 0 : 1;
if ($categoryId === "4") {
$question = $request->question;
$multipleResponse = $request->multiple_response === null ? 0 : 1;
$stopPoll = $request->stop_poll === null ? 0 : 1;
$answers = $request->answers;
$request->validate([
'question' => 'required|max:255',
'answers' => 'required',
]);
}
$imageName = Str::slug($title) . '-le-gaulois-' . date('d-m-Y') . '.' . $picture->extension();
$picture->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m')), $imageName);
$imageName_r = Str::slug($title) . '-le-gaulois-' . date('d-m-Y') . '_r.' . $picture_r->extension();
$picture_r->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m')), $imageName_r);
$a = new Articles();
$a->title = $title;
$a->slug = $slug;
$a->picture = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName;
$a->picture_r = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName_r;
$a->content = $content;
$a->category_id = $categoryId;
if ($categoryId === "4") {
$a->question = $question;
$a->multiple_response = $multipleResponse;
$a->stop_poll = $stopPoll;
}
$a->status = $status;
$a->notification = $notification;
$a->save();
if ($notification === 1) {
event(new NewNotification(NotificationType::info(), route('club.infos.child', $slug), $a));
}
if ($categoryId === "4") {
foreach ($answers as $answer) {
if (!empty($answer)) {
$a->answers()->create([
'answer' => $answer,
]);
}
}
}
return redirect()->route('bo.articles.index');
}
public function edit($id)
{
$a = Articles::findOrFail($id);
$ac = ArticlesCategories::all();
return view('bo.articles.edit')
->with('a', $a)
->with('ac', $ac);
}
public function update(Request $request, $id)
{
$request->validate([
'title' => 'required|max:255',
'content' => 'required',
]);
$title = $request->title;
$slug = Str::slug($title);
$picture = $request->picture;
$picture_r = $request->picture_r;
$content = $request->content;
$categoryId = $request->category;
$status = $request->status === null ? 0 : 1;
$notification = $request->notification === null ? 0 : 1;
if ($categoryId === "4") {
$question = $request->question;
$multipleResponse = $request->multiple_response === null ? 0 : 1;
$stopPoll = $request->stop_poll === null ? 0 : 1;
$answers = $request->answers;
$request->validate([
'question' => 'required|max:255',
'answers' => 'required',
]);
}
if ($picture !== null && $picture_r !== null) {
$request->validate([
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=340,height=340',
'picture_r' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=780,height=508',
]);
} elseif ($picture !== null && $picture_r === null) {
$request->validate([
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=340,height=340'
]);
} elseif ($picture === null && $picture_r !== null) {
$request->validate([
'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=780,height=508'
]);
}
$a = Articles::findOrFail($id);
if ($picture !== null) {
if (file_exists(public_path() . '/' . $a->picture)) {
unlink(public_path() . '/' . $a->picture);
}
$imageName = Str::slug($title) . '.' . $picture->extension();
$picture->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m') . '/'), $imageName);
}
if ($picture_r !== null) {
if (file_exists(public_path() . '/' . $a->picture_r)) {
unlink(public_path() . '/' . $a->picture_r);
}
$imageName_r = Str::slug($title) . '_r.' . $picture_r->extension();
$picture_r->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m') . '/'), $imageName_r);
}
$a->title = $title;
$a->slug = $slug;
if ($picture !== null) {
$a->picture = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName;
}
if ($picture_r !== null) {
$a->picture_r = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName_r;
}
$a->content = $content;
$a->category_id = $categoryId;
if ($categoryId === "4") {
$a->question = $question;
$a->multiple_response = $multipleResponse;
$a->stop_poll = $stopPoll;
$a->answers()->delete();
foreach ($answers as $answer) {
if (!empty($answer)) {
$a->answers()->create([
'answer' => $answer,
]);
}
}
} else {
$a->question = null;
$a->multiple_response = null;
$a->stop_poll = null;
$a->answers()->delete();
}
$a->status = $status;
$a->notification = $notification;
$a->save();
if ($notification === 1) {
event(new NewNotification(NotificationType::info(), route('club.infos.child', $slug), $a));
}
if ($a->category_id == 1 && $a->status === 1) {
$actus = Articles::where([['status', 1], ['category_id', 1]])->orderBy('created_at', 'DESC')->limit(3)->get();
Cache::put('actusGlobal', $actus);
}
return redirect()->route('bo.articles.edit', $a->id)->with('success', 'Enregistré !');
}
public function destroy($id)
{
$a = Articles::findOrFail($id);
if (file_exists(public_path() . '/' . $a->picture)) {
unlink(public_path() . '/' . $a->picture);
}
if (file_exists(public_path() . '/' . $a->picture_r)) {
unlink(public_path() . '/' . $a->picture_r);
}
$a->notifiable()->delete();
$a->delete();
return redirect()->route('bo.articles.index');
}
}