Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Admin/Paginator.php - 199 lines - 4838 bytes - Summary - Text - Print

Description: List pagination.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * List pagination.
  26   *
  27   * @since   4.7.0
  28   * @package Admin\Paginator
  29   */
  30  
  31  namespace Textpattern\Admin;
  32  
  33  class Paginator
  34  {
  35      /**
  36       * Textpattern event (panel) to which this paginator applies.
  37       *
  38       * @var string
  39       */
  40  
  41      protected $event = null;
  42  
  43      /**
  44       * Identifier for the pref that holds the current value.
  45       *
  46       * @var string
  47       */
  48  
  49      protected $prefKey = null;
  50  
  51      /**
  52       * Allowable set of items per page.
  53       *
  54       * @var array
  55       */
  56  
  57      protected $sizes = array(12, 24, 48, 96);
  58  
  59      /**
  60       * Default pagination value.
  61       *
  62       * Usually the first value in the $sizes array.
  63       *
  64       * @var int
  65       */
  66  
  67      protected $defaultVal = null;
  68  
  69      /**
  70       * Constructor.
  71       *
  72       * The available sizes can be changed via a '{$this->event}_ui > pageby_values'
  73       * callback event.
  74       *
  75       * @param string $evt    Textpattern event (panel)
  76       * @param string $prefix Prefix for the pref that holds the current paging value
  77       */
  78  
  79      public function __construct($evt = null, $prefix = null)
  80      {
  81          global $event;
  82  
  83          if ($evt === null) {
  84              $evt = $event;
  85          }
  86  
  87          if ($prefix === null) {
  88              $prefix = $evt;
  89          }
  90  
  91          $this->event = $evt;
  92          $this->prefKey = $prefix.'_list_pageby';
  93          $this->defaultVal = $this->sizes[0];
  94  
  95          callback_event_ref($evt.'_ui', 'pageby_values', 0, $this->sizes);
  96      }
  97  
  98      /**
  99       * Renders a widget to select various amounts to page lists by.
 100       *
 101       * @param  int $val Current pagination setting
 102       * @return string      HTML
 103       */
 104  
 105      public function render($val = null)
 106      {
 107          $step = $this->event.'_change_pageby';
 108  
 109          if (empty($this->sizes)) {
 110              return;
 111          }
 112  
 113          $val = $this->closest($val);
 114  
 115          $out = array();
 116  
 117          foreach ($this->sizes as $qty) {
 118              if ($qty == $val) {
 119                  $class = 'navlink-active';
 120                  $aria_pressed = 'true';
 121              } else {
 122                  $class = 'navlink';
 123                  $aria_pressed = 'false';
 124              }
 125  
 126              $out[] = href($qty, array(
 127                  'event'      => $this->event,
 128                  'step'       => $step,
 129                  'qty'        => $qty,
 130                  '_txp_token' => form_token(),
 131              ), array(
 132                  'class'        => $class,
 133                  'title'        => gTxt('view_per_page', array('{page}' => $qty)),
 134                  'aria-pressed' => $aria_pressed,
 135                  'role'         => 'button',
 136              ));
 137          }
 138  
 139          return n.tag(join('', $out), 'div', array('class' => 'nav-tertiary pageby'));
 140      }
 141  
 142  
 143      /**
 144       * Fetch the current paging limit, or lowest default.
 145       *
 146       * @return int
 147       */
 148      public function getLimit()
 149      {
 150          return $this->closest();
 151      }
 152  
 153      /**
 154       * Updates a list's per page number.
 155       *
 156       * Gets the per page number from a "qty" HTTP POST/GET parameter and
 157       * creates a user-specific preference value suffixed "_list_pageby".
 158       *
 159       * The assignment to $GLOBALS is for legacy plugins and can be
 160       * removed in future.
 161       */
 162  
 163      public function change()
 164      {
 165          global $prefs;
 166  
 167          $qty = intval(gps('qty'));
 168          $GLOBALS[$this->prefKey] = $prefs[$this->prefKey] = $qty;
 169  
 170          set_pref($this->prefKey, $qty, $this->event, PREF_HIDDEN, 'text_input', 0, PREF_PRIVATE);
 171      }
 172  
 173      /**
 174       * Find closest value to $val from $sizes.
 175       *
 176       * @param  int $val Value to compare
 177       * @return int      Closest supported value
 178       */
 179      protected function closest($val = null)
 180      {
 181          if (empty($val)) {
 182              $val = get_pref($this->prefKey, $this->defaultVal);
 183          }
 184  
 185          if (!in_array($val, $this->sizes)) {
 186              $closest = null;
 187  
 188              foreach ($this->sizes as $item) {
 189                  if ($closest === null || abs($val - $closest) > abs($item - $val)) {
 190                      $closest = $item;
 191                  }
 192              }
 193  
 194              $val = $closest;
 195          }
 196  
 197          return $val;
 198      }
 199  }

title

Description

title

Description

title

Description

title

title

Body