148 lines
5.4 KiB
PHP
148 lines
5.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\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:100',
|
|
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=340,max-height=340',
|
|
'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:max-width=780,max-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;
|
|
|
|
$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;
|
|
$a->status = $status;
|
|
$a->save();
|
|
|
|
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:100',
|
|
'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;
|
|
|
|
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;
|
|
$a->status = $status;
|
|
$a->save();
|
|
|
|
if ($a->category_id == 1 && $a->status === 1) {
|
|
$actus = Articles::where([['status', 1], ['category_id', 1]])->orderBy('updated_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->delete();
|
|
|
|
return redirect()->route('bo.articles.index');
|
|
}
|
|
}
|