You are here

function calendar_build_filter in Calendar 5.2

Same name and namespace in other branches
  1. 5 calendar.module \calendar_build_filter()

Compile the filter query for this view.

Create date objects for the minimum and maximum possible dates for this view and store them in the query (and ultimately in the view), then create the query needed to find dates in that range.

Parameters

object $query:

object $view:

1 call to calendar_build_filter()
_calendar_views_query_alter in ./calendar.inc
@file All the code used while processing a calendar is stored in this file and is included only when needed.

File

./calendar.inc, line 431
All the code used while processing a calendar is stored in this file and is included only when needed.

Code

function calendar_build_filter(&$query, &$view) {
  $now = date_now();
  if ($query->calendar_type == 'week' && calendar_part_is_valid($query->week, 'week')) {
    $range = date_week_range($query->week, $query->year);
    $date = $range[0];
    $max_date = $range[1];
  }
  else {
    $month = calendar_part_is_valid($query->month, 'month') ? $query->month : 1;
    $day = calendar_part_is_valid($query->day, 'day') ? $query->day : 1;
    $year = calendar_part_is_valid($query->year, 'year') ? $query->year : date_format($now, 'Y');
    $date = date_create($year . '-' . date_pad($month) . '-' . date_pad($day) . ' 00:00:00', date_default_timezone());
    $max_date = drupal_clone($date);
    date_modify($max_date, '+1 ' . $query->calendar_type);
    date_modify($max_date, '-1 second');
  }
  $query->min_date = $date;
  $query->max_date = $max_date;

  // find all datetime fields in this view and add filters for them to the query
  $queries = array();
  foreach ($view->field as $delta => $field) {
    $query_strings = calendar_build_field_query($query, $field);
    if (!empty($query_strings)) {
      $queries = array_merge($queries, $query_strings);
    }
    $view->date_fields[] = $field;
  }

  // bring the node type into the query so we can use it in the theme
  $query
    ->add_field('type', 'node');
  if ($queries) {
    $query
      ->add_where(implode(" OR ", $queries));
  }
  return;
}