You are here

function date_views_plugin_pager::query in Date 8

Same name and namespace in other branches
  1. 7.3 date_views/includes/date_views_plugin_pager.inc \date_views_plugin_pager::query()
  2. 7.2 date_views/includes/date_views_plugin_pager.inc \date_views_plugin_pager::query()

Transfer date information from the argument to the view so the pager theme can use it and update the date argument value to whatever is set by the pager.

File

date_views/includes/date_views_plugin_pager.inc, line 123
Date pager. Works with a Date argument, the argument filters the view and the pager provides back/next navigation.

Class

date_views_plugin_pager
Example plugin to handle paging by month.

Code

function query() {
  $calendar = system_calendar();

  // By fetching our data from the exposed input, it is possible to
  // feed pager data through some method other than $_GET.
  $input = $this->view
    ->get_exposed_input();
  $value = NULL;
  if (!empty($input) && !empty($input[$this->options['date_id']])) {
    $value = $input[$this->options['date_id']];
  }

  // Bring the argument information into the view so our theme can access it.
  $i = 0;
  foreach ($this->view->argument as $id => &$argument) {
    if (date_views_handler_is_date($argument, 'argument')) {

      // If the argument is empty, nothing to do. This could be from
      // an argument that does not set a default value.
      if (empty($argument->argument) || empty($argument->date_handler)) {
        continue;
      }

      // Storing this information in the pager so it's available for summary info.
      // The view argument information is not otherwise accessible to the pager.
      // Not working right yet, tho.
      $date_handler = $argument->date_handler;
      $this->options['date_argument'] = $id;
      $this->options['granularity'] = $argument->date_handler->granularity;

      // Reset values set by argument if pager requires it.
      if (!empty($value)) {
        $argument->argument = $value;
        $argument->date_range = $argument->date_handler
          ->arg_range($value);
        $argument->min_date = $argument->date_range[0];
        $argument->max_date = $argument->date_range[1];

        // $argument->is_default works correctly for normal arguments, but does not
        // work correctly if we are swapping in a new value from the pager.
        $argument->is_default = FALSE;
      }

      // The pager value might move us into a forbidden range, so test it.
      if ($this
        ->date_forbid($argument)) {
        $this->view->build_info['fail'] = TRUE;
        return;
      }
      if (empty($this->view->date_info)) {
        $this->view->date_info = new stdClass();
      }
      $this->view->date_info->granularity = $argument->date_handler->granularity;
      $format = $this->view->date_info->granularity == 'week' ? DATE_FORMAT_DATETIME : $argument->sql_format;
      $this->view->date_info->placeholders = isset($argument->placeholders) ? $argument->placeholders : $argument->date_handler->placeholders;
      $this->view->date_info->date_arg = $argument->argument;
      $this->view->date_info->date_arg_pos = $i;
      $this->view->date_info->year = date_format($argument->min_date, 'Y');
      $this->view->date_info->month = date_format($argument->min_date, 'n');
      $this->view->date_info->day = date_format($argument->min_date, 'j');
      $this->view->date_info->week = date_calendar_week(date_format($argument->min_date, DATE_FORMAT_DATE));
      $this->view->date_info->date_range = $argument->date_range;
      $this->view->date_info->min_date = $argument->min_date;
      $this->view->date_info->max_date = $argument->max_date;
      $this->view->date_info->limit = $argument->limit;
      $this->view->date_info->url = $this->view
        ->get_url();
      $this->view->date_info->pager_id = $this->options['date_id'];
      $this->view->date_info->date_pager_position = $this->options['pager_position'];
      $this->view->date_info->date_pager_format = $this->options['link_format'];
    }
    $i++;
  }

  // Is this a view that needs to be altered based on a pager value?
  // If there is pager input and the argument has set the placeholders,
  // swap the pager value in for the placeholder set by the argument.
  if (!empty($value) && !empty($this->view->date_info->placeholders)) {
    $placeholders = $this->view->date_info->placeholders;
    $count = count($placeholders);
    foreach ($this->view->query->where as $group => $data) {
      foreach ($data['conditions'] as $delta => $condition) {
        if (array_key_exists('value', $condition) && is_array($condition['value'])) {
          foreach ($condition['value'] as $placeholder => $placeholder_value) {
            if (array_key_exists($placeholder, $placeholders)) {

              // If we didn't get a match, this is a > $min < $max query that uses the view
              // min and max dates as placeholders.
              $date = $count == 2 ? $this->view->date_info->min_date : $this->view->date_info->max_date;
              $next_placeholder = array_shift($placeholders);
              $this->view->query->where[$group]['conditions'][$delta]['value'][$placeholder] = $date
                ->format($format);
              $count--;
            }
          }
        }
      }
    }
  }
}