You are here

function views_between_dates_filter_handler::op_between_dates in Views between dates filter 7

File

includes/views_between_dates_filter_handler.inc, line 41
A filter similar to separate filters for start and end date, but exposed in a single field.

Class

views_between_dates_filter_handler
@file A filter similar to separate filters for start and end date, but exposed in a single field.

Code

function op_between_dates($field) {
  $this
    ->get_query_fields();
  if (empty($this->query_fields)) {
    return;
  }
  $field_names = array();
  foreach ((array) $this->query_fields as $query_field) {
    $field = $query_field['field'];
    $this->date_handler = $query_field['date_handler'];

    // Respect relationships when determining the table alias.
    if ($field['table_name'] != $this->table || !empty($this->relationship)) {
      $this->related_table_alias = $this->query
        ->ensure_table($field['table_name'], $this->relationship);
    }
    $table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];

    // TODO: Research adding delta field code from parent:op_simple here.
    $field_name = $table_alias . '.' . $field['field_name'];
    $field_names[] = $field_name;
  }

  // SQL: start date <= value AND end date >= value.
  // If data is correct.
  if (count($field_names) == 2) {
    $value = $this
      ->get_filter_value('value', $this->value['value']);
    $comp_date = new DateObject($value, date_default_timezone(), $this->format);
    $start_field = $this->date_handler
      ->sql_field($field_names[0], NULL, $comp_date);
    $start_field = $this->date_handler
      ->sql_format($this->format, $start_field);
    $end_field = $this->date_handler
      ->sql_field($field_names[1], NULL, $comp_date);
    $end_field = $this->date_handler
      ->sql_format($this->format, $end_field);
    $placeholder = $this
      ->placeholder();
    $group = !empty($this->options['date_group']) ? $this->options['date_group'] : $this->options['group'];
    if ($this->operator == '=') {
      $this->query
        ->add_where_expression($group, "{$start_field} <= {$placeholder} AND {$end_field} >= {$placeholder}", array(
        $placeholder => $value,
      ));
    }
    else {
      $this->query
        ->add_where_expression($group, "{$start_field} > {$placeholder} OR {$end_field} < {$placeholder}", array(
        $placeholder => $value,
      ));
    }
  }
}