You are here

function _calendar_views_query_alter in Calendar 5.2

@file All the code used while processing a calendar is stored in this file and is included only when needed.

1 call to _calendar_views_query_alter()
calendar_views_query_alter in ./calendar.module
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…

File

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

Code

function _calendar_views_query_alter(&$query, &$view) {

  // make sure block views default to the current month
  // and make sure day view is not selected
  $now = date_now();
  $view->real_args = $view->args;
  $view->real_url = calendar_real_url($view, $view->args);
  $display_formats = calendar_get_formats($view);
  $calendar_fields = calendar_fields();

  // Embedded calendar views can have additional arguments.
  if ($view->build_type == 'block' || $view->build_type == 'embed') {
    $block_identifier = isset($view->block_identifier) ? $view->block_identifier : 'mini';
    $pos = calendar_arg_positions($view);

    // If we don't have any calendar arguments, look in the block_identifier for them.
    if ($pos[0] >= count($view->args) && !empty($_GET[$block_identifier])) {

      // Add the arguments from the block_identifier query param.
      $view->real_args = $view->args = array_merge($view->args, explode('/', str_replace($view->real_url . '/', '', $_GET[$block_identifier])));

      // Filter the query based on the newly acquired arguments.
      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 (!empty($view->args[$delta]) && $view->args[$delta] != CALENDAR_EMPTY_ARG) {
          if ($argument['type'] == 'calendar_year') {
            calendar_filter_year($query, $view->args[$delta]);
          }
          elseif ($argument['type'] == 'calendar_month') {
            calendar_filter_month($query, $view->args[$delta]);
          }
          elseif ($argument['type'] == 'calendar_day') {
            calendar_filter_day($query, $view->args[$delta]);
          }
          elseif ($argument['type'] == 'calendar_week') {
            calendar_filter_week($query, $view->args[$delta]);
          }
        }
      }
    }
  }

  // If no arguments are provided, and this is not the ical view,
  // default to current month view.
  if (empty($view->args) || !calendar_is_calendar_arg($view)) {

    // See what type of view the ical view is looking for from the args.
    // Find the position of the last non-empty arugment.
    if (is_array($view->args) && in_array('ical', $view->args)) {
      $pos = 0;
      foreach ($view->args as $delta => $arg) {
        if ($arg == 'ical') {

          // It was the previous argument, back up one position.
          $pos--;
          break;
        }
        elseif ($arg != CALENDAR_EMPTY_ARG) {
          $pos++;
        }
      }
      $query->calendar_type = str_replace('calendar_', '', $view->argument[$pos]['type']);
    }
    else {
      $query->calendar_type = 'month';
    }
    foreach ($view->argument as $delta => $argument) {
      if ($argument['type'] == 'calendar_year' && !$view->args[$delta]) {
        $view->args[$delta] = date_format($now, 'Y');
        calendar_filter_year($query, date_format($now, 'Y'));
      }
      elseif (($argument['type'] == 'calendar_month' || $argument['type'] == 'calendar_week') && !$view->args[$delta]) {
        $view->args[$delta] = date_format($now, 'm');
        calendar_filter_month($query, date_format($now, 'm'));
      }
      elseif ($argument['type'] == 'calendar_day' && !$view->args[$delta]) {
        $view->args[$delta] = CALENDAR_EMPTY_ARG;
      }
      else {
        $view->args[$delta] = !empty($view->real_args[$delta]) ? $view->real_args[$delta] : CALENDAR_EMPTY_ARG;
      }
    }

    // Default arguments are real arguments, too.
    $view->real_args = $view->args;
  }
  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 preceding '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 (($view->build_type == 'page' || $view->build_type == 'embed') && $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;
  $view->min_date = $query->min_date;
  $view->max_date = $query->max_date;
  $view->min_date_date = date_format($view->min_date, DATE_FORMAT_DATE);
  $view->max_date_date = date_format($view->max_date, DATE_FORMAT_DATE);

  // See if we're outside the allowed date range for our argument.
  $view_dates = calendar_year_range($view);
  $view_min_year = $view_dates[0];
  $view_max_year = $view_dates[1];
  if (date_format($view->min_date, 'Y') < $view_min_year || date_format($view->max_date, 'Y') > $view_max_year) {
    drupal_not_found();
    exit;
  }

  // Identify the kind of display we're using for this view.
  // Check first for 'view' in url to get displays set by the switch
  // block, then for 'calendar_display_format' variable to get displays
  // set in the setup, default to normal calendar display.
  if ($view->build_type == 'block') {
    $view->calendar_display = $display_formats['block'];
    $view->mini = TRUE;
  }
  elseif (isset($_GET['view']) && $view->build_type == 'page') {
    $view->calendar_display = !empty($_GET['view']) ? check_plain($_GET['view']) : 'calendar';
  }
  elseif ($view->build_type == 'page' || $view->build_type == 'embed') {
    $display_formats = calendar_get_formats($view);
    $view->calendar_display = $display_formats[$view->calendar_type];
  }

  // Global used to turn on the "Switch calendar" block.
  if ($view->build_type == 'page') {
    $GLOBALS['calendar_is_calendar'] = TRUE;
  }
  return;
}