/**
  * Show the application welcome screen to the user.
  *
  * @return Response
  */
 public function index()
 {
     DB::connection()->enableQueryLog();
     $data['location'] = GeoIP::getLocation();
     $l_city = $data['location']['city'];
     $l_state = $data['location']['state'];
     $agent = new Agent();
     $get_city = \App\City::where('city', '=', $l_city)->take(1)->get();
     if (count($get_city) != 0) {
         foreach ($get_city as $c) {
             $city = $c->id;
         }
     } else {
         $city = 894;
         $data['location']['city'] = 'Phoenix';
         $data['location']['state'] = 'AZ';
     }
     $data['recent_restaurants'] = \App\Restaurants::where('having_menu', '=', '1')->where('city_id', '=', $city)->orderBy(DB::raw('RAND()'))->take(4)->get();
     $data['recent_reviews'] = \App\Restaurant_Reviews::orderBy(DB::raw('RAND()'))->leftJoin('restaurants', 'restaurants_reviews.restaurants_id', '=', 'restaurants.id')->leftJoin('city', 'restaurants.city_id', '=', 'city.id')->leftJoin('state', 'restaurants.state_id', '=', 'state.id')->take(6)->get();
     //$data['nearest_zip'] = \App\Zip::where('zip', '>', (int)session('geoip-locations.postal_code')-10)->where('zip', '<', (int)session('geoip-locations.postal_code')+10)
     //  ->take(5)->get();
     //dd(DB::getQueryLog());
     //dd($data['recent_reviews']);
     if ($agent->isMobile()) {
         return view('mobile_home')->with($data);
     } else {
         return view('home')->with($data);
     }
 }
 public function submit()
 {
     if (Input::get('rating') == 'one') {
         $rating = 1;
     }
     if (Input::get('rating') == 'two') {
         $rating = 2;
     }
     if (Input::get('rating') == 'three') {
         $rating = 3;
     }
     if (Input::get('rating') == 'four') {
         $rating = 4;
     }
     if (Input::get('rating') == 'five') {
         $rating = 5;
     }
     DB::table('restaurants_reviews')->insert(['restaurants_id' => Input::get('rid'), 'user_id' => 0, 'text' => 'qwe', 'review_text' => Input::get('text'), 'rating' => $rating, 'source' => 'RL', 'source_link' => 'RL', 'updated_at' => '', 'user_name' => 'Guest', 'user_location' => Session::get('geoip-location.city') . ', ' . Session::get('geoip-location.state'), 'spin_status' => 1]);
     echo '<script>alert("Thank You for your valuable review");</script>';
     $data['restaurant'] = \App\Restaurants::where('permalink', '=', Input::get('rlink'))->take(1)->get();
     $agent = new Agent();
     if ($agent->isMobile()) {
         return \View::make('mobile_restaurant')->with('data', $data);
     } else {
         return \View::make('restaurant')->with('data', $data);
     }
 }
 function request_online_order()
 {
     if (Request::ajax()) {
         $data = Input::all();
         \App\Restaurants::where('id', '=', $data['id'])->increment('request_order');
     }
 }
 public static function normalSearch($user_config)
 {
     $recent = \App\Restaurants::where('name', 'like', '%' . $user_config['keywords'] . '%')->take(10)->get();
     return $recent;
 }
 public static function getBannerSearchData($user_config)
 {
     if ($user_config['name'] != '' && $user_config['city'] != '' && $user_config['state'] != '' && $user_config['zip'] != '') {
         $result['city'] = \App\City::where('city', 'LIKE', $user_config['city'] . '%')->pluck('id');
         $result['state'] = \App\State::where('state', 'LIKE', $user_config['state'] . '%')->pluck('id');
         $result_set = Restaurants::where('name', 'LIKE', $user_config['name'] . '%')->where('state_id', $result['state'])->where('city_id', $result['city'])->where('zip', $user_config['zip'])->get();
         return $result_set;
     } else {
         if ($user_config['name'] != '' || $user_config['city'] != '' || $user_config['state'] != '' || $user_config['zip'] != '') {
             if ($user_config['name'] != '') {
                 $result_set = Restaurants::where('name', 'LIKE', $user_config['name'] . '%')->get();
                 return $result_set;
             } else {
                 if ($user_config['city'] != '') {
                     $result = \App\City::where('city', 'LIKE', $user_config['city'] . '%')->pluck('id');
                     $result_set = Restaurants::where('city_id', $result)->get();
                     return $result_set;
                 } else {
                     if ($user_config['name'] != '' && $user_config['city'] != '') {
                         $result = \App\City::where('city', 'LIKE', $user_config['city'] . '%')->pluck('id');
                         $result_set = Restaurants::where('city_id', $result)->where('name', 'LIKE', $user_config['name'] . '%')->get();
                         return $result_set;
                     } else {
                         return false;
                     }
                 }
             }
         }
     }
 }
Route::post('write-a-review', array('as' => 'write-a-review/submit', 'uses' => 'WriteReviewController@submit'));
Route::get('write-a-review', function () {
    $data['meta_title'] = 'Write a Review for the Restaurants | Restaurant Listings|';
    $data['meta_description'] = 'Write a review | Restaurants Review | Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
    $data['meta_keywords'] = 'Write A Review, Restaurants Reviews, Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
    $data['location'] = Session::get('geoip-location.city') . ', ' . Session::get('geoip-location.state');
    $agent = new Agent();
    if ($agent->isMobile()) {
        return View::make('mobile_write_review')->with($data);
    } else {
        return View::make('write_review')->with($data);
    }
});
Route::post('write-a-review/search', array('as' => 'write-a-review/search', 'uses' => 'WriteReviewController@search'));
Route::get('write-a-review/{permalink}', function ($permalink) {
    $data['restaurant'] = \App\Restaurants::where('permalink', '=', $permalink)->take(1)->get();
    $data['meta_title'] = 'Write a Review for the restaurant | Restaurant Listings|';
    $data['meta_description'] = 'Write a review | Restaurants Review | Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
    $data['meta_keywords'] = 'Write A Review, Restaurants Reviews, Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
    $agent = new Agent();
    if ($agent->isMobile()) {
        return View::make('mobile_write_review_landing')->with($data);
    } else {
        return View::make('write_review_landing')->with($data);
    }
});
Route::get('voice', function () {
    return View::make('voice');
});
Route::post('voice', 'SearchController@voice');
Route::controllers(['auth' => 'Auth\\AuthController', 'password' => 'Auth\\PasswordController']);
 public function homeSearch()
 {
     DB::connection()->enableQueryLog();
     $user_config = array('keywords' => \Input::get('keywords'), 'location' => \Input::get('location'));
     $ltype = '';
     if (!is_numeric($user_config['location'])) {
         if (strpos($user_config['location'], ',') === false) {
             $data['location'] = $user_config['location'];
             $ltype = 'city';
         } else {
             $loc = explode(', ', $user_config['location']);
             $data['search_city'] = $loc[0];
             $data['search_state'] = $loc[1];
             $data['location'] = $data['search_city'] . ', ' . $data['search_state'];
             $ltype = 'default';
         }
     } else {
         //Assign zip code to the data
         $data['location'] = $user_config['location'];
         $ltype = 'zip';
     }
     //Get the filter options matching the location
     $data['filter_options']['city'] = \App\City::take(5)->get();
     if (count(Input::get()) == 3) {
         if ($ltype == 'default') {
             $search_type = 'default';
         } elseif ($ltype == 'city') {
             $search_type = 'default_city';
         } else {
             $search_type = 'default_zip';
         }
     } else {
         if ($ltype == 'default') {
             $search_type = 'filter';
         } elseif ($ltype == 'city') {
             $search_type = 'filter_city';
         } else {
             $search_type = 'filter_zip';
         }
     }
     //echo $search_type;
     //dd(Input::get());
     switch ($search_type) {
         case 'default':
             $data['restaurants'] = \App\Restaurants::join('city', 'city.id', '=', 'restaurants.city_id')->join('state', 'state.id', '=', 'restaurants.state_id')->join('restaurants_info', 'restaurants_info.restaurants_id', '=', 'restaurants.id')->where(function ($query) use($user_config) {
                 $query->where('restaurants.name', 'LIKE', '%' . $user_config['keywords'] . '%')->orWhere('restaurants.categories', 'LIKE', '%' . $user_config['keywords'] . '%');
             })->where('city.city', '=', $loc[0])->where('state.short', '=', $loc[1])->orderBy('restaurants.rank', 'desc')->orderBy('restaurants.categories', 'asc')->paginate(10);
             foreach ($data['restaurants'] as $rest) {
                 $reviews = $this->getReviewsThree($rest->permalink);
                 //$data['restaurants'][$rest->permalink] = $rest;
                 $rest->reviews_group = $reviews;
                 //dd($rest);
             }
             $data['meta_title'] = 'Find the Best ' . $user_config['keywords'] . ' Restaurants in ' . $loc[0] . ', ' . $loc[1] . ' | Restaurant Listings|';
             $data['meta_description'] = $user_config['keywords'] . ' in ' . $loc[0] . ', ' . $loc[1] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             $data['meta_keywords'] = $loc[0] . ', ' . $loc[1] . ', ' . $user_config['keywords'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             session(["search_city" => $loc[0], "search_state" => $loc[1], "search_zip" => ""]);
             break;
         case 'default_city':
             $data['restaurants'] = \App\Restaurants::join('city', 'city.id', '=', 'restaurants.city_id')->join('restaurants_info', 'restaurants_info.restaurants_id', '=', 'restaurants.id')->where(function ($query) use($user_config) {
                 $query->where('restaurants.name', 'LIKE', '%' . $user_config['keywords'] . '%')->orWhere('restaurants.categories', 'LIKE', '%' . $user_config['keywords'] . '%');
             })->where('city.city', '=', $data['location'])->orderBy('restaurants.rank', 'desc')->orderBy('restaurants.categories', 'asc')->paginate(10);
             foreach ($data['restaurants'] as $rest) {
                 $reviews = $this->getReviewsThree($rest->permalink);
                 //$data['restaurants'][$rest->permalink] = $rest;
                 $rest->reviews_group = $reviews;
                 //dd($rest);
             }
             $data['meta_title'] = 'Find the Best ' . $user_config['keywords'] . ' Restaurants in ' . $data['location'] . ' | Restaurant Listings|';
             $data['meta_description'] = $user_config['keywords'] . ' in ' . $data['location'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             $data['meta_keywords'] = $data['location'] . ', ' . $user_config['keywords'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             session(["search_city" => $data['location'], "search_state" => "", "search_zip" => ""]);
             break;
         case 'default_zip':
             //Get restaurants matching the keywords and the location
             $data['restaurants'] = \App\Restaurants::join('city', 'city.id', '=', 'restaurants.city_id')->join('state', 'state.id', '=', 'restaurants.state_id')->join('restaurants_info', 'restaurants_info.restaurants_id', '=', 'restaurants.id')->where(function ($query) use($user_config) {
                 $query->where('restaurants.name', 'LIKE', '%' . $user_config['keywords'] . '%')->orWhere('restaurants.categories', 'LIKE', '%' . $user_config['keywords'] . '%');
             })->where('restaurants.zip', 'LIKE', $user_config['location'])->orderBy('restaurants.rank', 'desc')->orderBy('restaurants.categories', 'asc')->paginate(10);
             foreach ($data['restaurants'] as $rest) {
                 $reviews = $this->getReviewsThree($rest->permalink);
                 //$data['restaurants'][$rest->permalink] = $rest;
                 $rest->reviews_group = $reviews;
                 //dd($rest);
             }
             $data['meta_title'] = 'Find the Best ' . $user_config['keywords'] . ' Restaurants in ' . $user_config['location'] . ' | Restaurant Listings|';
             $data['meta_description'] = $user_config['keywords'] . ' in ' . $user_config['location'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             $data['meta_keywords'] = $user_config['location'] . ', ' . $user_config['keywords'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             session(["search_city" => "", "search_state" => "", "search_zip" => $user_config['location']]);
             break;
         case 'filter':
             $data['restaurants'] = \App\Restaurants::join('city', 'city.id', '=', 'restaurants.city_id')->join('state', 'state.id', '=', 'restaurants.state_id')->join('restaurants_info', 'restaurants_info.restaurants_id', '=', 'restaurants.id')->where(function ($query) use($user_config) {
                 $query->where('restaurants.name', 'LIKE', '%' . $user_config['keywords'] . '%')->orWhere('restaurants.categories', 'LIKE', '%' . $user_config['keywords'] . '%');
             })->where('city.city', '=', $loc[0])->where('state.short', '=', $loc[1])->where(function ($query) {
                 $category = Input::has('category') ? Input::get('category') : NULL;
                 if (isset($category)) {
                     foreach ($category as $c) {
                         $query->orWhere('restaurants.categories', 'LIKE', '%' . $c . '%');
                     }
                 }
                 $ordering = Input::has('ordering') ? Input::get('ordering') : NULL;
                 if (isset($ordering)) {
                     foreach ($ordering as $o) {
                         $query->where('restaurants_info.more_info', 'LIKE', '%' . $o . '%');
                     }
                 }
                 $features = Input::has('features') ? Input::get('features') : NULL;
                 if (isset($features)) {
                     foreach ($features as $f) {
                         $query->where('restaurants_info.more_info', 'LIKE', '%' . $f . '%');
                     }
                 }
                 $reservation = Input::has('reservation') ? 'Takes Reservations  --> Yes' : NULL;
                 if (isset($reservation)) {
                     $query->where('restaurants_info.more_info', 'LIKE', '%' . $reservation . '%');
                 }
                 $price_1 = Input::has('price_1') ? '$' : NULL;
                 $price_2 = Input::has('price_2') ? '$$' : NULL;
                 $price_3 = Input::has('price_3') ? '$$$' : NULL;
                 $price_4 = Input::has('price_4') ? '$$$$' : NULL;
                 if (isset($price_1) || isset($price_2) || isset($price_3) || isset($price_4)) {
                     $query->where(function ($query) use($price_1, $price_2, $price_3, $price_4) {
                         $query->where('restaurants_info.price_range', '=', $price_1)->orWhere('restaurants_info.price_range', '=', $price_2)->orWhere('restaurants_info.price_range', '=', $price_3)->orWhere('restaurants_info.price_range', '=', $price_4);
                     });
                 }
             })->orderBy('restaurants.rank', 'desc')->orderBy('restaurants.categories', 'asc')->paginate(10);
             foreach ($data['restaurants'] as $rest) {
                 $reviews = $this->getReviewsThree($rest->permalink);
                 //$data['restaurants'][$rest->permalink] = $rest;
                 $rest->reviews_group = $reviews;
                 //dd($rest);
             }
             $data['meta_title'] = 'Find the Best ' . $user_config['keywords'] . ' Restaurants in ' . $loc[0] . ', ' . $loc[1] . ' | Restaurant Listings|';
             $data['meta_description'] = $user_config['keywords'] . ' in ' . $loc[0] . ', ' . $loc[1] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             $data['meta_keywords'] = $loc[0] . ', ' . $loc[1] . ', ' . $user_config['keywords'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             session(["search_city" => $loc[0], "search_state" => $loc[1], "search_zip" => ""]);
             break;
         case 'filter_city':
             $data['restaurants'] = \App\Restaurants::join('city', 'city.id', '=', 'restaurants.city_id')->join('restaurants_info', 'restaurants_info.restaurants_id', '=', 'restaurants.id')->where(function ($query) use($user_config) {
                 $query->where('restaurants.name', 'LIKE', '%' . $user_config['keywords'] . '%')->orWhere('restaurants.categories', 'LIKE', '%' . $user_config['keywords'] . '%');
             })->where('city.city', '=', $data['location'])->where(function ($query) {
                 $category = Input::has('category') ? Input::get('category') : NULL;
                 $reservation = Input::has('reservation') ? 'Takes Reservations  --> Yes' : NULL;
                 if (isset($category)) {
                     foreach ($category as $c) {
                         $query->orWhere('restaurants.categories', 'LIKE', '%' . $c . '%');
                     }
                 }
                 if (isset($reservation)) {
                     $query->where('restaurants_info.more_info', 'LIKE', '%' . $reservation . '%');
                 }
                 $price_1 = Input::has('price_1') ? '$' : NULL;
                 $price_2 = Input::has('price_2') ? '$$' : NULL;
                 $price_3 = Input::has('price_3') ? '$$$' : NULL;
                 $price_4 = Input::has('price_4') ? '$$$$' : NULL;
                 if (isset($price_1) || isset($price_2) || isset($price_3) || isset($price_4)) {
                     $query->where(function ($query) use($price_1, $price_2, $price_3, $price_4) {
                         $query->where('restaurants_info.price_range', '=', $price_1)->orWhere('restaurants_info.price_range', '=', $price_2)->orWhere('restaurants_info.price_range', '=', $price_3)->orWhere('restaurants_info.price_range', '=', $price_4);
                     });
                 }
             })->orderBy('restaurants.rank', 'desc')->orderBy('restaurants.categories', 'asc')->paginate(10);
             foreach ($data['restaurants'] as $rest) {
                 $reviews = $this->getReviewsThree($rest->permalink);
                 //$data['restaurants'][$rest->permalink] = $rest;
                 $rest->reviews_group = $reviews;
                 //dd($rest);
             }
             $data['meta_title'] = 'Find the Best ' . $user_config['keywords'] . ' Restaurants in ' . $data['location'] . ' | Restaurant Listings|';
             $data['meta_description'] = $user_config['keywords'] . ' in ' . $data['location'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             $data['meta_keywords'] = $data['location'] . ', ' . $user_config['keywords'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             session(["search_city" => $data['location'], "search_state" => "", "search_zip" => ""]);
             break;
         case 'filter_zip':
             $data['restaurants'] = \App\Restaurants::join('city', 'city.id', '=', 'restaurants.city_id')->join('state', 'state.id', '=', 'restaurants.state_id')->join('restaurants_info', 'restaurants_info.restaurants_id', '=', 'restaurants.id')->where(function ($query) use($user_config) {
                 $query->where('restaurants.name', 'LIKE', '%' . $user_config['keywords'] . '%')->orWhere('restaurants.categories', 'LIKE', '%' . $user_config['keywords'] . '%');
             })->where('restaurants.zip', 'LIKE', $user_config['location'])->where(function ($query) {
                 $category = Input::has('category') ? Input::get('category') : NULL;
                 $reservation = Input::has('reservation') ? 'Takes Reservations  --> Yes' : NULL;
                 if (isset($category)) {
                     foreach ($category as $c) {
                         $query->orWhere('restaurants.categories', 'LIKE', '%' . $c . '%');
                     }
                 }
                 if (isset($reservation)) {
                     $query->where('restaurants_info.more_info', 'LIKE', '%' . $reservation . '%');
                 }
                 $price_1 = Input::has('price_1') ? '$' : NULL;
                 $price_2 = Input::has('price_2') ? '$$' : NULL;
                 $price_3 = Input::has('price_3') ? '$$$' : NULL;
                 $price_4 = Input::has('price_4') ? '$$$$' : NULL;
                 if (isset($price_1) || isset($price_2) || isset($price_3) || isset($price_4)) {
                     $query->where(function ($query) use($price_1, $price_2, $price_3, $price_4) {
                         $query->where('restaurants_info.price_range', '=', $price_1)->orWhere('restaurants_info.price_range', '=', $price_2)->orWhere('restaurants_info.price_range', '=', $price_3)->orWhere('restaurants_info.price_range', '=', $price_4);
                     });
                 }
             })->orderBy('restaurants.rank', 'desc')->orderBy('restaurants.categories', 'asc')->paginate(10);
             foreach ($data['restaurants'] as $rest) {
                 $reviews = $this->getReviewsThree($rest->permalink);
                 //$data['restaurants'][$rest->permalink] = $rest;
                 $rest->reviews_group = $reviews;
                 //dd($rest);
             }
             $data['meta_title'] = 'Find the Best ' . $user_config['keywords'] . ' Restaurants in ' . $user_config['location'] . ' | Restaurant Listings|';
             $data['meta_description'] = $user_config['keywords'] . ' in ' . $user_config['location'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             $data['meta_keywords'] = $user_config['location'] . ', ' . $user_config['keywords'] . ' Online food Order, Get Menu, Reviews, Contact, Location Maps, Directions';
             session(["search_city" => "", "search_state" => "", "search_zip" => $user_config['location']]);
             break;
     }
     $data['cuisine'] = DB::table('categories')->get();
     foreach ($data['restaurants'] as $r) {
         /*    if($r->hours !!= ''){
                          $hours = explode('||', $r->hours);
         
                          foreach($hours as $h)
                          {
                              $ho = explode(' -->', $h);
         
                              if((date('D') == str_replace(' ', '',$ho[0])) && (strpos($ho[1], 'Closed') === false)){
                                  if(strpos($ho[1], 'Open 24 hours') === false){
                                      $r->id = $r->restaurants_id;
         
                                      $td_hours = explode('-', $ho[1]);
                                      $open_time = strtotime(str_replace(' ', '', $td_hours[0]));
                                      $close_time = strtotime(str_replace(' ', '', $td_hours[1]));
                                      if((strtotime(date('g:ia')) > $open_time) && (strtotime(date('g:ia')) < $close_time))
                                      {
                                          $r->opened = 'yes';
                                          break;
                                      }else{
                                          $r->opened = 'no';
                                          break;
                                      }
                                  }else{
                                      $r->opened = 'yes';
                                  }
                              }else{
                                  $r->opened = 'no';
                              }
                          }
                      }else{
                          $r->opened = 'no';
                      }*/
         $r->opened = 'yes';
     }
     $agent = new Agent();
     $agent = $agent->isMobile();
     //dd($data);
     //dd(DB::getQueryLog());
     if ($agent != false) {
         return \View::make('mobile_search')->with($data);
     } else {
         return \View::make('search')->with($data);
     }
 }