/**
  * Display a listing of the index resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     /*
      * --------------------------------------------------------------------------
      * Retrieve featured posts from Article model
      * --------------------------------------------------------------------------
      * Headline : indicate article with headline state
      * Trending : indicate article with trending state
      * Popular  : the top of post sort by most viewed on last 3 months
      * Ranked   : the top of post sort by most stared
      * Latest   : last published article
      */
     $article = new Article();
     $featured = $article->headline();
     $trending = $article->trending();
     $popular = $article->mostPopular();
     $ranked = $article->mostRanked();
     $latest = $article->latest();
     /*
      * --------------------------------------------------------------------------
      * Retrieve featured category
      * --------------------------------------------------------------------------
      * Selecting the most viewed articles on top 4 categories
      */
     $category = new Category();
     $summary = $category->featured();
     return view('pages.index', compact('featured', 'popular', 'ranked', 'trending', 'latest', 'summary'));
 }
 /**
  * Retrieve subcategory by category id request via AJAX.
  *
  * @param Request $request
  * @param $id
  * @return json
  */
 public function subcategories(Request $request, $id)
 {
     if ($request->ajax()) {
         $category = $this->category->findOrFail($id);
         return $category->subcategories;
     } else {
         abort(403, 'Resources are restricted.');
     }
 }
 /**
  * Display a listing of the subcategory.
  *
  * @param $category_slug
  * @param $subcategory_slug
  * @return \Illuminate\Http\Response
  */
 public function subcategory($category_slug, $subcategory_slug)
 {
     $category_name = str_replace('-', ' ', $category_slug);
     $subcategory_name = str_replace('-', ' ', $subcategory_slug);
     $category = $this->category->where('category', 'like', $category_name)->firstOrFail();
     $subcategory = $category->subcategories()->where('subcategory', 'like', $subcategory_name)->firstOrFail();
     $articles = $this->subcategory->subcategoryArticle($subcategory->id);
     $data = $this->reduceArticleData($articles);
     return ['request_id' => uniqid(), 'status' => 'success', 'timestamp' => Carbon::now(), 'articles' => $data];
 }
 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
     $visitor = new Visitor();
     $visitor->checkVisitor();
     $this->app->singleton('site_settings', function () {
         $settings = Setting::all();
         $keys = array();
         foreach ($settings as $setting) {
             $keys[$setting->key] = $setting->value;
         }
         return $keys;
     });
     View::share('site_settings', app('site_settings'));
     $this->app->singleton('site_menus', function () {
         $categories = Category::all();
         return $categories;
     });
     View::share('site_menus', app('site_menus'));
     $this->app->singleton('site_statistic', function () {
         $article = Article::whereStatus('published')->count();
         $member = Contributor::whereStatus('activated')->count();
         return ['article' => $article, 'member' => $member];
     });
     View::share('site_statistic', app('site_statistic'));
     Validator::extend('check_password', function ($attribute, $value, $parameter) {
         if (count($parameter) > 0 && $parameter[0] == 'admin') {
             return Hash::check($value, Auth::guard('admin')->user()->getAuthPassword());
         }
         return Hash::check($value, Auth::user()->getAuthPassword());
     });
     Blade::directive('datetime', function ($expression) {
         return "<?php echo with{$expression}->format('d/m/Y H:i'); ?>";
     });
     Blade::directive('simpledate', function ($expression) {
         return "<?php echo with{$expression}->format('d M Y'); ?>";
     });
     Blade::directive('fulldate', function ($expression) {
         return "<?php echo with{$expression}->format('d F Y'); ?>";
     });
     Blade::directive('datetime', function ($expression) {
         return "<?php echo with{$expression}->format('d F Y h:m A'); ?>";
     });
 }
 /**
  * Show the form for editing the specified article.
  *
  * @param $slug
  * @return \Illuminate\Http\Response
  */
 public function edit($slug)
 {
     /*
      * --------------------------------------------------------------------------
      * Show edit form
      * --------------------------------------------------------------------------
      * Populate category into list for build drop down, and try catch old
      * subcategory input because it depends on category, by default subcategory
      * should be exist on edit because all article should have it.
      */
     $article = Article::whereSlug($slug)->firstOrFail();
     $categories = Category::pluck('category', 'id');
     $subcategories = null;
     if (Input::old('category', '') != '') {
         $subcategories = Category::findOrFail(Input::old('category'))->subcategories;
     } else {
         $subcategories = Subcategory::whereCategoryId($article->subcategory->category->id)->get();
     }
     return view('contributor.article_edit', compact('article', 'categories', 'subcategories'));
 }
 /**
  * Remove the specified category from storage.
  *
  * @param Request $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function destroy(Request $request, $id = null)
 {
     /*
      * --------------------------------------------------------------------------
      * Delete category
      * --------------------------------------------------------------------------
      * Check if selected variable is not empty so user intends to select multiple
      * rows at once, and prepare the feedback message according the type of
      * deletion action.
      */
     if (!empty(trim($request->input('selected')))) {
         $delete = DB::transaction(function () use($request) {
             try {
                 $category_ids = explode(',', $request->input('selected'));
                 $delete_subcategory = 0;
                 if ($request->input('selected_sub') != '') {
                     $subcategory_ids = explode(',', $request->input('selected_sub'));
                     $delete_subcategory = Subcategory::whereIn('id', $subcategory_ids)->delete();
                 }
                 $delete = Category::whereIn('id', $category_ids)->delete();
                 return $delete + $delete_subcategory;
             } catch (\Exception $e) {
                 return redirect()->back()->withErrors(['error' => Lang::get('alert.error.transaction')])->withInput();
             }
         });
         $message = Lang::get('alert.category.delete_all', ['count' => $delete]);
     } else {
         $category = Category::findOrFail($id);
         $message = Lang::get('alert.category.delete', ['category' => $category->category]);
         $delete = $category->delete();
     }
     if ($delete instanceof RedirectResponse) {
         return $delete;
     }
     if ($delete) {
         return redirect(route('admin.category.index'))->with(['status' => 'warning', 'message' => $message]);
     } else {
         return redirect()->back()->withErrors(['error' => Lang::get('alert.error.database')]);
     }
 }