You are here

function geofield_handler_argument_proximity::query in Geofield 7.2

Set up the where clause for the contextual filter argument.

Overrides views_handler_argument::query

File

views/handlers/geofield_handler_argument_proximity.inc, line 65
Geofield contextual filter argument handler for Views.

Class

geofield_handler_argument_proximity
The proximity argument may be appended to URL in the following format: /lat,lon_dist where dist is a positive number representing a circular proximity in either kilometers or miles, as configured through the contextual filter UI.

Code

function query($group_by = FALSE) {
  $lat_lon_dist = array();
  foreach ($this->view->argument as $argument) {
    if ($argument->field == 'field_geofield_distance') {

      // Get and process args.
      $lat_lon_dist = $this
        ->parseArg($argument);
      break;
    }
  }

  // At this point we expect $lat_lon_dist to contain 3 numeric values.
  if (count($lat_lon_dist) < 3) {
    return;
  }

  // Provide hook_geofield_handler_default_argument_alter(), so args may be
  // altered by other modules.
  drupal_alter('geofield_handler_argument_proximity', $this, $lat_lon_dist);
  $table = $this
    ->ensure_my_table() ? $this->table_alias : $this->table;
  $haversine_args = array(
    'earth_radius' => $this->options['proximity']['radius_unit'],
    'origin_latitude' => $lat_lon_dist['latitude'],
    'origin_longitude' => $lat_lon_dist['longitude'],
    'destination_latitude' => $table . '.' . $this->definition['field_name'] . '_lat',
    'destination_longitude' => $table . '.' . $this->definition['field_name'] . '_lon',
  );
  $formula = geofield_haversine($haversine_args);
  $operator = $this->options['proximity']['operation'] == 'gt' ? '>' : '<';
  $where = $formula . $operator . $lat_lon_dist['distance'];
  $this->query
    ->add_where_expression(0, $where);
}