Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Support\Facades\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Collection;
  8. use App\Http\Controllers\Controller;
  9. use Carbon\Carbon;
  10.  
  11. class SearchController extends Controller
  12. {
  13. public function index(Request $request)
  14. {
  15. DB::connection()->enableQueryLog();
  16. $queries = DB::getQueryLog();
  17.  
  18. $user_zip = Request::input('zip');
  19.  
  20. $param = array("address"=>$user_zip);
  21. $response = json_decode(\Geocoder::geocode('json', $param), true);
  22.  
  23. $user_lat = $response['results'][0]['geometry']['location']['lat'];
  24. $user_long = $response['results'][0]['geometry']['location']['lng'];
  25.  
  26. /* THE PROVINCE ID */
  27. for ($i = 0; $i < count($response['results'][0]['address_components']); $i++ ){
  28. if ($response['results'][0]['address_components'][$i]['types']['0'] == 'locality'){
  29. $user_city = $response['results'][0]['address_components'][$i]['short_name'];
  30. }
  31. if ($response['results'][0]['address_components'][$i]['types']['0'] == 'administrative_area_level_1'){
  32. $user_province = $response['results'][0]['address_components'][$i]['short_name'];
  33. }
  34. }
  35.  
  36. $user_province_id = DB::table('provinces')->where('abbr',$user_province)->first()->id;
  37.  
  38.  
  39.  
  40.  
  41. /* CHECK IF MUNCHRABBIT EXISTS IN CITY.
  42. - IF NOT, ERROR.
  43. - IF SO, RETURN CITY ID */
  44. $user_city_id = DB::table('cities')->where([['province_id',$user_province_id],['name',$user_city]])->first();
  45.  
  46. if ($user_city_id == null){
  47. return view('search.locate_error');
  48. }
  49.  
  50. $user_city_id = $user_city_id->id;
  51.  
  52.  
  53.  
  54.  
  55. /* TODAY'S DATE */
  56. date_default_timezone_set('America/New_York');
  57. $date = Carbon::now();
  58. $formatted_date = $date->format('Y-m-d');
  59. $header_date = $date->format('l, F j, Y');
  60.  
  61. /* JOIN DEALS AND RESTAURANTS */
  62. $deals_restaurants = DB::table('restaurants')
  63. ->where('city_id', $user_city_id)
  64. ->join('deals','restaurants.id','=','deals.restaurant_id')
  65. ->join('deals_weekdays','deals_weekdays.deal_id','=','deals.id')
  66. ->where('weekday_id', $date->dayOfWeek)
  67. ->select('restaurants.name AS restaurant_name',
  68. 'restaurants.address AS restaurant_address',
  69. 'restaurants.zip AS restaurant_zip',
  70. 'restaurants.latitude AS restaurant_latitude',
  71. 'restaurants.longitude AS restaurant_longitude',
  72. 'restaurants.phone AS restaurant_phone',
  73. 'restaurants.website AS restaurant_website',
  74. 'restaurants.city_id AS restaurant_cityid',
  75. 'deals.*')->get();
  76.  
  77. foreach ($deals_restaurants as $dr){
  78. $dr->distance = $this -> vincenty($dr->restaurant_latitude, $dr->restaurant_longitude, $user_lat, $user_long);
  79. }
  80.  
  81. $deals_restaurants = $deals_restaurants->sortBy('distance');
  82.  
  83.  
  84. /* IF NO DEALS TODAY, RETURN ERROR */
  85. if (count($deals_restaurants)==0){
  86. return view('search.deal_error');
  87. }
  88.  
  89.  
  90. /* TABLES FOR SEARCH FILTERS */
  91. $establishments = DB::table('establishments')->get();
  92. $dealtypes = DB::table('dealtypes')->get();
  93. $datetypes = DB::table('datetypes')->get();
  94. $cuisines = DB::table('cuisines')->orderBy('name')->get();
  95.  
  96.  
  97.  
  98.  
  99. return view('search.search',
  100. [
  101. 'deals_restaurants'=>$deals_restaurants,
  102. 'header_date'=>$header_date,
  103. 'city'=>$user_city,
  104. 'province'=>$user_province,
  105. 'zip'=>$user_zip,
  106.  
  107. 'user_city_id' =>$user_city_id,
  108. 'user_lat' => $user_lat,
  109. 'user_long' => $user_long,
  110.  
  111. 'formatted_date'=>$formatted_date,
  112.  
  113. 'establishments' => $establishments,
  114. 'dealtypes' => $dealtypes,
  115. 'datetypes' => $datetypes,
  116. 'cuisines' => $cuisines
  117.  
  118. ]);
  119. }
  120.  
  121.  
  122. public function searchFilter(Request $request){
  123.  
  124. DB::connection()->enableQueryLog();
  125. $queries = DB::getQueryLog();
  126.  
  127. $user_city_id = Request::input('user_city_id');
  128. $user_city = Request::input('user_city');
  129. $user_province = Request::input('user_province');
  130. $user_lat = Request::input('user_lat');
  131. $user_long = Request::input('user_long');
  132.  
  133.  
  134. $date = strtotime(Request::input('date'));
  135. $dow = date('w', $date);
  136. $formatted_date = date('Y-m-d', $date);
  137. $header_date = date('l, F j, Y',$date);
  138.  
  139. $sorttype = intval(Request::input('sorttype'));
  140.  
  141.  
  142.  
  143. $datetypes = Request::input('datetypes');
  144. $establishments = Request::input('establishments');
  145. $dealtypes = Request::input('dealtypes');
  146.  
  147. $cuisines=Request::input('cuisines');
  148.  
  149. if (is_null($cuisines)){
  150. $cuisines = DB::table('cuisines')->pluck('id')->toArray();
  151. }
  152.  
  153.  
  154. if (is_null($datetypes)){
  155. $datetypes = DB::table('datetypes')->pluck('id')->toArray();
  156. }
  157.  
  158. if (is_null($establishments)){
  159. $establishments = DB::table('establishments')->pluck('id')->toArray();
  160. }
  161.  
  162. if (is_null($dealtypes)){
  163. $dealtypes = DB::table('dealtypes')->pluck('id')->toArray();
  164. }
  165.  
  166. $ungrouped_deals_restaurants = DB::table('restaurants')
  167. ->where('city_id', $user_city_id)
  168.  
  169. ->join('restaurants_establishments','restaurants.id','=','restaurants_establishments.restaurant_id')
  170. ->whereIn('establishment_id',$establishments)
  171.  
  172. ->join('deals','restaurants.id','=','deals.restaurant_id')
  173. ->whereIn('dt_id',$dealtypes)
  174. ->whereIn('datetype_id',$datetypes)
  175.  
  176. ->join('deals_cuisines','deals_cuisines.deal_id','=','deals.id')
  177. ->whereIn('deals_cuisines.cuisine_id',$cuisines)
  178.  
  179. ->leftJoin('deals_weekdays','deals.id','=','deals_weekdays.deal_id')
  180. ->where('weekday_id', $dow)
  181.  
  182. ->leftJoin('deals_limiteddays','deals.id','=','deals_limiteddays.deal_id')
  183.  
  184. ->select('restaurants.id AS restaurant_id',
  185. 'restaurants.name AS restaurant_name',
  186. 'restaurants.address AS restaurant_address',
  187. 'restaurants.zip AS restaurant_zip',
  188. 'restaurants.latitude AS restaurant_latitude',
  189. 'restaurants.longitude AS restaurant_longitude',
  190. 'restaurants.phone AS restaurant_phone',
  191. 'restaurants.website AS restaurant_website',
  192. 'restaurants.city_id AS restaurant_cityid',
  193. 'deals_weekdays.weekday_id AS weekday_id',
  194. 'deals.*')
  195.  
  196. ->get();
  197.  
  198. $grouped_deals_restaurants = $ungrouped_deals_restaurants->unique();
  199.  
  200. foreach ($grouped_deals_restaurants as $dr){
  201. $dr->distance = $this -> vincenty($dr->restaurant_latitude, $dr->restaurant_longitude, $user_lat, $user_long);
  202. }
  203. $deals_restaurants = $grouped_deals_restaurants->sortBy('distance');
  204.  
  205. /* TABLES FOR SEARCH FILTERS */
  206. $establishments = DB::table('establishments')->get();
  207. $dealtypes = DB::table('dealtypes')->get();
  208. $datetypes = DB::table('datetypes')->get();
  209. $cuisines = DB::table('cuisines')->orderBy('name')->get();
  210.  
  211. return view('search.search',
  212. [
  213. 'deals_restaurants'=>$deals_restaurants,
  214. 'header_date'=>$header_date,
  215. 'city'=>$user_city,
  216. 'province'=>$user_province,
  217.  
  218. 'user_city_id' =>$user_city_id,
  219. 'user_lat' => $user_lat,
  220. 'user_long' => $user_long,
  221.  
  222. 'formatted_date'=>$formatted_date,
  223.  
  224. 'establishments' => $establishments,
  225. 'dealtypes' => $dealtypes,
  226. 'datetypes' => $datetypes,
  227. 'cuisines' => $cuisines
  228.  
  229. ]);
  230.  
  231. }
  232. /*
  233. public function searchSort(Request $request){
  234.  
  235. $deals_restaurants = json_decode(Request::input('deals_restaurants'));
  236. $user_city_id = json_decode(Request::input('user_city_id'));
  237. $province = json_decode(Request::input('province'));
  238. $city = json_decode(Request::input('city'));
  239. $user_lat = json_decode(Request::input('user_lat'));
  240. $user_long = json_decode(Request::input('user_long'));
  241. $header_date = json_decode(Request::input('header_date'));
  242. $formatted_date = json_decode(Request::input('formatted_date'));
  243.  
  244. $sorttype = intval(Request::input('sorttype'));
  245.  
  246. $establishments = DB::table('establishments')->get();
  247. $dealtypes = DB::table('dealtypes')->get();
  248. $datetypes = DB::table('datetypes')->get();
  249. $cuisines = DB::table('cuisines')->get();
  250.  
  251. $new_deals_restaurants = collect();
  252. foreach ($deals_restaurants as $dr){
  253. $new_deals_restaurants->prepend($dr);
  254. }
  255.  
  256. $new_deals_restaurants->toArray();
  257.  
  258. if ($sorttype == 1){
  259. $new_deals_restaurants->sortBy('distance');
  260. }
  261.  
  262. if ($sorttype == 2){
  263. $new_deals_restaurants->sortBy('flat_value');
  264. }
  265.  
  266. if ($sorttype == 3){
  267. $new_deals_restaurants->sortBy('pctdiscount_value');
  268. }
  269.  
  270. if ($sorttype == 4){
  271. $new_deals_restaurants->sortBy('dollardiscount_value');
  272. }
  273.  
  274.  
  275. return dd($new_deals_restaurants);
  276.  
  277. }*/
  278.  
  279.  
  280. /* DISTANCE EARTH ALGORITHM */
  281. function vincenty(
  282. $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
  283. {
  284. // convert from degrees to radians
  285. $latFrom = deg2rad($latitudeFrom);
  286. $lonFrom = deg2rad($longitudeFrom);
  287. $latTo = deg2rad($latitudeTo);
  288. $lonTo = deg2rad($longitudeTo);
  289.  
  290. $lonDelta = $lonTo - $lonFrom;
  291. $a = pow(cos($latTo) * sin($lonDelta), 2) +
  292. pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  293. $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);
  294.  
  295. $angle = atan2(sqrt($a), $b);
  296. return ($angle * $earthRadius)/1000;
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement