· 5 years ago · Jan 15, 2021, 07:58 PM
1public function showStores($city_alias){
2 //Variants
3 /**
4 * 1. Nothing no city, no q, no location
5 * 2. Just city
6 * 3. City and Query
7 * 4. Just location
8 * 5. Location and query
9 * 6. Just Qury
10 */
11
12 //DATA
13 $hasQuery = \Request::has('q')&&strlen(\Request::input('q'))>1;
14 $hasLocation = \Request::has('location')&&strlen(\Request::input('location'))>1;
15 $expedition = \Request::input('expedition') && strlen(\Request::input('expedition'))>1 && \Request::input('expedition') == "delivery" ? 'can_deliver' : 'can_pickup';
16 $hasCity = $city_alias != null;
17
18 $sections=[];
19 $aditionInTitle=$hasQuery?" ".__('where you can find')." ".\Request::input('q'):"";
20
21 //Default headers
22 $settings=Settings::findOrFail(1)->first();
23 config(['global.header_title' => $settings->header_title]);
24 config(['global.header_subtitle' => $settings->header_subtitle]);
25
26
27 if($hasCity){
28 //CITY BASED SEARCH CASE 4 and 5
29
30 //Find the city
31 $city=City::where('alias',$city_alias)->first();
32
33 config(['global.header_title' => $city->header_title]);
34 config(['global.header_subtitle' => $city->header_subtitle]);
35
36 if(!$city){abort(404);}
37 $theRestorants=Restorant::where(['active'=>1,'city_id'=>$city->id]);
38
39 if($hasQuery){
40 //With Query
41 $restorants=$this->filterRestaurantsOnQuery($theRestorants->pluck('id')->toArray());
42 }else{
43 //No query
44 $restorants=$theRestorants->get()->shuffle();
45 }
46 array_push($sections,['title'=>__('Restaurants in')." ".$city->name.$aditionInTitle,'restorants' =>$restorants]);
47 }
48 else if($hasLocation){
49 //LOCATION BASED SEARCH CASE 4 and 5
50 //First, find the provided location, convert it to lat/lng
51 $client = new \GuzzleHttp\Client();
52 $geocoder = new Geocoder($client);
53 $geocoder->setApiKey(config('geocoder.key'));
54
55
56 try {
57 $geoResults=$geocoder->getCoordinatesForAddress(\Request::input('location'));
58 } catch (CouldNotGeocode $e) {
59 report($e);
60 return view('restorants.error_location',['message'=>"The provided api key GOOGLE_MAPS_API_KEY has restrictions and we can not geocode the address. Please look into the documentation of this product to see what APIs are required to be enabled."]);
61 }
62
63
64
65 if($geoResults['formatted_address']=="result_not_found"){
66 //No results found
67 return view('restorants.error_location',['message'=>'You have provided address that we can not find']);
68 }else{
69 //Ok, we have lat and lng
70 $restorantsQuery = Restorant::where(['active' => 1, $expedition => 1]);
71
72 $restorantsWithGeoIDS=$this->scopeIsWithinMaxDistance($restorantsQuery,$geoResults['lat'],$geoResults['lng'],env('LOCATION_SEARCH_RADIUS',50))->pluck('id');
73
74 $restorants=Restorant::whereIn('id',$restorantsWithGeoIDS)->get();
75
76
77 //Furthure, check restaurant's delivery area
78 $allRestorantDelivering=[];
79 $nearBytDelivering=[];
80 $featuredDelivering=[];
81
82 $allRestorantDeliveringIDS=[];
83 $nearBytDeliveringIDS=[];
84 $featuredDeliveringIDS=[];
85
86 $hasRestorantsWithDelivery = false;
87
88 $limitOfNearby=env('MOST_NEARBY_LIMIT',4);
89
90 $point = json_decode('{"lat": '.$geoResults['lat'].', "lng":'.$geoResults['lng'].'}');
91 foreach ($restorants as $key => $restorant) {
92 //Check if restorant delivery area is within
93 if(!is_array($restorant->radius)){
94 continue;
95 }
96 $polygon= json_decode(json_encode($restorant->radius));
97 $numItems=sizeof($restorant->radius);
98 //dd($restorant->radius);
99 //dd($numItems);
100
101 //If disabled deliver - no delivery area
102
103 if($restorant->can_deliver == 0 || (isset($polygon[0])&&$this->withinArea($point,$polygon,$numItems))){
104 $hasRestorantsWithDelivery = true;
105
106 //add in allRestorantDelivering
107 array_push($allRestorantDelivering,$restorant);
108 array_push($allRestorantDeliveringIDS,$restorant->id);
109
110 if(count($nearBytDelivering)<$limitOfNearby){
111 array_push($nearBytDelivering,$restorant);
112 array_push($nearBytDeliveringIDS,$restorant->id);
113 }
114
115 //Featured
116 if($restorant->is_featured.""=="1"){
117 array_push($featuredDelivering,$restorant);
118 array_push($featuredDeliveringIDS,$restorant->id);
119 }
120 }
121 }
122
123 if($hasQuery){
124 //CASE 5
125 //we have some query
126 $allRestorantDeliveringCollection = collect($this->filterRestaurantsOnQuery($allRestorantDeliveringIDS));
127 $nearBytDeliveringCollection = collect($this->filterRestaurantsOnQuery($nearBytDeliveringIDS));
128 $featuredDeliveringCollection = collect($this->filterRestaurantsOnQuery($featuredDeliveringIDS));
129 }else{
130 //CASE 4
131 //No additinal qury
132 $allRestorantDeliveringCollection = collect($allRestorantDelivering)->shuffle();
133 $nearBytDeliveringCollection = collect($nearBytDelivering)->shuffle();
134 $featuredDeliveringCollection = collect($featuredDelivering)->shuffle();
135 }
136
137
138 if($featuredDeliveringCollection->count()>0){
139 array_push($sections,['title'=>__('Featured restaurants').$aditionInTitle,'restorants' =>$featuredDeliveringCollection]);
140 }
141 if($nearBytDeliveringCollection->count()>0){
142 array_push($sections,['title'=>__('Popular restaurants near you').$aditionInTitle,'restorants' =>$nearBytDeliveringCollection]);
143 }
144
145 $allReastaurantsTitle=__('All restaurants delivering to your address');
146 /*if(env('DISABLE_DELIVER',false)){
147 $allReastaurantsTitle=__('All restaurants');
148 }*/
149 if(!$hasRestorantsWithDelivery){
150 $allReastaurantsTitle=__('All restaurants');
151 }
152
153 array_push($sections,['title'=>$allReastaurantsTitle.$aditionInTitle,'restorants' => $allRestorantDeliveringCollection]);
154 }
155
156 }
157 else if($hasQuery){
158 //CASE 6
159 //IS IS Query String Search
160 $restorants=$this->filterRestaurantsOnQuery(Restorant::where(['active' => 1, $expedition => 1])->pluck('id')->toArray());
161 array_push($sections,['title'=>__('Restaurants').$aditionInTitle,'restorants' =>$restorants]);
162 }
163 else{
164 //CASE 1 - nothing at all
165 //No query at all
166 array_push($sections,['title'=>__('Popular restaurants'),'restorants' =>Restorant::where('active', 1)->get()->shuffle()]);
167 }
168
169
170 $banners_data = Banners::all();
171 $banners = [];
172 foreach($banners_data as $key => $banner){
173 if((new Carbon($banner->active_to))->gt(new Carbon($banner->active_from)) && Carbon::now()->between(new Carbon($banner->active_from), new Carbon($banner->active_to)) || (new Carbon($banner->active_from))->eq(new Carbon($banner->active_to)) && (new Carbon(Carbon::now()->toDateString()))->eq(new Carbon($banner->active_from)) && (new Carbon(Carbon::now()->toDateString()))->eq(new Carbon($banner->active_to))){
174 array_push($banners, $banner);
175 }
176 }
177
178 //Set the cookie of the last entered address
179 $lastaddress = Cookie::get('lastaddress');
180 $response = new \Illuminate\Http\Response(view('welcome',[
181 'sections' => $sections,
182 'lastaddress'=> $lastaddress,
183 'banners' => $banners,
184 ]));
185 if(\Request::has('location')&&strlen(\Request::input('location'))>1){
186 $response->withCookie(cookie('lastaddress', \Request::input('location'), 120));
187 }
188 return $response;
189
190
191 }