You are here

function getlocations_fields_handler_filter_distance::query in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_fields/handlers/getlocations_fields_handler_filter_distance.inc \getlocations_fields_handler_filter_distance::query()

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides views_handler_filter::query

File

modules/getlocations_fields/handlers/getlocations_fields_handler_filter_distance.inc, line 285
getlocations_fields_handler_filter_distance.inc @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Class

getlocations_fields_handler_filter_distance
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 = getlocations_fields_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) || !$coordinates['latitude'] || !$coordinates['longitude'] || empty($this->value['search_distance'])) {
    return;
  }
  $this
    ->ensure_my_table();
  $lat = $coordinates['latitude'];
  $lon = $coordinates['longitude'];
  $distance_meters = getlocations_convert_distance_to_meters($this->value['search_distance'], $this->value['search_units']);
  $latrange = getlocations_earth_latitude_range($lat, $lon, $distance_meters);
  $lonrange = getlocations_earth_longitude_range($lat, $lon, $distance_meters);

  // If the table alias is specified, add on the separator.
  $table_alias = empty($this->table_alias) ? '' : $this->table_alias . '.';

  // Add MBR check (always).
  // In case we go past the 180/-180 mark for longitude.
  if ($lonrange[0] > $lonrange[1]) {
    $where = $table_alias . "latitude > :minlat\n      AND " . $table_alias . "latitude < :maxlat\n      AND ((" . $table_alias . "longitude < 180\n      AND " . $table_alias . "longitude > :minlon)\n      OR (" . $table_alias . "longitude < :maxlon\n      AND " . $table_alias . "longitude > -180))";
  }
  else {
    $where = $table_alias . "latitude > :minlat\n      AND " . $table_alias . "latitude < :maxlat\n      AND " . $table_alias . "longitude > :minlon\n      AND " . $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],
  ));
  if ($this->operator == 'dist') {

    // Add radius check.
    $this->query
      ->add_where_expression($this->options['group'], getlocations_earth_distance_sql($lat, $lon, $this->table_alias) . ' < :distance', array(
      ':distance' => $distance_meters,
    ));
  }
}