public function searchBano($sellzone_id, $q, $limit = 30, $offset = 0, $lat = 0, $lng = 0) { return call_user_func_array([$this, 'suggestAddressBySellzone'], func_get_args()); if (fnmatch('* *', $q)) { $tab = explode(' ', $q); $first = Arrays::first($tab); if (is_numeric($first)) { $number = true; array_shift($tab); $q = implode(' ', $tab); } } $keyCache = 'l.cachersBanos.' . sha1(serialize(func_get_args())); $collection = redis()->get($keyCache); $cachedZip = redis()->get('zips.covered.' . $sellzone_id); if (!$cachedZip) { $coll = []; $zips = Model::Coveredcity()->where(['sellzone_id', '=', (int) $sellzone_id])->cursor(); foreach ($zips as $zip) { $coll[] = $zip['zip']; } redis()->set('zips.covered.' . $sellzone_id, serialize($coll)); $cachedZip = $coll; } else { $cachedZip = unserialize($cachedZip); } $sz = Model::Sellzone()->refresh()->find((int) $sellzone_id); $lat = 0 < $lat ? $lat : (double) $sz->latitude; $lng = 0 < $lng ? $lng : (double) $sz->longitude; $calcDistance = 0 < $lat && 0 < $lng; if (!$collection) { $collection = []; if ($sz) { $q = str_replace(' ', '%', Inflector::lower(Inflector::unaccent($q))); $cursor = Model::GeoBano()->where(['sellzone_id', '=', $sz->id])->where(['street', 'LIKE', "%{$q}%"])->cursor(); $c = 0; while ($row = $cursor->fetch()) { unset($row['id']); unset($row['sellzone_id']); unset($row['created_at']); unset($row['updated_at']); $address = $row['street']; ksort($row); $distances = distanceKmMiles(floatval($row['lng']), floatval($row['lat']), floatval($lng), floatval($lat)); if ((double) $distances['km'] > 5) { continue; } $old = $row; if (isset($number)) { $row = $this->getCoords($first . ' ' . $row['street'] . ' ' . $row['zip'] . ' ' . $row['city']); } else { $row = $this->getCoords($row['street'] . ' ' . $row['zip'] . ' ' . $row['city']); } if (isset($row['zip'])) { if (!in_array($row['zip'], $cachedZip)) { continue; } } else { continue; } if (isset($row['city'])) { if (is_array($row['city'])) { $row['city_id'] = $row['city']['code']; $row['city'] = $old['city']; } } $row['address'] = $address; if ($calcDistance) { if (isset($row['lng']) && isset($row['lat']) && isset($lat) && isset($lng)) { $distances = distanceKmMiles(floatval($row['lng']), floatval($row['lat']), floatval($lng), floatval($lat)); $km = floatval($distances['km']); $row['distance'] = $km; } else { $row['distance'] = 0; } } foreach ($row as $k => $v) { if (fnmatch('*_id', $k)) { $row[$k] = (int) $v; } else { if (is_numeric($v) && (fnmatch('*.*', $v) || fnmatch('*.*', $v))) { $row[$k] = (double) $v; } } } if (isset($row['address_label'])) { $row['name'] = $row['address_label']; unset($row['address_label']); } if (fnmatch($sz->department . '*', $row['zip'])) { // if (count($collection) >= $limit) { // return $this->sortBy($collection, 'distance'); // } $collection[] = $row; $c++; } } } redis()->set($keyCache, serialize($collection)); } else { $collection = unserialize($collection); } if (empty($collection)) { $sugColl = []; $suggest = $this->suggestAddressBySellzone($sellzone_id, $q); foreach ($suggest as $sug) { if (isset($sug['lng']) && isset($sug['lat']) && isset($lat) && isset($lng)) { $distances = distanceKmMiles(floatval($sug['lng']), floatval($sug['lat']), floatval($lng), floatval($lat)); $km = floatval($distances['km']); $sug['distance'] = $km; $sugColl[] = $sug; } } return $this->sortBy($sugColl, 'distance'); } if (isset($calcDistance)) { if ($calcDistance) { $collection = $this->sortBy($collection, 'distance'); } } return array_slice($collection, $offset, $limit); }