/** * Update the specified article in storage. * * @param \Illuminate\Http\Request $request * @param $slug * @return \Illuminate\Http\Response * @throws \Illuminate\Foundation\Validation\ValidationException */ public function update(Request $request, $slug) { /* * -------------------------------------------------------------------------- * Validate data * -------------------------------------------------------------------------- * Build validation rules, slug must be unique except its own, featured * not required to change or re-upload. */ $article = Article::whereSlug($slug)->firstOrFail(); $rules = ['title' => 'required|max:70', 'slug' => 'required|alpha_dash|max:100|unique:articles,slug,' . $article->id, 'type' => 'required|in:standard,gallery,video', 'category' => 'required', 'subcategory' => 'required', 'featured' => 'mimes:jpg,jpeg,gif,png|max:1000', 'tags' => 'required', 'content' => 'required', 'excerpt' => 'max:300', 'status' => 'required|in:pending,draft,published,reject']; $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { $request->session()->flash('status', 'danger'); $request->session()->flash('message', 'Your inputs data are invalid, please check again'); $this->throwValidationException($request, $validator); } /* * -------------------------------------------------------------------------- * Populate tags * -------------------------------------------------------------------------- * tags [many-to] -- article_tags -- [many] articles * * This process should be similar with create article, little bit difference * at old tags synchronization, some tags maybe removed from article and new * tags need to insert again. */ $articleController = $this; $result = DB::transaction(function () use($request, $article, $articleController) { try { // get all tags which ALREADY EXIST by tags are given $tag = Tag::whereIn('tag', explode(',', $request->get('tags'))); // collect tags which existed into tags_id and leave for a while $tags_id = $tag->pluck('id')->toArray(); // retrieve tags label which already exist to compare by a given array $available_tags = $tag->pluck('tag')->toArray(); // new tags need to insert into tags table $new_tags = array_diff(explode(',', $request->get('tags')), $available_tags); $article->tags()->sync($tags_id); foreach ($new_tags as $tag_label) { $newTag = new Tag(); $newTag->tag = $tag_label; $newTag->save(); // insert new tag immediately after inserted if (!$article->tags->contains($newTag->id)) { $article->tags()->save($newTag); } } /* * -------------------------------------------------------------------------- * Update the article * -------------------------------------------------------------------------- * Finally populate article data like create process and check if featured * need to change and upload the image then update the changes. */ $autoApprove = Setting::whereKey('Auto Approve')->first(); $content = $article->content; $content_update = $request->input('content'); $status = $request->input('status'); if ($autoApprove->value) { $content = $request->input('content'); $content_update = ''; if ($status == 'pending') { $status = 'published'; } } $article->subcategory_id = $request->input('subcategory'); $article->title = $request->input('title'); $article->slug = $request->input('slug'); $article->type = $request->input('type'); $article->content = $content; $article->content_update = $content_update; $article->excerpt = $request->input('excerpt'); $article->status = $status; $image = new Uploader(); if ($image->upload($request, 'featured', base_path('public/images/featured/'), 'featured_' . uniqid())) { $article->featured = $request->input('featured'); } $article->save(); /* * -------------------------------------------------------------------------- * Update article activity * -------------------------------------------------------------------------- * Create new instance of Activity and insert update article activity. */ Activity::create(['contributor_id' => Auth::user()->id, 'activity' => Activity::updateArticleActivity(Auth::user()->username, $article->title, $article->slug)]); if (!$autoApprove->value) { $articleController->sendAdminArticleNotification(Auth::user(), $article, true); } return redirect(route('account.article.index'))->with(['status' => 'success', 'message' => Lang::get('alert.article.update', ['title' => $article->title])]); } catch (\Exception $e) { return redirect()->back()->withErrors(['error' => Lang::get('alert.error.transaction')])->withInput(); } }); return $result; }
/** * Update the specified article in storage. * * @param \Illuminate\Http\Request $request * @param param int $slug * @return \Illuminate\Http\Response */ public function update(Request $request, $slug) { $validator = Validator::make($request->all(), ['featured' => 'mimes:jpg,jpeg,gif,png|max:1000']); if ($validator->fails()) { return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => "Featured must image and less than 1MB", 'timestamp' => Carbon::now()], 400); } $articleController = $this; $article = Article::whereSlug($slug)->firstOrFail(); $exist = Article::whereSlug($request->input('slug'))->where('id', '!=', $article->id)->count(); if ($exist) { return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => 'Unique slug has taken', 'timestamp' => Carbon::now()], 400); } $result = DB::transaction(function () use($request, $article, $articleController) { try { /* * -------------------------------------------------------------------------- * Populate tags * -------------------------------------------------------------------------- * Sync last tags and new article, extract tags from request, break down * between new tags and tags that available in database, merge new inserted * tag id with available tags id and remove the old which is removed. */ $tag = Tag::whereIn('tag', explode(',', $request->get('tags'))); $tags_id = $tag->pluck('id')->toArray(); $available_tags = $tag->pluck('tag')->toArray(); $new_tags = array_diff(explode(',', $request->get('tags')), $available_tags); $article->tags()->sync($tags_id); foreach ($new_tags as $tag_label) { $newTag = new Tag(); $newTag->tag = $tag_label; $newTag->save(); if (!$article->tags->contains($newTag->id)) { $article->tags()->save($newTag); } } /* * -------------------------------------------------------------------------- * Update the article * -------------------------------------------------------------------------- * Finally populate article data like create process and check if featured * need to change and upload the image then update the changes. */ $autoApprove = Setting::whereKey('Auto Approve')->first(); $content = $article->content; $content_update = $request->input('content'); $status = $request->input('status'); if ($autoApprove->value) { $content = $request->input('content'); $content_update = ''; if ($status == 'pending') { $status = 'published'; } } $article->subcategory_id = $request->input('subcategory_id'); $article->title = $request->input('title'); $article->slug = $request->input('slug'); $article->type = $request->input('type'); $article->content = $content; $article->content_update = $content_update; $article->excerpt = $request->input('excerpt'); $article->status = $status; $image = new Uploader(); if ($image->upload($request, 'featured', base_path('public/images/featured/'), 'featured_' . uniqid())) { $article->featured = $request->input('featured'); } $article->save(); $contributor = Contributor::findOrFail($request->input('contributor_id')); Activity::create(['contributor_id' => $contributor->id, 'activity' => Activity::updateArticleActivity($contributor->username, $article->title, $article->slug)]); if (!$autoApprove->value) { $articleController->sendAdminArticleNotification($contributor, $article, true); } return response()->json(['request_id' => uniqid(), 'status' => 'success', 'message' => 'Article was updated', 'auto_approve' => $autoApprove->value, 'timestamp' => Carbon::now()]); } catch (\Exception $e) { return response()->json(['request_id' => uniqid(), 'status' => 'failure', 'message' => Lang::get('alert.error.transaction'), 'timestamp' => Carbon::now()], 500); } }); return $result; }
/** * Update the specified article in storage. * * @param \Illuminate\Http\Request $request * @param $slug * @return \Illuminate\Http\Response * @throws \Illuminate\Foundation\Validation\ValidationException */ public function update(Request $request, $slug) { $article = Article::whereSlug($slug)->firstOrFail(); /* * -------------------------------------------------------------------------- * Validate data * -------------------------------------------------------------------------- * Build validation rules, slug must be unique except its own, featured * not required to change or re-upload. */ $rules = ['title' => 'required|max:70', 'slug' => 'required|alpha_dash|max:100|unique:articles,slug,' . $article->id, 'type' => 'required|in:standard,gallery,video', 'category' => 'required', 'subcategory' => 'required', 'featured' => 'mimes:jpg,jpeg,gif,png', 'tags' => 'required', 'content' => 'required', 'excerpt' => 'max:300', 'status' => 'required|in:pending,draft,published,reject']; $validator = Validator::make($request->all(), $rules); if ($validator->fails()) { $this->throwValidationException($request, $validator); } $result = DB::transaction(function () use($request, $article) { try { /* * -------------------------------------------------------------------------- * Populate tags * -------------------------------------------------------------------------- * tags [many-to] -- article_tags -- [many] articles * * This process should be similar with create article, little bit difference * at old tags synchronization, some tags maybe removed from article and new * tags need to insert again. */ // get all tags which ALREADY EXIST by tags are given $tag = Tag::whereIn('tag', explode(',', $request->get('tags'))); // collect tags which existed into tags_id and leave for a while $tags_id = $tag->pluck('id')->toArray(); // retrieve tags label which already exist to compare with given array $available_tags = $tag->pluck('tag')->toArray(); // new tags need to insert into tags table $new_tags = array_diff(explode(',', $request->get('tags')), $available_tags); $article->tags()->sync($tags_id); foreach ($new_tags as $tag_label) { $newTag = new Tag(); $newTag->tag = $tag_label; $newTag->save(); if (!$article->tags->contains($newTag->id)) { $article->tags()->save($newTag); } } $article->subcategory_id = $request->input('subcategory'); $article->title = $request->input('title'); $article->slug = $request->input('slug'); $article->type = $request->input('type'); $article->content = $request->input('content'); $article->excerpt = $request->input('excerpt'); $article->status = $request->input('status'); $image = new Uploader(); if ($image->upload($request, 'featured', base_path('public/images/featured/'), 'featured_' . uniqid())) { $article->featured = $request->input('featured'); } $article->save(); return $article; } catch (\Exception $e) { return redirect()->back()->withErrors(['error' => Lang::get('alert.error.transaction')])->withInput(); } }); if ($result instanceof RedirectResponse) { return $result; } return redirect(route('admin.article.index'))->with(['status' => 'success', 'message' => Lang::get('alert.article.update', ['title' => $result->title])]); }