You are here

function _datex_date_views_argument_handler_simple::_query in Datex 7.3

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 _datex_date_views_argument_handler_simple::_query()
_datex_date_views_argument_handler_simple::query in ./datex.views.inc
Fix the query for localized dates.

File

./datex.views.inc, line 808
Datex views integration.

Class

_datex_date_views_argument_handler_simple
Copied from date_views module. So we have no hard dependency on it.

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.
  if ($this->options['granularity_reset'] && ($granularity = $this->date_handler
    ->arg_granularity($this->argument))) {
    $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([
      '_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 = [
    $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}", [
      $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}", [
      $view_min_placeholder => $view_min,
      $view_max_placeholder => $view_max,
    ]);
  }
}