--- handlers/location_views_handler_filter_proximity.inc 3 Dec 2008 22:51:23 -0000 1.3 +++ handlers/location_views_handler_filter_proximity.inc 19 Apr 2009 13:02:37 -0000 @@ -83,7 +83,7 @@ $form['value'] = array( '#tree' => TRUE, ); - + $form['value']['latitude'] = array( '#type' => 'textfield', '#title' => t('Latitude'), @@ -152,6 +152,7 @@ } // Used from the distance field. + // IS THIS NOW REDUNDANT? DOES IT NEED RE-WRITING AND USING? function calculate_coords() { if (!empty($this->value['latitude']) && !empty($this->value['longitude'])) { // If there are already coordinates, there's no work for us. @@ -167,7 +168,14 @@ // 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,23 +196,34 @@ } 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') { + 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(); - + $lat = $this->value['latitude']; $lon = $this->value['longitude']; @@ -222,3 +241,4 @@ } } } +