You are here

function calendar_views_query_alter in Calendar 5

Same name and namespace in other branches
  1. 5.2 calendar.module \calendar_views_query_alter()
  2. 6.2 includes/calendar.views.inc \calendar_views_query_alter()
  3. 7 includes/calendar.views.inc \calendar_views_query_alter()
  4. 7.2 includes/calendar.views.inc \calendar_views_query_alter()

Implementation of hook_views_query() Insert filters into the query based on the current calendar view and the selected fields Used when the actual view arguments don't provide enough info to construct the query. i.e. on a view with no arguments or one with partial arguments like year or year/month.

Parameters

object $query: @param object $view

File

./calendar.module, line 791
Adds calendar filtering and displays to Views.

Code

function calendar_views_query_alter(&$query, &$view) {
  $view->real_args = $view->args;
  $view->real_url = calendar_real_url($view, $view->args);
  if (!calendar_has_calendar_args($view) || empty($view->args) && !calendar_is_calendar_arg($view) && $view->argument[0]['argdefault'] != 2) {
    return;
  }
  calendar_load_calendar_api();
  calendar_load_date_api();

  // make sure block views default to the current month
  // and make sure day view is not selected
  if ($view->build_type == 'block') {
    $query->calendar_type = 'month';
    $view->args = explode('/', str_replace($view->url . '/', '', $_GET['mini']));
    foreach ($view->argument as $delta => $argument) {

      // Special handling for OG gid argument.
      // Find a default value for the gid when used in a block.
      if ($argument['type'] == 'gid') {
        $groupnodes = calendar_og_groups($view);
        $view->args[$delta] = $groupnodes[0];
        $query
          ->ensure_table('og_ancestry');
        $query
          ->add_where("og_ancestry.group_nid IN (%d)", implode(',', $groupnodes));
      }
      if ($argument['type'] == 'calendar_year') {
        if (!$view->args[$delta]) {
          $view->args[$delta] = calendar_user_date('year');
        }
        $query->year = $view->args[$delta];
        calendar_filter_year($query, $query->year);
      }
      elseif ($argument['type'] == 'calendar_month' || $argument['type'] == 'calendar_week') {
        if (!$view->args[$delta]) {
          $view->args[$delta] = calendar_user_date('month');
        }
        $query->month = $view->args[$delta];
        calendar_filter_month($query, $query->month);
      }
      elseif ($argument['type'] == 'calendar_day') {
        $query->day = CALENDAR_EMPTY_ARG;
        $view->args[$delta] = CALENDAR_EMPTY_ARG;
      }
    }
  }

  // Either a month or a week argument could occupy the second position of the group
  // this is done so that a single view has the capability to switch between all calendar layouts
  // to make this work we must make some adjustments to the view
  if ($view->build_type == 'page') {
    $GLOBALS['calendar_is_calendar'] = TRUE;
    if (empty($view->args) || !calendar_is_calendar_arg($view)) {

      // If no arguments are provided, default to current month view.
      $query->calendar_type = 'month';
      foreach ($view->argument as $delta => $argument) {
        if ($argument['type'] == 'calendar_year' && !$view->args[$delta]) {
          $view->args[$delta] = calendar_user_date('year');
          calendar_filter_year($query, calendar_user_date('year'));
        }
        elseif ($argument['type'] == 'calendar_month' && !$view->args[$delta]) {
          $view->args[$delta] = calendar_user_date('month');
          calendar_filter_month($query, calendar_user_date('month'));
        }
        elseif ($argument['type'] == 'calendar_day' && !$view->args[$delta]) {
          $view->args[$delta] = CALENDAR_EMPTY_ARG;
        }
        else {
          $view->args[$delta] = $view->real_args[$delta];
        }
      }
    }
    foreach ($view->argument as $delta => $argument) {
      if (in_array($argument['type'], calendar_args())) {

        // make sure 'display all values' is selected for the calendar arguments
        // summary views are meaningless and create errors in this context
        $view->argument[$delta]['argdefault'] = 2;

        // Pad any unused values in the view arguments with CALENDAR_EMPTY_ARG to indicate all values.
        if (empty($view->args[$delta])) {
          $view->args[$delta] = CALENDAR_EMPTY_ARG;
        }
      }

      // Calendar_week and Calendar_month can swap positions as the second arg in the url.
      // Do some work here to make sure we know which is which and swap view data to match it.
      // the difference between a calendar_month arg and a calendar_week arg is the preceeding 'W'
      if ($argument['type'] == 'calendar_week' || $argument['type'] == 'calendar_month') {
        if (strstr($view->args[$delta], 'W')) {
          calendar_filter_week($query, $view->args[$delta]);
          $view->argument[$delta]['type'] = 'calendar_week';
          $view->argument[$delta]['id'] = 'calendar_week';
          $view->argument[$delta + 1]['type'] = 'calendar_day';
          $view->argument[$delta + 1]['id'] = 'calendar_day';

          // Make sure that there is no day set for the week view.
          $view->args[$delta + 1] = CALENDAR_EMPTY_ARG;
        }
        elseif (!strstr($view->args[$delta], 'W') && $view->build_type == 'page' && $view->argument[$delta]['type'] == 'calendar_week') {
          calendar_filter_month($query, $view->args[$delta]);
          $view->argument[$delta]['type'] = 'calendar_month';
          $view->argument[$delta]['id'] = 'calendar_month';
          $view->argument[$delta + 1]['type'] = 'calendar_day';
          $view->argument[$delta + 1]['id'] = 'calendar_day';
        }
      }
    }
  }

  // Make sure the calendar query gets inserted. May not have finished yet on views like year or year/month.
  if (!$query->calendar_finished) {
    calendar_build_filter($query, $view);
  }
  $view->calendar_type = $query->calendar_type;
  $view->year = $query->year;
  $view->month = $query->month;
  $view->day = $query->day;
  $view->week = $query->week;
}