public function GetRestaurantsList($aData) { $aRestaurants = array(); /* * Get the search term from the Data */ $searchTerms = explode(' ', $aData['searchTerm']); /* * Put the table name in order to be used in updating the query */ $query = DB::table('restaurants'); $aSearchTerms = array(); if (!empty($searchTerms[0])) { /* * Make sure to get all the combination of the data */ foreach ($searchTerms as $term) { $aSearchTerms[] = ucfirst($term); $aSearchTerms[] = strtolower($term); $aSearchTerms[] = ucfirst(strtolower($term)); } /* * This foreach, will update the query, puting the likes * in searching for the used columns. * if you want to serach in another column * just add a new line like how it is written. */ foreach ($aSearchTerms as $term) { $query->where('restaurant_name', 'LIKE', '%' . $term . '%')->orWhere('username', 'LIKE', '%' . $term . '%')->orWhere('bio', 'LIKE', '%' . $term . '%')->orWhere('cuisines', 'LIKE', '%' . $term . '%'); } /* * Get a list of Restaurants IDS */ $aRestaurantsIdsSearchTerm = $query->get(array("id_restaurant")); } /* * If the location data was sent with the query */ if (isset($aData['location']) && $aData['location'] != '') { if (!empty($aData['location'])) { $query->where('id_country', '=', $aData['location']); } } if (isset($aData['no_smoking']) && ($aData['no_smoking'] = 'yes')) { $sSmoking = 'yes'; } else { if (isset($aData['no_smoking']) && ($aData['no_smoking'] = 'no')) { $sSmoking = 'no'; } } if (isset($sSmoking)) { $query->orWhere('smoking_allowed', '=', $sSmoking); } $aRestaurantsIds = $query->get(array("id_restaurant")); if (isset($aRestaurantsIdsSearchTerm)) { array_merge($aRestaurantsIds, $aRestaurantsIdsSearchTerm); } foreach ($aRestaurantsIds as $aRestaurantsId) { $aResults[] = $aRestaurantsId->id_restaurant; } $query = DB::table('food'); if (!empty($searchTerms[0])) { foreach ($aSearchTerms as $term) { $query->where('name', 'LIKE', '%' . $term . '%')->orWhere("type", 'LIKE', '%' . $term . '%')->orWhere("description", 'LIKE', '%' . $term . '%'); } } /* * Check if the needed food is healthy */ if (isset($aData['food_health'])) { $query->where('healthy', '=', $aData['food_health']); } if (isset($aData['price_range'])) { $aPriceRange = explode(" - ", $aData['price_range']); $dFrom = trim(str_replace('$', '', $aPriceRange[0])); $dTo = trim(str_replace('$', '', $aPriceRange[1])); if (isset($dTo) && isset($dFrom)) { $query->whereBetween("price", array($dFrom, $dTo)); } } $aRestaurantsIds = $query->get(array("id_restaurant")); foreach ($aRestaurantsIds as $aRestaurantsId) { $aResults[] = $aRestaurantsId->id_restaurant; } if (!empty($aResults[0])) { $aRestaurants = DB::table("restaurants")->whereIn("id_restaurant", $aResults)->get(Backend::$aRestaurantsParams); foreach ($aRestaurants as $restaurant) { $restaurant->opening_days = Backend::checkifOpen($restaurant->opening_days); } } $aFinalResult = array(); //32.0265737,35.8360242 lat long, use if failed if (isset($aData['userlat']) && isset($aData['userlong'])) { if ($aData['userlat'] != 0 && $aData['userlong'] != 0) { foreach ($aRestaurants as $restaurant) { if (!empty($restaurant->location)) { $point = json_decode($restaurant->location); } if (isset($point->lat)) { $dCalculatedDist = $this->haversineGreatCircleDistance($aData['userlat'], $aData['userlong'], $point->lat, $point->long) / 1000; if ($dCalculatedDist <= $aData['distance']) { $restaurant->location = $dCalculatedDist; $aFinalResult[] = $restaurant; } } } //Search based on location! return $aFinalResult; } } return $aRestaurants; }