You are here

function location_views_handler_filter_proximity::query in Location 6.3

Same name and namespace in other branches
  1. 7.5 handlers/location_views_handler_filter_proximity.inc \location_views_handler_filter_proximity::query()
  2. 7.3 handlers/location_views_handler_filter_proximity.inc \location_views_handler_filter_proximity::query()
  3. 7.4 handlers/location_views_handler_filter_proximity.inc \location_views_handler_filter_proximity::query()

File

handlers/location_views_handler_filter_proximity.inc, line 287

Class

location_views_handler_filter_proximity
General proximity filter for location latitude/longitude.

Code

function query() {
  if (empty($this->value)) {
    return;
  }

  // We need to merge with $this->options['value'] for filter values
  // and $this->value for exposed filter values.
  $options = array_merge($this->options, $this->options['value'], $this->value);
  $coordinates = location_views_proximity_get_reference_location($this->view, $options);

  // If we don't have any coordinates or distance, there's nothing to filter
  // by, so don't modify the query at all.
  if (empty($coordinates) || empty($this->value['search_distance'])) {
    return;
  }
  $this
    ->ensure_my_table();
  $lat = $coordinates['latitude'];
  $lon = $coordinates['longitude'];
  $distance_meters = _location_convert_distance_to_meters($this->value['search_distance'], $this->value['search_units']);
  $latrange = earth_latitude_range($lon, $lat, $distance_meters);
  $lonrange = earth_longitude_range($lon, $lat, $distance_meters);

  // Add MBR check (always).
  // In case we go past the 180/-180 mark for longitude.
  if ($lonrange[0] > $lonrange[1]) {
    $where = "{$this->table_alias}.latitude > %f AND {$this->table_alias}.latitude < %f AND (({$this->table_alias}.longitude < 180 AND {$this->table_alias}.longitude > %f) OR ({$this->table_alias}.longitude < %f AND {$this->table_alias}.longitude > -180))";
  }
  else {
    $where = "{$this->table_alias}.latitude > %f AND {$this->table_alias}.latitude < %f AND {$this->table_alias}.longitude > %f AND {$this->table_alias}.longitude < %f";
  }
  $this->query
    ->add_where($this->options['group'], $where, $latrange[0], $latrange[1], $lonrange[0], $lonrange[1]);
  if ($this->operator == 'dist') {

    // Add radius check.
    $this->query
      ->add_where($this->options['group'], earth_distance_sql($lon, $lat, $this->table_alias) . ' < %f', $distance_meters);
  }
}