/**
  * 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 website setting in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request)
 {
     /*
      * --------------------------------------------------------------------------
      * Validating data
      * --------------------------------------------------------------------------
      * Define rules before populating data, check if checkbox is unchecked then
      * gives default value 0, regex rule for checking coordinate, and merge
      * error message for notification.
      */
     $rules = ['website' => 'required|max:50', 'keywords' => 'required|max:300', 'status' => 'required|in:online,maintenance', 'address' => 'required|max:100', 'contact' => 'required|max:50', 'email' => 'required|email|max:30', 'description' => 'required|max:160', 'owner' => 'required|max:30', 'latitude' => 'required|regex:/^[+-]?\\d+\\.\\d+$/', 'longitude' => 'required|regex:/^[+-]?\\d+\\.\\d+$/', 'facebook' => 'required|url', 'twitter' => 'required|url', 'googleplus' => 'required|url', 'favicon' => 'mimes:jpg,jpeg,gif,png,ico|max:500', 'background' => 'mimes:jpg,jpeg,gif,png|max:1000', 'article' => 'boolean', 'feedback' => 'boolean', 'member' => 'boolean', 'approve' => 'boolean', 'reward' => 'required|numeric|min:0|max:1000000', 'withdrawal' => 'required|numeric|min:10000|max:5000000', 'email_admin' => 'required|email|max:30|unique:users,email,' . Auth::guard('admin')->user()->id, 'name' => 'required|max:50', 'avatar' => 'mimes:jpg,jpeg,gif,png|max:1000', 'password' => 'required|check_password:admin', 'new_password' => 'confirmed|min:6'];
     if (!$request->has('article')) {
         $request->merge(['article' => 0]);
     }
     if (!$request->has('feedback')) {
         $request->merge(['feedback' => 0]);
     }
     if (!$request->has('member')) {
         $request->merge(['member' => 0]);
     }
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         $failedRules = $validator->failed();
         if (isset($failedRules['latitude']['Required']) || isset($failedRules['longitude']['Required'])) {
             $validator->errors()->add('location.Required', 'Location is required');
         }
         if (isset($failedRules['latitude']['Regex']) || isset($failedRules['longitude']['Regex'])) {
             $validator->errors()->add('location.Regex', 'Invalid GPS coordinate');
         }
         if (isset($failedRules['article']['Boolean']) || isset($failedRules['feedback']['Boolean']) || isset($failedRules['member']['Boolean'])) {
             $validator->errors()->add('notification', 'Notification should be yes or no value');
         }
         $this->throwValidationException($request, $validator);
     }
     $result = DB::transaction(function () use($request) {
         try {
             Setting::where('key', 'Website Name')->update(['value' => $request->input('website')]);
             Setting::where('key', 'Keywords')->update(['value' => $request->input('keywords')]);
             Setting::where('key', 'Status')->update(['value' => $request->input('status')]);
             Setting::where('key', 'Address')->update(['value' => $request->input('address')]);
             Setting::where('key', 'Contact')->update(['value' => $request->input('contact')]);
             Setting::where('key', 'Email')->update(['value' => $request->input('email')]);
             Setting::where('key', 'Description')->update(['value' => $request->input('description')]);
             Setting::where('key', 'Owner')->update(['value' => $request->input('owner')]);
             Setting::where('key', 'Latitude')->update(['value' => $request->input('latitude')]);
             Setting::where('key', 'Longitude')->update(['value' => $request->input('longitude')]);
             Setting::where('key', 'Facebook')->update(['value' => $request->input('facebook')]);
             Setting::where('key', 'Twitter')->update(['value' => $request->input('twitter')]);
             Setting::where('key', 'Google Plus')->update(['value' => $request->input('googleplus')]);
             Setting::where('key', 'Email Article')->update(['value' => $request->input('article')]);
             Setting::where('key', 'Email Feedback')->update(['value' => $request->input('feedback')]);
             Setting::where('key', 'Email Contributor')->update(['value' => $request->input('member')]);
             Setting::where('key', 'Auto Approve')->update(['value' => $request->input('approve')]);
             Setting::where('key', 'Article Reward')->update(['value' => $request->input('reward')]);
             Setting::where('key', 'Withdrawal Minimum')->update(['value' => $request->input('withdrawal')]);
             $image = new Uploader();
             if ($image->upload($request, 'favicon', base_path('public/'), 'favicon')) {
                 Setting::where('key', 'Favicon')->update(['value' => $request->input('favicon')]);
             }
             if ($image->upload($request, 'background', base_path('public/images/misc/'), 'background')) {
                 Setting::where('key', 'Background')->update(['value' => $request->input('background')]);
             }
             $user = Auth::guard('admin')->user();
             $user->name = $request->input('name');
             $user->email = $request->input('email_admin');
             if ($request->has('new_password') && !empty($request->get('new_password'))) {
                 $request->merge(['password' => Hash::make($request->input('new_password'))]);
                 $user->password = $request->input('password');
             }
             if ($image->upload($request, 'avatar', base_path('public/images/contributors/'), 'admin_' . Auth::guard('admin')->user()->id)) {
                 $user->avatar = $request->input('avatar');
             }
             return $user->save();
         } catch (\Exception $e) {
             return redirect()->back()->withErrors(['error' => Lang::get('alert.error.transaction')])->withInput();
         }
     });
     if ($result instanceof RedirectResponse) {
         return $result;
     }
     return redirect(route('admin.setting'))->with(['status' => 'success', 'message' => Lang::get('alert.setting.update', ['name' => Auth::guard('admin')->user()->name])]);
 }
 /**
  * Update the specified contributor in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request)
 {
     $rules = ['avatar' => 'mimes:jpg,jpeg,gif,png|max:1000', 'cover' => 'mimes:jpg,jpeg,gif,png|max:1000'];
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         $failedRules = $validator->failed();
         $validAvatar = isset($failedRules['avatar']);
         $validCover = isset($failedRules['cover']);
         $errorMessage = "Invalid avatar or cover";
         if ($validAvatar && $validCover) {
             $errorMessage = "Cover and Avatar is invalid";
         } else {
             if ($validAvatar) {
                 $errorMessage = "Avatar is invalid";
             } else {
                 if ($validCover) {
                     $errorMessage = "Cover is invalid";
                 }
             }
         }
         $errorMessage .= ", must image and less than 1MB";
         return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => $errorMessage, 'timestamp' => Carbon::now()], 400);
     }
     $contributor = Contributor::findOrFail($request->input('contributor_id'));
     if ($request->has('new_password') && !empty($request->get('new_password'))) {
         $credential = Hash::check($request->input('password'), $contributor->password);
         if (!$credential) {
             return response()->json(['request_id' => uniqid(), 'status' => 'mismatch', 'message' => 'Current password is mismatch', 'timestamp' => Carbon::now()], 401);
         }
     }
     $usernameExist = Contributor::whereUsername($request->input('username'))->where('id', '!=', $contributor->id)->count();
     if ($usernameExist) {
         return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => 'Username has been taken', 'timestamp' => Carbon::now()], 400);
     }
     $emailExist = Contributor::whereEmail($request->input('email'))->where('id', '!=', $contributor->id)->count();
     if ($emailExist) {
         return response()->json(['request_id' => uniqid(), 'status' => 'denied', 'message' => 'Email has been taken', 'timestamp' => Carbon::now()], 400);
     }
     $contributor->name = $request->input('name');
     $contributor->gender = $request->input('gender');
     $contributor->birthday = $request->input('birthday');
     $contributor->location = $request->input('location');
     $contributor->contact = $request->input('contact');
     $contributor->about = $request->input('about');
     $contributor->username = $request->input('username');
     $contributor->email = $request->input('email');
     $image = new Uploader();
     if ($image->upload($request, 'avatar', base_path('public/images/contributors/'), 'avatar_' . $request->input('contributor_id'))) {
         $contributor->avatar = $request->input('avatar');
     }
     if ($image->upload($request, 'cover', base_path('public/images/covers/'), 'cover_' . $request->input('contributor_id'))) {
         $contributor->cover = $request->input('cover');
     }
     $contributor->instagram = $request->input('instagram');
     $contributor->facebook = $request->input('facebook');
     $contributor->twitter = $request->input('twitter');
     $contributor->googleplus = $request->input('googleplus');
     $contributor->email_subscription = $request->input('email_subscription');
     $contributor->email_message = $request->input('email_message');
     $contributor->email_follow = $request->input('email_follow');
     $contributor->email_feed = $request->input('email_feed');
     $contributor->mobile_notification = $request->input('mobile_notification');
     if ($request->has('new_password') && !empty($request->get('new_password'))) {
         $request->merge(['password' => Hash::make($request->input('new_password'))]);
         $contributor->password = $request->input('password');
     }
     $contributor->bank_id = $request->input('bank_id');
     $contributor->account_name = $request->input('account_name');
     $contributor->account_number = $request->input('account_number');
     if ($contributor->save()) {
         return response()->json(['request_id' => uniqid(), 'status' => 'success', 'message' => 'Setting was updated', 'timestamp' => Carbon::now(), 'contributor' => $contributor->profile($contributor->username, false, $request->input('contributor_id'), true)]);
     } else {
         return response()->json(['request_id' => uniqid(), 'status' => 'failure', 'message' => Lang::get('alert.database.generic'), 'timestamp' => Carbon::now()], 500);
     }
 }
 /**
  * Update the specified contributor in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $rules = ['name' => 'required|max:50', 'gender' => 'required|in:male,female,other', 'date' => 'required|max:31', 'month' => 'required|max:12', 'year' => 'required|max:' . (int) Carbon::now()->addYear(-8)->format('Y'), 'location' => 'required|max:30', 'contact' => 'required|max:20', 'about' => 'required|min:15|max:160', 'email_subscription' => 'boolean', 'email_message' => 'boolean', 'email_follow' => 'boolean', 'email_feed' => 'boolean', 'mobile_notification' => 'boolean', 'avatar' => 'mimes:jpg,jpeg,gif,png|max:1000', 'cover' => 'mimes:jpg,jpeg,gif,png|max:1000', 'username' => 'required|alpha_dash|max:20|unique:contributors,username,' . $id, 'email' => 'required|email|max:50|unique:contributors,email,' . $id, 'new_password' => 'confirmed|min:6'];
     if (!$request->has('email_subscription')) {
         $request->merge(['email_subscription' => 0]);
     }
     if (!$request->has('email_message')) {
         $request->merge(['email_message' => 0]);
     }
     if (!$request->has('email_follow')) {
         $request->merge(['email_follow' => 0]);
     }
     if (!$request->has('email_feed')) {
         $request->merge(['email_feed' => 0]);
     }
     if (!$request->has('mobile_notification')) {
         $request->merge(['mobile_notification' => 0]);
     }
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         $failedRules = $validator->failed();
         $date = isset($failedRules['date']['Required']);
         $month = isset($failedRules['month']['Required']);
         $year = isset($failedRules['year']['Required']);
         if ($date || $month || $year) {
             $validator->errors()->add('birthday', 'Birthday is required');
         }
         $this->throwValidationException($request, $validator);
     }
     $request->merge(['birthday' => implode('-', Input::only('year', 'month', 'date'))]);
     /*
      * --------------------------------------------------------------------------
      * Populate contributor data
      * --------------------------------------------------------------------------
      * Retrieve all data from request and populate into contributor object model
      * then store it into database, check if avatar and cover is available then
      * attempt to upload to asset directory.
      */
     $contributor = Contributor::findOrFail($id);
     $contributor->name = $request->input('name');
     $contributor->gender = $request->input('gender');
     $contributor->birthday = $request->input('birthday');
     $contributor->location = $request->input('location');
     $contributor->contact = $request->input('contact');
     $contributor->about = $request->input('about');
     $contributor->username = $request->input('username');
     $contributor->email = $request->input('email');
     $contributor->instagram = $request->input('instagram');
     $contributor->facebook = $request->input('facebook');
     $contributor->twitter = $request->input('twitter');
     $contributor->googleplus = $request->input('googleplus');
     $contributor->email_subscription = $request->input('email_subscription');
     $contributor->email_message = $request->input('email_message');
     $contributor->email_follow = $request->input('email_follow');
     $contributor->email_feed = $request->input('email_feed');
     $contributor->mobile_notification = $request->input('mobile_notification');
     $image = new Uploader();
     if ($image->upload($request, 'avatar', base_path('public/images/contributors/'), 'avatar_' . $id)) {
         $contributor->avatar = $request->input('avatar');
     }
     if ($image->upload($request, 'cover', base_path('public/images/covers/'), 'cover_' . $id)) {
         $contributor->cover = $request->input('cover');
     }
     if ($request->has('new_password') && !empty($request->get('new_password'))) {
         $contributor->password = Hash::make($request->input('new_password'));
     }
     if ($contributor->save()) {
         return redirect(route('admin.contributor.index'))->with(['status' => 'success', 'message' => Lang::get('alert.contributor.update', ['name' => $contributor->name])]);
     } else {
         return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]);
     }
 }
 /**
  * Send new message to another contributor in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function send(Request $request)
 {
     /*
      * --------------------------------------------------------------------------
      * Create message
      * --------------------------------------------------------------------------
      * Each conversation will handle by one message record as identity, first
      * check if contributor sender or receiver ever make conversation, if they
      * did not then create new one of message.
      */
     $sender = Auth::user()->id;
     $receiver = (int) $request->input('contributor_id');
     $lastMessage = $this->conversation->whereSender($sender)->whereReceiver($receiver)->orWhere('sender', $receiver)->whereReceiver($sender)->first();
     if (count($lastMessage) == 0) {
         $message = new Message();
         $message->save();
         $messageId = $message->id;
     } else {
         $messageId = $lastMessage->message_id;
     }
     /*
      * --------------------------------------------------------------------------
      * Create conversation
      * --------------------------------------------------------------------------
      * Populate message id from last conversation or last inserted new message
      * then create the first conversation or continue with last message, check
      * if there is request of attachment, if so then upload it.
      */
     $conversation = new Conversation(['message_id' => $messageId, 'sender' => $sender, 'receiver' => $receiver, 'message' => $request->input('message')]);
     if ($conversation->save()) {
         $contributor = Contributor::findOrFail($receiver);
         if ($contributor->email_message) {
             $this->sendEmailNotification(Auth::user(), $contributor, $request->input('message'));
         }
         $conversation->broadcastConversation(Auth::user(), $contributor, $request->input('message'));
         if ($request->has('async') && $request->ajax()) {
             $image = new Uploader();
             if ($image->upload($request, 'attachment', base_path('public/file/'), 'attachment_' . uniqid())) {
                 $attachment = new Attachment();
                 $attachment->conversation_id = $conversation->id;
                 $attachment->file = $request->input('attachment');
                 if (!$attachment->save()) {
                     return false;
                 }
             }
             return 'sent';
         }
         return redirect()->back()->with(['status' => 'success', 'message' => Lang::get('alert.message.send', ['receiver' => $contributor->name])]);
     }
     if ($request->has('async') && $request->ajax()) {
         return false;
     }
     return redirect()->back()->with(['status' => 'danger', 'message' => Lang::get('alert.error.database')]);
 }
 /**
  * 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 $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])]);
 }
 /**
  * Update the specified contributor in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request)
 {
     /*
      * --------------------------------------------------------------------------
      * Validating data
      * --------------------------------------------------------------------------
      * Define rules before populating data, check if checkbox is unchecked then
      * gives default value 0, merge notification for date, month, year as
      * birthday, and throw it back if errors occur.
      */
     $user = Auth::user();
     $rules = ['name' => 'required|max:50', 'gender' => 'required|in:male,female,other', 'date' => 'required|max:31', 'month' => 'required|max:12', 'year' => 'required|max:' . (int) Carbon::now()->addYear(-8)->format('Y'), 'location' => 'required|max:30', 'contact' => 'required|max:20', 'about' => 'required|min:15|max:160', 'email_subscription' => 'boolean', 'email_message' => 'boolean', 'email_follow' => 'boolean', 'email_feed' => 'boolean', 'mobile_notification' => 'boolean', 'account_name' => 'max:50', 'account_number' => 'min:7|max:15', 'avatar' => 'mimes:jpg,jpeg,gif,png|max:1000', 'cover' => 'mimes:jpg,jpeg,gif,png|max:1000', 'username' => 'required|alpha_dash|max:20|unique:contributors,username,' . $user->id, 'email' => 'required|email|max:50|unique:contributors,email,' . $user->id, 'new_password' => 'confirmed|min:6'];
     if ($user->vendor == "web" || $user->vendor == "mobile") {
         $rules['password'] = '******';
     }
     if (!$request->has('email_subscription')) {
         $request->merge(['email_subscription' => 0]);
     }
     if (!$request->has('email_message')) {
         $request->merge(['email_message' => 0]);
     }
     if (!$request->has('email_follow')) {
         $request->merge(['email_follow' => 0]);
     }
     if (!$request->has('email_feed')) {
         $request->merge(['email_feed' => 0]);
     }
     if (!$request->has('mobile_notification')) {
         $request->merge(['mobile_notification' => 0]);
     }
     $request->merge(['birthday' => implode('-', Input::only('year', 'month', 'date'))]);
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         $failedRules = $validator->failed();
         $date = isset($failedRules['date']['Required']);
         $month = isset($failedRules['month']['Required']);
         $year = isset($failedRules['year']['Required']);
         if ($date || $month || $year) {
             $validator->errors()->add('birthday', Lang::get('alert.validation.birthday'));
         }
         $this->throwValidationException($request, $validator);
     }
     /*
      * --------------------------------------------------------------------------
      * Populating data and update
      * --------------------------------------------------------------------------
      * Retrieve all data, if avatar or cover input is not empty then try to
      * upload the image and get the filename, if new_password is not empty then
      * the contributor intend to change their password, in the end perform save.
      */
     $contributor = Contributor::findOrFail($user->id);
     $contributor->name = $request->input('name');
     $contributor->gender = $request->input('gender');
     $contributor->birthday = $request->input('birthday');
     $contributor->location = $request->input('location');
     $contributor->contact = $request->input('contact');
     $contributor->about = $request->input('about');
     $contributor->username = $request->input('username');
     $contributor->email = $request->input('email');
     $image = new Uploader();
     if ($image->upload($request, 'avatar', base_path('public/images/contributors/'), 'avatar_' . $contributor->id)) {
         $contributor->avatar = $request->input('avatar');
     }
     if ($image->upload($request, 'cover', base_path('public/images/covers/'), 'cover_' . $contributor->id)) {
         $contributor->cover = $request->input('cover');
     }
     $contributor->instagram = $request->input('instagram');
     $contributor->facebook = $request->input('facebook');
     $contributor->twitter = $request->input('twitter');
     $contributor->googleplus = $request->input('googleplus');
     $contributor->email_subscription = $request->input('email_subscription');
     $contributor->email_message = $request->input('email_message');
     $contributor->email_follow = $request->input('email_follow');
     $contributor->email_feed = $request->input('email_feed');
     $contributor->mobile_notification = $request->input('mobile_notification');
     if ($request->has('new_password') && !empty($request->get('new_password'))) {
         $request->merge(['password' => Hash::make($request->input('new_password'))]);
         $contributor->password = $request->input('password');
     }
     $contributor->bank_id = $request->input('bank');
     $contributor->account_name = $request->input('account_name');
     $contributor->account_number = $request->input('account_number');
     if ($contributor->save()) {
         return redirect(route('account.setting'))->with(['status' => 'success', 'message' => Lang::get('alert.contributor.account')]);
     }
     return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]);
 }