Pagination.php 9.1 KB
Newer Older
1 2 3
<?php
namespace ACP3\Core;

Tino Goratsch's avatar
Tino Goratsch committed
4
use ACP3\Core\Http\RequestInterface;
5 6 7 8 9 10 11 12 13
use ACP3\Core\Validator\Rules\Misc;

/**
 * Class Pagination
 * @package ACP3\Core
 */
class Pagination
{
    /**
14
     * @var \ACP3\Core\User
15
     */
16
    protected $user;
17 18 19 20 21 22 23 24 25 26 27 28 29
    /**
     * @var \ACP3\Core\Breadcrumb
     */
    protected $breadcrumb;
    /**
     * @var \ACP3\Core\Lang
     */
    protected $lang;
    /**
     * @var \ACP3\Core\SEO
     */
    protected $seo;
    /**
Tino Goratsch's avatar
Tino Goratsch committed
30
     * @var \ACP3\Core\Http\RequestInterface
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
     */
    protected $request;
    /**
     * @var \ACP3\Core\Router
     */
    protected $router;
    /**
     * @var \ACP3\Core\View
     */
    protected $view;
    /**
     * @var \ACP3\Core\Validator\Rules\Misc
     */
    protected $miscValidator;
    /**
     * @var int
     */
    private $resultsPerPage = 0;
    /**
     * @var int
     */
    private $totalResults = 0;
    /**
     * @var string
     */
    private $urlFragment = '';
    /**
     * @var int
     */
    private $showFirstLast = 5;
    /**
     * @var int
     */
    private $showPreviousNext = 2;
    /**
     * @var int
     */
    private $pagesToDisplay = 7;
    /**
     * @var int
     */
    private $totalPages = 1;
    /**
     * @var int
     */
    private $currentPage = 1;
    /**
     * @var array
     */
    private $pagination = [];

    /**
83
     * @param \ACP3\Core\User                  $user
Tino Goratsch's avatar
Tino Goratsch committed
84 85 86 87 88 89 90
     * @param \ACP3\Core\Breadcrumb            $breadcrumb
     * @param \ACP3\Core\Lang                  $lang
     * @param \ACP3\Core\SEO                   $seo
     * @param \ACP3\Core\Http\RequestInterface $request
     * @param \ACP3\Core\Router                $router
     * @param \ACP3\Core\View                  $view
     * @param \ACP3\Core\Validator\Rules\Misc  $miscValidator
91 92
     */
    public function __construct(
93
        User $user,
94 95 96
        Breadcrumb $breadcrumb,
        Lang $lang,
        SEO $seo,
97
        RequestInterface $request,
98 99 100 101
        Router $router,
        View $view,
        Misc $miscValidator)
    {
102
        $this->user = $user;
103 104 105 106 107 108 109 110
        $this->breadcrumb = $breadcrumb;
        $this->lang = $lang;
        $this->seo = $seo;
        $this->request = $request;
        $this->router = $router;
        $this->view = $view;
        $this->miscValidator = $miscValidator;

111
        $this->resultsPerPage = $user->getEntriesPerPage();
112 113 114
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
115
     * @param int $results
116 117 118 119 120 121 122
     */
    public function setResultsPerPage($results)
    {
        $this->resultsPerPage = (int)$results;
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
123
     * @param int $results
124 125 126 127 128 129 130
     */
    public function setTotalResults($results)
    {
        $this->totalResults = (int)$results;
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
131
     * @param string $fragment
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
     */
    public function setUrlFragment($fragment)
    {
        $this->urlFragment = $fragment;
    }

    /**
     * @param int $pagesToDisplay
     */
    public function setPagesToDisplay($pagesToDisplay)
    {
        $this->pagesToDisplay = (int)$pagesToDisplay;
    }

    /**
     * @param int $showFirstLast
     */
    public function setShowFirstLast($showFirstLast)
    {
        $this->showFirstLast = (int)$showFirstLast;
    }

    /**
     * @param int $showPreviousNext
     */
    public function setShowPreviousNext($showPreviousNext)
    {
        $this->showPreviousNext = (int)$showPreviousNext;
    }

    /**
     * @param string $tplVariable
     *
     * @return string
     */
    public function display($tplVariable = 'pagination')
    {
        $output = '';
        if ($this->totalResults > $this->resultsPerPage) {
171
            $link = $this->router->route(($this->request->getArea() === 'admin' ? 'acp/' : '') . $this->request->getUriWithoutPages());
Tino Goratsch's avatar
Tino Goratsch committed
172
            $this->currentPage = (int)$this->request->getParameters()->get('page', 1);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            $this->totalPages = (int)ceil($this->totalResults / $this->resultsPerPage);

            $this->setMetaStatements($link);
            $range = $this->calculateRange();

            // Erste Seite
            $this->showFirstPageLink($link, $range);

            // Vorherige Seite
            $this->showPreviousPageLink($link);

            for ($i = (int)$range['start']; $i <= $range['end']; ++$i) {
                $this->pagination[] = $this->buildPageNumber(
                    $this->currentPage === $i,
                    $i,
                    $link . ($i > 1 ? 'page_' . $i . '/' : '') . $this->urlFragment
                );
            }

            // Nächste Seite
            $this->showNextPageLink($link);

            // Letzte Seite
            $this->showLastPageLink($link, $range);

            $this->view->assign('pagination', $this->pagination);

            $output = $this->view->fetchTemplate('system/pagination.tpl');
        }
        $this->view->assign($tplVariable, $output);
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
206
     * @param string $link
207 208 209 210 211 212 213 214 215
     */
    private function setMetaStatements($link)
    {
        if ($this->currentPage > 1) {
            $postfix = sprintf($this->lang->t('system', 'page_x'), $this->currentPage);
            $this->breadcrumb->setTitlePostfix($postfix);
        }

        // Vorherige und nächste Seite für Suchmaschinen und Prefetching propagieren
216
        if ($this->request->getArea() !== 'admin') {
217 218 219 220 221 222 223 224
            if ($this->currentPage - 1 > 0) {
                // Seitenangabe in der Seitenbeschreibung ab Seite 2 angeben
                $this->seo->setDescriptionPostfix(sprintf($this->lang->t('system', 'page_x'), $this->currentPage));
                $this->seo->setPreviousPage($link . 'page_' . ($this->currentPage - 1) . '/');
            }
            if ($this->currentPage + 1 <= $this->totalPages) {
                $this->seo->setNextPage($link . 'page_' . ($this->currentPage + 1) . '/');
            }
225
            if ($this->request->getParameters()->get('page', 0) === 1) {
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                $this->seo->setCanonicalUri($link);
            }
        }
    }

    /**
     * @return array
     */
    private function calculateRange()
    {
        $rangeStart = 1;
        $rangeEnd = $this->totalPages;
        if ($this->totalPages > $this->pagesToDisplay) {
            $center = floor($this->pagesToDisplay / 2);
            // Beginn der anzuzeigenden Seitenzahlen
            if ($this->currentPage - $center > 0) {
                $rangeStart = $this->currentPage - $center;
            }
            // Ende der anzuzeigenden Seitenzahlen
            if ($rangeStart + $this->pagesToDisplay - 1 <= $this->totalPages) {
                $rangeEnd = $rangeStart + $this->pagesToDisplay - 1;
            }

            // Anzuzeigende Seiten immer auf dem Wert von $this->pagesToDisplay halten
            if ($rangeEnd - $rangeStart < $this->pagesToDisplay && $rangeEnd - $this->pagesToDisplay > 0) {
                $rangeStart = $rangeEnd - $this->pagesToDisplay + 1;
            }
        }

        return [
            'start' => $rangeStart,
            'end' => $rangeEnd
        ];
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
262 263
     * @param string $link
     * @param array  $range
264
     */
Tino Goratsch's avatar
Tino Goratsch committed
265
    private function showFirstPageLink($link, array $range)
266 267 268 269 270 271 272 273 274 275 276 277
    {
        if ($this->totalPages > $this->showFirstLast && $range['start'] > 1) {
            $this->pagination[] = $this->buildPageNumber(
                false,
                '&laquo;',
                $link . $this->urlFragment,
                $this->lang->t('system', 'first_page')
            );
        }
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
278 279 280
     * @param bool   $selected
     * @param int    $pageNumber
     * @param string $uri
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
     * @param string $title
     *
     * @return array
     */
    private function buildPageNumber($selected, $pageNumber, $uri, $title = '')
    {
        return [
            'selected' => (bool)$selected,
            'page' => $pageNumber,
            'uri' => $uri,
            'title' => $title
        ];
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
296
     * @param string $link
297 298 299 300 301 302 303 304 305 306 307 308 309 310
     */
    private function showPreviousPageLink($link)
    {
        if ($this->totalPages > $this->showPreviousNext && $this->currentPage !== 1) {
            $this->pagination[] = $this->buildPageNumber(
                false,
                '&lsaquo;',
                $link . ($this->currentPage - 1 > 1 ? 'page_' . ($this->currentPage - 1) . '/' : '') . $this->urlFragment,
                $this->lang->t('system', 'previous_page')
            );
        }
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
311
     * @param string $link
312 313 314 315 316 317 318 319 320 321 322 323 324 325
     */
    private function showNextPageLink($link)
    {
        if ($this->totalPages > $this->showPreviousNext && $this->currentPage !== $this->totalPages) {
            $this->pagination[] = $this->buildPageNumber(
                false,
                '&rsaquo;',
                $link . 'page_' . ($this->currentPage + 1) . '/' . $this->urlFragment,
                $this->lang->t('system', 'next_page')
            );
        }
    }

    /**
Tino Goratsch's avatar
Tino Goratsch committed
326
     * @param string $link
Tino Goratsch's avatar
Tino Goratsch committed
327
     * @param array  $range
328
     */
Tino Goratsch's avatar
Tino Goratsch committed
329
    private function showLastPageLink($link, array $range)
330 331 332 333 334 335 336 337 338 339 340
    {
        if ($this->totalPages > $this->showFirstLast && $this->totalPages !== $range['end']) {
            $this->pagination[] = $this->buildPageNumber(
                false,
                '&raquo;',
                $link . 'page_' . $this->totalPages . '/' . $this->urlFragment,
                $this->lang->t('system', 'last_page')
            );
        }
    }
}