Pager.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Block\Html;
  7. /**
  8. * Html pager block
  9. *
  10. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Pager extends \Magento\Framework\View\Element\Template
  15. {
  16. /**
  17. * Current template name
  18. *
  19. * @var string
  20. */
  21. protected $_template = 'Magento_Theme::html/pager.phtml';
  22. /**
  23. * @var \Magento\Framework\Data\Collection
  24. */
  25. protected $_collection;
  26. /**
  27. * @var string
  28. */
  29. protected $_pageVarName = 'p';
  30. /**
  31. * @var string
  32. */
  33. protected $_limitVarName = 'limit';
  34. /**
  35. * The list of available pager limits
  36. *
  37. * @var array
  38. */
  39. protected $_availableLimit = [10 => 10, 20 => 20, 50 => 50];
  40. /**
  41. * @var int
  42. */
  43. protected $_displayPages = 5;
  44. /**
  45. * @var bool
  46. */
  47. protected $_showPerPage = true;
  48. /**
  49. * @var int
  50. */
  51. protected $_limit;
  52. /**
  53. * @var bool
  54. */
  55. protected $_outputRequired = true;
  56. /**
  57. * Pages quantity per frame
  58. *
  59. * @var int
  60. */
  61. protected $_frameLength = 5;
  62. /**
  63. * Next/previous page position relatively to the current frame
  64. *
  65. * @var int
  66. */
  67. protected $_jump = 5;
  68. /**
  69. * Frame initialization flag
  70. *
  71. * @var bool
  72. */
  73. protected $_frameInitialized = false;
  74. /**
  75. * Start page position in frame
  76. *
  77. * @var int
  78. */
  79. protected $_frameStart;
  80. /**
  81. * Finish page position in frame
  82. *
  83. * @var int
  84. */
  85. protected $_frameEnd;
  86. /**
  87. * Url Fragment for pagination
  88. *
  89. * @var string|null
  90. */
  91. protected $_fragment = null;
  92. /**
  93. * Set pager data
  94. *
  95. * @return void
  96. */
  97. protected function _construct()
  98. {
  99. parent::_construct();
  100. $this->setData('show_amounts', true);
  101. $this->setData('use_container', true);
  102. }
  103. /**
  104. * Return current page
  105. *
  106. * @return int
  107. */
  108. public function getCurrentPage()
  109. {
  110. if (is_object($this->_collection)) {
  111. return $this->_collection->getCurPage();
  112. }
  113. return (int)$this->getRequest()->getParam($this->getPageVarName(), 1);
  114. }
  115. /**
  116. * Return current page limit
  117. *
  118. * @return int
  119. */
  120. public function getLimit()
  121. {
  122. if ($this->_limit !== null) {
  123. return $this->_limit;
  124. }
  125. $limits = $this->getAvailableLimit();
  126. if ($limit = $this->getRequest()->getParam($this->getLimitVarName())) {
  127. if (isset($limits[$limit])) {
  128. return $limit;
  129. }
  130. }
  131. $limits = array_keys($limits);
  132. return $limits[0];
  133. }
  134. /**
  135. * Setter for limit items per page
  136. *
  137. * @param int $limit
  138. * @return $this
  139. */
  140. public function setLimit($limit)
  141. {
  142. $this->_limit = $limit;
  143. return $this;
  144. }
  145. /**
  146. * Set collection for pagination
  147. *
  148. * @param \Magento\Framework\Data\Collection $collection
  149. * @return $this
  150. */
  151. public function setCollection($collection)
  152. {
  153. $this->_collection = $collection->setCurPage($this->getCurrentPage());
  154. // If not int - then not limit
  155. if ((int)$this->getLimit()) {
  156. $this->_collection->setPageSize($this->getLimit());
  157. }
  158. $this->_setFrameInitialized(false);
  159. return $this;
  160. }
  161. /**
  162. * Returns data collection
  163. *
  164. * @return \Magento\Framework\Data\Collection
  165. */
  166. public function getCollection()
  167. {
  168. return $this->_collection;
  169. }
  170. /**
  171. * Set page variable name
  172. *
  173. * @param string $varName
  174. *
  175. * @return $this
  176. */
  177. public function setPageVarName($varName)
  178. {
  179. $this->_pageVarName = $varName;
  180. return $this;
  181. }
  182. /**
  183. * Get page variable name
  184. *
  185. * @return string
  186. */
  187. public function getPageVarName()
  188. {
  189. return $this->_pageVarName;
  190. }
  191. /**
  192. * Set show per page param
  193. *
  194. * @param bool $varName
  195. *
  196. * @return $this
  197. */
  198. public function setShowPerPage($varName)
  199. {
  200. $this->_showPerPage = $varName;
  201. return $this;
  202. }
  203. /**
  204. * Is show per page
  205. *
  206. * @return bool
  207. */
  208. public function isShowPerPage()
  209. {
  210. if (sizeof($this->getAvailableLimit()) <= 1) {
  211. return false;
  212. }
  213. return $this->_showPerPage;
  214. }
  215. /**
  216. * Set the name for pager limit data
  217. *
  218. * @param string $varName
  219. *
  220. * @return $this
  221. */
  222. public function setLimitVarName($varName)
  223. {
  224. $this->_limitVarName = $varName;
  225. return $this;
  226. }
  227. /**
  228. * Retrieve name for pager limit data
  229. *
  230. * @return string
  231. */
  232. public function getLimitVarName()
  233. {
  234. return $this->_limitVarName;
  235. }
  236. /**
  237. * Set pager limit
  238. *
  239. * @param array $limits
  240. * @return $this
  241. */
  242. public function setAvailableLimit(array $limits)
  243. {
  244. $this->_availableLimit = $limits;
  245. return $this;
  246. }
  247. /**
  248. * Retrieve pager limit
  249. *
  250. * @return array
  251. */
  252. public function getAvailableLimit()
  253. {
  254. return $this->_availableLimit;
  255. }
  256. /**
  257. * Get first number
  258. *
  259. * @return int
  260. */
  261. public function getFirstNum()
  262. {
  263. $collection = $this->getCollection();
  264. return $collection->getPageSize() * ($collection->getCurPage() - 1) + 1;
  265. }
  266. /**
  267. * Get last number
  268. *
  269. * @return int
  270. */
  271. public function getLastNum()
  272. {
  273. $collection = $this->getCollection();
  274. return $collection->getPageSize() * ($collection->getCurPage() - 1) + $collection->count();
  275. }
  276. /**
  277. * Retrieve total number of pages
  278. *
  279. * @return int
  280. */
  281. public function getTotalNum()
  282. {
  283. return $this->getCollection()->getSize();
  284. }
  285. /**
  286. * Check if current page is a first page in collection
  287. *
  288. * @return bool
  289. */
  290. public function isFirstPage()
  291. {
  292. return $this->getCollection()->getCurPage() == 1;
  293. }
  294. /**
  295. * Retrieve number of last page
  296. *
  297. * @return int
  298. */
  299. public function getLastPageNum()
  300. {
  301. return $this->getCollection()->getLastPageNumber();
  302. }
  303. /**
  304. * Check if current page is a last page in collection
  305. *
  306. * @return bool
  307. */
  308. public function isLastPage()
  309. {
  310. return $this->getCollection()->getCurPage() >= $this->getLastPageNum();
  311. }
  312. /**
  313. * Is limit current
  314. *
  315. * @param int $limit
  316. *
  317. * @return bool
  318. */
  319. public function isLimitCurrent($limit)
  320. {
  321. return $limit == $this->getLimit();
  322. }
  323. /**
  324. * Is page current
  325. *
  326. * @param int $page
  327. *
  328. * @return bool
  329. */
  330. public function isPageCurrent($page)
  331. {
  332. return $page == $this->getCurrentPage();
  333. }
  334. /**
  335. * Get pages
  336. *
  337. * @return array
  338. */
  339. public function getPages()
  340. {
  341. $collection = $this->getCollection();
  342. if ($collection->getLastPageNumber() <= $this->_displayPages) {
  343. return range(1, $collection->getLastPageNumber());
  344. } else {
  345. $half = ceil($this->_displayPages / 2);
  346. if ($collection->getCurPage() >= $half &&
  347. $collection->getCurPage() <= $collection->getLastPageNumber() - $half
  348. ) {
  349. $start = $collection->getCurPage() - $half + 1;
  350. $finish = $start + $this->_displayPages - 1;
  351. } elseif ($collection->getCurPage() < $half) {
  352. $start = 1;
  353. $finish = $this->_displayPages;
  354. } elseif ($collection->getCurPage() > $collection->getLastPageNumber() - $half) {
  355. $finish = $collection->getLastPageNumber();
  356. $start = $finish - $this->_displayPages + 1;
  357. }
  358. return range($start, $finish);
  359. }
  360. }
  361. /**
  362. * Get first page url
  363. *
  364. * @return string
  365. */
  366. public function getFirstPageUrl()
  367. {
  368. return $this->getPageUrl(1);
  369. }
  370. /**
  371. * Retrieve previous page URL
  372. *
  373. * @return string
  374. */
  375. public function getPreviousPageUrl()
  376. {
  377. return $this->getPageUrl($this->getCollection()->getCurPage(-1));
  378. }
  379. /**
  380. * Retrieve next page URL
  381. *
  382. * @return string
  383. */
  384. public function getNextPageUrl()
  385. {
  386. return $this->getPageUrl($this->getCollection()->getCurPage(+1));
  387. }
  388. /**
  389. * Retrieve last page URL
  390. *
  391. * @return string
  392. */
  393. public function getLastPageUrl()
  394. {
  395. return $this->getPageUrl($this->getCollection()->getLastPageNumber());
  396. }
  397. /**
  398. * Retrieve page URL
  399. *
  400. * @param string $page
  401. *
  402. * @return string
  403. */
  404. public function getPageUrl($page)
  405. {
  406. return $this->getPagerUrl([$this->getPageVarName() => $page]);
  407. }
  408. /**
  409. * Get limit url
  410. *
  411. * @param int $limit
  412. *
  413. * @return string
  414. */
  415. public function getLimitUrl($limit)
  416. {
  417. return $this->getPagerUrl([$this->getLimitVarName() => $limit]);
  418. }
  419. /**
  420. * Retrieve page URL by defined parameters
  421. *
  422. * @param array $params
  423. *
  424. * @return string
  425. */
  426. public function getPagerUrl($params = [])
  427. {
  428. $urlParams = [];
  429. $urlParams['_current'] = true;
  430. $urlParams['_escape'] = true;
  431. $urlParams['_use_rewrite'] = true;
  432. $urlParams['_fragment'] = $this->getFragment();
  433. $urlParams['_query'] = $params;
  434. return $this->getUrl($this->getPath(), $urlParams);
  435. }
  436. /**
  437. * Get path
  438. *
  439. * @return string
  440. */
  441. protected function getPath()
  442. {
  443. return $this->_getData('path') ?: '*/*/*';
  444. }
  445. /**
  446. * Getter for $_frameStart
  447. *
  448. * @return int
  449. */
  450. public function getFrameStart()
  451. {
  452. $this->_initFrame();
  453. return $this->_frameStart;
  454. }
  455. /**
  456. * Getter for $_frameEnd
  457. *
  458. * @return int
  459. */
  460. public function getFrameEnd()
  461. {
  462. $this->_initFrame();
  463. return $this->_frameEnd;
  464. }
  465. /**
  466. * Return array of pages in frame
  467. *
  468. * @return array
  469. */
  470. public function getFramePages()
  471. {
  472. $start = $this->getFrameStart();
  473. $end = $this->getFrameEnd();
  474. return range($start, $end);
  475. }
  476. /**
  477. * Return page number of Previous jump
  478. *
  479. * @return int|null
  480. */
  481. public function getPreviousJumpPage()
  482. {
  483. if (!$this->getJump()) {
  484. return null;
  485. }
  486. $frameStart = $this->getFrameStart();
  487. if ($frameStart - 1 > 1) {
  488. return max(2, $frameStart - $this->getJump());
  489. }
  490. return null;
  491. }
  492. /**
  493. * Prepare URL for Previous Jump
  494. *
  495. * @return string
  496. */
  497. public function getPreviousJumpUrl()
  498. {
  499. return $this->getPageUrl($this->getPreviousJumpPage());
  500. }
  501. /**
  502. * Return page number of Next jump
  503. *
  504. * @return int|null
  505. */
  506. public function getNextJumpPage()
  507. {
  508. if (!$this->getJump()) {
  509. return null;
  510. }
  511. $frameEnd = $this->getFrameEnd();
  512. if ($this->getLastPageNum() - $frameEnd > 1) {
  513. return min($this->getLastPageNum() - 1, $frameEnd + $this->getJump());
  514. }
  515. return null;
  516. }
  517. /**
  518. * Prepare URL for Next Jump
  519. *
  520. * @return string
  521. */
  522. public function getNextJumpUrl()
  523. {
  524. return $this->getPageUrl($this->getNextJumpPage());
  525. }
  526. /**
  527. * Getter for $_frameLength
  528. *
  529. * @return int
  530. */
  531. public function getFrameLength()
  532. {
  533. return $this->_frameLength;
  534. }
  535. /**
  536. * Getter for $_jump
  537. *
  538. * @return int
  539. */
  540. public function getJump()
  541. {
  542. return $this->_jump;
  543. }
  544. /**
  545. * Setter for $_frameLength
  546. *
  547. * @param int $frame
  548. * @return $this
  549. */
  550. public function setFrameLength($frame)
  551. {
  552. $frame = abs((int)$frame);
  553. if ($frame == 0) {
  554. $frame = $this->_frameLength;
  555. }
  556. if ($this->getFrameLength() != $frame) {
  557. $this->_setFrameInitialized(false);
  558. $this->_frameLength = $frame;
  559. }
  560. return $this;
  561. }
  562. /**
  563. * Setter for $_jump
  564. *
  565. * @param int $jump
  566. * @return $this
  567. */
  568. public function setJump($jump)
  569. {
  570. $jump = abs((int)$jump);
  571. if ($this->getJump() != $jump) {
  572. $this->_setFrameInitialized(false);
  573. $this->_jump = $jump;
  574. }
  575. return $this;
  576. }
  577. /**
  578. * Whether to show first page in pagination or not
  579. *
  580. * @return bool
  581. */
  582. public function canShowFirst()
  583. {
  584. return $this->getJump() > 1 && $this->getFrameStart() > 1;
  585. }
  586. /**
  587. * Whether to show last page in pagination or not
  588. *
  589. * @return bool
  590. */
  591. public function canShowLast()
  592. {
  593. return $this->getJump() > 1 && $this->getFrameEnd() < $this->getLastPageNum();
  594. }
  595. /**
  596. * Whether to show link to Previous Jump
  597. *
  598. * @return bool
  599. */
  600. public function canShowPreviousJump()
  601. {
  602. return $this->getPreviousJumpPage() !== null;
  603. }
  604. /**
  605. * Whether to show link to Next Jump
  606. *
  607. * @return bool
  608. */
  609. public function canShowNextJump()
  610. {
  611. return $this->getNextJumpPage() !== null;
  612. }
  613. /**
  614. * Initialize frame data, such as frame start, frame start etc.
  615. *
  616. * @return $this
  617. */
  618. protected function _initFrame()
  619. {
  620. if (!$this->isFrameInitialized()) {
  621. $start = 0;
  622. $end = 0;
  623. $collection = $this->getCollection();
  624. if ($collection->getLastPageNumber() <= $this->getFrameLength()) {
  625. $start = 1;
  626. $end = $collection->getLastPageNumber();
  627. } else {
  628. $half = ceil($this->getFrameLength() / 2);
  629. if ($collection->getCurPage() >= $half &&
  630. $collection->getCurPage() <= $collection->getLastPageNumber() - $half
  631. ) {
  632. $start = $collection->getCurPage() - $half + 1;
  633. $end = $start + $this->getFrameLength() - 1;
  634. } elseif ($collection->getCurPage() < $half) {
  635. $start = 1;
  636. $end = $this->getFrameLength();
  637. } elseif ($collection->getCurPage() > $collection->getLastPageNumber() - $half) {
  638. $end = $collection->getLastPageNumber();
  639. $start = $end - $this->getFrameLength() + 1;
  640. }
  641. }
  642. $this->_frameStart = $start;
  643. $this->_frameEnd = $end;
  644. $this->_setFrameInitialized(true);
  645. }
  646. return $this;
  647. }
  648. /**
  649. * Setter for flag _frameInitialized
  650. *
  651. * @param bool $flag
  652. * @return $this
  653. */
  654. protected function _setFrameInitialized($flag)
  655. {
  656. $this->_frameInitialized = (bool)$flag;
  657. return $this;
  658. }
  659. /**
  660. * Check if frame data was initialized
  661. *
  662. * @return bool
  663. */
  664. public function isFrameInitialized()
  665. {
  666. return $this->_frameInitialized;
  667. }
  668. /**
  669. * Getter for alternative text for Previous link in pagination frame
  670. *
  671. * @return string
  672. */
  673. public function getAnchorTextForPrevious()
  674. {
  675. return $this->_scopeConfig->getValue(
  676. 'design/pagination/anchor_text_for_previous',
  677. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  678. );
  679. }
  680. /**
  681. * Getter for alternative text for Next link in pagination frame
  682. *
  683. * @return string
  684. */
  685. public function getAnchorTextForNext()
  686. {
  687. return $this->_scopeConfig->getValue(
  688. 'design/pagination/anchor_text_for_next',
  689. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  690. );
  691. }
  692. /**
  693. * Set whether output of the pager is mandatory
  694. *
  695. * @param bool $isRequired
  696. * @return $this
  697. */
  698. public function setIsOutputRequired($isRequired)
  699. {
  700. $this->_outputRequired = (bool)$isRequired;
  701. return $this;
  702. }
  703. /**
  704. * Determine whether the pagination should be eventually rendered
  705. *
  706. * @return string
  707. */
  708. protected function _toHtml()
  709. {
  710. if ($this->_outputRequired || $this->getTotalNum() > $this->getLimit()) {
  711. return parent::_toHtml();
  712. }
  713. return '';
  714. }
  715. /**
  716. * Get the URL fragment
  717. *
  718. * @return string|null
  719. */
  720. public function getFragment()
  721. {
  722. return $this->_fragment;
  723. }
  724. /**
  725. * Set the URL fragment
  726. *
  727. * @param string|null $fragment
  728. * @return $this
  729. */
  730. public function setFragment($fragment)
  731. {
  732. $this->_fragment = $fragment;
  733. return $this;
  734. }
  735. }