123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Theme\Block\Html;
- /**
- * Html pager block
- *
- * @SuppressWarnings(PHPMD.ExcessivePublicCount)
- * @api
- * @since 100.0.2
- */
- class Pager extends \Magento\Framework\View\Element\Template
- {
- /**
- * Current template name
- *
- * @var string
- */
- protected $_template = 'Magento_Theme::html/pager.phtml';
- /**
- * @var \Magento\Framework\Data\Collection
- */
- protected $_collection;
- /**
- * @var string
- */
- protected $_pageVarName = 'p';
- /**
- * @var string
- */
- protected $_limitVarName = 'limit';
- /**
- * The list of available pager limits
- *
- * @var array
- */
- protected $_availableLimit = [10 => 10, 20 => 20, 50 => 50];
- /**
- * @var int
- */
- protected $_displayPages = 5;
- /**
- * @var bool
- */
- protected $_showPerPage = true;
- /**
- * @var int
- */
- protected $_limit;
- /**
- * @var bool
- */
- protected $_outputRequired = true;
- /**
- * Pages quantity per frame
- *
- * @var int
- */
- protected $_frameLength = 5;
- /**
- * Next/previous page position relatively to the current frame
- *
- * @var int
- */
- protected $_jump = 5;
- /**
- * Frame initialization flag
- *
- * @var bool
- */
- protected $_frameInitialized = false;
- /**
- * Start page position in frame
- *
- * @var int
- */
- protected $_frameStart;
- /**
- * Finish page position in frame
- *
- * @var int
- */
- protected $_frameEnd;
- /**
- * Url Fragment for pagination
- *
- * @var string|null
- */
- protected $_fragment = null;
- /**
- * Set pager data
- *
- * @return void
- */
- protected function _construct()
- {
- parent::_construct();
- $this->setData('show_amounts', true);
- $this->setData('use_container', true);
- }
- /**
- * Return current page
- *
- * @return int
- */
- public function getCurrentPage()
- {
- if (is_object($this->_collection)) {
- return $this->_collection->getCurPage();
- }
- return (int)$this->getRequest()->getParam($this->getPageVarName(), 1);
- }
- /**
- * Return current page limit
- *
- * @return int
- */
- public function getLimit()
- {
- if ($this->_limit !== null) {
- return $this->_limit;
- }
- $limits = $this->getAvailableLimit();
- if ($limit = $this->getRequest()->getParam($this->getLimitVarName())) {
- if (isset($limits[$limit])) {
- return $limit;
- }
- }
- $limits = array_keys($limits);
- return $limits[0];
- }
- /**
- * Setter for limit items per page
- *
- * @param int $limit
- * @return $this
- */
- public function setLimit($limit)
- {
- $this->_limit = $limit;
- return $this;
- }
- /**
- * Set collection for pagination
- *
- * @param \Magento\Framework\Data\Collection $collection
- * @return $this
- */
- public function setCollection($collection)
- {
- $this->_collection = $collection->setCurPage($this->getCurrentPage());
- // If not int - then not limit
- if ((int)$this->getLimit()) {
- $this->_collection->setPageSize($this->getLimit());
- }
- $this->_setFrameInitialized(false);
- return $this;
- }
- /**
- * Returns data collection
- *
- * @return \Magento\Framework\Data\Collection
- */
- public function getCollection()
- {
- return $this->_collection;
- }
- /**
- * Set page variable name
- *
- * @param string $varName
- *
- * @return $this
- */
- public function setPageVarName($varName)
- {
- $this->_pageVarName = $varName;
- return $this;
- }
- /**
- * Get page variable name
- *
- * @return string
- */
- public function getPageVarName()
- {
- return $this->_pageVarName;
- }
- /**
- * Set show per page param
- *
- * @param bool $varName
- *
- * @return $this
- */
- public function setShowPerPage($varName)
- {
- $this->_showPerPage = $varName;
- return $this;
- }
- /**
- * Is show per page
- *
- * @return bool
- */
- public function isShowPerPage()
- {
- if (sizeof($this->getAvailableLimit()) <= 1) {
- return false;
- }
- return $this->_showPerPage;
- }
- /**
- * Set the name for pager limit data
- *
- * @param string $varName
- *
- * @return $this
- */
- public function setLimitVarName($varName)
- {
- $this->_limitVarName = $varName;
- return $this;
- }
- /**
- * Retrieve name for pager limit data
- *
- * @return string
- */
- public function getLimitVarName()
- {
- return $this->_limitVarName;
- }
- /**
- * Set pager limit
- *
- * @param array $limits
- * @return $this
- */
- public function setAvailableLimit(array $limits)
- {
- $this->_availableLimit = $limits;
- return $this;
- }
- /**
- * Retrieve pager limit
- *
- * @return array
- */
- public function getAvailableLimit()
- {
- return $this->_availableLimit;
- }
- /**
- * Get first number
- *
- * @return int
- */
- public function getFirstNum()
- {
- $collection = $this->getCollection();
- return $collection->getPageSize() * ($collection->getCurPage() - 1) + 1;
- }
- /**
- * Get last number
- *
- * @return int
- */
- public function getLastNum()
- {
- $collection = $this->getCollection();
- return $collection->getPageSize() * ($collection->getCurPage() - 1) + $collection->count();
- }
- /**
- * Retrieve total number of pages
- *
- * @return int
- */
- public function getTotalNum()
- {
- return $this->getCollection()->getSize();
- }
- /**
- * Check if current page is a first page in collection
- *
- * @return bool
- */
- public function isFirstPage()
- {
- return $this->getCollection()->getCurPage() == 1;
- }
- /**
- * Retrieve number of last page
- *
- * @return int
- */
- public function getLastPageNum()
- {
- return $this->getCollection()->getLastPageNumber();
- }
- /**
- * Check if current page is a last page in collection
- *
- * @return bool
- */
- public function isLastPage()
- {
- return $this->getCollection()->getCurPage() >= $this->getLastPageNum();
- }
- /**
- * Is limit current
- *
- * @param int $limit
- *
- * @return bool
- */
- public function isLimitCurrent($limit)
- {
- return $limit == $this->getLimit();
- }
- /**
- * Is page current
- *
- * @param int $page
- *
- * @return bool
- */
- public function isPageCurrent($page)
- {
- return $page == $this->getCurrentPage();
- }
- /**
- * Get pages
- *
- * @return array
- */
- public function getPages()
- {
- $collection = $this->getCollection();
- if ($collection->getLastPageNumber() <= $this->_displayPages) {
- return range(1, $collection->getLastPageNumber());
- } else {
- $half = ceil($this->_displayPages / 2);
- if ($collection->getCurPage() >= $half &&
- $collection->getCurPage() <= $collection->getLastPageNumber() - $half
- ) {
- $start = $collection->getCurPage() - $half + 1;
- $finish = $start + $this->_displayPages - 1;
- } elseif ($collection->getCurPage() < $half) {
- $start = 1;
- $finish = $this->_displayPages;
- } elseif ($collection->getCurPage() > $collection->getLastPageNumber() - $half) {
- $finish = $collection->getLastPageNumber();
- $start = $finish - $this->_displayPages + 1;
- }
- return range($start, $finish);
- }
- }
- /**
- * Get first page url
- *
- * @return string
- */
- public function getFirstPageUrl()
- {
- return $this->getPageUrl(1);
- }
- /**
- * Retrieve previous page URL
- *
- * @return string
- */
- public function getPreviousPageUrl()
- {
- return $this->getPageUrl($this->getCollection()->getCurPage(-1));
- }
- /**
- * Retrieve next page URL
- *
- * @return string
- */
- public function getNextPageUrl()
- {
- return $this->getPageUrl($this->getCollection()->getCurPage(+1));
- }
- /**
- * Retrieve last page URL
- *
- * @return string
- */
- public function getLastPageUrl()
- {
- return $this->getPageUrl($this->getCollection()->getLastPageNumber());
- }
- /**
- * Retrieve page URL
- *
- * @param string $page
- *
- * @return string
- */
- public function getPageUrl($page)
- {
- return $this->getPagerUrl([$this->getPageVarName() => $page]);
- }
- /**
- * Get limit url
- *
- * @param int $limit
- *
- * @return string
- */
- public function getLimitUrl($limit)
- {
- return $this->getPagerUrl([$this->getLimitVarName() => $limit]);
- }
- /**
- * Retrieve page URL by defined parameters
- *
- * @param array $params
- *
- * @return string
- */
- public function getPagerUrl($params = [])
- {
- $urlParams = [];
- $urlParams['_current'] = true;
- $urlParams['_escape'] = true;
- $urlParams['_use_rewrite'] = true;
- $urlParams['_fragment'] = $this->getFragment();
- $urlParams['_query'] = $params;
- return $this->getUrl($this->getPath(), $urlParams);
- }
- /**
- * Get path
- *
- * @return string
- */
- protected function getPath()
- {
- return $this->_getData('path') ?: '*/*/*';
- }
- /**
- * Getter for $_frameStart
- *
- * @return int
- */
- public function getFrameStart()
- {
- $this->_initFrame();
- return $this->_frameStart;
- }
- /**
- * Getter for $_frameEnd
- *
- * @return int
- */
- public function getFrameEnd()
- {
- $this->_initFrame();
- return $this->_frameEnd;
- }
- /**
- * Return array of pages in frame
- *
- * @return array
- */
- public function getFramePages()
- {
- $start = $this->getFrameStart();
- $end = $this->getFrameEnd();
- return range($start, $end);
- }
- /**
- * Return page number of Previous jump
- *
- * @return int|null
- */
- public function getPreviousJumpPage()
- {
- if (!$this->getJump()) {
- return null;
- }
- $frameStart = $this->getFrameStart();
- if ($frameStart - 1 > 1) {
- return max(2, $frameStart - $this->getJump());
- }
- return null;
- }
- /**
- * Prepare URL for Previous Jump
- *
- * @return string
- */
- public function getPreviousJumpUrl()
- {
- return $this->getPageUrl($this->getPreviousJumpPage());
- }
- /**
- * Return page number of Next jump
- *
- * @return int|null
- */
- public function getNextJumpPage()
- {
- if (!$this->getJump()) {
- return null;
- }
- $frameEnd = $this->getFrameEnd();
- if ($this->getLastPageNum() - $frameEnd > 1) {
- return min($this->getLastPageNum() - 1, $frameEnd + $this->getJump());
- }
- return null;
- }
- /**
- * Prepare URL for Next Jump
- *
- * @return string
- */
- public function getNextJumpUrl()
- {
- return $this->getPageUrl($this->getNextJumpPage());
- }
- /**
- * Getter for $_frameLength
- *
- * @return int
- */
- public function getFrameLength()
- {
- return $this->_frameLength;
- }
- /**
- * Getter for $_jump
- *
- * @return int
- */
- public function getJump()
- {
- return $this->_jump;
- }
- /**
- * Setter for $_frameLength
- *
- * @param int $frame
- * @return $this
- */
- public function setFrameLength($frame)
- {
- $frame = abs((int)$frame);
- if ($frame == 0) {
- $frame = $this->_frameLength;
- }
- if ($this->getFrameLength() != $frame) {
- $this->_setFrameInitialized(false);
- $this->_frameLength = $frame;
- }
- return $this;
- }
- /**
- * Setter for $_jump
- *
- * @param int $jump
- * @return $this
- */
- public function setJump($jump)
- {
- $jump = abs((int)$jump);
- if ($this->getJump() != $jump) {
- $this->_setFrameInitialized(false);
- $this->_jump = $jump;
- }
- return $this;
- }
- /**
- * Whether to show first page in pagination or not
- *
- * @return bool
- */
- public function canShowFirst()
- {
- return $this->getJump() > 1 && $this->getFrameStart() > 1;
- }
- /**
- * Whether to show last page in pagination or not
- *
- * @return bool
- */
- public function canShowLast()
- {
- return $this->getJump() > 1 && $this->getFrameEnd() < $this->getLastPageNum();
- }
- /**
- * Whether to show link to Previous Jump
- *
- * @return bool
- */
- public function canShowPreviousJump()
- {
- return $this->getPreviousJumpPage() !== null;
- }
- /**
- * Whether to show link to Next Jump
- *
- * @return bool
- */
- public function canShowNextJump()
- {
- return $this->getNextJumpPage() !== null;
- }
- /**
- * Initialize frame data, such as frame start, frame start etc.
- *
- * @return $this
- */
- protected function _initFrame()
- {
- if (!$this->isFrameInitialized()) {
- $start = 0;
- $end = 0;
- $collection = $this->getCollection();
- if ($collection->getLastPageNumber() <= $this->getFrameLength()) {
- $start = 1;
- $end = $collection->getLastPageNumber();
- } else {
- $half = ceil($this->getFrameLength() / 2);
- if ($collection->getCurPage() >= $half &&
- $collection->getCurPage() <= $collection->getLastPageNumber() - $half
- ) {
- $start = $collection->getCurPage() - $half + 1;
- $end = $start + $this->getFrameLength() - 1;
- } elseif ($collection->getCurPage() < $half) {
- $start = 1;
- $end = $this->getFrameLength();
- } elseif ($collection->getCurPage() > $collection->getLastPageNumber() - $half) {
- $end = $collection->getLastPageNumber();
- $start = $end - $this->getFrameLength() + 1;
- }
- }
- $this->_frameStart = $start;
- $this->_frameEnd = $end;
- $this->_setFrameInitialized(true);
- }
- return $this;
- }
- /**
- * Setter for flag _frameInitialized
- *
- * @param bool $flag
- * @return $this
- */
- protected function _setFrameInitialized($flag)
- {
- $this->_frameInitialized = (bool)$flag;
- return $this;
- }
- /**
- * Check if frame data was initialized
- *
- * @return bool
- */
- public function isFrameInitialized()
- {
- return $this->_frameInitialized;
- }
- /**
- * Getter for alternative text for Previous link in pagination frame
- *
- * @return string
- */
- public function getAnchorTextForPrevious()
- {
- return $this->_scopeConfig->getValue(
- 'design/pagination/anchor_text_for_previous',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Getter for alternative text for Next link in pagination frame
- *
- * @return string
- */
- public function getAnchorTextForNext()
- {
- return $this->_scopeConfig->getValue(
- 'design/pagination/anchor_text_for_next',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Set whether output of the pager is mandatory
- *
- * @param bool $isRequired
- * @return $this
- */
- public function setIsOutputRequired($isRequired)
- {
- $this->_outputRequired = (bool)$isRequired;
- return $this;
- }
- /**
- * Determine whether the pagination should be eventually rendered
- *
- * @return string
- */
- protected function _toHtml()
- {
- if ($this->_outputRequired || $this->getTotalNum() > $this->getLimit()) {
- return parent::_toHtml();
- }
- return '';
- }
- /**
- * Get the URL fragment
- *
- * @return string|null
- */
- public function getFragment()
- {
- return $this->_fragment;
- }
- /**
- * Set the URL fragment
- *
- * @param string|null $fragment
- * @return $this
- */
- public function setFragment($fragment)
- {
- $this->_fragment = $fragment;
- return $this;
- }
- }
|