public function broadcastArticle($slug)
 {
     $article = Article::whereSlug($slug)->firstOrFail();
     $article->featured_ref = asset('images/featured/' . $article->featured);
     $headers = array('Authorization: key=' . env('GCM_KEY'), 'Content-Type: application/json');
     $data = ["to" => "/topics/article", "notification" => ["title" => "Infogue.id updates", "body" => $article->title, "id" => $article->id, "title" => $article->title, "slug" => $article->slug, "featured_ref" => $article->featured_ref]];
     // Open connection
     $ch = curl_init();
     // Set the url, number of POST vars, POST data
     curl_setopt($ch, CURLOPT_URL, env('GCM_URL'));
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     // Disabling SSL Certificate support temporarily
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
     // Execute post
     $result = curl_exec($ch);
     if ($result === FALSE) {
         // die('Curl failed: ' . curl_error($ch));
     }
     // Close connection
     curl_close($ch);
     return $result;
 }
 /**
  * Store a newly comment in storage.
  *
  * @param \Illuminate\Http\Request
  * @param $slug
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request, $slug)
 {
     $rules = ['comment' => 'required|max:2000'];
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     $article = Article::whereSlug($slug)->firstOrFail();
     $comment = new Comment();
     $comment->contributor_id = Auth::user()->id;
     $comment->article_id = $article->id;
     $comment->comment = $request->input('comment');
     if ($comment->save()) {
         return redirect(route('article.show', [$slug]) . '#form-comment')->with(['status' => 'success', 'message' => Lang::get('alert.comment.send')]);
     }
     return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]);
 }
 /**
  * Remove the specified article from storage.
  *
  * @param $slug
  * @return \Illuminate\Http\Response
  */
 public function destroy($slug)
 {
     $article = Article::whereSlug($slug)->firstOrFail();
     if ($article->delete()) {
         return response()->json(['request_id' => uniqid(), 'status' => 'success', 'message' => 'Article was deleted', 'timestamp' => Carbon::now()]);
     }
     return response()->json(['request_id' => uniqid(), 'status' => 'failure', 'message' => Lang::get('alert.error.generic'), 'timestamp' => Carbon::now()], 500);
 }
 /**
  * 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])]);
 }