View source  
  <?php
namespace Drupal\pagerer\Plugin\pagerer;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\TypedConfigManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Pager\PagerManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Template\Attribute;
use Drupal\pagerer\Entity\PagererPreset;
use Drupal\pagerer\Pagerer;
use Drupal\pagerer\PagererParameters;
use Drupal\pagerer\Plugin\PagererStyleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PagererStyleBase extends PluginBase implements PagererStyleInterface, PluginFormInterface, ContainerFactoryPluginInterface {
  
  protected $pager;
  
  protected $parameters;
  
  protected $configFactory;
  
  protected $pagerManager;
  
  protected $typedConfigManager;
  
  protected $pagererPreset;
  
  protected $pagererPresetPane;
  
  public function __construct(array $configuration, $plugin_id, $plugin_definition, TypedConfigManager $typed_config_manager, ConfigFactoryInterface $config_factory, PagerManagerInterface $pager_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->typedConfigManager = $typed_config_manager;
    $this->configFactory = $config_factory;
    $this->pagerManager = $pager_manager;
  }
  
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.typed'), $container
      ->get('config.factory'), $container
      ->get('pager.manager'));
  }
  
  public function setPager(Pagerer $pager) {
    $this->pager = $pager;
    return $this;
  }
  
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $config = [];
    $d = $this->configFactory
      ->get('pagerer.style.' . $this
      ->getPluginId())
      ->get('default_config');
    
    $display_options = [
      'pages' => $this
        ->t('Pages'),
      'items' => $this
        ->t('Items'),
      'item_ranges' => $this
        ->t('Item ranges'),
    ];
    $config['display'] = [
      '#type' => 'select',
      '#title' => $this
        ->t("Display"),
      '#options' => $display_options,
      '#default_value' => $this->configuration['display'],
      '#description' => $this
        ->t("Select whether to display pages, items, or item ranges."),
      '#required' => TRUE,
    ];
    
    $config['display_container'] = [
      '#type' => 'details',
      '#title' => $this
        ->t("Main options"),
    ];
    if (isset($d['quantity'])) {
      $config['display_container']['quantity'] = [
        '#type' => 'number',
        '#title' => $this
          ->t("Quantity"),
        '#default_value' => $this->configuration['quantity'],
        '#description' => $this
          ->t("The number of pages in the pager list."),
        '#required' => TRUE,
        '#size' => 3,
        '#maxlength' => 3,
        '#min' => 1,
      ];
    }
    if (isset($d['display_mode'])) {
      $options = [
        'normal' => $this
          ->t('Text elements.'),
      ];
      $config['display_container']['display_mode'] = [
        '#type' => 'radios',
        '#title' => $this
          ->t("Pager mode"),
        '#options' => $options,
        '#default_value' => $this->configuration['display_mode'],
        '#description' => $this
          ->t("Select how to render the pager."),
        '#required' => TRUE,
      ];
    }
    if (isset($d['prefix_display'])) {
      $config['display_container']['prefix_display'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t("Prefix label"),
        '#default_value' => $this->configuration['prefix_display'],
        '#description' => $this
          ->t("Display a text label (e.g. 'Page') before the pager. Configure the label in the 'Text strings' section below."),
      ];
    }
    if (isset($d['suffix_display'])) {
      $config['display_container']['suffix_display'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t("Suffix label"),
        '#default_value' => $this->configuration['suffix_display'],
        '#description' => $this
          ->t("Display a text label (e.g. 'of @total') after the pager. Configure the label in the 'Text strings' section below."),
      ];
    }
    if (isset($d['display_restriction'])) {
      $options = [
        2 => $this
          ->t('Display pager if there are at least two pages of results (default).'),
        1 => $this
          ->t('Display pager if there is at least one page of results.'),
        0 => $this
          ->t('Display pager even if the result set is empty.'),
      ];
      $config['display_container']['display_restriction'] = [
        '#type' => 'radios',
        '#title' => $this
          ->t("Restriction"),
        '#options' => $options,
        '#default_value' => $this->configuration['display_restriction'],
        '#required' => TRUE,
      ];
    }
    if (isset($d['progr_links'])) {
      $options = [
        'relative',
        'absolute',
      ];
      $options = array_combine($options, $options);
      $config['display_container']['progr_links'] = [
        '#type' => 'select',
        '#title' => $this
          ->t("Outer pages"),
        '#options' => $options,
        '#default_value' => $this->configuration['progr_links'],
        '#description' => $this
          ->t("Select how to render links to pages far from the current, as 'absolute' page numbers (or items/item ranges), or as 'relative' offsets from current (e.g. +10 +100 +1000)."),
        '#required' => TRUE,
      ];
    }
    
    $config['links_container'] = [
      '#type' => 'details',
      '#title' => $this
        ->t("Links"),
      '#description' => $this
        ->t("Configure link elements like '« First', '‹ Previous', 'Next ›' and 'Last »'."),
    ];
    if (isset($d['first_link'])) {
      $options = [
        'never',
        'not_on_first',
        'always',
      ];
      $options = array_combine($options, $options);
      $config['links_container']['first_link'] = [
        '#type' => 'select',
        '#title' => $this
          ->t("First"),
        '#options' => $options,
        '#default_value' => $this->configuration['first_link'],
        '#description' => $this
          ->t("Select when to render a link to the first page (e.g. '« First'). Options are 'never' (not displayed), 'not_on_first' (not displayed if current page is the first), 'always' (always displayed)."),
        '#required' => TRUE,
      ];
    }
    if (isset($d['previous_link'])) {
      $options = [
        'never',
        'not_on_first',
        'always',
      ];
      $options = array_combine($options, $options);
      $config['links_container']['previous_link'] = [
        '#type' => 'select',
        '#title' => $this
          ->t("Previous"),
        '#options' => $options,
        '#default_value' => $this->configuration['previous_link'],
        '#description' => $this
          ->t("Select when to render a link to the previous page (e.g. '‹ Previous'). Options are 'never' (not displayed), 'not_on_first' (not displayed if current page is the first), 'always' (always displayed)."),
        '#required' => TRUE,
      ];
    }
    if (isset($d['next_link'])) {
      $options = [
        'never',
        'not_on_last',
        'always',
      ];
      $options = array_combine($options, $options);
      $config['links_container']['next_link'] = [
        '#type' => 'select',
        '#title' => $this
          ->t("Next"),
        '#options' => $options,
        '#default_value' => $this->configuration['next_link'],
        '#description' => $this
          ->t("Select when to render a link to the next page (e.g. 'Next ›'). Options are 'never' (not displayed), 'not_on_last' (not displayed if current page is the last), 'always' (always displayed)."),
        '#required' => TRUE,
      ];
    }
    if (isset($d['last_link'])) {
      $options = [
        'never',
        'not_on_last',
        'always',
      ];
      $options = array_combine($options, $options);
      $config['links_container']['last_link'] = [
        '#type' => 'select',
        '#title' => $this
          ->t("Last"),
        '#options' => $options,
        '#default_value' => $this->configuration['last_link'],
        '#description' => $this
          ->t("Select when to render a link to the last page (e.g. 'Last »'). Options are 'never' (not displayed), 'not_on_last' (not displayed if current page is the last), 'always' (always displayed)."),
        '#required' => TRUE,
      ];
    }
    
    $config['separators_container'] = [
      '#type' => 'details',
      '#title' => $this
        ->t("Separators"),
      '#description' => $this
        ->t("Configure separators."),
    ];
    if (isset($d['breaker_display'])) {
      $config['separators_container']['breaker_display'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t("Page breaker"),
        '#default_value' => $this->configuration['breaker_display'],
        '#description' => $this
          ->t("Display a breaker when the page sequence breaks."),
      ];
    }
    if (isset($d['tags']['page_breaker'])) {
      $config['separators_container']['page_breaker'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t("Breaker"),
        '#default_value' => $this->configuration['tags']['page_breaker'],
        '#description' => $this
          ->t("Text to use as page breaker."),
        '#states' => [
          'visible' => [
            ':input[name="breaker_display"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    if (isset($d['separator_display'])) {
      $config['separators_container']['separator_display'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t("Page separator"),
        '#default_value' => $this->configuration['separator_display'],
        '#description' => $this
          ->t("Display a separator between the page links."),
      ];
    }
    if (isset($d['tags']['page_separator'])) {
      $config['separators_container']['page_separator'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t("Separator"),
        '#default_value' => $this->configuration['tags']['page_separator'],
        '#description' => $this
          ->t("Text to use as page separator."),
        '#states' => [
          'visible' => [
            ':input[name="separator_display"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    
    $display_tags = $this->typedConfigManager
      ->getDefinition('pagerer.tags_display_config.' . $this
      ->getPluginId());
    $config['tags_container'] = [
      '#tree' => TRUE,
    ];
    foreach ($d['tags'] as $tags_key => $tags_set) {
      if (in_array($tags_key, [
        'pages',
        'items',
        'item_ranges',
      ])) {
        $config['tags_container'][$tags_key] = [
          '#type' => 'details',
          '#title' => $display_options[$tags_key] . ' - ' . $this
            ->t("Text strings"),
          '#description' => $this
            ->t("Configure text strings."),
          '#states' => [
            'visible' => [
              ':input[name="display"]' => [
                'value' => $tags_key,
              ],
            ],
          ],
        ];
        foreach ($tags_set as $tag => $map) {
          $config['tags_container'][$tags_key][$tag] = [
            '#type' => 'textfield',
            '#title' => $display_tags['mapping'][$tag]['label'],
            '#default_value' => $this->configuration['tags'][$tags_key][$tag],
          ];
        }
      }
    }
    return $config;
  }
  
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }
  
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $default_configuration = $this->configFactory
      ->get('pagerer.style.' . $this
      ->getPluginId())
      ->get('default_config');
    $display_mode = $form_state
      ->getValue('display');
    foreach ($default_configuration as $conf_key => $conf_item) {
      
      if ($form_state
        ->hasValue($conf_key)) {
        $this->pagererPreset
          ->setPaneData($this->pagererPresetPane, "config.{$conf_key}", $form_state
          ->getValue($conf_key));
      }
      elseif ($conf_key == 'tags') {
        foreach ($conf_item as $tag_key => $tag_value) {
          
          if ($form_state
            ->hasValue($tag_key)) {
            
            $this->pagererPreset
              ->setPaneData($this->pagererPresetPane, "config.tags.{$tag_key}", $form_state
              ->getValue($tag_key));
          }
          elseif (in_array($tag_key, [
            'pages',
            'items',
            'item_ranges',
          ])) {
            
            foreach ($tag_value as $tag_display_key => $tag_display_value) {
              $tag_display_value = $form_state
                ->getValue([
                'tags_container',
                $tag_key,
                $tag_display_key,
              ]);
              if ($tag_key == $display_mode || $tag_display_value != $default_configuration['tags'][$tag_key][$tag_display_key]) {
                $this->pagererPreset
                  ->setPaneData($this->pagererPresetPane, "config.tags.{$tag_key}.{$tag_display_key}", $tag_display_value);
              }
              else {
                $this->pagererPreset
                  ->unsetPaneData($this->pagererPresetPane, "config.tags.{$tag_key}.{$tag_display_key}");
              }
            }
          }
        }
      }
    }
  }
  
  public function setConfigurationContext(PagererPreset $pagerer_preset, $pagerer_preset_pane) {
    $this->pagererPreset = $pagerer_preset;
    $this->pagererPresetPane = $pagerer_preset_pane;
  }
  
  public function preprocess(array &$variables) {
    
    $this->parameters = $variables['pager']['#parameters'];
    
    if ($this->pager
      ->getTotalPages() < $this
      ->getOption('display_restriction')) {
      return;
    }
    if ($this->pager
      ->getTotalPages() == 0) {
      
      $items['pages'] = $this
        ->buildEmptyPager();
    }
    else {
      
      $items = [];
      
      if ($this
        ->getOption('first_link') == 'always' or $this
        ->getOption('first_link') == 'not_on_first' and $this->pager
        ->getCurrentPage() != 0) {
        $items['first'] = $this
          ->getNavigationItem('first');
      }
      if ($this
        ->getOption('previous_link') == 'always' or $this
        ->getOption('previous_link') == 'not_on_first' and $this->pager
        ->getCurrentPage() != 0) {
        $items['previous'] = $this
          ->getNavigationItem('previous');
      }
      
      if ($this
        ->getOption('prefix_display')) {
        $items['prefix'] = [
          'text' => $this
            ->getDisplayTag('prefix_label'),
          'attributes' => new Attribute(),
        ];
      }
      
      $items['pages'] = $this
        ->buildPagerItems();
      
      if ($this
        ->getOption('suffix_display')) {
        $items['suffix'] = [
          'text' => $this
            ->getDisplayTag('suffix_label'),
          'attributes' => new Attribute(),
        ];
      }
      
      if ($this
        ->getOption('next_link') == 'always' or $this
        ->getOption('next_link') == 'not_on_last' and $this->pager
        ->getCurrentPage() != $this->pager
        ->getLastPage()) {
        $items['next'] = $this
          ->getNavigationItem('next');
      }
      if ($this
        ->getOption('last_link') == 'always' or $this
        ->getOption('last_link') == 'not_on_last' and $this->pager
        ->getCurrentPage() != $this->pager
        ->getLastPage()) {
        $items['last'] = $this
          ->getNavigationItem('last');
      }
    }
    
    $variables['items'] = $items;
  }
  
  protected function buildEmptyPager() {
    return [
      [
        'text' => $this
          ->getDisplayTag('pageset_empty'),
        'attributes' => new Attribute(),
      ],
    ];
  }
  
  protected function getOption($key) {
    $keys = explode('.', $key);
    $n = $this->configuration;
    foreach ($keys as $k) {
      if (!empty($n[$k])) {
        if (is_array($n[$k])) {
          $n = $n[$k];
        }
        else {
          return $n[$k];
        }
      }
      else {
        return NULL;
      }
    }
  }
  
  protected function getTag($key) {
    return $this
      ->getOption('tags.' . $key);
  }
  
  protected function getDisplayTag($key, $offset = 0) {
    
    $tag = $this
      ->getTag($this
      ->getOption('display') . '.' . $key);
    
    $l_item = ($this->pager
      ->getCurrentPage() + $offset) * $this->pager
      ->getLimit() + 1;
    $h_item = min(($this->pager
      ->getCurrentPage() + $offset + 1) * $this->pager
      ->getLimit(), $this->pager
      ->getTotalItems());
    $item_offset = abs($offset * $this->pager
      ->getLimit());
    
    $number = $this->pager
      ->getCurrentPage() + $offset + 1;
    $t_offset = abs($offset);
    
    return new FormattableMarkup($tag, [
      '@number' => $number,
      '@offset' => $t_offset,
      '@total' => $this->pager
        ->getTotalPages(),
      '@item_low' => $l_item,
      '@item_high' => $h_item,
      '@item' => $l_item,
      '@item_offset' => $item_offset,
      '@total_items' => $this->pager
        ->getTotalItems(),
    ]);
  }
  
  protected function getPageItem($offset, $offset_mode = 'absolute', $progr_page = FALSE, $title_tag = 'page', $set_query = TRUE) {
    
    if ($offset == 0) {
      $page_tag_key = 'page_current';
    }
    else {
      if ($progr_page && $offset_mode == 'relative') {
        $page_tag_key = $offset < 0 ? 'page_previous_relative' : 'page_next_relative';
      }
      else {
        $page_tag_key = $offset < 0 ? 'page_previous' : 'page_next';
      }
    }
    
    if ($offset == 0 && $this
      ->getOption('display_mode') == 'none') {
      return [];
    }
    
    $ret = [
      'text' => $this
        ->getDisplayTag($page_tag_key, $offset),
      'href' => $this->pagerManager
        ->getHref($this->pager, $this->parameters, $this->pager
        ->getCurrentPage() + $offset, [], $set_query),
      'title' => $this
        ->getDisplayTag($title_tag . '_title', $offset),
      'reader_text' => $this
        ->getDisplayTag($title_tag . '_reader', $offset),
      'attributes' => new Attribute(),
    ];
    if ($offset == 0) {
      $ret['is_current'] = TRUE;
    }
    return $ret;
  }
  
  protected function getNavigationItem($scope, $href = TRUE) {
    
    switch ($scope) {
      case 'first':
        $offset = -$this->pager
          ->getCurrentPage();
        break;
      case 'previous':
        $offset = $this->pager
          ->getCurrentPage() == 0 ? 0 : -1;
        break;
      case 'next':
        $offset = $this->pager
          ->getCurrentPage() == $this->pager
          ->getLastPage() ? 0 : 1;
        break;
      case 'last':
        $offset = $this->pager
          ->getLastPage() - $this->pager
          ->getCurrentPage();
        break;
    }
    
    $ret = [
      'text' => $this
        ->getDisplayTag($scope, $offset),
      'title' => $this
        ->getDisplayTag($scope . '_title', $offset),
      'reader_text' => $this
        ->getDisplayTag($scope . '_reader', $offset),
      'attributes' => new Attribute(),
    ];
    
    if ($href) {
      $ret['href'] = $this->pagerManager
        ->getHref($this->pager, $this->parameters, $this->pager
        ->getCurrentPage() + $offset);
    }
    return $ret;
  }
  
  protected function prepareJsState(array &$state_settings) {
    
    $settings = $this->configFactory
      ->get('pagerer.settings');
    $querystring_override = $settings
      ->get('url_querystring.core_override');
    $index_base = $querystring_override ? $settings
      ->get('url_querystring.index_base') : 0;
    $querystring_key = $querystring_override ? $settings
      ->get('url_querystring.querystring_key') : 'page';
    $querystring_value_separator = $querystring_override ? PagererParameters::VALUE_SEPARATOR : ',';
    
    $query = $this->pagerManager
      ->getPagererUpdatedParameters($this->pager, $this->parameters, 'pagererpage');
    
    $querystring = UrlHelper::buildQuery($query);
    
    if ($this
      ->getOption('display') == 'pages') {
      $current = $this->pager
        ->getCurrentPage() + 1;
      $interval = 1;
    }
    else {
      $current = $this->pager
        ->getLimit() * $this->pager
        ->getCurrentPage() + 1;
      $interval = $this->pager
        ->getLimit();
    }
    
    $default_settings = [
      'url' => $this->pagerManager
        ->getHref($this->pager, [], NULL, [], FALSE)
        ->toString(),
      'queryString' => $querystring,
      'qsIndexBase' => $index_base,
      'qsKey' => $querystring_key,
      'qsValueSep' => $querystring_value_separator,
      'element' => $this->pager
        ->getElement(),
      'total' => $this->pager
        ->getTotalPages(),
      'totalItems' => $this->pager
        ->getTotalItems(),
      'current' => $this->pager
        ->getCurrentPage(),
      'interval' => $interval,
      'display' => $this
        ->getOption('display'),
      'value' => $current,
      'pageSeparator' => $this
        ->getOption('separator_display') ? $this
        ->getTag('page_separator') : 'none',
      'pageTag' => [
        'page_current' => $this
          ->getTag($this
          ->getOption('display') . '.page_current'),
        'page_previous' => $this
          ->getTag($this
          ->getOption('display') . '.page_previous'),
        'page_next' => $this
          ->getTag($this
          ->getOption('display') . '.page_next'),
      ],
    ];
    $state_settings = NestedArray::mergeDeep($default_settings, $state_settings);
    $random_generator = new Random();
    $pagerer_widget_id = 'pagerer-widget-' . $random_generator
      ->name(8, TRUE);
    return $pagerer_widget_id;
  }
}