You are here

function calendar_views_query_alter in Calendar 5.2

Same name and namespace in other branches
  1. 5 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 249
Adds calendar filtering and displays to Views.

Code

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

  // If there are args before the calendar args and they don't have values
  // we don't have enough information to create calendar navigation links
  // so exit here. Except if this is a block because we construct
  // missing arguments for the block.
  $pos = calendar_arg_positions($view);
  if ($pos[0] > count($view->args) && $view->build_type != 'block') {
    return;
  }

  // Check if a new date has been selected and if so redirect.
  if (isset($_POST['calendar_goto'])) {
    $parts = $view->args;
    require_once './' . drupal_get_path('module', 'date_api') . '/date_api_elements.inc';
    $format = date_limit_format(variable_get('date_format_short', 'm/d/Y - H:i'), array(
      'year',
      'month',
      'day',
    ));
    if (!empty($_POST['calendar_goto']['date'])) {
      $date = date_convert_from_custom($_POST['calendar_goto']['date'], $format);
    }
    else {
      $date = date_convert($_POST['calendar_goto'], DATE_ARRAY, DATE_DATETIME);
    }
    switch ($_POST['calendar_type']) {
      case 'year':
        $parts[$pos[0]] = date_pad(date_part_extract($date, 'year'), 4);
        break;
      case 'month':
        $parts[$pos[0]] = date_pad(date_part_extract($date, 'year'), 4);
        $parts[$pos[1]] = date_pad(date_part_extract($date, 'month'));
        break;
      case 'week':
        $parts[$pos[0]] = date_pad(date_part_extract($date, 'year'), 4);
        $parts[$pos[1]] = 'W' . date_pad(date_week($date));
        break;
      default:
        $parts[$pos[0]] = date_pad(date_part_extract($date, 'year'), 4);
        $parts[$pos[1]] = date_pad(date_part_extract($date, 'month'));
        $parts[$pos[2]] = date_pad(date_part_extract($date, 'day'));
        break;
    }
    if ($view->build_type == 'page') {

      // Append the date parts on to the end of the path.
      drupal_goto($view->url . '/' . implode('/', $parts), calendar_querystring($view));
    }
    else {

      // Append the date parts on to the end of the block_identifier query param.
      $block_identifier = isset($view->block_identifier) ? $view->block_identifier : 'mini';
      $view->real_url = calendar_real_url($view, $view->args);
      drupal_goto($view->real_url, calendar_querystring($view, array(
        $block_identifier => $view->real_url . '/' . implode('/', $parts),
      )));
    }
    drupal_exit();
  }
  require_once './' . drupal_get_path('module', 'calendar') . '/calendar.inc';
  require_once './' . drupal_get_path('module', 'calendar') . '/calendar.theme';
  return _calendar_views_query_alter($query, $view);
}