222 lines
7.8 KiB
PHP
222 lines
7.8 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;
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
class ArticlesController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$a = $this->filterArticles($request);
|
|
|
|
$a->with('category');
|
|
$a = $a->paginate(10);
|
|
|
|
return view('bo.articles.index', compact('a'));
|
|
}
|
|
|
|
protected function filterArticles(Request $request)
|
|
{
|
|
$articles = Articles::query();
|
|
|
|
if ($request->rst) {
|
|
Session::remove('articles.sort_col');
|
|
Session::remove('articles.sort_way');
|
|
}
|
|
if ($request->sort_col) {
|
|
Session::put('articles.sort_col', $request->sort_col);
|
|
Session::save();
|
|
}
|
|
if ($request->sort_way) {
|
|
Session::put('articles.sort_way', $request->sort_way);
|
|
Session::save();
|
|
}
|
|
|
|
if (Session::has('articles.sort_col') && Session::has('articles.sort_way')) {
|
|
switch (Session::get('articles.sort_col')) {
|
|
case 'category_id':
|
|
$articles = $articles->orderBy('category_id', Session::get('articles.sort_way'));
|
|
break;
|
|
case 'status':
|
|
$articles = $articles->orderBy('status', Session::get('articles.sort_way'));
|
|
break;
|
|
}
|
|
}
|
|
|
|
return $articles;
|
|
}
|
|
|
|
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) . '-jeusselin-' . date('d-m-Y') . '.' . $picture->extension();
|
|
$picture->move(
|
|
storage_path(sprintf('app/public/modelfiles/articles/%s/%s', date('Y'), date('m'))),
|
|
$imageName
|
|
);
|
|
$imageName_r = Str::slug($title) . '-jeusselin-' . date('d-m-Y') . '_r.' . $picture_r->extension();
|
|
$picture_r->move(
|
|
storage_path(sprintf('app/public/modelfiles/articles/%s/%s', date('Y'), date('m'))),
|
|
$imageName_r
|
|
);
|
|
|
|
$a = new Articles();
|
|
$a->title = $title;
|
|
$a->slug = $slug;
|
|
$a->picture = sprintf('storage/modelfiles/articles/%s/%s/%s', date('Y'), date('m'), $imageName);
|
|
$a->picture_r = sprintf('storage/modelfiles/articles/%s/%s/%s', date('Y'), date('m'), $imageName_r);
|
|
$a->content = $content;
|
|
$a->category_id = $categoryId;
|
|
$a->status = $status;
|
|
|
|
if ($a->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.articles.index')
|
|
->with('success', trans(
|
|
':model a été crée',
|
|
['model' => 'Article']
|
|
));
|
|
}
|
|
return back();
|
|
}
|
|
|
|
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(storage_path(Str::replaceFirst('storage', 'app/public', $a->picture)))) {
|
|
unlink(storage_path(Str::replaceFirst('storage', 'app/public', $a->picture)));
|
|
}
|
|
$imageName = Str::slug($title) . '.' . $picture->extension();
|
|
$picture->move(
|
|
storage_path(sprintf('app/public/modelfiles/articles/%s/%s', date('Y'), date('m'))),
|
|
$imageName
|
|
);
|
|
}
|
|
if ($picture_r !== null) {
|
|
if (file_exists(storage_path(Str::replaceFirst('storage', 'app/public', $a->picture_r)))) {
|
|
unlink(storage_path(Str::replaceFirst('storage', 'app/public', $a->picture_r)));
|
|
}
|
|
$imageName_r = Str::slug($title) . '_r.' . $picture_r->extension();
|
|
$picture_r->move(
|
|
storage_path(sprintf('app/public/modelfiles/articles/%s/%s', date('Y'), date('m'))),
|
|
$imageName_r
|
|
);
|
|
}
|
|
|
|
$a->title = $title;
|
|
$a->slug = $slug;
|
|
if ($picture !== null) {
|
|
$a->picture = sprintf('storage/modelfiles/articles/%s/%s/%s', date('Y'), date('m'), $imageName);
|
|
}
|
|
if ($picture_r !== null) {
|
|
$a->picture_r = sprintf('storage/modelfiles/articles/%s/%s/%s', 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);
|
|
}
|
|
|
|
if ($a->saveOrFail()) {
|
|
return redirect()
|
|
->route('bo.articles.index')
|
|
->with('success', trans(
|
|
':model a été mis à jour',
|
|
['model' => 'Article']
|
|
));
|
|
}
|
|
return back();
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$a = Articles::findOrFail($id);
|
|
|
|
if (file_exists(storage_path(Str::replaceFirst('storage', 'app/public', $a->picture)))) {
|
|
unlink(storage_path(Str::replaceFirst('storage', 'app/public', $a->picture)));
|
|
}
|
|
if (file_exists(storage_path(Str::replaceFirst('storage', 'app/public', $a->picture_r)))) {
|
|
unlink(storage_path(Str::replaceFirst('storage', 'app/public', $a->picture_r)));
|
|
}
|
|
|
|
if ($a->deleteOrFail()) {
|
|
return redirect()
|
|
->route('bo.articles.index')
|
|
->with('warning', trans(
|
|
':model a bien été supprimé',
|
|
['model' => 'Article']
|
|
));
|
|
}
|
|
return back();
|
|
}
|
|
}
|