Ejemplo n.º 1
0
 public function getSearch($term = "")
 {
     if (Auth::user()->allow_search == 0) {
         App::abort(403, 'Insufficient permissions.');
     }
     $page_size = 24;
     $start = 0;
     if (Request::ajax()) {
         $search_term = Input::get('search_term');
     } else {
         $search_term = $term;
     }
     $actor_ids = Actor::where("name", "LIKE", "%" . $search_term . "%")->join('titles_actors', 'titles_actors.actor_id', '=', 'actors.id')->lists('title_id');
     $director_ids = Director::where("name", "like", "%" . $search_term . "%")->join('titles_directors', 'titles_directors.director_id', '=', 'directors.id')->lists('title_id');
     $producer_ids = Producer::where("name", "LIKE", "%" . $search_term . "%")->join('titles_producers', 'titles_producers.producer_id', '=', 'producers.id')->lists('title_id');
     $writer_ids = Writer::where("name", "LIKE", "%" . $search_term . "%")->join('titles_writers', 'titles_writers.writer_id', '=', 'writers.id')->lists('title_id');
     $ids = array_merge($actor_ids, $director_ids, $producer_ids, $writer_ids);
     if (empty($ids)) {
         $ids = array(0);
     }
     $myFaves = Favorite::where("user_id", "=", Auth::user()->id)->get();
     $fave_array = array();
     foreach ($myFaves as $fave_rec) {
         $fave_array[] = $fave_rec->title_id;
     }
     if (Request::ajax()) {
         if (Input::has('page')) {
             $start = (intval(Input::get('page')) - 1) * $page_size;
         }
         $return_more = Input::get('fetch') == "more";
         $columns = $return_more ? array("*") : array("id", "name");
         $tmptitles = Title::select($columns)->where('status_id', '=', '3')->where(function ($query) use($search_term, $ids) {
             $query->whereIn("id", $ids)->orWhere("name", "like", "%" . $search_term . "%")->orWhere("actors", "like", "%" . $search_term . "%")->orWhere("directors", "like", "%" . $search_term . "%")->orWhere("producers", "like", "%" . $search_term . "%")->orWhere("writers", "like", "%" . $search_term . "%");
         });
         if ($return_more) {
             $tmptitles = $tmptitles->with(array("genre", "thumbnail"));
             $titles = array_slice($tmptitles->get()->toArray(), $start, $page_size);
             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])
                 }
             }
             return json_encode(array('titles' => View::make('titles.title_grid_template', array('titles' => $titles, 'mode' => "normal"))->render(), 'totalCount' => $tmptitles->count(), 'mode' => 'search_mode', 'currentPageCount' => count($titles)));
         } else {
             $titles = $tmptitles->get()->toArray();
             foreach ($titles as $key => $title) {
                 $titles[$key]['name'] = repair_a_the($title['name']);
             }
             return $this->makeSuccessArray(true, array("title_results" => $titles, "found" => count($titles) ? "1" : "0"));
         }
     } else {
         $search_term = $term;
         $genre_param = "";
         // placeholder stuff
         $genre_label = "Results for: ";
         $genre_label .= "<span style='font-size:35px'>&ldquo;</span>";
         $genre_label .= $search_term;
         $genre_label .= "<span style='font-size:35px'>&rdquo;</span>";
         // Prep the left sidebar filtering widget data
         $genres = Genre::orderBy("name", "asc")->get()->toArray();
         $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);
         }
         // ------------------  Finally, the actual query to get matching titles ------------------
         $tempresults = Title::with(array("genre", "thumbnail", "producttype"))->where('status_id', '=', '3')->where(function ($query) use($search_term, $ids) {
             $query->whereIn("id", $ids)->orWhere("name", "like", "%" . $search_term . "%")->orWhere("actors", "like", "%" . $search_term . "%")->orWhere("directors", "like", "%" . $search_term . "%")->orWhere("producers", "like", "%" . $search_term . "%")->orWhere("writers", "like", "%" . $search_term . "%");
         });
         $results = array_slice($tempresults->get()->toArray(), $start, $page_size);
         foreach ($results as $key => $title) {
             if (in_array($title['id'], $fave_array)) {
                 $results[$key]['faved'] = 1;
             } else {
                 $results[$key]['faved'] = 0;
             }
             if (count($title['thumbnail']) > 0) {
                 $results[$key]['thumbpath'] = asset(Config::get('nbc.imageRootImagePath') . '/' . $title['thumbnail'][0]['file']);
             } else {
                 $results[$key]['thumbpath'] = asset('images/publicThumb.jpg');
             }
         }
         $data = array('collections' => $collection_dropdown, 'titles' => $results, 'checked_genre' => $genre_param, 'first_collection_id' => $first_collection_id, 'initial_view_col_link' => '/collection/manage/' . $first_collection_id, 'logged_in_user_id' => $user_id, 'mode' => "search_mode");
         $total_count = $tempresults->count();
         return View::make('titles.list', compact("data", "total_count", "genres", "genre_label", "sidebar_categories"));
     }
 }