You are here

class date_views_plugin_pager in Date 7.3

Same name and namespace in other branches
  1. 8 date_views/includes/date_views_plugin_pager.inc \date_views_plugin_pager
  2. 7.2 date_views/includes/date_views_plugin_pager.inc \date_views_plugin_pager

Views pager plugin to page by month.

Hierarchy

Expanded class hierarchy of date_views_plugin_pager

1 string reference to 'date_views_plugin_pager'
date_views_views_plugins in date_views/includes/date_views.views.inc
Implements hook_views_plugins().

File

date_views/includes/date_views_plugin_pager.inc, line 61
Date pager.

View source
class date_views_plugin_pager extends views_plugin_pager {

  /**
   * {@inheritdoc}
   */
  public function use_count_query() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function has_more_records() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function summary_title() {
    return t("Position: @position, format: @format.", array(
      '@position' => $this->options['pager_position'],
      '@format' => $this->options['link_format'],
    ));
  }

  /**
   * {@inheritdoc}
   */
  public function option_definition() {
    $options = parent::option_definition();
    $options['date_id'] = array(
      'default' => 'date',
    );
    $options['pager_position'] = array(
      'default' => 'top',
    );
    $options['link_format'] = array(
      'default' => 'pager',
    );
    $options['date_argument'] = array(
      'default' => 'Unknown',
    );
    $options['granularity'] = array(
      'default' => 'Unknown',
    );
    $options['skip_empty_pages'] = array(
      'default' => FALSE,
    );
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function options_form(&$form, &$form_state) {
    $form['markup']['#markup'] = t('This pager works together with a Date or Content date field contextual filter. If a Date filter has been added to the view, this pager will provide back/next paging to match the granularity of that filter (i.e. paging by year, month, week, or day). The filter must also be configured to use a DATE default value. If there is no Date contextual filter on this view, or if it has not been set to use a default date, the pager will not appear.');
    $form['date_id'] = array(
      '#title' => t('Date identifier'),
      '#type' => 'textfield',
      '#description' => t('The query identifier to use when fetching date data from in the URL. Note that if you have more than one display in the same view that uses the date pager (like a page and a block), the pager id must be different for each one or both will change when the pager value changes.'),
      '#default_value' => $this->options['date_id'],
      '#required' => TRUE,
    );
    $form['pager_position'] = array(
      '#title' => t('Pager position'),
      '#type' => 'select',
      '#options' => array(
        'bottom' => t('Bottom'),
        'top' => t('Top'),
        'both' => t('Both'),
      ),
      '#description' => t('Where to place the date pager, on the top, bottom, or both top and bottom of the content.'),
      '#default_value' => $this->options['pager_position'],
      '#required' => TRUE,
    );
    $form['link_format'] = array(
      '#title' => t('Link format'),
      '#type' => 'select',
      '#options' => array(
        'pager' => t('Pager'),
        'clean' => t('Clean URL'),
      ),
      '#description' => t("The format for pager link urls. With the Pager format, the links look like 'calendar/?date=2020-05'. The Clean URL format links look like 'calendar/2020-05'. The Clean format links look nicer but the Pager format links are likely to work better if the calendar is used in blocks or panels."),
      '#default_value' => $this->options['link_format'],
      '#required' => TRUE,
    );
    $form['skip_empty_pages'] = array(
      '#title' => t('Skip empty pages'),
      '#type' => 'checkbox',
      '#description' => t('When selected, the pager will not display pages with no result for the given date. This causes a slight performance degradation because two additional queries need to be executed.'),
      '#default_value' => $this->options['skip_empty_pages'],
    );
    $form['date_argument']['#type'] = 'hidden';
    $form['date_argument']['#value'] = $this->options['date_argument'];
    $form['granularity']['#type'] = 'hidden';
    $form['granularity']['#value'] = $this->options['granularity'];
  }

  /**
   * {@inheritdoc}
   */
  public function query() {

    // 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)) {
          $this
            ->set_argument_value($argument, $value);
        }

        // 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;
        }

        // Write date_info to store information to be used in the theming
        // functions.
        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->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'];
        $this->view->date_info->skip_empty_pages = $this->options['skip_empty_pages'] == 1;

        // Execute two additional queries to find the previous and next page
        // with values.
        if ($this->view->date_info->skip_empty_pages) {
          $q = clone $argument->query;
          $field = $argument->table_alias . '.' . $argument->real_field;
          $fieldsql = $date_handler
            ->sql_field($field);
          $fieldsql = $date_handler
            ->sql_format($format, $fieldsql);
          $q
            ->clear_fields();
          $q->orderby = array();
          $q
            ->set_distinct(TRUE, TRUE);

          // Date limits of this argument.
          $datelimits = $argument->date_handler
            ->arg_range($argument->limit[0] . '--' . $argument->limit[1]);

          // Find the first two dates between the minimum date and the upper
          // bound of the current value.
          $q
            ->add_orderby(NULL, $fieldsql, 'DESC', 'date');
          $this
            ->set_argument_placeholders($this->view->date_info->placeholders, $datelimits[0], $argument->max_date, $q, $format);
          $compiledquery = $q
            ->query();
          $compiledquery
            ->range(0, 2);
          $results = $compiledquery
            ->execute()
            ->fetchCol(0);
          $prevdate = array_shift($results);
          $prevdatealt = array_shift($results);

          // Find the first two dates between the lower bound
          // of the current value and the maximum date.
          $q
            ->add_orderby(NULL, $fieldsql, 'ASC', 'date');
          $this
            ->set_argument_placeholders($this->view->date_info->placeholders, $argument->min_date, $datelimits[1], $q, $format);
          $compiledquery = $q
            ->query();
          $compiledquery
            ->range(0, 2);
          $results = $compiledquery
            ->execute()
            ->fetchCol(0);
          $nextdate = array_shift($results);
          $nextdatealt = array_shift($results);

          // Set the default value of the query to $prevfirst or $nextfirst
          // when there is no value and $prevsecond or $nextsecond is set.
          if (empty($value)) {

            // @todo find out which of $prevdate or $nextdate is closest to the
            // default argument date value and choose that one.
            if ($prevdate && $prevdatealt) {
              $this
                ->set_argument_value($argument, $prevdate);
              $value = $prevdate;
              $prevdate = $prevdatealt;

              // If the first next date is the same as the first previous date,
              // move to the following next date.
              if ($value == $nextdate) {
                $nextdate = $nextdatealt;
                $nextdatealt = NULL;
              }
            }
            elseif ($nextdate && $nextdatealt) {
              $this
                ->set_argument_value($argument, $nextdate);
              $value = $nextdate;
              $nextdate = $nextdatealt;

              // If the first previous date is the same as the first next date,
              // move to the following previous date.
              if ($value == $prevdate) {
                $prevdate = $prevdatealt;
                $prevdatealt = NULL;
              }
            }
          }
          else {

            // $prevdate and $nextdate are the same as $value, so move to the
            // next values.
            $prevdate = $prevdatealt;
            $nextdate = $nextdatealt;
          }
          $this->view->date_info->prev_date = $prevdate ? new DateObject($prevdate, NULL, $format) : NULL;
          $this->view->date_info->next_date = $nextdate ? new DateObject($nextdate, NULL, $format) : NULL;
        }
        else {
          $this->view->date_info->prev_date = clone $argument->min_date;
          date_modify($this->view->date_info->prev_date, '-1 ' . $argument->date_handler->granularity);
          $this->view->date_info->next_date = clone $argument->min_date;
          date_modify($this->view->date_info->next_date, '+1 ' . $argument->date_handler->granularity);
        }

        // Write the date_info properties that depend on the current value.
        $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_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;
      }
      $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)) {
      $this
        ->set_argument_placeholders($this->view->date_info->placeholders, $this->view->date_info->min_date, $this->view->date_info->max_date, $this->view->query, $format);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function set_argument_value($argument, $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;
  }

  /**
   * {@inheritdoc}
   */
  public function set_argument_placeholders($placeholders, $mindate, $maxdate, $query, $format) {
    $count = count($placeholders);
    foreach ($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 ? $mindate : $maxdate;
              $next_placeholder = array_shift($placeholders);
              $query->where[$group]['conditions'][$delta]['value'][$placeholder] = $date
                ->format($format);
              $count--;
            }
          }
        }
      }
    }
  }

  /**
   * Determine if we have moved outside the valid date range for this argument.
   */
  public function date_forbid($argument) {

    // See if we're outside the allowed date range for our argument.
    $limit = date_range_years($argument->options['year_range']);
    if (date_format($argument->min_date, 'Y') < $limit[0] || date_format($argument->max_date, 'Y') > $limit[1]) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function pre_render(&$result) {

    // Load theme functions for the pager.
    module_load_include('inc', 'date_views', 'theme/theme');
  }

  /**
   * {@inheritdoc}
   */
  public function render($input) {

    // This adds all of our template suggestions based upon the view name and
    // display id.
    $pager_theme = views_theme_functions('date_views_pager', $this->view, $this->display);
    return theme($pager_theme, array(
      'plugin' => $this,
      'input' => $input,
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
date_views_plugin_pager::date_forbid public function Determine if we have moved outside the valid date range for this argument.
date_views_plugin_pager::has_more_records public function Determine if there are more records available. Overrides views_plugin_pager::has_more_records
date_views_plugin_pager::options_form public function Provide a form to edit options for this plugin. Overrides views_plugin::options_form
date_views_plugin_pager::option_definition public function Information about options for all kinds of purposes will be held here. Overrides views_object::option_definition
date_views_plugin_pager::pre_render public function Perform any needed actions just before rendering. Overrides views_plugin_pager::pre_render
date_views_plugin_pager::query public function Modify the query for paging Overrides views_plugin_pager::query
date_views_plugin_pager::render public function Render the pager. Overrides views_plugin_pager::render
date_views_plugin_pager::set_argument_placeholders public function
date_views_plugin_pager::set_argument_value public function
date_views_plugin_pager::summary_title public function Return a string to display as the clickable title for the pager plugin. Overrides views_plugin_pager::summary_title
date_views_plugin_pager::use_count_query public function Determine if a pager needs a count query. Overrides views_plugin_pager::use_count_query
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::destroy public function Destructor. 2
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin::validate public function Validate that the plugin is correct and can be saved. 3
views_plugin_pager::$current_page public property
views_plugin_pager::$total_items public property
views_plugin_pager::execute_count_query public function Execute the count query, which will be done just prior to the query itself being executed. 1
views_plugin_pager::exposed_form_alter public function 1
views_plugin_pager::exposed_form_submit public function
views_plugin_pager::exposed_form_validate public function 1
views_plugin_pager::get_current_page public function Get the current page.
views_plugin_pager::get_items_per_page public function Get how many items per page this pager will display. 1
views_plugin_pager::get_offset public function Get the page offset, or how many items to skip.
views_plugin_pager::get_pager_id public function Get the pager id, if it exists.
views_plugin_pager::get_total_items public function Get the total number of items.
views_plugin_pager::init public function Initialize the plugin. 1
views_plugin_pager::items_per_page_exposed public function 1
views_plugin_pager::offset_exposed public function 1
views_plugin_pager::options_submit public function Provide the default form form for submitting options. Overrides views_plugin::options_submit
views_plugin_pager::options_validate public function Provide the default form form for validating options. Overrides views_plugin::options_validate 1
views_plugin_pager::post_execute public function Perform any needed actions just after the query executing. 1
views_plugin_pager::pre_execute public function Perform any needed actions just prior to the query executing.
views_plugin_pager::set_current_page public function Set the current page. 1
views_plugin_pager::set_items_per_page public function Set how many items per page this pager will display.
views_plugin_pager::set_offset public function Set the page offset, or how many items to skip.
views_plugin_pager::update_page_info public function If there are pagers that need global values set, this method can be used to set them. It will be called when the count query is run. 1
views_plugin_pager::uses_exposed public function 1
views_plugin_pager::use_pager public function Determine if this pager actually uses a pager. 2