1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  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:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 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: 
<?php
namespace Omeka\View\Helper;

use Omeka\Stdlib\Paginator;
use Zend\View\Helper\AbstractHelper;

/**
 * View helper for rendering pagination.
 */
class Pagination extends AbstractHelper
{
    /**
     * The default partial view script.
     */
    const PARTIAL_NAME = 'common/pagination';

    /**
     * @var \Omeka\Stdlib\Paginator
     */
    protected $paginator;

    /**
     * Name of view script, or a view model
     *
     * @var string|\Zend\View\Model\ModelInterface
     */
    protected $partialName;

    /**
     * @var string A URL fragment
     */
    protected $fragment;

    /**
     * Construct the helper.
     *
     * @param Paginator $paginator
     */
    public function __construct(Paginator $paginator)
    {
        $this->paginator = $paginator;
    }

    /**
     * Configure the pagination.
     *
     * @param string|null $partialName Name of view script
     * @param int|null $totalCount The total record count
     * @param int|null $currentPage The current page number
     * @param int|null $perPage The number of records per page
     * @return self
     */
    public function __invoke($partialName = null, $totalCount = null, $currentPage = null,
        $perPage = null
    ) {
        if (null !== $totalCount) {
            $this->getPaginator()->setTotalCount($totalCount);
        }
        if (null !== $currentPage) {
            $this->getPaginator()->setCurrentPage($currentPage);
        }
        if (null !== $perPage) {
            $this->getPaginator()->setPerPage($perPage);
        }
        $this->partialName = $partialName ?: self::PARTIAL_NAME;
        return $this;
    }

    /**
     * Render the pagination markup.
     *
     * @return string
     */
    public function __toString()
    {
        $paginator = $this->getPaginator();

        // Page count
        $pageCount = $paginator->getPageCount();

        // Current page number cannot be more than page count
        if ($paginator->getCurrentPage() > $pageCount) {
            $paginator->setCurrentPage($pageCount);
        }

        return $this->getView()->partial(
            $this->partialName,
            [
                'totalCount' => $paginator->getTotalCount(),
                'perPage' => $paginator->getPerPage(),
                'currentPage' => $paginator->getCurrentPage(),
                'previousPage' => $paginator->getPreviousPage(),
                'nextPage' => $paginator->getNextPage(),
                'pageCount' => $pageCount,
                'query' => $this->getView()->params()->fromQuery(),
                'firstPageUrl' => $this->getUrl(1),
                'previousPageUrl' => $this->getUrl($paginator->getPreviousPage()),
                'nextPageUrl' => $this->getUrl($paginator->getNextPage()),
                'lastPageUrl' => $this->getUrl($pageCount),
                'pagelessUrl' => $this->getPagelessUrl(),
                'offset' => $paginator->getOffset(),
            ]
        );
    }

    public function getPaginator()
    {
        return $this->paginator;
    }

    /**
     * Set a fragment to pagination URLs.
     *
     * @param string $fragment
     */
    public function setFragment($fragment)
    {
        $this->fragment = $fragment;
    }

    /**
     * Get a pagination URL.
     *
     * @param int $page The page number
     * @return string
     */
    protected function getUrl($page)
    {
        $query = $this->getView()->params()->fromQuery();
        $query['page'] = (int) $page;
        $options = ['query' => $query];
        if (is_string($this->fragment)) {
            $options['fragment'] = $this->fragment;
        }
        return $this->getView()->url(null, [], $options, true);
    }

    /**
     * Get a URL with the page parameter removed.
     *
     * Suitable for use as a form action URL.
     *
     * @return string
     */
    protected function getPagelessUrl()
    {
        $query = $this->getView()->params()->fromQuery();
        unset($query['page']);
        $options = ['query' => $query];
        if (is_string($this->fragment)) {
            $options['fragment'] = $this->fragment;
        }
        return $this->getView()->url(null, [], $options, true);
    }
}