';
- $form['value']['wrapper_location_open']['#value'] = '
';
+
$form['value']['location'] = array(
'#title' => t('Location'),
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $this->options['value']['location'],
'#description' => t('Location where to start to search from.'),
- '#process' => array('views_process_dependency'),
'#dependency' => array(
'radio:options[location_as]' => array('location'),
),
@@ -120,12 +118,10 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
'#default_value' => $this->options['value']['node'],
'#options' => $this->get_node_options(),
'#description' => t('Node where to start to search from.'),
- '#process' => array('views_process_dependency'),
'#dependency' => array(
'radio:options[location_as]' => array('node'),
),
);
- $form['value']['wrapper_location_close']['#value'] = '
';
$form['value']['unit'] = array(
'#title' => t('Unit of measurement'),
'#type' => 'select',
@@ -135,7 +131,6 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
'#prefix' => '
',
'#suffix' => '
',
);
- $form['value']['wrapper_close']['#value'] = '
';
$form['location_as'] = array(
'#type' => 'radios',
'#title' => t('Location'),
@@ -146,53 +141,62 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
),
);
}
-
+
/**
* Provide a form for setting the operator.
*/
function operator_form(&$form, &$form_state) {
parent::operator_form($form, $form_state);
- // At the end of the day proximity search is a rough estimation.
+ // At the end of the day proximity search is a rough estimation.
$this->unset_attributes($form['operator']['#options'], array('<=', '=', '!=', '>='));
}
-
+
/**
* Render our chunk of the exposed filter form when selecting
*/
function exposed_form(&$form, &$form_state) {
-
+
if (empty($this->options['exposed'])) {
return;
}
-
+
$force_operator = FALSE;
$operators = $this->operator_options();
if (empty($this->options['expose']['use_operator'])) {
$this->options['expose']['use_operator'] = TRUE;
$force_operator = TRUE;
}
-
+
parent::exposed_form($form, $form_state);
-
+
if ($force_operator) {
$operator = $this->options['expose']['operator'];
$form[$operator]['#type'] = 'hidden';
- $form[$operator]['#value'] = $form[$operator]['#default_value'];
+ $form[$operator]['#value'] = $form[$operator]['#default_value'];
$this->unset_attributes($form[$operator], array('#options'));
}
-
+
// When exposed pull location and unit out of value form item.
if (!empty($this->options['expose']['identifier'])) {
-
+
// Unset useless form elements.
$filter = &$form[$this->options['expose']['identifier']];
- $this->unset_attributes($filter, array('wrapper_open', 'wrapper_close', 'wrapper_location_open', 'wrapper_location_close'));
+
+ // Strip dependencies off on exposed form.
+ $key = $this->options['expose']['identifier'];
+ foreach (element_children($form[$key]) as $el) {
+ if (!empty($form[$key][$el]['#dependency']) && $form[$key][$el]['#title'] == 'Location') {
+ $form[$key][$el]['#dependency'] = array();
+ }
+ }
+
+
$this->unset_attributes($form, array('location_as'));
-
+
// Turn exposed form into a fieldset to have better control over its rendering.
$filter['#type'] = 'fieldset';
- $filter['#attributes'] = array('class' => "openlayers-proximity-filter-exposed");
-
+ $filter['#attributes'] = array('class' => array("openlayers-proximity-filter-exposed"));
+
// Expose unit of measurement form, if necessary.
if ($this->filter_item_is_exposed('unit')) {
$name = $this->rename_exposed_filter_item($form, 'unit');
@@ -201,9 +205,9 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
else {
$this->unset_attributes($filter, array('unit'));
}
-
+
// Expose location form, if necessary.
- $name = $this->options['location_as'];
+ $name = $this->options['location_as'];
if ($this->filter_item_is_exposed($name)) {
$name = $this->rename_exposed_filter_item($form, $name);
@@ -214,9 +218,9 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
}
else {
$this->unset_attributes($filter, array($name));
- $filter[$name]['#value'] = $this->filter_item_is_exposed('unit') ? ''. $this->get_suffix() .'
' : '';
- }
-
+ $filter[$name]['#value'] = $this->filter_item_is_exposed('unit') ? '' . $this->get_suffix() . '
' : '';
+ }
+
if ($this->options['location_as'] == 'location') {
$this->unset_attributes($filter, array('node'));
}
@@ -226,24 +230,24 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
// Add meaningful suffix to value.
if (isset($filter['value'])) {
-
+
$filter['value']['#size'] = 3;
$filter['value']['#field_prefix'] = $force_operator ? $operators[$this->options['operator']] : '';
- $filter['value']['#field_suffix'] = $this->get_suffix();;
+ $filter['value']['#field_suffix'] = $this->get_suffix();
$this->unset_attributes($filter['value'], array('#default_value'));
}
-
+
// Add meaningful prefix/suffix to min max.
if (isset($filter['min']) && isset($filter['max'])) {
-
+
$filter['min']['#size'] = 3;
$filter['min']['#field_prefix'] = $force_operator ? $operators[$this->options['operator']] : '';
$filter['max']['#size'] = 3;
$filter['max']['#field_prefix'] = t('and');
$filter['max']['#field_suffix'] = $this->get_suffix();
-
+
$this->unset_attributes($filter['max'], array('#title', '#default_value'));
$this->unset_attributes($filter['min'], array('#default_value'));
}
@@ -254,45 +258,44 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
/**
* Validate the exposed filter form
*/
- function exposed_validate(&$form, &$form_state) {
- parent::exposed_validate(&$form, &$form_state);
-
+ function exposed_validate(&$form, &$form_state) {
+ parent::exposed_validate($form, $form_state);
// Satitize "value", "min" and "max"
$operators = $this->operators();
$identifier = $this->options['expose']['identifier'];
$location = $this->options['expose']['location'];
foreach (array('value', 'min', 'max') as $field) {
- $form_state['input'][$identifier][$field] = str_replace(',', '.', $form_state['input'][$identifier][$field]);
+ $form_state['values'][$identifier][$field] = str_replace(',', '.', $form_state['values'][$identifier][$field]);
}
- $values = $form_state['input'];
+ $values = $form_state['values'];
$op = $values[$this->options['expose']['operator']];
-
- // Validate "value", "min" and "max"
- $fields = ($operators[$op]['method'] == 'op_between') ? array('min', 'max') : array('value');
+
+ // Validate "value", "min" and "max"
+ $fields = ($operators[$op]['method'] == 'op_between') ? array('min', 'max') : array('value');
foreach ($fields as $key => $field) {
if ($values[$identifier][$field]) {
if (!is_numeric($values[$identifier][$field])) {
- form_set_error($identifier .']['. $field, t('Please, provide a valid numeric value.'));
+ form_set_error($identifier . '][' . $field, t('Please, provide a valid numeric value.'));
}
elseif ($values[$identifier][$field] <= 0) {
- form_set_error($identifier .']['. $field, t('Please, provide a value greater then zero.'));
+ form_set_error($identifier . '][' . $field, t('Please, provide a value greater then zero.'));
}
elseif ($key && $values[$identifier]['min'] >= $values[$identifier]['max']) {
- form_set_error($identifier .'][min', t('Please, make sure that the first value is greater then the second one.'));
+ form_set_error($identifier . '][min', t('Please, make sure that the first value is greater then the second one.'));
}
}
}
-
- // Validate location
+
+ // Validate location
if ($this->options['location_as'] == 'location' && !empty($values[$identifier][$location])) {
if ($response = openlayers_proximity_geocode($values[$identifier][$location])) {
$this->response = $response;
}
else {
- form_set_error($identifier .']['. $location, t('Location not found'));
+ form_set_error($identifier . '][' . $location, t('Location not found'));
}
}
-
+
}
/**
@@ -302,14 +305,14 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
*/
function process_node_proximity() {
$nid = $this->value['node'];
-
+
// Get square boundaries.
- $x1 = db_result(db_query("SELECT lon FROM {openlayers_proximity} WHERE nid = %d ORDER BY lon ASC LIMIT 0, 1", $nid));
- $y1 = db_result(db_query("SELECT lat FROM {openlayers_proximity} WHERE nid = %d ORDER BY lat ASC LIMIT 0, 1", $nid));
- $x2 = db_result(db_query("SELECT lon FROM {openlayers_proximity} WHERE nid = %d ORDER BY lon DESC LIMIT 0, 1", $nid));
- $y2 = db_result(db_query("SELECT lat FROM {openlayers_proximity} WHERE nid = %d ORDER BY lat DESC LIMIT 0, 1", $nid));
-
- $delta = (sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2)) / 2) * OPENLAYERS_PROXIMITY_KM_PER_LAT;
+ $x1 = db_query("SELECT lon FROM {openlayers_proximity} WHERE nid = :nid ORDER BY lon ASC LIMIT 0, 1", array(':nid' => $nid))->fetchField();
+ $y1 = db_query("SELECT lat FROM {openlayers_proximity} WHERE nid = :nid ORDER BY lat ASC LIMIT 0, 1", array(':nid' => $nid))->fetchField();
+ $x2 = db_query("SELECT lon FROM {openlayers_proximity} WHERE nid = :nid ORDER BY lon DESC LIMIT 0, 1", array(':nid' => $nid))->fetchField();
+ $y2 = db_query("SELECT lat FROM {openlayers_proximity} WHERE nid = :nid ORDER BY lat DESC LIMIT 0, 1", array(':nid' => $nid))->fetchField();
+
+ $delta = (sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2)) / 2) * OPENLAYERS_PROXIMITY_KM_PER_LAT;
$this->value['value'] += $delta;
$this->value['min'] += $delta;
$this->value['max'] += $delta;
@@ -329,7 +332,7 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
}
drupal_alter('process_location_proximity', $this->value);
}
-
+
function secure_input() {
$this->value['value'] = isset($this->value['value']) ? $this->value['value'] : $this->options['value']['value'];
$this->value['min'] = isset($this->value['min']) ? $this->value['min'] : $this->options['value']['min'];
@@ -342,23 +345,22 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
$this->value['min'] = openlayers_proximity_measurement_units_convert($this->value['unit'], $this->value['min']);
$this->value['max'] = openlayers_proximity_measurement_units_convert($this->value['unit'], $this->value['max']);
}
-
+
function unset_attributes(&$item, $attributes) {
foreach ($attributes as $name) {
unset($item[$name]);
}
- }
-
+ }
+
function clean_exposed_filter_item(&$item) {
$this->unset_attributes($item, array('#prefix', '#suffix', '#title', '#description'));
}
-
+
function expose_option_form(&$form, $name, $title) {
-
- $form['expose']['use_'. $name] = array(
+ $form['expose']['use_' . $name] = array(
'#type' => 'checkbox',
'#title' => t('Unlock @title', array('@title' => $title)),
- '#default_value' => $this->options['expose']['use_'. $name],
+ '#default_value' => $this->options['expose']['use_' . $name],
'#description' => t('When checked, this filter will be exposed to the user'),
);
$form['expose'][$name] = array(
@@ -367,11 +369,13 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
'#title' => t('@title identifier', array('@title' => $title)),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this filter.'),
- '#process' => array('views_process_dependency'),
+/*
+ '#process' => array(' ctools_dependent_process'),
+*/
'#dependency' => array(
- 'edit-options-expose-use-'. $name => array(1)
+ 'edit-options-expose-use-' . $name => array(1),
),
- );
+ );
}
@@ -384,29 +388,29 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
}
return $value;
}
-
+
function filter_item_is_exposed($name) {
- return !empty($this->options['expose']['use_'. $name]) && !empty($this->options['expose'][$name]);
+ return !empty($this->options['expose']['use_' . $name]) && !empty($this->options['expose'][$name]);
}
-
+
function get_node_options() {
$options = array();
// Avoid query if filter is not exposed. @TODO: get a better condition check.
if ($this->filter_item_is_exposed('node') && $this->options['location_as'] == 'node' || arg(2) == 'views') {
- $results = db_query("SELECT n.title, n.nid FROM {openlayers_proximity} op LEFT JOIN {node} n ON n.nid = op.nid WHERE n.status = 1 ORDER BY n.title ASC");
- while ($row = db_fetch_object($results)) {
+ $results = db_query("SELECT n.title, n.nid FROM {openlayers_proximity} op LEFT JOIN {node} n ON n.nid = op.nid WHERE n.status = :nstatus ORDER BY n.title ASC", array(':nstatus' => 1))->fetchAllAssoc('nid');
+ foreach ($results as $row) {
$options[$row->nid] = check_plain(drupal_substr($row->title, 0, 40));
}
}
return $options;
}
-
+
function location_name() {
if ($this->options['location_as'] == 'location') {
return $this->options['value']['location'];
}
if ($this->options['location_as'] == 'node') {
- return db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $this->options['value']['node']));
+ return db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $this->options['value']['node']))->fetchField();
}
}
@@ -415,15 +419,15 @@ class openlayers_proximity_handler_filter extends views_handler_filter_float {
$units = openlayers_proximity_get_available_units();
// If unit is not exposed add unit string to suffix.
if (!$this->filter_item_is_exposed('unit')) {
- $suffix = '@unit '. $suffix;
+ $suffix = '@unit ' . $suffix;
}
// If location is not exposed add location string to suffix.
if (!$this->filter_item_is_exposed($this->options['location_as'])) {
- $suffix = $suffix .' from @location';
+ $suffix = $suffix . ' from @location';
}
return t($suffix, array('@unit' => $units[$this->options['value']['unit']]['long'], '@location' => $this->location_name()));
}
-}
\ No newline at end of file
+}
diff --git a/views/openlayers_proximity_handler_filter_circle.inc b/views/openlayers_proximity_handler_filter_circle.inc
index 39a5756..aa9b993 100644
--- a/views/openlayers_proximity_handler_filter_circle.inc
+++ b/views/openlayers_proximity_handler_filter_circle.inc
@@ -1,50 +1,61 @@
-value['lat']) && !is_null($this->value['lon'])) {
-
+
$join = new views_join();
$table = $this->table_formula_alias();
$join->definition['table formula'] = $this->table_formula($this->value['lat'], $this->value['lon']);
$join->construct($table, 'node', 'nid', 'nid', array(), 'RIGHT');
- $this->query->queue_table($table, 'node', $join);
+ $this->query->add_table($table, 'node', $join);
$this->query->add_field($table, 'distance');
-
+
if ($op == 'simple') {
$radius = $this->value['value'];
- $where = ($this->operator == '<') ? $this->build_where('<', $table, $radius) : $this->build_where('>', $table, $radius);
+ $operator = $this->operator;
+ $this->query->add_where($this->options['group'], $table.'.distance', $radius, $operator);
}
else {
- $inner = $this->build_where('>', $table, $this->value['min']);
- $outer = $this->build_where('<', $table, $this->value['max']);
- $where = ($this->operator == 'between') ? "($inner AND $outer)" : "NOT($inner AND $outer)";
+ if ($this->operator == 'between') {
+ $this->query->add_where($this->options['group'], $table.'.distance', array($this->value['min'], $this->value['max']), 'BETWEEN');
+ }
+ else {
+ $this->query->add_where($this->options['group'], db_or()->condition($table.'.distance', $this->value['min'], '<')->condition($table.'.distance', $this->value['max'], '>'));
+ }
}
- $this->query->add_where($this->options['group'], $where);
}
$this->query->add_groupby("nid");
}
/**
- * Build WHERE clause.
+ * Build the subselect.
*/
- function build_where($operator, $alias, $radius) {
- return sprintf("($alias.distance $operator= %f)", $radius);
- }
function table_formula($lat, $lon) {
- $args = array('!lat1' => 'lat', '!lon1' => 'lon', '!lat2' => $lat, '!lon2' => $lon);
- $great_circle = strtr(OPENLAYERS_PROXIMITY_SQL_GREAT_CIRCLE, $args);
- return "(SELECT nid, lat, lon, $great_circle AS distance FROM {openlayers_proximity})";
+
+ $args = array(
+ ':lat2' => $lat,
+ ':lon2' => $lon,
+ );
+
+ $query = db_select('openlayers_proximity', 'op');
+ $query->fields('op', array('nid'));
+ $query->fields('op', array('lat'));
+ $query->fields('op', array('lon'));
+ $query->addExpression(OPENLAYERS_PROXIMITY_SQL_GREAT_CIRCLE, 'distance', $args);
+ return $query;
+
}
-
+
function table_formula_alias() {
- return $this->table_alias .'_filter_'. $this->options['id'];
+ return $this->table_alias . '_filter_' . $this->options['id'];
}
}
diff --git a/views/openlayers_proximity_handler_filter_square.inc b/views/openlayers_proximity_handler_filter_square.inc
index 6294249..af6d6fd 100644
--- a/views/openlayers_proximity_handler_filter_square.inc
+++ b/views/openlayers_proximity_handler_filter_square.inc
@@ -1,7 +1,8 @@
-response) {
-
- $this->query->add_field($this->table_alias, 'lat');
- $this->query->add_field($this->table_alias, 'lon');
+
+ $table = $this->table_alias;
+
+ $this->query->add_field($table, 'lat');
+ $this->query->add_field($table, 'lon');
$lat = $this->value['lat'];
$lon = $this->value['lon'];
@@ -19,36 +22,38 @@ class openlayers_proximity_handler_filter_square extends openlayers_proximity_ha
if ($op == 'simple') {
$radius = $this->value['value'];
$bounds = $this->get_bounds($lat, $lon, $radius);
- $where = $this->build_where($this->operator, $bounds, $this->table_alias);
+ $operator = $this->operator;
+ $where = $this->build_where($this->operator, $bounds, $table);
+ //$this->query->add_where_expression($this->options['group'], $where);
}
else {
$radius = $this->value['max'];
$bounds = $this->get_bounds($lat, $lon, $radius);
- $inner = $this->build_where('<', $bounds, $this->table_alias);
-
+ $inner = $this->build_where('<', $bounds, $table);
+
$radius = $this->value['min'];
$bounds = $this->get_bounds($lat, $lon, $radius);
- $outer = $this->build_where('>', $bounds, $this->table_alias);
+ $outer = $this->build_where('>', $bounds, $table);
$where = ($this->operator == 'between') ? "($inner AND $outer)" : "NOT($inner AND $outer)";
}
-
- $this->query->add_where($this->options['group'], $where);
+
+ $this->query->add_where_expression($this->options['group'], $where);
}
$this->query->add_groupby("nid");
}
-
+
/**
* Calculate search bounds give lat/lon and distance in kilometers.
* @see http://imaginerc.com/software/GeoCalc/
*/
function get_bounds($latitude, $longitude, $radius) {
-
+
// Calculate lat/lon shifts
$latitude_shift = $radius / OPENLAYERS_PROXIMITY_KM_PER_LAT;
$longitude_shift = (1 / (OPENLAYERS_PROXIMITY_KM_PER_LAT * cos($latitude * OPENLAYERS_PROXIMITY_DEGREE_TO_RADIANTS))) * $radius;
-
+
// Calculate the boundaries
$bounds = array();
$bounds['north'] = $latitude + $latitude_shift;
@@ -57,14 +62,14 @@ class openlayers_proximity_handler_filter_square extends openlayers_proximity_ha
$bounds['east'] = $longitude + $longitude_shift;
return $bounds;
}
-
-
+
+
/**
* Build WHERE clause.
*/
function build_where($operator, $bounds, $alias) {
$where = vsprintf("($alias.lat <= %f AND $alias.lat >= %f AND $alias.lon >= %f AND $alias.lon <= %f)", $bounds);
- return ($operator == '<' || $operator == '<=') ? $where : 'NOT'. $where;
- }
-
-}
\ No newline at end of file
+ return ($operator == '<' || $operator == '<=') ? $where : 'NOT' . $where;
+ }
+
+}
diff --git a/views/openlayers_proximity_handler_sort.inc b/views/openlayers_proximity_handler_sort.inc
index 9a7b2ab..df2c1a8 100644
--- a/views/openlayers_proximity_handler_sort.inc
+++ b/views/openlayers_proximity_handler_sort.inc
@@ -1,24 +1,25 @@
- '');
return $options;
}
-
+
/**
* Basic options for all sort criteria
*/
function options_form(&$form, &$form_state) {
-
+
if ($handlers = $this->view->display_handler->get_handlers('filter')) {
$options = array();
foreach ($handlers as $name => $handler) {
- if ($handler->definition['handler'] == 'openlayers_proximity_handler_filter_circle') {
- $options[$name] = $handler->definition['group'] .': '. $handler->definition['title'] .' ('. $handler->admin_summary() .')';
- }
+ if ($handler->definition['handler'] == 'openlayers_proximity_handler_filter_circle') {
+ $options[$name] = $handler->definition['group'] . ': ' . $handler->definition['title'] . ' (' . $handler->admin_summary() . ')';
+ }
}
$form['location_provider'] = array(
'#title' => t('Location provider'),
@@ -28,13 +29,13 @@ class openlayers_proximity_handler_sort extends views_handler_sort {
}
parent::options_form($form, $form_state);
}
-
+
function query() {
$handler = $this->view->display_handler->get_handler('filter', $this->options['location_provider']);
if ($handler && $handler->value['location'] && isset($this->query->table_queue[$handler->table_formula_alias()])) {
$this->query->add_orderby($handler->table_formula_alias(), $this->real_field, $this->options['order']);
}
-
+
}
-
+
}