| 1 |
<?php
|
| 2 |
// $Id: views_handler_filter_numeric.inc,v 1.6 2008/10/28 20:11:20 merlinofchaos Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Simple filter to handle greater than/less than filters
|
| 6 |
*/
|
| 7 |
class views_handler_filter_numeric extends views_handler_filter {
|
| 8 |
var $no_single = TRUE;
|
| 9 |
function option_definition() {
|
| 10 |
$options = parent::option_definition();
|
| 11 |
|
| 12 |
$options['value'] = array(
|
| 13 |
'contains' => array(
|
| 14 |
'min' => array('default' => ''),
|
| 15 |
'max' => array('default' => ''),
|
| 16 |
'value' => array('default' => ''),
|
| 17 |
),
|
| 18 |
);
|
| 19 |
|
| 20 |
return $options;
|
| 21 |
}
|
| 22 |
|
| 23 |
function operators() {
|
| 24 |
$operators = array(
|
| 25 |
'<' => array(
|
| 26 |
'title' => t('Is less than'),
|
| 27 |
'method' => 'op_simple',
|
| 28 |
'short' => t('<'),
|
| 29 |
'values' => 1,
|
| 30 |
),
|
| 31 |
'<=' => array(
|
| 32 |
'title' => t('Is less than or equal to'),
|
| 33 |
'method' => 'op_simple',
|
| 34 |
'short' => t('<='),
|
| 35 |
'values' => 1,
|
| 36 |
),
|
| 37 |
'=' => array(
|
| 38 |
'title' => t('Is equal to'),
|
| 39 |
'method' => 'op_simple',
|
| 40 |
'short' => t('='),
|
| 41 |
'values' => 1,
|
| 42 |
),
|
| 43 |
'!=' => array(
|
| 44 |
'title' => t('Is not equal to'),
|
| 45 |
'method' => 'op_simple',
|
| 46 |
'short' => t('!='),
|
| 47 |
'values' => 1,
|
| 48 |
),
|
| 49 |
'>=' => array(
|
| 50 |
'title' => t('Is greater than or equal to'),
|
| 51 |
'method' => 'op_simple',
|
| 52 |
'short' => t('>='),
|
| 53 |
'values' => 1,
|
| 54 |
),
|
| 55 |
'>' => array(
|
| 56 |
'title' => t('Is greater than'),
|
| 57 |
'method' => 'op_simple',
|
| 58 |
'short' => t('>'),
|
| 59 |
'values' => 1,
|
| 60 |
),
|
| 61 |
'between' => array(
|
| 62 |
'title' => t('Is between'),
|
| 63 |
'method' => 'op_between',
|
| 64 |
'short' => t('between'),
|
| 65 |
'values' => 2,
|
| 66 |
),
|
| 67 |
'not between' => array(
|
| 68 |
'title' => t('Is not between'),
|
| 69 |
'method' => 'op_between',
|
| 70 |
'short' => t('not between'),
|
| 71 |
'values' => 2,
|
| 72 |
),
|
| 73 |
);
|
| 74 |
|
| 75 |
// if the definition allows for the empty operator, add it.
|
| 76 |
if (!empty($this->definition['allow empty'])) {
|
| 77 |
$operators += array(
|
| 78 |
'empty' => array(
|
| 79 |
'title' => t('Is empty (NULL)'),
|
| 80 |
'method' => 'op_empty',
|
| 81 |
'short' => t('empty'),
|
| 82 |
'values' => 0,
|
| 83 |
),
|
| 84 |
'not empty' => array(
|
| 85 |
'title' => t('Is not empty (NULL)'),
|
| 86 |
'method' => 'op_empty',
|
| 87 |
'short' => t('not empty'),
|
| 88 |
'values' => 0,
|
| 89 |
),
|
| 90 |
);
|
| 91 |
}
|
| 92 |
|
| 93 |
return $operators;
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Provide a list of all the numeric operators
|
| 98 |
*/
|
| 99 |
function operator_options($which = 'title') {
|
| 100 |
$options = array();
|
| 101 |
foreach ($this->operators() as $id => $info) {
|
| 102 |
$options[$id] = $info[$which];
|
| 103 |
}
|
| 104 |
|
| 105 |
return $options;
|
| 106 |
}
|
| 107 |
|
| 108 |
function operator_values($values = 1) {
|
| 109 |
$options = array();
|
| 110 |
foreach ($this->operators() as $id => $info) {
|
| 111 |
if ($info['values'] == $values) {
|
| 112 |
$options[] = $id;
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
return $options;
|
| 117 |
}
|
| 118 |
/**
|
| 119 |
* Provide a simple textfield for equality
|
| 120 |
*/
|
| 121 |
function value_form(&$form, &$form_state) {
|
| 122 |
$form['value']['#tree'] = TRUE;
|
| 123 |
|
| 124 |
// We have to make some choices when creating this as an exposed
|
| 125 |
// filter form. For example, if the operator is locked and thus
|
| 126 |
// not rendered, we can't render dependencies; instead we only
|
| 127 |
// render the form items we need.
|
| 128 |
$which = 'all';
|
| 129 |
if (!empty($form['operator'])) {
|
| 130 |
$source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
|
| 131 |
}
|
| 132 |
|
| 133 |
if (!empty($form_state['exposed'])) {
|
| 134 |
$identifier = $this->options['expose']['identifier'];
|
| 135 |
|
| 136 |
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator'])) {
|
| 137 |
// exposed and locked.
|
| 138 |
$which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
|
| 139 |
}
|
| 140 |
else {
|
| 141 |
$source = 'edit-' . form_clean_id($this->options['expose']['operator']);
|
| 142 |
}
|
| 143 |
}
|
| 144 |
|
| 145 |
if ($which == 'all') {
|
| 146 |
$form['value']['value'] = array(
|
| 147 |
'#type' => 'textfield',
|
| 148 |
'#title' => empty($form_state['exposed']) ? t('Value') : '',
|
| 149 |
'#size' => 30,
|
| 150 |
'#default_value' => $this->value['value'],
|
| 151 |
'#process' => array('views_process_dependency'),
|
| 152 |
'#dependency' => array($source => $this->operator_values(1)),
|
| 153 |
);
|
| 154 |
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
|
| 155 |
$form_state['input'][$identifier]['value'] = $this->value['value'];
|
| 156 |
}
|
| 157 |
}
|
| 158 |
else if ($which == 'value') {
|
| 159 |
// When exposed we drop the value-value and just do value if
|
| 160 |
// the operator is locked.
|
| 161 |
$form['value'] = array(
|
| 162 |
'#type' => 'textfield',
|
| 163 |
'#title' => empty($form_state['exposed']) ? t('Value') : '',
|
| 164 |
'#size' => 30,
|
| 165 |
'#default_value' => $this->value['value'],
|
| 166 |
);
|
| 167 |
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
|
| 168 |
$form_state['input'][$identifier] = $this->value['value'];
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
| 172 |
if ($which == 'all' || $which == 'minmax') {
|
| 173 |
$form['value']['min'] = array(
|
| 174 |
'#type' => 'textfield',
|
| 175 |
'#title' => empty($form_state['exposed']) ? t('Min') : '',
|
| 176 |
'#size' => 30,
|
| 177 |
'#default_value' => $this->value['min'],
|
| 178 |
);
|
| 179 |
$form['value']['max'] = array(
|
| 180 |
'#type' => 'textfield',
|
| 181 |
'#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
|
| 182 |
'#size' => 30,
|
| 183 |
'#default_value' => $this->value['max'],
|
| 184 |
);
|
| 185 |
if ($which == 'all') {
|
| 186 |
$dependency = array(
|
| 187 |
'#process' => array('views_process_dependency'),
|
| 188 |
'#dependency' => array($source => $this->operator_values(2)),
|
| 189 |
);
|
| 190 |
$form['value']['min'] += $dependency;
|
| 191 |
$form['value']['max'] += $dependency;
|
| 192 |
}
|
| 193 |
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min'])) {
|
| 194 |
$form_state['input'][$identifier]['min'] = $this->value['min'];
|
| 195 |
}
|
| 196 |
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max'])) {
|
| 197 |
$form_state['input'][$identifier]['max'] = $this->value['max'];
|
| 198 |
}
|
| 199 |
|
| 200 |
if (!isset($form['value'])) {
|
| 201 |
// Ensure there is something in the 'value'.
|
| 202 |
$form['value'] = array(
|
| 203 |
'#type' => 'value',
|
| 204 |
'#value' => NULL
|
| 205 |
);
|
| 206 |
}
|
| 207 |
}
|
| 208 |
}
|
| 209 |
|
| 210 |
function query() {
|
| 211 |
$this->ensure_my_table();
|
| 212 |
$field = "$this->table_alias.$this->real_field";
|
| 213 |
|
| 214 |
$info = $this->operators();
|
| 215 |
if (!empty($info[$this->operator]['method'])) {
|
| 216 |
$this->{$info[$this->operator]['method']}($field);
|
| 217 |
}
|
| 218 |
}
|
| 219 |
|
| 220 |
function op_between($field) {
|
| 221 |
if ($this->operator == 'between') {
|
| 222 |
$this->query->add_where($this->options['group'], "$field >= %d", $this->value['min']);
|
| 223 |
$this->query->add_where($this->options['group'], "$field <= %d", $this->value['max']);
|
| 224 |
}
|
| 225 |
else {
|
| 226 |
$this->query->add_where($this->options['group'], "$field <= %d OR $field >= %d", $this->value['min'], $this->value['max']);
|
| 227 |
}
|
| 228 |
}
|
| 229 |
|
| 230 |
function op_simple($field) {
|
| 231 |
$this->query->add_where($this->options['group'], "$field $this->operator %d", $this->value['value']);
|
| 232 |
}
|
| 233 |
|
| 234 |
function op_empty($field) {
|
| 235 |
if ($this->operator == 'empty') {
|
| 236 |
$operator = "IS NULL";
|
| 237 |
}
|
| 238 |
else {
|
| 239 |
$operator = "IS NOT NULL";
|
| 240 |
}
|
| 241 |
|
| 242 |
$this->query->add_where($this->options['group'], "$field $operator");
|
| 243 |
}
|
| 244 |
|
| 245 |
function admin_summary() {
|
| 246 |
if (!empty($this->options['exposed'])) {
|
| 247 |
return t('exposed');
|
| 248 |
}
|
| 249 |
|
| 250 |
$options = $this->operator_options('short');
|
| 251 |
$output = check_plain($options[$this->operator]);
|
| 252 |
if (in_array($this->operator, $this->operator_values(2))) {
|
| 253 |
$output .= ' ' . t('@min and @max', array('@min' => $this->value['min'], '@max' => $this->value['max']));
|
| 254 |
}
|
| 255 |
elseif (in_array($this->operator, $this->operator_values(1))) {
|
| 256 |
$output .= ' ' . check_plain($this->value['value']);
|
| 257 |
}
|
| 258 |
return $output;
|
| 259 |
}
|
| 260 |
|
| 261 |
/**
|
| 262 |
* Do some minor translation of the exposed input
|
| 263 |
*/
|
| 264 |
function accept_exposed_input($input) {
|
| 265 |
if (empty($this->options['exposed'])) {
|
| 266 |
return TRUE;
|
| 267 |
}
|
| 268 |
|
| 269 |
// rewrite the input value so that it's in the correct format so that
|
| 270 |
// the parent gets the right data.
|
| 271 |
if (!empty($this->options['expose']['identifier'])) {
|
| 272 |
$value = &$input[$this->options['expose']['identifier']];
|
| 273 |
if (!is_array($value)) {
|
| 274 |
$value = array(
|
| 275 |
'value' => $value,
|
| 276 |
);
|
| 277 |
}
|
| 278 |
}
|
| 279 |
|
| 280 |
$rc = parent::accept_exposed_input($input);
|
| 281 |
|
| 282 |
if (!empty($this->options['expose']['optional'])) {
|
| 283 |
// We have to do some of our own optional checking.
|
| 284 |
$info = $this->operators();
|
| 285 |
if (!empty($info[$this->operator]['values'])) {
|
| 286 |
switch ($info[$this->operator]['values']) {
|
| 287 |
case 1:
|
| 288 |
if ($value['value'] === '') {
|
| 289 |
return FALSE;
|
| 290 |
}
|
| 291 |
break;
|
| 292 |
case 2:
|
| 293 |
if ($value['min'] === '' && $value['max'] === '') {
|
| 294 |
return FALSE;
|
| 295 |
}
|
| 296 |
break;
|
| 297 |
}
|
| 298 |
}
|
| 299 |
}
|
| 300 |
|
| 301 |
return $rc;
|
| 302 |
}
|
| 303 |
}
|