You are here

function date_views_argument_handler_simple::query in Date 8

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

Inject a test for valid date range before the regular query. Override the parent query to be able to control the $group.

1 call to date_views_argument_handler_simple::query()
date_views_argument_handler::query in date_views/includes/date_views_argument_handler.inc
Set up the query for this argument.
1 method overrides date_views_argument_handler_simple::query()
date_views_argument_handler::query in date_views/includes/date_views_argument_handler.inc
Set up the query for this argument.

File

date_views/includes/date_views_argument_handler_simple.inc, line 262
Date API views argument handler.

Class

date_views_argument_handler_simple
Date API argument handler.

Code

function query($group_by = FALSE) {

  // @TODO Not doing anything with $group_by yet, need to figure out what has to be done.
  if ($this
    ->date_forbid()) {
    return;
  }

  // See if we need to reset granularity based on an argument value.
  // Make sure we don't try to reset to some bogus value if someone has typed in an unexpected argument.
  $granularity = $this->date_handler
    ->arg_granularity($this->argument);
  if (!empty($granularity)) {
    $this->date_handler->granularity = $granularity;
    $this->format = $this->date_handler
      ->views_formats($this->date_handler->granularity, 'display');
    $this->sql_format = $this->date_handler
      ->views_formats($this->date_handler->granularity, 'sql');
  }
  $this->granularity = $this->date_handler->granularity;
  $this
    ->ensure_my_table();
  $group = !empty($this->options['date_group']) ? $this->options['date_group'] : 0;

  // If requested, add the delta field to the view so we can later find the value that matched our query.
  if (!empty($this->options['add_delta']) && (substr($this->real_field, -6) == '_value' || substr($this->real_field, -7) == '_value2')) {
    $this->query
      ->add_field($this->table_alias, 'delta');
    $real_field_name = str_replace(array(
      '_value',
      '_value2',
    ), '', $this->real_field);
    $this->query
      ->add_field($this->table_alias, 'entity_id', 'date_id_' . $real_field_name);
    $this->query
      ->add_field($this->table_alias, 'delta', 'date_delta_' . $real_field_name);
  }
  $format = $this->date_handler->granularity == 'week' ? DATE_FORMAT_DATETIME : $this->sql_format;
  $view_min = date_format($this->min_date, $format);
  $view_max = date_format($this->max_date, $format);
  $view_min_placeholder = $this
    ->placeholder();
  $view_max_placeholder = $this
    ->placeholder();
  $this->date_handler->placeholders = array(
    $view_min_placeholder => $view_min,
    $view_max_placeholder => $view_max,
  );

  // Are we comparing this field only or the Start/End date range to the view criteria?
  if (!empty($this->options['use_fromto'])) {

    // The simple case, match the field to the view range.
    $field = $this->date_handler
      ->sql_field($this->table_alias . '.' . $this->real_field, NULL, $this->min_date);
    $field = $this->date_handler
      ->sql_format($format, $field);
    $this->query
      ->add_where_expression($group, "{$field} >= {$view_min_placeholder} AND {$field} <= {$view_max_placeholder}", array(
      $view_min_placeholder => $view_min,
      $view_max_placeholder => $view_max,
    ));
  }
  else {

    // Look for the intersection of the range of the date field with the range of the view.
    // Get the Start/End values for this field. Retrieve using the original table name.
    // Swap the current table name (adjusted for relationships) into the query.
    // @TODO We may be able to use Views substitutions here, investigate that later.
    $fields = date_views_fields($this->base_table);
    $fields = $fields['name'];
    $fromto = $fields[$this->original_table . '.' . $this->real_field]['fromto'];
    $value_min = str_replace($this->original_table, $this->table_alias, $fromto[0]);
    $value_max = str_replace($this->original_table, $this->table_alias, $fromto[1]);
    $field_min = $this->date_handler
      ->sql_field($value_min, NULL, $this->min_date);
    $field_min = $this->date_handler
      ->sql_format($format, $field_min);
    $field_max = $this->date_handler
      ->sql_field($value_max, NULL, $this->max_date);
    $field_max = $this->date_handler
      ->sql_format($format, $field_max);
    $this->query
      ->add_where_expression($group, "{$field_max} >= {$view_min_placeholder} AND {$field_min} <= {$view_max_placeholder}", array(
      $view_min_placeholder => $view_min,
      $view_max_placeholder => $view_max,
    ));
  }
}