From fe6ca8a17a778feb327645ced06fcbcd12138363 Mon Sep 17 00:00:00 2001 From: HDVinnie Date: Fri, 18 May 2018 14:20:43 -0400 Subject: [PATCH] (Update) Articles/News System - general cleanup - moved validation rules out of model and into controller --- app/Article.php | 8 --- app/Http/Controllers/ArticleController.php | 10 +--- .../Controllers/Staff/ArticleController.php | 53 +++++++++++++------ 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/app/Article.php b/app/Article.php index fc5d440e2..d318149d2 100755 --- a/app/Article.php +++ b/app/Article.php @@ -17,14 +17,6 @@ use App\Helpers\Bbcode; class Article extends Model { - - public $rules = [ - 'title' => 'required', - 'slug' => 'required', - 'content' => 'required|min:100', - 'user_id' => 'required' - ]; - /** * Belongs to User * diff --git a/app/Http/Controllers/ArticleController.php b/app/Http/Controllers/ArticleController.php index fedc4f718..fb2487faf 100755 --- a/app/Http/Controllers/ArticleController.php +++ b/app/Http/Controllers/ArticleController.php @@ -16,16 +16,13 @@ use App\Article; class ArticleController extends Controller { - /** * Show Articles * - * @access public - * @return post.articles + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function articles() { - // Fetch posts by created_at DESC order $articles = Article::latest()->paginate(6); return view('article.articles', ['articles' => $articles]); @@ -34,14 +31,11 @@ class ArticleController extends Controller /** * Show Article * - * @access public - * @return post.post + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function post($slug, $id) { - // Find de right post $article = Article::findOrFail($id); - // Get comments on this post $comments = $article->comments()->latest()->get(); return view('article.article', ['article' => $article, 'comments' => $comments]); diff --git a/app/Http/Controllers/Staff/ArticleController.php b/app/Http/Controllers/Staff/ArticleController.php index 97310fce7..c948c5c2f 100755 --- a/app/Http/Controllers/Staff/ArticleController.php +++ b/app/Http/Controllers/Staff/ArticleController.php @@ -19,7 +19,6 @@ use \Toastr; class ArticleController extends Controller { - /** * Get All Articles * @@ -45,22 +44,22 @@ class ArticleController extends Controller /** * Add A Article * + * @return Illuminate\Http\RedirectResponse */ public function add(Request $request) { - $input = $request->all(); $article = new Article(); - $article->title = $input['title']; + $article->title = $request->input('title'); $article->slug = str_slug($article->title); - $article->content = $input['content']; + $article->content = $request->input('content'); $article->user_id = auth()->user()->id; - // Verify that an image was upload + if ($request->hasFile('image') && $request->file('image')->getError() == 0) { - // The file is an image - if (in_array($request->file('image')->getClientOriginalExtension(), ['jpg', 'jpeg', 'bmp', 'png', 'tiff'])) { - // Move and add the name to the object that will be saved - $article->image = 'article-' . uniqid() . '.' . $request->file('image')->getClientOriginalExtension(); - $request->file('image')->move(getcwd() . '/files/img/', $article->image); + $image = $request->file('image'); + if (in_array($image->getClientOriginalExtension(), ['jpg', 'JPG', 'jpeg', 'bmp', 'png', 'PNG', 'tiff', 'gif']) && preg_match('#image/*#', $image->getMimeType())) { + $filename = 'article-' . uniqid() . '.' . $image->getClientOriginalExtension(); + $path = public_path('/files/img/' . $filename); + Image::make($image->getRealPath())->fit(100, 100)->encode('png', 100)->save($path); } else { // Image null or wrong format $article->image = null; @@ -70,16 +69,24 @@ class ArticleController extends Controller $article->image = null; } - $v = validator($article->toArray(), $article->rules); + $v = validator($article->toArray(), [ + 'title' => 'required', + 'slug' => 'required', + 'content' => 'required|min:100', + 'user_id' => 'required' + ]); + if ($v->fails()) { // Delete the image because the validation failed if (file_exists($request->file('image')->move(getcwd() . '/files/img/' . $article->image))) { unlink($request->file('image')->move(getcwd() . '/files/img/' . $article->image)); } - return redirect()->route('staff_article_index')->with(Toastr::error('Your article has failed to published!', 'Whoops!', ['options'])); + return redirect()->route('staff_article_index') + ->with(Toastr::error($v->errors()->toJson(), 'Whoops!', ['options'])); } else { auth()->user()->articles()->save($article); - return redirect()->route('staff_article_index')->with(Toastr::success('Your article has successfully published!', 'Yay!', ['options'])); + return redirect()->route('staff_article_index') + ->with(Toastr::success('Your article has successfully published!', 'Yay!', ['options'])); } } @@ -102,6 +109,7 @@ class ArticleController extends Controller * * @param $slug * @param $id + * @return Illuminate\Http\RedirectResponse */ public function edit(Request $request, $slug, $id) { @@ -128,12 +136,20 @@ class ArticleController extends Controller $article->image = null; } - $v = validator($article->toArray(), $article->rules); + $v = validator($article->toArray(), [ + 'title' => 'required', + 'slug' => 'required', + 'content' => 'required|min:100', + 'user_id' => 'required' + ]); + if ($v->fails()) { - return redirect()->route('staff_article_index')->with(Toastr::error('Your article changes have failed to publish!', 'Whoops!', ['options'])); + return redirect()->route('staff_article_index') + ->with(Toastr::error('Your article changes have failed to publish!', 'Whoops!', ['options'])); } else { $article->save(); - return redirect()->route('staff_article_index')->with(Toastr::success('Your article changes have successfully published!', 'Yay!', ['options'])); + return redirect()->route('staff_article_index') + ->with(Toastr::success('Your article changes have successfully published!', 'Yay!', ['options'])); } } @@ -142,11 +158,14 @@ class ArticleController extends Controller * * @param $slug * @param $id + * @return Illuminate\Http\RedirectResponse */ public function delete($slug, $id) { $article = Article::findOrFail($id); $article->delete(); - return redirect()->route('staff_article_index')->with(Toastr::success('Article has successfully been deleted', 'Yay!', ['options'])); + + return redirect()->route('staff_article_index') + ->with(Toastr::success('Article has successfully been deleted', 'Yay!', ['options'])); } }