/**
  * Display the specified contributor stream or profile.
  *
  * @param $username
  * @return \Illuminate\Http\Response
  */
 public function show($username)
 {
     /*
      * --------------------------------------------------------------------------
      * Populating stream
      * --------------------------------------------------------------------------
      * Retrieve stream article 10 data per request, because we implement lazy
      * pagination via ajax so return json data when 'page' variable exist, and
      * return view if doesn't.
      */
     $contributor = $this->contributor->profile($username, true);
     $stream = $this->contributor->stream($username);
     if (Input::get('page', false)) {
         return $stream;
     } else {
         return view('profile.stream', compact('contributor', 'stream'));
     }
 }
 /**
  * Display the specified article.
  *
  * @param Request $requests
  * @param $slug
  * @return \Illuminate\Http\Response
  */
 public function show(Request $requests, $slug)
 {
     // retrieve article and related data
     $article = $this->article->whereSlug($slug)->with('subcategory', 'subcategory.category', 'tags')->firstOrFail();
     // retrieve contributor and profile
     $contributor = new Contributor();
     $contributor_id = $requests->get('contributor_id');
     $username = $article->contributor->username;
     $author = $contributor->profile($username, false, $contributor_id, true);
     unset($author->token);
     unset($author->api_token);
     unset($author->email);
     unset($author->gender);
     unset($author->birthday);
     unset($author->contact);
     unset($author->facebook);
     unset($author->twitter);
     unset($author->googleplus);
     unset($author->instagram);
     unset($author->vendor);
     unset($author->mobile_notification);
     unset($author->email_subscription);
     unset($author->email_message);
     unset($author->email_follow);
     unset($author->email_feed);
     unset($author->created_at);
     unset($author->updated_at);
     unset($author->following_status);
     unset($author->following_text);
     unset($author->avatar);
     unset($author->cover);
     unset($author->contributor_ref);
     // retrieve rating
     $rating = round($article->ratings()->avg('rate'));
     // reduce related article data
     $related = $this->article->related($article->id);
     $related->map(function ($row) {
         unset($row->id);
         unset($row->content);
         unset($row->view);
         unset($row->featured);
     });
     // reduce tags data
     $tags = $article->tags;
     $tags->map(function ($tag) {
         unset($tag->created_at);
         unset($tag->updated_at);
     });
     $article = $article->toArray();
     $article['content'] = str_replace('src="//www.youtube.com/', 'src="https://www.youtube.com/', $article['content']);
     $article['tags'] = $tags;
     $article['contributor'] = $author;
     $article['rating'] = $rating;
     $article['related'] = $related;
     return response()->json(['request_id' => uniqid(), 'status' => 'success', 'timestamp' => Carbon::now(), 'article' => $article]);
 }
 /**
  * Display a listing of the contributor following.
  *
  * @param Request $requests
  * @param $username
  * @return \Illuminate\Http\Response
  */
 public function following(Request $requests, $username)
 {
     $contributor = $this->contributor->profile($username, false, $requests->get('contributor_id'), true);
     $following = $this->contributor->contributorFollowing($username, $requests->get('contributor_id'), true);
     return $this->responseData($contributor, 'following', $following);
 }
 /**
  * Store a newly created account in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function login(Request $request)
 {
     $user = Contributor::where('email', $request->input('username'))->orWhere('username', $request->input('username'))->first();
     $respond = ['request_id' => uniqid(), 'timestamp' => Carbon::now()];
     if (count($user)) {
         $respond['status'] = $user->status;
         if ($user->status != 'activated') {
             $respond['login'] = '******';
             $respond['message'] = 'The account is pending or suspended';
             $respond['token'] = $user->token;
             $code = 403;
         } else {
             $field = filter_var($request->input('username'), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
             $request->merge([$field => $request->input('username')]);
             $credentials = $request->only($field, 'password');
             if (Auth::attempt($credentials)) {
                 $contributor = new Contributor();
                 $user = $contributor->profile($user->username, true);
                 $user->article_total = $user->articles()->where('status', 'published')->count();
                 $user->followers_total = $user->followers()->count();
                 $user->following_total = $user->following()->count();
                 $respond['login'] = '******';
                 $respond['message'] = 'Credentials are valid';
                 $respond['user'] = $user;
                 $code = 200;
             } else {
                 $respond['login'] = '******';
                 $respond['message'] = 'Username or password is incorrect';
                 $code = 401;
             }
         }
     } else {
         $respond['status'] = 'unregistered';
         $respond['login'] = '******';
         $respond['message'] = 'These credentials do not match our records';
         $code = 403;
     }
     return response()->json($respond, $code);
 }
 /**
  * Display the specified article.
  *
  * @param  int $slug
  * @return \Illuminate\Http\Response
  */
 public function show($slug)
 {
     /*
      * --------------------------------------------------------------------------
      * Populate single article view
      * --------------------------------------------------------------------------
      * Find published article by slug, build breadcrumb stack, retrieve article
      * author, article tags, related and popular article.
      */
     $article = $this->article->whereSlug($slug)->firstOrFail();
     if ($article->status == 'pending') {
         return view('article.pending', compact('article'));
     }
     if ($article->status == 'published') {
         $category = $article->subcategory->category->category;
         $subcategory = $article->subcategory->subcategory;
         $comments = $article->comments;
         $breadcrumb = ['Archive' => route('article.archive'), $category => route('article.category', [str_slug($category)]), $subcategory => route('article.subcategory', [str_slug($category), str_slug($subcategory)])];
         $previous_article = $this->article->navigateArticle($article->id, 'prev');
         $next_article = $this->article->navigateArticle($article->id, 'next');
         $prev_ref = $previous_article != null ? route('article.show', [$previous_article->slug]) : '#';
         $next_ref = $next_article != null ? route('article.show', [$next_article->slug]) : '#';
         $tags = $article->tags()->get();
         $related = $this->article->related($article->id);
         $popular = $this->article->mostPopular(5);
         $contributor = new Contributor();
         $author = $contributor->profile($article->contributor->username);
         return view('article.article', compact('breadcrumb', 'prev_ref', 'next_ref', 'article', 'author', 'comments', 'tags', 'related', 'popular'));
     } else {
         abort(404);
     }
 }