You are here

function mvf_handler_filter_mvf::op_between in Measured Value Field 7

Overrides views_handler_filter_numeric::op_between

File

views/mvf_handler_filter_mvf.inc, line 233

Class

mvf_handler_filter_mvf
Base Views Filter Handler for field types defined in MVF module.

Code

function op_between($field) {
  $field = array(
    'value' => $this->table_alias . '.' . $this->definition['field_name'] . '_' . mvf_subfield_to_column('value'),
    'unit' => $this->table_alias . '.' . $this->definition['field_name'] . '_' . mvf_subfield_to_column('unit'),
  );
  $measure = array_pop($this->options['field_definition']['settings']['unit']['handler_settings']['target_bundles']);
  $where = db_or();
  foreach (units_unit_by_measure_load_multiple($measure) as $to_unit) {
    $from_units = array(
      'min' => $this->value['min'][mvf_subfield_to_column('unit')],
      'max' => $this->value['max'][mvf_subfield_to_column('unit')],
    );
    $tmp = units_unit_load_multiple(array_values($from_units));
    $converted_value = array();
    foreach ($from_units as $k => $v) {
      $from_units[$k] = $tmp[$v];
      $converted_value[$k] = units_convert($this->value[$k][mvf_subfield_to_column('value')], $from_units[$k]->machine_name, $to_unit->machine_name);
    }
    $to_unit = entity_extract_ids('units_unit', $to_unit);
    $to_unit = array_shift($to_unit);
    switch ($this->operator) {
      case 'between':
        $where
          ->condition(db_and()
          ->condition($field['value'], $converted_value, $this->operator)
          ->condition($field['unit'], $to_unit));
        break;
      case 'not between':
        $where
          ->condition(db_and()
          ->condition(db_or()
          ->condition($field['value'], $converted_value['min'], '<=')
          ->condition($field['value'], $converted_value['max'], '>='))
          ->condition($field['unit'], $to_unit));
        break;
    }
  }
  $this->query
    ->add_where($this->options['group'], $where);
}