get(); return view('back.posts.index', compact('posts')); } /** * Show the form for creating a new resource. * * @param Post $post * @return \Illuminate\Http\Response */ // phpcs:ignore 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:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass public function edit(Post $post) { return view('back.posts.edit', compact('post')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\UpdatePostRequest $request * @param Post $post * @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; } }