You are here

function date_views_argument_handler::query in Date 7

Same name and namespace in other branches
  1. 8 date_views/includes/date_views_argument_handler.inc \date_views_argument_handler::query()
  2. 7.3 date_views/includes/date_views_argument_handler.inc \date_views_argument_handler::query()
  3. 7.2 date_views/includes/date_views_argument_handler.inc \date_views_argument_handler::query()

Set up the query for this argument.

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

Overrides views_handler_argument_formula::query

File

date_views/includes/date_views_argument_handler.inc, line 294
Date API views argument handler.

Class

date_views_argument_handler
Date API argument handler.

Code

function query() {
  $this
    ->get_query_fields();
  $block_identifier = date_block_identifier($this->view);
  if (!empty($this->view->block_identifier) || isset($_GET[$block_identifier])) {

    // Retrieve the block arguments in a way that will work for
    // urls like user/%/calendar/2009-04.
    if (!empty($_GET[$block_identifier])) {
      $path_args = explode('/', $this->view
        ->get_path());
      $mini_args = explode('/', $_GET[$block_identifier]);
      foreach ($path_args as $pos => $key) {
        if ($path_args[$pos] != '%') {
          unset($mini_args[$pos]);
        }
      }

      // Get rid of gaps in the array caused by embedded args.
      $mini_args = array_values($mini_args);
      $this->view->args = $mini_args;
    }
    $i = 0;
    foreach ($this->view->argument as $argument) {
      if ($argument->field == 'date_argument') {
        $this->argument = $this->view->args[$argument->position];
        break;
      }
      $i++;
    }
  }
  $parts = $this->date_handler
    ->arg_parts($this->argument);
  foreach ($parts as $type) {
    foreach ($type as $part) {
      foreach ($part as $key => $value) {
        if (!empty($value)) {

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

  // See if we're outside the allowed date range for our argument.
  if (date_format($min_date, 'Y') < $this->view->date_info->min_allowed_year || date_format($max_date, 'Y') > $this->view->date_info->max_allowed_year) {
    $this->forbid = TRUE;
    $this->query
      ->add_where_expression('date', "0=1", array());
    return;
  }

  // The second option seems to work better in the block view if
  // set to something other than the original value.
  // Need to keep an eye on this to be sure nothing else breaks.

  //$format = $this->date_handler->views_formats($this->options['granularity'], 'sql');
  $format = $this->date_handler
    ->views_formats($this->granularity, 'sql');
  if (!empty($this->query_fields)) {

    // 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');
    foreach ($this->query_fields as $query_field) {
      $field = $query_field['field'];
      $date_handler = $query_field['date_handler'];
      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'];
      $from_field = str_replace($field['table_name'] . '_', $table_alias . '.', $field['fromto'][0]);
      $to_field = str_replace($field['table_name'] . '_', $table_alias . '.', $field['fromto'][1]);
      if ($this->granularity != 'week') {
        $from = $date_handler
          ->sql_where_format($format, $from_field, '<=', date_format($max_date, $format));
        $to = $date_handler
          ->sql_where_format($format, $to_field, '>=', date_format($min_date, $format));
      }
      else {
        $format = DATE_FORMAT_DATETIME;
        $from = $date_handler
          ->sql_where_date('DATE', $from_field, '<=', date_format($max_date, $format));
        $to = $date_handler
          ->sql_where_date('DATE', $to_field, '>=', date_format($min_date, $format));
      }
      $sql = str_replace('***table***', $field['table_name'], "({$from} AND {$to})");
      if ($sql) {
        $this->query
          ->add_where_expression('date', $sql, array());
      }
    }
  }
}