You are here

function _date_views_query_alter in Date 5

Same name and namespace in other branches
  1. 5.2 date/date_views.inc \_date_views_query_alter()

Implementation of hook_views_query() Used to make sure view defaults to current date if no date selected

1 call to _date_views_query_alter()
date_views_query_alter in ./date.module

File

./date_views.inc, line 605

Code

function _date_views_query_alter(&$query, &$view) {
  $date_views_browser_views = date_views_browser_get_views();
  if (in_array($view->name, array_keys($date_views_browser_views))) {
    $path = explode('/', $view->url);
    $pos = sizeof($path);
    if ($view->build_type == 'block' || arg($pos) == '') {
      $arg = NULL;
    }
    else {
      $arg = arg($pos);
    }
    if ($arg == NULL) {

      // if no argument specified, add the current date range to the query
      $arg = date_views_browser_period_arg($arg, $view->argument[0]['options']);
      $name = explode(':', $view->argument[0]['type']);
      $field_name = trim($name[1]);
      $field = content_fields($field_name);
      $field_type = $field['type'] == 'datestamp' ? 'int' : 'iso';
      $db_info = content_database_info($field);
      $value = $db_info['columns']['value']['column'];
      $table = 'node_data_' . $field['field_name'];
      $offset = date_views_offset($field);
      if ($range = date_views_date_range($arg, $field)) {
        $query
          ->ensure_table($table);
        $query
          ->add_field('nid', 'node');
        $query
          ->add_field($value, $table);
        $query
          ->add_where(date_sql('DATE', $table . '.' . $value, $field_type, $offset) . ">='" . str_replace('T', ' ', $range[0]) . "'");
        $query
          ->add_where(date_sql('DATE', $table . '.' . $value, $field_type, $offset) . "<='" . str_replace('T', ' ', $range[1]) . "'");
      }
    }
  }
}