Index: handlers/location_handler_argument_location_proximity.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/location/handlers/location_handler_argument_location_proximity.inc,v retrieving revision 1.4 diff -u -p -r1.4 location_handler_argument_location_proximity.inc --- handlers/location_handler_argument_location_proximity.inc 7 Feb 2011 17:29:12 -0000 1.4 +++ handlers/location_handler_argument_location_proximity.inc 14 Feb 2011 22:26:56 -0000 @@ -10,6 +10,13 @@ * Argument handler to accept proximity */ class location_handler_argument_location_proximity extends views_handler_argument { + + function init(&$view, &$options) { + parent::init($view, $options); + + $this->value = array(); + } + function option_definition() { $options = parent::option_definition(); // As only us and uk use miles, make km the default otherwise. @@ -99,51 +106,57 @@ class location_handler_argument_location return TRUE; } - /** - * Set up the query for this argument. - * - * The argument sent may be found at $this->argument. - */ - function query() { - // Get and process argument. + function set_argument($arg) { if ($this->options['type'] == 'postal') { - foreach ($this->view->argument as $argument) { - if ($argument->field == 'distance') { - $arg_parts = explode('_', $this->view->args[$argument->position]); - if (count($arg_parts) == 3) { - $this->value['country'] = drupal_strtolower($arg_parts[0]); - $this->value['postal_code'] = $arg_parts[1]; - $this->value['search_distance'] = $arg_parts[2]; - } - else { - $this->value['postal_code'] = $arg_parts[0]; - $this->value['search_distance'] = $arg_parts[1]; - } - break; - } + $arg_parts = explode('_', $arg); + if (count($arg_parts) == 3) { + $this->value['country'] = drupal_strtolower($arg_parts[0]); + $this->value['postal_code'] = $arg_parts[1]; + $this->value['search_distance'] = $arg_parts[2]; + } + else { + $this->value['postal_code'] = $arg_parts[0]; + $this->value['search_distance'] = $arg_parts[1]; } } else if ($this->options['type'] == 'latlon') { - foreach ($this->view->argument as $argument) { - if ($argument->field == 'distance') { - list($coords, $this->value['search_distance']) = explode('_', $this->view->args[$argument->position]); - list($this->value['latitude'], $this->value['longitude']) = explode(',', $coords); + list($coords, $this->value['search_distance']) = explode('_', $arg); + list($this->value['latitude'], $this->value['longitude']) = explode(',', $coords); + } + $parts = array(); + if (preg_match('/([0-9\.,]+)([A-Za-z]+)/', $this->value['search_distance'], $parts)) { + $this->value['search_distance'] = $parts[1]; + switch ($parts[2]) { + case 'mi': + case 'mile': + case 'miles': + $this->options['search_units'] = 'mile'; + break; + case 'km': + $this->options['search_units'] = 'km'; + break; + case 'm': + case 'meter': + case 'meters': + $this->options['search_units'] = 'm'; + break; + case 'dd': + $this->options['search_units'] = 'dd'; break; - } } } - - // 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'], "1 = 0"); - } - return; + if (!$this->calculate_coords() || empty($this->value['search_distance'])) { + return FALSE; // Failure to validate. } + return parent::set_argument($arg); + } + /** + * Set up the query for this argument. + * + * The argument sent may be found at $this->argument. + */ + function query() { $this->ensure_my_table(); $lat = $this->value['latitude']; @@ -178,11 +191,11 @@ class location_handler_argument_location else { $where = "$this->table_alias.latitude > :minlat AND $this->table_alias.latitude < :maxlat AND $this->table_alias.longitude > :minlon AND $this->table_alias.longitude < :maxlon"; } - $this->query->add_where_expression($this->options['group'], $where, array(':minlat' => $latrange[0], ':maxlat' => $latrange[1], ':minlon' => $lonrange[0], ':maxlon' => $lonrange[1])); + $this->query->add_where_expression(0, $where, array(':minlat' => $latrange[0], ':maxlat' => $latrange[1], ':minlon' => $lonrange[0], ':maxlon' => $lonrange[1])); if ($this->options['search_method'] == 'dist') { // Add radius check. - $this->query->add_where_expression($this->options['group'], earth_distance_sql($lon, $lat, $this->table_alias) . ' < :distance', array(':distance' => $distance_meters)); + $this->query->add_where_expression(0, earth_distance_sql($lon, $lat, $this->table_alias) . ' < :distance', array(':distance' => $distance_meters)); } } }