You are here

function date_views_filter_handler::query in Date 7

Custom implementation of query() so we can get the AND and OR methods in the right places.

Overrides views_handler_filter_numeric::query

File

date_views/includes/date_views_filter_handler.inc, line 420
A flexible, configurable date filter.

Class

date_views_filter_handler
This filter allows you to select a granularity of date parts to filter on, such as year, month, day, etc.

Code

function query() {
  $this
    ->ensure_my_table();
  $this
    ->get_query_fields();

  // If we don't add a dummy where clause and there is no other filter defined for this view,
  // Views will dump in an invalid WHERE () in addition to our custom filters, so give it a valid value.
  // @TODO This is probably the wrong way to solve this problem.
  if (empty($this->query->where[0]['conditions'])) {
    $this->query
      ->add_where_expression(NULL, '1=1', array());
  }
  if (!empty($this->query_fields)) {
    foreach ((array) $this->query_fields as $query_field) {
      $field = $query_field['field'];
      if ($field['table_name'] != $this->table || !empty($this->relationship)) {
        $this->related_table_alias = $this->query
          ->queue_table($field['table_name'], $this->relationship);
      }
      $table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];
      $query_field['field']['fullname'] = $table_alias . '.' . $query_field['field']['field_name'];
      $sql = '';
      $sql_parts = array();
      switch ($this->operator) {
        case 'between':
          $sql_parts[] = $this
            ->date_filter('min', $query_field, '>=');
          $sql_parts[] = $this
            ->date_filter('max', $query_field, '<=');
          $sql = implode(' AND ', array_filter($sql_parts));
          break;
        case 'not between':
          $sql_parts[] = $this
            ->date_filter('min', $query_field, '<');
          $sql_parts[] = $this
            ->date_filter('max', $query_field, '>');
          $sql = implode(' OR ', array_filter($sql_parts));
          break;
        default:
          $sql = $this
            ->date_filter('value', $query_field, $this->operator);
          break;
      }
      if (!empty($sql)) {

        // Use set_where_group() with the selected date_method
        // of 'AND' or 'OR' to combine the field WHERE clauses.
        $this->query
          ->set_where_group($this->options['date_method'], 'date');
        $this->query
          ->add_where_expression('date', $sql, array());
      }
    }
  }
}