| 1 |
<?php //$Id: views_handler_filter_geo_distance.inc,v 1.4 2009/06/18 01:24:11 vauxia Exp $ |
<?php //$Id: views_handler_filter_geo_distance.inc,v 1.5 2009/06/18 21:00:28 vauxia Exp $ |
| 2 |
|
|
| 3 |
class views_handler_filter_geo_distance extends views_handler_filter { |
class views_handler_filter_geo_distance extends views_handler_filter { |
| 4 |
|
|
| 5 |
function option_definition() { |
function option_definition() { |
| 6 |
$options = parent::option_definition(); |
$options = parent::option_definition(); |
| 7 |
$options['gis_input'] = array('default' => 'geo_wkt'); |
$options['gis_input'] = array('default' => 'geo_latlon'); |
| 8 |
$options['gis_input_value'] = array('default' => ''); |
$options['gis_input_value'] = array('default' => array()); |
| 9 |
return $options; |
return $options; |
| 10 |
} |
} |
| 11 |
|
|
| 12 |
function value_form(&$form, &$form_state, $key = 'value') { |
function value_form(&$form, &$form_state) { |
| 13 |
$exposed = $key != 'value'; |
$exposed = $form_state['exposed']; |
| 14 |
$gis_input = $this->value['gis_input']; |
$key = $exposed ? $this->options['expose']['identifier'] : 'value'; |
| 15 |
|
|
| 16 |
$form[$key] = array('#tree' => TRUE); |
// TODO handle this based on exposdieness - or delete it outright? |
| 17 |
|
if ($exposed && empty($form_state['input'])) { |
| 18 |
$form[$key]['gis_input'] = geo('gis_input_selector', $gis_input); |
$form_state['input'][$key] = $this->value; |
| 19 |
$form[$key]['gis_input']['#access'] = !$exposed; |
} |
| 20 |
|
|
| 21 |
if ($gis_input) { |
if (!$exposed) { |
| 22 |
|
$form['gis_input'] = geo('gis_input_selector', $this->options['gis_input']); |
| 23 |
$form[$key]['geo_distance'] = array( |
} |
| 24 |
'#type' => 'textfield', |
|
| 25 |
'#title' => t('Distance'), |
$form['value'] = array('#tree' => TRUE); |
| 26 |
'#default_value' => $this->value['geo_distance'], |
|
| 27 |
'#size' => 5, |
$form['value']['geo_distance'] = array( |
| 28 |
'#access' => !$exposed || $this->options['show_geo_distance'], |
'#type' => 'textfield', |
| 29 |
); |
'#title' => t('Distance'), |
| 30 |
|
'#default_value' => $this->value['geo_distance'], |
| 31 |
$form[$key]['geo_units'] = array( |
'#size' => 5, |
| 32 |
'#type' => 'select', |
'#access' => !$exposed || $this->options['show_geo_distance'], |
| 33 |
'#title' => t('Units'), |
'#process' => array('views_process_dependency'), |
| 34 |
'#options' => geo_units(), |
'#dependency' => array('edit-options-value-gis-input'), |
| 35 |
'#default_value' => $this->value['geo_units'], |
); |
| 36 |
'#access' => !$exposed || $this->options['show_geo_units'], |
|
| 37 |
); |
$form['value']['geo_units'] = array( |
| 38 |
|
'#type' => 'select', |
| 39 |
|
'#title' => t('Units'), |
| 40 |
|
'#options' => geo_units(), |
| 41 |
|
'#default_value' => $this->value['geo_units'], |
| 42 |
|
'#access' => !$exposed || $this->options['show_geo_units'], |
| 43 |
|
'#process' => array('views_process_dependency'), |
| 44 |
|
'#dependency' => array('edit-options-value-gis-input'), |
| 45 |
|
); |
| 46 |
|
|
| 47 |
$info = module_invoke_all('gis_input_info'); |
// The "target" value gets converted to the value that is actually used. |
| 48 |
$info = $info[$this->value['gis_input']]; |
// The gis_input_value field stores the input as it was entered. |
| 49 |
$form[$key]['target'] = array_merge($info['element'], array( |
$form['value']['target'] = array('#tree' => TRUE); |
| 50 |
|
|
| 51 |
|
foreach (module_invoke_all('gis_input_info') as $name => $info) { |
| 52 |
|
// The exposed form only shows one input. |
| 53 |
|
if ($exposed && ($name != $this->options['gis_input'])) continue; |
| 54 |
|
|
| 55 |
|
// Ignore nonexistent element types. |
| 56 |
|
if (!$element = _element_info($info['element']['#type'])) continue; |
| 57 |
|
//unset($element['#parents'], $element['#tree']); |
| 58 |
|
unset($element['#parents']); |
| 59 |
|
|
| 60 |
|
//$value = is_array($this->value['gis_input_value']) ? $this->value['gis_input_value'][$name] : NULL; |
| 61 |
|
$value = is_array($this->value['target']) ? $this->value['target'][$name] : NULL; |
| 62 |
|
|
| 63 |
|
$element = array_merge($element, $info['element']); |
| 64 |
|
$element = array_merge($element, array( |
| 65 |
'#title' => $this->options['label'], |
'#title' => $this->options['label'], |
| 66 |
'#default_value' => $this->value['gis_input_value'], |
'#default_value' => $value, |
| 67 |
'#gis_type' => 'point', // TODO derived. |
'#gis_type' => 'point', // TODO derived. |
| 68 |
'#gis_input' => $info['gis input'], // The format expected by the element. |
'#gis_input' => $info['gis input'], // The format expected by the element. |
| 69 |
'#gis_output' => 'array', // The format expected by this handler. |
'#gis_output' => 'array', // The format expected by this handler. |
| 70 |
'#weight' => 1, |
// '#access' => (!$exposed || ($name == $this->options['gis_input'])), |
| 71 |
)); |
)); |
| 72 |
|
|
| 73 |
if (!isset($form[$key]['#after_build'])) { |
if (!$exposed) { |
| 74 |
$form[$key]['#after_build'] = array(); |
$element['#process'][] = 'views_process_dependency'; |
| 75 |
|
$element['#dependency'] = array('edit-options-gis-input' => array($name)); |
| 76 |
} |
} |
|
$form[$key]['target']['#after_build'][] = 'geo_element_set_value'; |
|
|
} |
|
| 77 |
|
|
| 78 |
// Create an additional field to store the input as it was submitted. |
// Add the geo_element_set_value function to the as-translated version. |
| 79 |
$form[$key]['gis_input_value'] = $form[$key]['target']; |
//$element['#after_build'][] = 'geo_element_set_value'; |
|
$form[$key]['gis_input_value']['#access'] = FALSE; |
|
|
} |
|
|
|
|
|
function value_validate($form, &$form_state, $key = 'value') { |
|
|
parent::value_validate($form, $form_state); |
|
|
form_set_value($form[$key]['gis_input_value'], $form[$key]['target']['#gis_input_value'], $form_state); |
|
|
} |
|
|
|
|
|
function exposed_form(&$form, &$form_state) { |
|
|
$key = $this->options['expose']['identifier']; |
|
|
$this->value_form($form, $form_state, $key); |
|
|
} |
|
| 80 |
|
|
| 81 |
function exposed_validate($form, &$form_state) { |
// Add this element to the form. |
| 82 |
$key = $this->options['expose']['identifier']; |
$element['#tree'] = TRUE; |
| 83 |
$this->value_validate($form, $form_state, $key); |
$form['value']['target'][$name] = $element; |
| 84 |
|
} |
| 85 |
} |
} |
| 86 |
|
|
| 87 |
function accept_exposed_input(&$input) { |
function accept_exposed_input(&$input) { |
| 88 |
|
if (empty($this->options['exposed'])) { |
| 89 |
|
return TRUE; |
| 90 |
|
} |
| 91 |
|
|
| 92 |
$key = $this->options['expose']['identifier']; |
$key = $this->options['expose']['identifier']; |
| 93 |
$value = $input[$key]; |
$value = $input[$key]; |
| 94 |
|
|
| 95 |
// Ensure that the entered value is stored in the user's session. |
// Ensure that the entered value is stored in the user's session. |
| 96 |
$input[$key]['target'] = $input[$key]['gis_input_value']; |
//$input[$key]['target'] = $input[$key]['gis_input_value']; |
| 97 |
|
|
| 98 |
if ($value['geo_distance'] && $value['geo_units'] && $value['target']) { |
if (!$this->_value_is_empty($value['target'][$this->options['gis_input']])) { |
| 99 |
$this->value = $value; |
$this->value = $value; |
| 100 |
return TRUE; |
return TRUE; |
| 101 |
} |
} |
| 102 |
|
elseif (!$this->_value_is_empty($this->value['target'][$this->options['gis_input']])) { |
| 103 |
|
return TRUE; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
// TODO really? |
// TODO really? |
| 107 |
return FALSE; |
return FALSE; |
| 108 |
} |
} |
| 109 |
|
|
| 110 |
|
function admin_summary() { |
| 111 |
|
$out = ''; |
| 112 |
|
if ($this->value) { |
| 113 |
|
$out = (int) $this->value['geo_distance']; |
| 114 |
|
$out .= ' '. geo_units($this->value['geo_units']); |
| 115 |
|
$out .= ' '. t('from'); |
| 116 |
|
$out .= ' '. $this->value['target'][$this->options['gis_input']]; |
| 117 |
|
} |
| 118 |
|
return $out; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
function _value_is_empty($value) { |
| 122 |
|
// TODO this is not very flexible... |
| 123 |
|
if (is_scalar($value)) return empty($value); |
| 124 |
|
if (is_array($value)) { |
| 125 |
|
$val = end($value); |
| 126 |
|
return empty($val); |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
|
|
| 130 |
function query() { |
function query() { |
| 131 |
$value = $this->value; |
$value = $this->value; |
| 132 |
|
|
| 133 |
$limit = $value['geo_distance']; |
$limit = $value['geo_distance']; |
| 134 |
$units = $value['geo_units']; |
$units = $value['geo_units']; |
| 135 |
$target = $value['target']; |
if ($value['target']) { |
| 136 |
|
$target = $value['target'][$this->options['gis_input']]; |
| 137 |
|
} |
| 138 |
|
else return; |
| 139 |
|
|
| 140 |
if (!$value['gis_input_value']) return; |
foreach (module_invoke_all('gis_input_info') as $name => $info) { |
| 141 |
|
if ($name == $this->options['gis_input']) { |
| 142 |
|
// TODO the output format (array) should not be hard-coded. |
| 143 |
|
$target = geo_value_convert($target, $info['gis input'], 'array'); |
| 144 |
|
break; |
| 145 |
|
} |
| 146 |
|
} |
| 147 |
|
|
| 148 |
// Set the distance limit to meters. |
// Set the distance limit to meters. |
| 149 |
$limit = (int) geo_unit_convert($limit, $units, 'm'); |
$limit = (int) geo_unit_convert($limit, $units, 'm'); |