diff --git handlers/location_views_handler_filter_proximity.inc handlers/location_views_handler_filter_proximity.inc index c8e368f..696dae2 100644 --- handlers/location_views_handler_filter_proximity.inc +++ handlers/location_views_handler_filter_proximity.inc @@ -167,7 +167,14 @@ class location_views_handler_filter_proximity extends views_handler_filter { // Zip code lookup. if (!empty($this->value['postal_code']) && !empty($this->value['country'])) { - $coord = location_latlon_rough($this->value); + $location = array('country'=> $this->value['country'], 'postal_code' => $this->value['postal_code']); + $res = db_query("SELECT latitude, longitude FROM {zipcodes} WHERE"." zip = '%s' AND country = '%s'", $location['postal_code'], $location['country']); + if ($r = db_fetch_array($res)) { + $this->value['latitude'] = $r['latitude']; + $this->value['longitude'] = $r['longitude']; + } + $coord = location_latlon_rough($location); + if ($coord) { $this->value['latitude'] = $coord['lat']; $this->value['longitude'] = $coord['lon']; @@ -188,20 +195,34 @@ class location_views_handler_filter_proximity extends views_handler_filter { } function query() { + $this->value['country'] = variable_get('location_default_country', 'us'); if (empty($this->value)) { return; } - - // Coordinates available? - if (!$this->calculate_coords()) { - // Distance set? - if (!empty($this->value['search_distance'])) { - // Hmm, distance set but unable to resolve coordinates. - // Force nothing to match. - $this->query->add_where($this->options['group'], "0"); - } + + if (empty($this->value['postal_code'])) { return; } + if (!empty($this->value['postal_code'])) { + // uk postcodes are weird - we need to make a special case: + if ($this->value['country'] == 'uk') { + $postcode = strtoupper(str_replace(' ','',$this->value['postal_code'])); + if (preg_match("/^[A-Z]{1,2}[0-9]{2,3}[A-Z]{2}$/",$postcode) || preg_match("/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/",$postcode) || preg_match("/^GIR0[A-Z]{2}$/",$postcode)) { + preg_match('/^[a-zA-Z]*[0-9 ]+/', $this->value['postal_code'], $matches); + $this->value['postal_code'] = substr_replace(str_replace(' ', '', $matches[0]), '', -1); + } + } + + $res = db_query("SELECT latitude, longitude FROM {zipcodes} WHERE"." zip = '%s' AND country = '%s'", $this->value['postal_code'], $this->value['country']); + if ($r = db_fetch_array($res)) { + $this->value['latitude'] = $r['latitude']; + $this->value['longitude'] = $r['longitude']; + } + } + if (empty($this->value['search_distance'])) { + $this->value['search_distance'] = 1; + } + $this->ensure_my_table();