00001 <?php
00002
00008 class views_handler_filter_numeric extends views_handler_filter {
00009 var $always_multiple = TRUE;
00010 function option_definition() {
00011 $options = parent::option_definition();
00012
00013 $options['value'] = array(
00014 'contains' => array(
00015 'min' => array('default' => ''),
00016 'max' => array('default' => ''),
00017 'value' => array('default' => ''),
00018 ),
00019 );
00020
00021 return $options;
00022 }
00023
00024 function operators() {
00025 $operators = array(
00026 '<' => array(
00027 'title' => t('Is less than'),
00028 'method' => 'op_simple',
00029 'short' => t('<'),
00030 'values' => 1,
00031 ),
00032 '<=' => array(
00033 'title' => t('Is less than or equal to'),
00034 'method' => 'op_simple',
00035 'short' => t('<='),
00036 'values' => 1,
00037 ),
00038 '=' => array(
00039 'title' => t('Is equal to'),
00040 'method' => 'op_simple',
00041 'short' => t('='),
00042 'values' => 1,
00043 ),
00044 '!=' => array(
00045 'title' => t('Is not equal to'),
00046 'method' => 'op_simple',
00047 'short' => t('!='),
00048 'values' => 1,
00049 ),
00050 '>=' => array(
00051 'title' => t('Is greater than or equal to'),
00052 'method' => 'op_simple',
00053 'short' => t('>='),
00054 'values' => 1,
00055 ),
00056 '>' => array(
00057 'title' => t('Is greater than'),
00058 'method' => 'op_simple',
00059 'short' => t('>'),
00060 'values' => 1,
00061 ),
00062 'between' => array(
00063 'title' => t('Is between'),
00064 'method' => 'op_between',
00065 'short' => t('between'),
00066 'values' => 2,
00067 ),
00068 'not between' => array(
00069 'title' => t('Is not between'),
00070 'method' => 'op_between',
00071 'short' => t('not between'),
00072 'values' => 2,
00073 ),
00074 );
00075
00076
00077 if (!empty($this->definition['allow empty'])) {
00078 $operators += array(
00079 'empty' => array(
00080 'title' => t('Is empty (NULL)'),
00081 'method' => 'op_empty',
00082 'short' => t('empty'),
00083 'values' => 0,
00084 ),
00085 'not empty' => array(
00086 'title' => t('Is not empty (NOT NULL)'),
00087 'method' => 'op_empty',
00088 'short' => t('not empty'),
00089 'values' => 0,
00090 ),
00091 );
00092 }
00093
00094
00095 if (Database::getConnection()->databaseType() == 'mysql') {
00096 $operators += array(
00097 'regular_expression' => array(
00098 'title' => t('Regular expression'),
00099 'short' => t('regex'),
00100 'method' => 'op_regex',
00101 'values' => 1,
00102 ),
00103 );
00104 }
00105
00106 return $operators;
00107 }
00108
00112 function operator_options($which = 'title') {
00113 $options = array();
00114 foreach ($this->operators() as $id => $info) {
00115 $options[$id] = $info[$which];
00116 }
00117
00118 return $options;
00119 }
00120
00121 function operator_values($values = 1) {
00122 $options = array();
00123 foreach ($this->operators() as $id => $info) {
00124 if ($info['values'] == $values) {
00125 $options[] = $id;
00126 }
00127 }
00128
00129 return $options;
00130 }
00134 function value_form(&$form, &$form_state) {
00135 $form['value']['#tree'] = TRUE;
00136
00137
00138
00139
00140
00141 $which = 'all';
00142 if (!empty($form['operator'])) {
00143 $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
00144 }
00145
00146 if (!empty($form_state['exposed'])) {
00147 $identifier = $this->options['expose']['identifier'];
00148
00149 if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
00150
00151 $which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
00152 }
00153 else {
00154 $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
00155 }
00156 }
00157
00158 if ($which == 'all') {
00159 $form['value']['value'] = array(
00160 '#type' => 'textfield',
00161 '#title' => empty($form_state['exposed']) ? t('Value') : '',
00162 '#size' => 30,
00163 '#default_value' => $this->value['value'],
00164 '#dependency' => array($source => $this->operator_values(1)),
00165 );
00166 if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
00167 $form_state['input'][$identifier]['value'] = $this->value['value'];
00168 }
00169 }
00170 elseif ($which == 'value') {
00171
00172
00173 $form['value'] = array(
00174 '#type' => 'textfield',
00175 '#title' => empty($form_state['exposed']) ? t('Value') : '',
00176 '#size' => 30,
00177 '#default_value' => $this->value['value'],
00178 );
00179 if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
00180 $form_state['input'][$identifier] = $this->value['value'];
00181 }
00182 }
00183
00184 if ($which == 'all' || $which == 'minmax') {
00185 $form['value']['min'] = array(
00186 '#type' => 'textfield',
00187 '#title' => empty($form_state['exposed']) ? t('Min') : '',
00188 '#size' => 30,
00189 '#default_value' => $this->value['min'],
00190 );
00191 $form['value']['max'] = array(
00192 '#type' => 'textfield',
00193 '#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
00194 '#size' => 30,
00195 '#default_value' => $this->value['max'],
00196 );
00197 if ($which == 'all') {
00198 $dependency = array(
00199 '#dependency' => array($source => $this->operator_values(2)),
00200 );
00201 $form['value']['min'] += $dependency;
00202 $form['value']['max'] += $dependency;
00203 }
00204 if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min'])) {
00205 $form_state['input'][$identifier]['min'] = $this->value['min'];
00206 }
00207 if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max'])) {
00208 $form_state['input'][$identifier]['max'] = $this->value['max'];
00209 }
00210
00211 if (!isset($form['value'])) {
00212
00213 $form['value'] = array(
00214 '#type' => 'value',
00215 '#value' => NULL
00216 );
00217 }
00218 }
00219 }
00220
00221 function query() {
00222 $this->ensure_my_table();
00223 $field = "$this->table_alias.$this->real_field";
00224
00225 $info = $this->operators();
00226 if (!empty($info[$this->operator]['method'])) {
00227 $this->{$info[$this->operator]['method']}($field);
00228 }
00229 }
00230
00231 function op_between($field) {
00232 if ($this->operator == 'between') {
00233 $this->query->add_where($this->options['group'], $field, array($this->value['min'], $this->value['max']), 'BETWEEN');
00234 }
00235 else {
00236 $this->query->add_where($this->options['group'], db_or()->condition($field, $this->value['min'], '<=')->condition($field, $this->value['max'], '>='));
00237 }
00238 }
00239
00240 function op_simple($field) {
00241 $this->query->add_where($this->options['group'], $field, $this->value['value'], $this->operator);
00242 }
00243
00244 function op_empty($field) {
00245 if ($this->operator == 'empty') {
00246 $operator = "IS NULL";
00247 }
00248 else {
00249 $operator = "IS NOT NULL";
00250 }
00251
00252 $this->query->add_where($this->options['group'], $field, NULL, $operator);
00253 }
00254
00255 function op_regex($field) {
00256 $this->query->add_where($this->options['group'], $field, $this->value, 'RLIKE');
00257 }
00258
00259 function admin_summary() {
00260 if (!empty($this->options['exposed'])) {
00261 return t('exposed');
00262 }
00263
00264 $options = $this->operator_options('short');
00265 $output = check_plain($options[$this->operator]);
00266 if (in_array($this->operator, $this->operator_values(2))) {
00267 $output .= ' ' . t('@min and @max', array('@min' => $this->value['min'], '@max' => $this->value['max']));
00268 }
00269 elseif (in_array($this->operator, $this->operator_values(1))) {
00270 $output .= ' ' . check_plain($this->value['value']);
00271 }
00272 return $output;
00273 }
00274
00278 function accept_exposed_input($input) {
00279 if (empty($this->options['exposed'])) {
00280 return TRUE;
00281 }
00282
00283
00284
00285 if (!empty($this->options['expose']['identifier'])) {
00286 $value = &$input[$this->options['expose']['identifier']];
00287 if (!is_array($value)) {
00288 $value = array(
00289 'value' => $value,
00290 );
00291 }
00292 }
00293
00294 $rc = parent::accept_exposed_input($input);
00295
00296 if (empty($this->options['expose']['required'])) {
00297
00298 $info = $this->operators();
00299 if (!empty($info[$this->operator]['values'])) {
00300 switch ($info[$this->operator]['values']) {
00301 case 1:
00302 if ($value['value'] === '') {
00303 return FALSE;
00304 }
00305 break;
00306 case 2:
00307 if ($value['min'] === '' && $value['max'] === '') {
00308 return FALSE;
00309 }
00310 break;
00311 }
00312 }
00313 }
00314
00315 return $rc;
00316 }
00317 }