You are here

function date_api_argument_handler::query in Date 6

Same name and namespace in other branches
  1. 6.2 includes/date_api_argument_handler.inc \date_api_argument_handler::query()

Set up the query for this argument.

The argument sent may be found at $this->argument.

File

./date_api.views.inc, line 307
Defines date-related Views data and plugins:

Class

date_api_argument_handler
Date API argument handler.

Code

function query() {
  $parts = $this->date_handler
    ->arg_parts($this->argument);
  foreach ($parts[0]['date'] as $key => $part) {

    // The last part evaluated is the one that will 'stick'
    // as the date type.
    $this->date_type = $key;
    $this->{$key} = $part;
  }
  $range = $this->date_handler
    ->arg_range($this->argument);
  $min_date = $range[0];
  $max_date = $range[1];

  // Create min and max dates in both local and UTC time.
  // We'll compare fields to the UTC date whenever possible
  // to avoid the need to do timezone conversions. When that
  // isn't possible (the date is not stored in UTC or needs to
  // be converted back to a time that may be different than
  // the local timezone) we will have to do tz conversions in
  // the database.
  $this->min_date = $min_date;
  $this->min_utc = drupal_clone($min_date);
  date_timezone_set($this->min_utc, timezone_open('UTC'));
  $this->max_date = $max_date;
  $this->max_utc = drupal_clone($max_date);
  date_timezone_set($this->max_utc, timezone_open('UTC'));

  // Use set_where_group() with the selected date_method
  // of 'AND' or 'OR' to create the where clause.
  $this->query
    ->set_where_group($this->options['date_method'], 'date');
  $this
    ->ensure_my_table();
  $this
    ->get_query_fields();
  foreach ($this->query_fields as $query_field) {
    $field = $query_field['field'];
    $date_handler = $query_field['date_handler'];

    // Make sure this field is added to the query.
    $this->query
      ->add_field($field['table_name'], $field['field_name']);
    foreach ($field['related_fields'] as $related) {
      $bits = explode('.', $related);
      if ($bits[1] != $field['field_name']) {
        $this->query
          ->add_field($field['table_name'], $bits[1]);
      }
    }
    $from = $date_handler
      ->sql_where_date('DATE', $field['fullname'], '>=', date_format($date_handler->min_date, DATE_FORMAT_DATETIME));
    $to = $date_handler
      ->sql_where_date('DATE', $field['fullname'], '<=', date_format($date_handler->max_date, DATE_FORMAT_DATETIME));
    $sql = str_replace('***table***', $this->table_alias, "({$from} AND {$to})");
    if ($sql) {
      $this->query
        ->add_where('date', $sql);
    }
  }
}