Skip to content
Code-Schnipsel Gruppen Projekte
Pagination.php 7 KiB
Newer Older
  • Learn to ignore specific revisions
  • Henning Leutz's avatar
    Henning Leutz committed
    <?php
    
    /**
     * This file contains \QUI\Bricks\Controls\Pagination
     */
    
    namespace QUI\Bricks\Controls;
    
    use QUI;
    
    /**
     * Pagination
     *
     * @author  www.pcsg.de (Henning Leutz)
     * @licence For copyright and license information, please view the /README.md
     */
    class Pagination extends QUI\Control
    {
        /**
         * GET Params
         *
         * @var array
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected $getParams = array();
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
         * URL Params
         *
         * @var array
         */
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected $urlParams = array();
    
    Henning Leutz's avatar
    Henning Leutz committed
    
        /**
         * constructor
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @param array $attributes
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
        public function __construct($attributes = array())
        {
            $this->setAttributes(array(
                'showLimit' => false,
                'limits'    => '[10,20,50]',
                'limit'     => 10,
                'order'     => false,
    
                'showmax'   => 10,
                'anchor'    => false
    
    Henning Leutz's avatar
    Henning Leutz committed
            ));
    
            parent::__construct($attributes);
    
            $this->addCSSFile(
    
                dirname(__FILE__) . '/Pagination.css'
    
    Henning Leutz's avatar
    Henning Leutz committed
            );
    
    
    
            if ($this->getAttribute('useAjax')) {
                $this->setAttribute(
                    'data-qui',
                    'package/quiqqer/bricks/bin/Controls/Pagination'
                );
            } else {
                $this->setAttribute('data-qui', false);
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->setAttribute('class', 'quiqqer-pagination grid-100 grid-parent');
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    
        /**
         * (non-PHPdoc)
         *
         * @see \QUI\Control::create()
         */
        public function getBody()
        {
    
            $Engine  = QUI::getTemplateManager()->getEngine();
            $Site    = $this->getAttribute('Site');
    
    Henning Leutz's avatar
    Henning Leutz committed
            $Project = $Site->getProject();
    
            $count = $this->getAttribute('sheets');
    
    
            if ($count === false) {
                if ($this->getAttribute('limit') &&
    
                    $this->getAttribute('count')
                ) {
    
                    $count = ceil(
                        (int)$this->getAttribute('count') /
                        (int)$this->getAttribute('limit')
                    );
    
                    $this->setAttribute('sheets', $count);
                }
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            $showmax = $this->getAttribute('showmax');
    
            $limits  = $this->getAttribute('limits');
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
            if ($this->getAttribute('useAjax')) {
                $this->setAttribute(
                    'data-qui',
                    'package/quiqqer/bricks/bin/Controls/Pagination'
                );
            } else {
                $this->setAttribute('data-qui', false);
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
            if ($limits && is_string($limits)) {
                $limits = json_decode($limits, true);
    
                if (!is_array($limits)) {
                    $limits = false;
                }
            }
    
            $active = $this->getAttribute('sheet');
            $anchor = '';
    
            if ($this->getAttribute('anchor')) {
                $anchor = $this->getAttribute('anchor');
            }
    
            if ($showmax >= $count) {
                $showmax = false;
            }
    
            if (!$showmax) {
                $showmax = $count * 2;
            }
    
            $gap = floor($showmax / 2);
    
            $start = $active - $gap;
    
            $end   = $active + $gap;
    
    
            if ($showmax % 2 === 0) {
    
                $end--; // -1, weil aktuelle seite nicht mit berechnet werden soll
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            if ($start <= 0) {
                $start = 1;
    
    Patrick Müller's avatar
    Patrick Müller committed
                $end   = $showmax;
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
            if ($end >= $count) {
    
    Patrick Müller's avatar
    Patrick Müller committed
                $end   = $count;
    
                $start = $end - $showmax + 1;
    
    Patrick Müller's avatar
    Patrick Müller committed
    
                if ($start <= 0) {
                    $start = 1;
                }
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
            // get params
            $limit = $this->getAttribute('limit');
            $order = $this->getAttribute('order');
            $sheet = $this->getAttribute('sheet');
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->getParams['sheet'] = $sheet;
            $this->getParams['limit'] = $limit;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                $this->getParams['order'] = $order;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
                && $this->getAttribute('limit') === false
            ) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                return '';
            }
    
            $Engine->assign(array(
                'this'       => $this,
                'count'      => $count,
                'start'      => $start,
                'end'        => $end,
                'active'     => $active,
    
    Henning Leutz's avatar
    Henning Leutz committed
                'pathParams' => $this->urlParams,
                'getParams'  => $this->getParams,
    
    Henning Leutz's avatar
    Henning Leutz committed
                'anchor'     => $anchor,
                'limit'      => $limit,
                'limits'     => $limits,
                'Site'       => $Site,
                'Project'    => $Project
            ));
    
    
            return $Engine->fetch(dirname(__FILE__) . '/Pagination.html');
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    
        /**
         * Load the GET request variables into the sheet
         */
        public function loadFromRequest()
        {
            $limit = $this->getAttribute('limit');
            $order = $this->getAttribute('order');
            $sheet = $this->getAttribute('sheet');
    
            if (isset($_GET['limit']) && is_numeric($_GET['limit'])) {
                $limit = (int)$_GET['limit'];
            }
    
            if (isset($_GET['order'])) {
                $order = $_GET['order'];
            }
    
            if (isset($_GET['sheet'])) {
                $sheet = $_GET['sheet'];
            }
    
            $this->setAttribute('limit', $limit);
            $this->setAttribute('order', $order);
            $this->setAttribute('sheet', $sheet);
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->urlParams = QUI::getRewrite()->getUrlParamsList();
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    
        /**
         * Return SQL params
         *
         * @example $this->getSQLParams() : array(
         *     'limit' => '0,20',
         *     'order' => 'field'
         * )
         *
         * @return array
         */
        public function getSQLParams()
        {
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            if ($this->getAttribute('limit')) {
    
                $result['limit'] = $this->getStart()
                                   . ',' . $this->getAttribute('limit');
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
    
            if ($this->getAttribute('order')) {
                $result['order'] = $this->getAttribute('order');
            }
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    
        /**
         * Return the start
         *
         * @return integer
         */
        public function getStart()
        {
            $limit = $this->getAttribute('limit');
            $sheet = $this->getAttribute('sheet');
    
            return ($sheet - 1) * $limit;
        }
    
        /**
         * Set GET parameter to the links
         *
         * @param $name
         * @param $value
         */
        public function setGetParams($name, $value)
        {
    
            $name  = QUI\Utils\Security\Orthos::clear($name);
    
            $value = QUI\Utils\Security\Orthos::clearFormRequest($value);
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            if (empty($value)) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                if (isset($this->getParams[$name])) {
                    unset($this->getParams[$name]);
    
    Henning Leutz's avatar
    Henning Leutz committed
                }
    
                return;
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->getParams[$name] = urlencode($value);
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    
        /**
         * Set URL parameter to the links
         *
         * @param $name
         * @param $value
         */
        public function setUrlParams($name, $value)
        {
    
            $name  = QUI\Utils\Security\Orthos::clear($name);
    
    Henning Leutz's avatar
    Henning Leutz committed
            $value = QUI\Utils\Security\Orthos::clear($value);
    
            if (empty($value)) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                if (isset($this->urlParams[$name])) {
                    unset($this->urlParams[$name]);
    
    Henning Leutz's avatar
    Henning Leutz committed
                }
    
                return;
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            $this->urlParams[$name] = $value;
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    }