Index: location_views_handler_filter_proximity.inc =================================================================== RCS file: /cvs/drupal/contributions/modules/location/handlers/location_views_handler_filter_proximity.inc,v retrieving revision 1.3 diff -u -r1.3 location_views_handler_filter_proximity.inc --- location_views_handler_filter_proximity.inc 3 Dec 2008 22:51:23 -0000 1.3 +++ location_views_handler_filter_proximity.inc 23 Apr 2009 12:37:50 -0000 @@ -4,6 +4,8 @@ /** * General proximity filter for location latitude/longitude. */ +include(drupal_get_path('module','location').'/geocoding/google.inc'); + class location_views_handler_filter_proximity extends views_handler_filter { // This is always single, because of the distance field's possible dependency // on it. @@ -21,6 +23,7 @@ 'default' => array( 'latitude' => '', 'longitude' => '', + 'city' => '', 'postal_code' => '', 'country' => '', 'search_distance' => 100, @@ -49,6 +52,7 @@ '#title' => t('Form mode'), // @@@ Less stupid title? '#options' => array( 'latlon' => t('Latitude / Longitude input'), + 'city' => t('City'), 'postal' => t('Postal Code / Country'), 'postal_default' => t('Postal Code (assume default country)'), ), @@ -99,6 +103,14 @@ '#dependency' => array('edit-options-type' => array('latlon')), ); + $form['value']['city'] = array( + '#type' => 'textfield', + '#title' => t('City'), + '#default_value' => $this->value['city'], + '#process' => array('views_process_dependency'), + '#dependency' => array('edit-options-type' => array('city')), + ); + $form['value']['postal_code'] = array( '#type' => 'textfield', '#title' => t('Postal code'), @@ -143,6 +155,9 @@ unset($form[$key]['latitude']); unset($form[$key]['longitude']); } + if ($type != 'city') { + unset($form[$key]['city']); + } if ($type != 'postal' && $type != 'postal_default') { unset($form[$key]['postal_code']); } @@ -167,7 +182,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']; @@ -181,6 +203,24 @@ return false; } } + + if ($this->options['type'] == 'city') { + // City lookup + if (!empty($this->value['city'])) { + $this->value['country'] = variable_get('location_default_country', 'us'); + $location = array('country'=> $this->value['country'], 'city' => $this->value['city']); + + $coord = google_geocode_location($location); + if ($coord) { + $this->value['latitude'] = $coord['lat']; + $this->value['longitude'] = $coord['lon']; + } + else { + return false; + } + } + } + if (empty($this->value['latitude']) || empty($this->value['longitude'])) { return false; } @@ -188,20 +228,45 @@ } 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 (($this->options['type'] == 'city') && empty($this->value['city'])) { + return; + } + if (($this->options['type'] == 'postal' || $this->options['type'] == 'postal_default') && 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['city'])) { + $location = array('country'=> $this->value['country'], 'city' => $this->value['city']); + + $coord = google_geocode_location($location); + if ($coord) { + $this->value['latitude'] = $coord['lat']; + $this->value['longitude'] = $coord['lon']; + } + } + if (empty($this->value['search_distance'])) { + $this->value['search_distance'] = 1; + } $this->ensure_my_table();