138 lines
4.2 KiB
PHP
138 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StorePostRequest;
|
|
use App\Http\Requests\UpdatePostRequest;
|
|
use App\Lib\Utils;
|
|
use App\Models\Post;
|
|
|
|
class PostsController extends Controller
|
|
{
|
|
/**
|
|
* Display the posts view.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$posts = Post::orderBy('created_at', 'DESC')->get();
|
|
|
|
return view('back.posts.index', compact('posts'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @param Post $post
|
|
* @return \Illuminate\Http\Response
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
*/
|
|
public function create(Post $post)
|
|
{
|
|
return view('back.posts.create', compact('post'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\StorePostRequest $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(StorePostRequest $request)
|
|
{
|
|
$post = new Post($request->validated());
|
|
if (is_a($request->picture_home, "SplFileInfo")) {
|
|
$post->picture_home = Utils::storeImage($request->picture_home, $post->getTable());
|
|
}
|
|
if (is_a($request->picture_news, "SplFileInfo")) {
|
|
$post->picture_news = Utils::storeImage($request->picture_news, $post->getTable());
|
|
}
|
|
$response = $this->changeUniquePostOnTop($post->on_top);
|
|
|
|
if ($response) {
|
|
$response = $post->saveOrFail();
|
|
}
|
|
if (!$response) {
|
|
return redirect()->route('bo.posts.edit', $post->id)->with('error', 'L\'actualité n\'a pas été créee');
|
|
}
|
|
return redirect()->route('bo.posts.edit', $post->id)->with('success', 'Actualité créée !');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param Post $post
|
|
* @return \Illuminate\Http\Response
|
|
*
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
*/
|
|
public function edit(Post $post)
|
|
{
|
|
return view('back.posts.edit', compact('post'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param Post $post
|
|
* @param \Illuminate\Http\UpdatePostRequest $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Post $post, UpdatePostRequest $request)
|
|
{
|
|
$post->fill($request->validated());
|
|
if (is_a($request->picture_home, "SplFileInfo")) {
|
|
$post->picture_home = Utils::storeImage($request->picture_home, $post->getTable());
|
|
}
|
|
if (is_a($request->picture_news, "SplFileInfo")) {
|
|
$post->picture_news = Utils::storeImage($request->picture_news, $post->getTable());
|
|
}
|
|
$response = $this->changeUniquePostOnTop($post->on_top);
|
|
|
|
if ($response) {
|
|
$response = $post->saveOrFail();
|
|
}
|
|
if (!$response) {
|
|
return redirect()->route('bo.posts.edit', $post->id)
|
|
->with('error', trans('La modification a échoué'));
|
|
}
|
|
|
|
return redirect()->route('bo.posts.edit', $post->id)
|
|
->with('success', trans('Modifications enregistrées !'));
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param Post $post
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Post $post)
|
|
{
|
|
if (!$post->delete()) {
|
|
return redirect()->route('bo.posts.index')->with('error', trans('La suppression a échoué'));
|
|
}
|
|
|
|
return redirect()->route('bo.posts.index')->with('success', trans('Suppression faite avec succès'));
|
|
}
|
|
|
|
/**
|
|
* Change on_top column to a post if necessary
|
|
*
|
|
* @param boolean $isCurrentPostOnTop
|
|
* @return boolean response
|
|
*/
|
|
private function changeUniquePostOnTop(bool $isCurrentPostOnTop): bool
|
|
{
|
|
if ($isCurrentPostOnTop) {
|
|
$postOnTop = Post::where('on_top', true)->first();
|
|
if ($postOnTop) {
|
|
$postOnTop->on_top = false;
|
|
return $postOnTop->saveOrFail();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|