Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Support\Facades\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Collection;
- use App\Http\Controllers\Controller;
- use Carbon\Carbon;
- class SearchController extends Controller
- {
- public function index(Request $request)
- {
- DB::connection()->enableQueryLog();
- $queries = DB::getQueryLog();
- $user_zip = Request::input('zip');
- $param = array("address"=>$user_zip);
- $response = json_decode(\Geocoder::geocode('json', $param), true);
- $user_lat = $response['results'][0]['geometry']['location']['lat'];
- $user_long = $response['results'][0]['geometry']['location']['lng'];
- /* THE PROVINCE ID */
- for ($i = 0; $i < count($response['results'][0]['address_components']); $i++ ){
- if ($response['results'][0]['address_components'][$i]['types']['0'] == 'locality'){
- $user_city = $response['results'][0]['address_components'][$i]['short_name'];
- }
- if ($response['results'][0]['address_components'][$i]['types']['0'] == 'administrative_area_level_1'){
- $user_province = $response['results'][0]['address_components'][$i]['short_name'];
- }
- }
- $user_province_id = DB::table('provinces')->where('abbr',$user_province)->first()->id;
- /* CHECK IF MUNCHRABBIT EXISTS IN CITY.
- - IF NOT, ERROR.
- - IF SO, RETURN CITY ID */
- $user_city_id = DB::table('cities')->where([['province_id',$user_province_id],['name',$user_city]])->first();
- if ($user_city_id == null){
- return view('search.locate_error');
- }
- $user_city_id = $user_city_id->id;
- /* TODAY'S DATE */
- date_default_timezone_set('America/New_York');
- $date = Carbon::now();
- $formatted_date = $date->format('Y-m-d');
- $header_date = $date->format('l, F j, Y');
- /* JOIN DEALS AND RESTAURANTS */
- $deals_restaurants = DB::table('restaurants')
- ->where('city_id', $user_city_id)
- ->join('deals','restaurants.id','=','deals.restaurant_id')
- ->join('deals_weekdays','deals_weekdays.deal_id','=','deals.id')
- ->where('weekday_id', $date->dayOfWeek)
- ->select('restaurants.name AS restaurant_name',
- 'restaurants.address AS restaurant_address',
- 'restaurants.zip AS restaurant_zip',
- 'restaurants.latitude AS restaurant_latitude',
- 'restaurants.longitude AS restaurant_longitude',
- 'restaurants.phone AS restaurant_phone',
- 'restaurants.website AS restaurant_website',
- 'restaurants.city_id AS restaurant_cityid',
- 'deals.*')->get();
- foreach ($deals_restaurants as $dr){
- $dr->distance = $this -> vincenty($dr->restaurant_latitude, $dr->restaurant_longitude, $user_lat, $user_long);
- }
- $deals_restaurants = $deals_restaurants->sortBy('distance');
- /* IF NO DEALS TODAY, RETURN ERROR */
- if (count($deals_restaurants)==0){
- return view('search.deal_error');
- }
- /* TABLES FOR SEARCH FILTERS */
- $establishments = DB::table('establishments')->get();
- $dealtypes = DB::table('dealtypes')->get();
- $datetypes = DB::table('datetypes')->get();
- $cuisines = DB::table('cuisines')->orderBy('name')->get();
- return view('search.search',
- [
- 'deals_restaurants'=>$deals_restaurants,
- 'header_date'=>$header_date,
- 'city'=>$user_city,
- 'province'=>$user_province,
- 'zip'=>$user_zip,
- 'user_city_id' =>$user_city_id,
- 'user_lat' => $user_lat,
- 'user_long' => $user_long,
- 'formatted_date'=>$formatted_date,
- 'establishments' => $establishments,
- 'dealtypes' => $dealtypes,
- 'datetypes' => $datetypes,
- 'cuisines' => $cuisines
- ]);
- }
- public function searchFilter(Request $request){
- DB::connection()->enableQueryLog();
- $queries = DB::getQueryLog();
- $user_city_id = Request::input('user_city_id');
- $user_city = Request::input('user_city');
- $user_province = Request::input('user_province');
- $user_lat = Request::input('user_lat');
- $user_long = Request::input('user_long');
- $date = strtotime(Request::input('date'));
- $dow = date('w', $date);
- $formatted_date = date('Y-m-d', $date);
- $header_date = date('l, F j, Y',$date);
- $sorttype = intval(Request::input('sorttype'));
- $datetypes = Request::input('datetypes');
- $establishments = Request::input('establishments');
- $dealtypes = Request::input('dealtypes');
- $cuisines=Request::input('cuisines');
- if (is_null($cuisines)){
- $cuisines = DB::table('cuisines')->pluck('id')->toArray();
- }
- if (is_null($datetypes)){
- $datetypes = DB::table('datetypes')->pluck('id')->toArray();
- }
- if (is_null($establishments)){
- $establishments = DB::table('establishments')->pluck('id')->toArray();
- }
- if (is_null($dealtypes)){
- $dealtypes = DB::table('dealtypes')->pluck('id')->toArray();
- }
- $ungrouped_deals_restaurants = DB::table('restaurants')
- ->where('city_id', $user_city_id)
- ->join('restaurants_establishments','restaurants.id','=','restaurants_establishments.restaurant_id')
- ->whereIn('establishment_id',$establishments)
- ->join('deals','restaurants.id','=','deals.restaurant_id')
- ->whereIn('dt_id',$dealtypes)
- ->whereIn('datetype_id',$datetypes)
- ->join('deals_cuisines','deals_cuisines.deal_id','=','deals.id')
- ->whereIn('deals_cuisines.cuisine_id',$cuisines)
- ->leftJoin('deals_weekdays','deals.id','=','deals_weekdays.deal_id')
- ->where('weekday_id', $dow)
- ->leftJoin('deals_limiteddays','deals.id','=','deals_limiteddays.deal_id')
- ->select('restaurants.id AS restaurant_id',
- 'restaurants.name AS restaurant_name',
- 'restaurants.address AS restaurant_address',
- 'restaurants.zip AS restaurant_zip',
- 'restaurants.latitude AS restaurant_latitude',
- 'restaurants.longitude AS restaurant_longitude',
- 'restaurants.phone AS restaurant_phone',
- 'restaurants.website AS restaurant_website',
- 'restaurants.city_id AS restaurant_cityid',
- 'deals_weekdays.weekday_id AS weekday_id',
- 'deals.*')
- ->get();
- $grouped_deals_restaurants = $ungrouped_deals_restaurants->unique();
- foreach ($grouped_deals_restaurants as $dr){
- $dr->distance = $this -> vincenty($dr->restaurant_latitude, $dr->restaurant_longitude, $user_lat, $user_long);
- }
- $deals_restaurants = $grouped_deals_restaurants->sortBy('distance');
- /* TABLES FOR SEARCH FILTERS */
- $establishments = DB::table('establishments')->get();
- $dealtypes = DB::table('dealtypes')->get();
- $datetypes = DB::table('datetypes')->get();
- $cuisines = DB::table('cuisines')->orderBy('name')->get();
- return view('search.search',
- [
- 'deals_restaurants'=>$deals_restaurants,
- 'header_date'=>$header_date,
- 'city'=>$user_city,
- 'province'=>$user_province,
- 'user_city_id' =>$user_city_id,
- 'user_lat' => $user_lat,
- 'user_long' => $user_long,
- 'formatted_date'=>$formatted_date,
- 'establishments' => $establishments,
- 'dealtypes' => $dealtypes,
- 'datetypes' => $datetypes,
- 'cuisines' => $cuisines
- ]);
- }
- /*
- public function searchSort(Request $request){
- $deals_restaurants = json_decode(Request::input('deals_restaurants'));
- $user_city_id = json_decode(Request::input('user_city_id'));
- $province = json_decode(Request::input('province'));
- $city = json_decode(Request::input('city'));
- $user_lat = json_decode(Request::input('user_lat'));
- $user_long = json_decode(Request::input('user_long'));
- $header_date = json_decode(Request::input('header_date'));
- $formatted_date = json_decode(Request::input('formatted_date'));
- $sorttype = intval(Request::input('sorttype'));
- $establishments = DB::table('establishments')->get();
- $dealtypes = DB::table('dealtypes')->get();
- $datetypes = DB::table('datetypes')->get();
- $cuisines = DB::table('cuisines')->get();
- $new_deals_restaurants = collect();
- foreach ($deals_restaurants as $dr){
- $new_deals_restaurants->prepend($dr);
- }
- $new_deals_restaurants->toArray();
- if ($sorttype == 1){
- $new_deals_restaurants->sortBy('distance');
- }
- if ($sorttype == 2){
- $new_deals_restaurants->sortBy('flat_value');
- }
- if ($sorttype == 3){
- $new_deals_restaurants->sortBy('pctdiscount_value');
- }
- if ($sorttype == 4){
- $new_deals_restaurants->sortBy('dollardiscount_value');
- }
- return dd($new_deals_restaurants);
- }*/
- /* DISTANCE EARTH ALGORITHM */
- function vincenty(
- $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
- {
- // convert from degrees to radians
- $latFrom = deg2rad($latitudeFrom);
- $lonFrom = deg2rad($longitudeFrom);
- $latTo = deg2rad($latitudeTo);
- $lonTo = deg2rad($longitudeTo);
- $lonDelta = $lonTo - $lonFrom;
- $a = pow(cos($latTo) * sin($lonDelta), 2) +
- pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
- $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);
- $angle = atan2(sqrt($a), $b);
- return ($angle * $earthRadius)/1000;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement