View source
<?php
define('GEOFIELD_PROXIMITY_REGEXP_PATTERN', '/[,_ +]+/');
class geofield_handler_argument_proximity extends views_handler_argument {
function option_definition() {
$options = parent::option_definition();
$options['proximity']['operation'] = array(
'default' => 'lt',
);
$options['proximity']['default_radius'] = array(
'default' => 100,
);
$options['proximity']['radius_unit'] = array(
'default' => GEOFIELD_KILOMETERS,
);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['proximity'] = array(
'#type' => 'fieldset',
'#title' => t('Filter locations by proximity to a reference point'),
'#description' => t('The reference point is normally apppend to the URL as lat,lon, or if you have the !geocoder module enabled, as a partial address, like "/New York"', array(
'!geocoder' => l('Geocoder', 'http://drupal.org/project/geocoder'),
)),
);
$form['proximity']['operation'] = array(
'#type' => 'select',
'#title' => t('Select locations'),
'#options' => array(
'lt' => t('Inside radius'),
'gt' => t('Outside radius'),
),
'#default_value' => $this->options['proximity']['operation'],
'#description' => t("Reference point coordinates (<strong>lat,lon</strong>) and <strong>distance</strong> are typically appended to the URL. A fallback may be entered above under <em>Provide default value</em> as either a <em>Fixed value</em> or as <em>PHP Code</em>. <br/>In all cases use this format: <strong>lat,lon distance</strong>. You may omit either <strong>lat,lon</strong> or <strong>distance</strong>. Instead of spaces and comma's, you can use underscores."),
);
$form['proximity']['default_radius'] = array(
'#type' => 'textfield',
'#title' => t('Default radius'),
'#size' => 8,
'#default_value' => $this->options['proximity']['default_radius'],
'#description' => t("Used if no distance is specified. If left blank <em>Fixed value</em> or <em>PHP Code</em> will be used instead."),
);
$form['proximity']['radius_unit'] = array(
'#type' => 'select',
'#title' => t('Unit of distance'),
'#options' => geofield_radius_options(),
'#default_value' => $this->options['proximity']['radius_unit'],
'#description' => t('Select the unit of distance.'),
);
}
function query($group_by = FALSE) {
$lat_lon_dist = array();
foreach ($this->view->argument as $argument) {
if ($argument->field == 'field_geofield_distance') {
$lat_lon_dist = $this
->parseArg($argument);
break;
}
}
if (count($lat_lon_dist) < 3) {
return;
}
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);
}
function parseArg($argument) {
if (!empty($this->view->args) && ($lat_lon_dist = $this
->parseLatLonDistArg($this->view->args[$argument->position]))) {
return $lat_lon_dist;
}
$args = array_slice(arg(), $argument->position + 1);
return $this
->parseAddress($args);
}
function parseLatLonDistArg($arg_string) {
$args = array_filter(preg_split(GEOFIELD_PROXIMITY_REGEXP_PATTERN, $arg_string));
foreach ($args as $value) {
if (!is_numeric($value)) {
return FALSE;
}
}
if (count($args) == 3) {
return array(
'latitude' => reset($args),
'longitude' => next($args),
'distance' => next($args),
);
}
$lat = reset($args);
$lon = next($args);
if ($lat !== FALSE && $lon !== FALSE) {
if ($dist = $this
->getDefaultDist()) {
return array(
'latitude' => $lat,
'longitude' => $lon,
'distance' => $dist,
);
}
return FALSE;
}
if ($lat !== FALSE && $lon === FALSE) {
$dist = $lat;
$defaults = $this
->getDefaultLatLonDist();
if (count($defaults) < 2) {
return FALSE;
}
return array(
'latitude' => reset($defaults),
'longitude' => next($defaults),
'distance' => $dist,
);
}
return FALSE;
}
private function parseAddress($args) {
if (empty($args)) {
return FALSE;
}
$dist = is_numeric(end($args)) ? array_pop($args) : $this
->getDefaultDist();
$address = implode(', ', $args);
if (!empty($address) && module_exists('geocoder')) {
$all_geocoder_engines = array_keys(geocoder_handler_info('text'));
$geocoder_engine = in_array('google', $all_geocoder_engines) ? 'google' : reset($all_geocoder_engines);
if ($geocoded_data = geocoder($geocoder_engine, $address)) {
return array(
'latitude' => $geocoded_data
->getY(),
'longitude' => $geocoded_data
->getX(),
'distance' => $dist,
);
}
}
return FALSE;
}
private function getDefaultLatLonDist() {
$default_arg = $this
->get_default_argument();
return $default_arg ? array_filter(preg_split(GEOFIELD_PROXIMITY_REGEXP_PATTERN, $default_arg)) : array();
}
public function getDefaultDist() {
if (!empty($this->options['proximity']['default_radius'])) {
return $this->options['proximity']['default_radius'];
}
$defaults = $this
->getDefaultLatLonDist();
if (empty($defaults) || count($defaults) == 2) {
return 100;
}
return isset($defaults[2]) ? $defaults[2] : reset($defaults);
}
}