Exemplo n.º 1
0
 public function getList($param = null, $genre_param = null)
 {
     // We may want to separate the ajax calls in another route, but for now, I just used this feature of Laravel, Request::ajax(),
     // that detects if the call to this route was via Ajax, and included it with the regular call for the initial page load
     // of title listing
     $user_id = Auth::user()->id;
     $user = User::find($user_id);
     $usertype = $user->usertype_id;
     $myFaves = Favorite::where("user_id", "=", $user_id)->get();
     $fave_array = array();
     foreach ($myFaves as $fave_rec) {
         $fave_array[] = $fave_rec->title_id;
     }
     if (Request::ajax()) {
         // 3 ways to get here:  1.) The "Get More" button in list.blade.php, 2.) ProductType/Category checkboxes on left side 3.) the Sorting Dropdown at upper right
         $totalcount = 0;
         if (Input::has('page')) {
             $page = Input::get('page');
             // The url paramater in the $.get call  would include the page parameter
         } else {
             $page = 1;
         }
         if (Input::has('sortby')) {
             $sortparam = Input::get('sortby');
         } else {
             $sortparam = "a-z";
         }
         if ($sortparam == "a-z") {
             $sortstring_a = 'titles.name';
             $sortstring_b = 'asc';
         } else {
             if ($sortparam == "z-a") {
                 $sortstring_a = 'titles.name';
                 $sortstring_b = 'desc';
             } else {
                 $sortstring_a = 'titles.created_at';
                 $sortstring_b = 'desc';
             }
         }
         $tempresult = Title::with(array("genre", "thumbnail"));
         // "producttype"));
         // prod id's not relevant until they give us the final decision on taxonomy implementation
         // ---------------------------------------------------------------------------
         /* if ( Input::has('prodids') ) 
                     {
                         $prodid_array =  Input::get('prodids');
                         
                         $tempresult = $tempresult->whereHas('producttype', function($producttype) use ($prodid_array)
                         {
                             $producttype->whereIn('id',  $prodid_array);
             
                         });
         
                     }*/
         if ($param == "favorites") {
             $tempresult = $tempresult->with(array("favorite"))->whereHas('favorite', function ($favorite) use($user_id) {
                 $favorite->where('user_id', "=", $user_id);
             });
         }
         if (Input::has('genres')) {
             $genre_array = Input::get('genres');
             $tempresult = $tempresult->with(array("genre"))->whereHas('genre', function ($genre) use($genre_array) {
                 $genre->whereIn('genre_id', $genre_array);
                 //->with('genretitle');
             });
         }
         if (Input::has('categories')) {
             $category_array = Input::get('categories');
             $tempresult = $tempresult->whereIn('producttype_id', $category_array);
         }
         if ($usertype == 2 || $usertype == 5 || $usertype == 3) {
             $tempresult = $tempresult->where("status_id", "=", '3');
         } else {
             if (Input::has('published_status')) {
                 $tempresult = $tempresult->where("status_id", "=", Input::get('published_status'));
             }
         }
         $titles = $tempresult->orderBy($sortstring_a, $sortstring_b)->paginate(24);
         foreach ($titles as $key => $title) {
             // error_log($title);
             if (in_array($title['id'], $fave_array)) {
                 $titles[$key]['faved'] = 1;
             } else {
                 $titles[$key]['faved'] = 0;
             }
             if (count($title['thumbnail']) > 0) {
                 $titles[$key]['thumbpath'] = asset(Config::get('nbc.imageRootImagePath') . '/' . $title['thumbnail'][0]['file']);
             } else {
                 $titles[$key]['thumbpath'] = asset('images/publicThumb.jpg');
                 //unset($titles[$key])
             }
         }
         $totalcount = $tempresult->count();
         $success = is_null($titles) ? false : true;
         $success = true;
         if ($success) {
             if ($param) {
                 $param_send = $param;
             } else {
                 $param_send = "normal";
             }
             $data = json_encode(array('titles' => View::make('titles.title_grid_template', array('titles' => $titles, 'mode' => $param))->render(), 'totalCount' => $totalcount, 'mode' => $param_send, 'currentPageCount' => $titles->count()));
             return $data;
         } else {
             return $this->makeSuccessArray($success);
         }
     } else {
         // Normal call to the route for a title listing here
         $genre_label = "";
         $category_label = "";
         // Prep the left sidebar filtering widget data
         $genres = Genre::orderBy("name", "asc")->where('show', '=', 1)->get()->toArray();
         // Prep the left sidebar filtering widget data
         $sidebar_categories = $this->getCategoriesForSidebar();
         // If this input exists, it means User clicked "Add More Titles" button
         // From the "View Collections" page
         if (Input::has('default_collection_id')) {
             $default_collection_id = Input::get('default_collection_id');
         } else {
             $default_collection_id = $this->getUserLastCollectionUsed();
         }
         $user_id = Auth::user()->id;
         //$collections = Collection::where("user_id","=",$user_id)->get(array('id', 'name'))->toArray();
         $collections = Collection::select('id', 'name')->where("user_id", "=", $user_id)->where("status_id", "<>", 6)->orderBy('name')->get()->toArray();
         $collection_dropdown = array();
         foreach ($collections as $collection) {
             $shortened_coll_name = str_limit($collection['name'], 25);
             $collection_dropdown[$collection['id']] = $shortened_coll_name;
         }
         if (isset($default_collection_id) && $default_collection_id) {
             $first_collection_id = $default_collection_id;
         } else {
             reset($collection_dropdown);
             $first_collection_id = key($collection_dropdown);
         }
         //$tempresult = Title::with(array("genre","thumbnail", "producttype"))->orderBy('created_at', 'desc');
         $tempresult = Title::with(array("genre", "thumbnail", "producttype"))->orderBy('name', 'asc');
         if ($param) {
             $param_send = $param;
         } else {
             $param_send = "normal";
         }
         if ($param == "favorites") {
             $tempresult = $tempresult->with(array("favorite"))->where("status_id", "=", 3)->whereHas('favorite', function ($favorite) use($user_id) {
                 $favorite->where('user_id', "=", $user_id);
             });
         }
         if ($param == "recommended") {
             $tempresult = $tempresult->with(array("recommendation"))->where("status_id", "=", 3)->whereHas('recommendation', function ($recommendation) use($user_id) {
                 $recommendation->where('user_id', "=", $user_id);
             });
         }
         if ($param == "genre") {
             if (!$genre_param) {
                 Redirect::to("/");
             }
             $genre_label = Genre::where("id", "=", $genre_param)->pluck("name");
             $tempresult = $tempresult->with(array("genre"))->where("status_id", "=", 3)->whereHas('genre', function ($genre) use($genre_param) {
                 $genre->where('genre_id', "=", $genre_param);
             });
         }
         if ($param == "category") {
             if (!$genre_param) {
                 Redirect::to("/");
             }
             $category_label = $this->getCategoryLabel($genre_param);
             $tempresult = $tempresult->where("status_id", "=", 3)->where('producttype_id', "=", $genre_param);
         }
         $rev_category = array("tvseries" => 10, "featurefilms" => 11, "orighomerelease" => 12, "tvmovie" => 15, "miniseries" => 17, "specials" => 18);
         if (isset($rev_category[$param])) {
             $genre_param = $rev_category[$param];
             $param_send = "category";
             $tempresult = $tempresult->with(array("producttype"))->where("status_id", "=", 3)->whereHas('producttype', function ($producttype) use($genre_param) {
                 $producttype->where('id', "=", $genre_param);
             });
             $category_label = $this->categories[$genre_param];
         }
         if ($usertype == 2 || $usertype == 5 || $usertype == 3) {
             $tempresult = $tempresult->where("status_id", "=", '3');
         }
         $titles = $tempresult->with(array("thumbnail"))->take(24)->get()->toArray();
         //dd($titles);
         foreach ($titles as $key => $title) {
             if (in_array($title['id'], $fave_array)) {
                 $titles[$key]['faved'] = 1;
             } else {
                 $titles[$key]['faved'] = 0;
             }
             if (count($title['thumbnail']) > 0) {
                 $titles[$key]['thumbpath'] = asset(Config::get('nbc.imageRootImagePath') . '/' . $title['thumbnail'][0]['file']);
             } else {
                 $titles[$key]['thumbpath'] = asset('images/publicThumb.jpg');
             }
         }
         $total_count = $tempresult->count();
         $data = array('collections' => $collection_dropdown, 'titles' => $titles, 'checked_genre' => $genre_param, 'checked_category' => $genre_param, 'first_collection_id' => $first_collection_id, 'initial_view_col_link' => '/collection/manage/' . $first_collection_id, 'logged_in_user_id' => $user_id, 'mode' => $param_send);
         return View::make('titles.list', compact("data", "total_count", "genres", "genre_label", "category_label", "sidebar_categories"));
     }
 }