View source
<?php
class views_geojson_bbox_argument extends views_handler_argument {
function option_definition() {
$options = parent::option_definition();
$options['arg_id'] = array(
'default' => 'bbox',
);
return $options;
}
function default_argument_form(&$form, &$form_state) {
parent::default_argument_form($form, $form_state);
$form['no_argument']['#title'] = 'When the filter value is NOT in the URL <em>as a normal Drupal argument</em>';
}
function _get_fields($data_source) {
$field_ids = array();
switch ($data_source['value']) {
case 'latlon':
$field_ids[] = $data_source['latitude'];
$field_ids[] = $data_source['longitude'];
break;
case 'geofield':
$field_ids[] = $data_source['geofield'];
break;
case 'wkt':
$field_ids[] = $data_source['wkt'];
break;
}
return $field_ids;
}
function _get_bbox_coords() {
if (isset($_GET[$this->options['arg_id']])) {
$this->argument = $_GET[$this->options['arg_id']];
}
return $this
->_explode_bbox_coords($this->argument);
}
function _explode_bbox_coords($bbox_coords_str) {
$arg_get_prefix = '?' . $this->options['arg_id'] . '=';
$bbox_coords_str = str_ireplace($arg_get_prefix, '', $bbox_coords_str);
$bbox_coords = explode(',', $bbox_coords_str);
if (count($bbox_coords) == 4) {
$bbox = array(
'left' => $bbox_coords[0],
'bottom' => $bbox_coords[1],
'right' => $bbox_coords[2],
'top' => $bbox_coords[3],
);
return $bbox;
}
}
function query() {
$this
->ensure_my_table();
if (!empty($this->argument) && $this->argument !== TRUE) {
$bbox = $this
->_explode_bbox_coords($this->argument);
}
else {
$bbox = $this
->_get_bbox_coords();
}
if (!isset($bbox['left']) || !isset($bbox['bottom']) || !isset($bbox['right']) || !isset($bbox['top'])) {
return;
}
if (isset($this->view->display_handler->display->display_options['style_options'])) {
$data_source = $this->view->display_handler->display->display_options['style_options']['data_source'];
}
else {
$data_source = $this->view->display['default']->display_options['style_options']['data_source'];
}
if ($data_source['value'] == 'latlon') {
$lat_field_obj = $this->view->field[$data_source['latitude']];
$lon_field_obj = $this->view->field[$data_source['longitude']];
$lat_field_table = $lat_field_obj->table;
$lon_field_table = $lon_field_obj->table;
}
elseif ($data_source['value'] == 'geofield') {
$lat_field_obj = $lon_field_obj = $this->view->field[$data_source['geofield']];
$lat_field_table = $lon_field_table = $lat_field_obj->table;
}
else {
return;
}
if (isset($lat_field_obj->field_info) && $lat_field_obj->field_info['type'] == 'geofield') {
$lat_field_name = $lat_field_obj->field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$lat_field_table]['lat'];
}
else {
$lat_field_name = $lat_field_obj->field;
}
if (isset($lon_field_obj->field_info) && $lon_field_obj->field_info['type'] == 'geofield') {
$lon_field_name = $lon_field_obj->field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$lon_field_table]['lon'];
}
else {
$lon_field_name = $lon_field_obj->field;
}
$this->query
->add_table($lat_field_table);
if ($lon_field_table != $lat_field_table) {
$this->query
->add_table($lon_field_table);
}
$left = $bbox['left'];
$right = $bbox['right'];
if ($right - $left < 360) {
$group = $this->query
->set_where_group('AND');
if ($left > -180) {
$this->query
->add_where($group, "{$lon_field_table}.{$lon_field_name}", $left, '>=');
}
else {
$this->query
->set_where_group('OR', $group);
$left = -1 * ($left % 180);
$this->query
->add_where($group, "{$lon_field_table}.{$lon_field_name}", array(
$left,
0,
), 'BETWEEN');
}
if ($right < 180) {
$this->query
->add_where($group, "{$lon_field_table}.{$lon_field_name}", $right, '<=');
}
else {
$this->query
->set_where_group('OR', $group);
$right = -1 * ($right % 180);
$this->query
->add_where($group, "{$lon_field_table}.{$lon_field_name}", array(
$right,
0,
), 'BETWEEN');
}
}
$bottom = $bbox['bottom'];
$top = $bbox['top'];
$group = $this->query
->set_where_group('AND');
$this->query
->add_where($group, "{$lat_field_table}.{$lat_field_name}", $bottom, '>=');
$this->query
->add_where($group, "{$lat_field_table}.{$lat_field_name}", $top, '<=');
}
}