You are here

function calendar_build_field_query in Calendar 5.2

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

Build a filtering query for an individual date field

Parameters

object $query - the query object:

array $field - the view field array:

1 call to calendar_build_field_query()
calendar_build_filter in ./calendar.inc
Compile the filter query for this view.

File

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

Code

function calendar_build_field_query(&$query, $field) {
  require_once './' . drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
  $queries = array();
  $fields = calendar_fields();
  $field_name = $field['field'];
  $this_field = $fields[$field_name];
  $view_fields[] = $field_name;
  $field_type = strstr($this_field['type'], 'string') ? DATE_ISO : DATE_UNIX;

  // Create minimum and maximum comparison dates in DATETIME format.
  $min_compare = drupal_clone($query->min_date);
  $max_compare = drupal_clone($query->max_date);
  $min = date_format($min_compare, DATE_FORMAT_DATETIME);
  $max = date_format($max_compare, DATE_FORMAT_DATETIME);
  if (array_key_exists($field_name, $fields)) {
    $query
      ->ensure_table($this_field['table'], $this_field['table']);
    $tz_handling = $this_field['tz_handling'];
    $date_handler = new date_sql_handler();
    $date_handler
      ->construct($field_type);
    $date_handler->db_timezone = date_get_timezone_db($tz_handling);
    $date_handler->local_timezone = date_get_timezone($tz_handling, date_default_timezone_name());
    if ($tz_handling == 'date') {
      $date_handler->local_timezone_field = $fields['tz_field'];
      $date_handler->offset_field = $fields['offset_field'];
    }

    // Figure out where this field is in the query's field array
    // so we know which query field to adjust.
    foreach ($query->fields as $delta => $query_field) {
      if (strstr($query_field, $this_field['fullname'] . ' AS')) {
        $field_delta = $delta;
      }
    }
    if ($this_field['fromto'] && $this_field['fromto'][0] != $this_field['fromto'][1]) {

      // Format dates with from and to times into a single value that is made
      // up of the two local DATETIME values separated with a pipe (|).
      $queries[] = "(" . $date_handler
        ->sql_where_date('DATE', $this_field['fromto'][1], '>=', $min) . " AND " . $date_handler
        ->sql_where_date('DATE', $this_field['fromto'][0], '<=', $max) . ")";
    }
    else {
      $queries[] = "(" . $date_handler
        ->sql_where_date('DATE', $this_field['fullname'], '>=', $min) . ' AND ' . $date_handler
        ->sql_where_date('DATE', $this_field['fullname'], '<=', $max) . ")";
    }
  }
  return $queries;
}