Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pager.php
Go to the documentation of this file.
1 <?php
7 
16 {
22  protected $_template = 'Magento_Theme::html/pager.phtml';
23 
27  protected $_collection;
28 
32  protected $_pageVarName = 'p';
33 
37  protected $_limitVarName = 'limit';
38 
44  protected $_availableLimit = [10 => 10, 20 => 20, 50 => 50];
45 
49  protected $_displayPages = 5;
50 
54  protected $_showPerPage = true;
55 
59  protected $_limit;
60 
64  protected $_outputRequired = true;
65 
71  protected $_frameLength = 5;
72 
78  protected $_jump = 5;
79 
85  protected $_frameInitialized = false;
86 
92  protected $_frameStart;
93 
99  protected $_frameEnd;
100 
106  protected $_fragment = null;
107 
113  protected function _construct()
114  {
115  parent::_construct();
116  $this->setData('show_amounts', true);
117  $this->setData('use_container', true);
118  }
119 
125  public function getCurrentPage()
126  {
127  if (is_object($this->_collection)) {
128  return $this->_collection->getCurPage();
129  }
130  return (int)$this->getRequest()->getParam($this->getPageVarName(), 1);
131  }
132 
138  public function getLimit()
139  {
140  if ($this->_limit !== null) {
141  return $this->_limit;
142  }
143 
144  $limits = $this->getAvailableLimit();
145  if ($limit = $this->getRequest()->getParam($this->getLimitVarName())) {
146  if (isset($limits[$limit])) {
147  return $limit;
148  }
149  }
150 
151  $limits = array_keys($limits);
152  return $limits[0];
153  }
154 
161  public function setLimit($limit)
162  {
163  $this->_limit = $limit;
164  return $this;
165  }
166 
173  public function setCollection($collection)
174  {
175  $this->_collection = $collection->setCurPage($this->getCurrentPage());
176  // If not int - then not limit
177  if ((int)$this->getLimit()) {
178  $this->_collection->setPageSize($this->getLimit());
179  }
180 
181  $this->_setFrameInitialized(false);
182 
183  return $this;
184  }
185 
189  public function getCollection()
190  {
191  return $this->_collection;
192  }
193 
198  public function setPageVarName($varName)
199  {
200  $this->_pageVarName = $varName;
201  return $this;
202  }
203 
207  public function getPageVarName()
208  {
209  return $this->_pageVarName;
210  }
211 
216  public function setShowPerPage($varName)
217  {
218  $this->_showPerPage = $varName;
219  return $this;
220  }
221 
225  public function isShowPerPage()
226  {
227  if (sizeof($this->getAvailableLimit()) <= 1) {
228  return false;
229  }
230  return $this->_showPerPage;
231  }
232 
239  public function setLimitVarName($varName)
240  {
241  $this->_limitVarName = $varName;
242  return $this;
243  }
244 
250  public function getLimitVarName()
251  {
252  return $this->_limitVarName;
253  }
254 
261  public function setAvailableLimit(array $limits)
262  {
263  $this->_availableLimit = $limits;
264  return $this;
265  }
266 
272  public function getAvailableLimit()
273  {
274  return $this->_availableLimit;
275  }
276 
280  public function getFirstNum()
281  {
282  $collection = $this->getCollection();
283  return $collection->getPageSize() * ($collection->getCurPage() - 1) + 1;
284  }
285 
289  public function getLastNum()
290  {
291  $collection = $this->getCollection();
292  return $collection->getPageSize() * ($collection->getCurPage() - 1) + $collection->count();
293  }
294 
300  public function getTotalNum()
301  {
302  return $this->getCollection()->getSize();
303  }
304 
310  public function isFirstPage()
311  {
312  return $this->getCollection()->getCurPage() == 1;
313  }
314 
320  public function getLastPageNum()
321  {
322  return $this->getCollection()->getLastPageNumber();
323  }
324 
330  public function isLastPage()
331  {
332  return $this->getCollection()->getCurPage() >= $this->getLastPageNum();
333  }
334 
339  public function isLimitCurrent($limit)
340  {
341  return $limit == $this->getLimit();
342  }
343 
348  public function isPageCurrent($page)
349  {
350  return $page == $this->getCurrentPage();
351  }
352 
356  public function getPages()
357  {
358  $collection = $this->getCollection();
359  if ($collection->getLastPageNumber() <= $this->_displayPages) {
360  return range(1, $collection->getLastPageNumber());
361  } else {
362  $half = ceil($this->_displayPages / 2);
363  if ($collection->getCurPage() >= $half &&
364  $collection->getCurPage() <= $collection->getLastPageNumber() - $half
365  ) {
366  $start = $collection->getCurPage() - $half + 1;
367  $finish = $start + $this->_displayPages - 1;
368  } elseif ($collection->getCurPage() < $half) {
369  $start = 1;
370  $finish = $this->_displayPages;
371  } elseif ($collection->getCurPage() > $collection->getLastPageNumber() - $half) {
372  $finish = $collection->getLastPageNumber();
373  $start = $finish - $this->_displayPages + 1;
374  }
375  return range($start, $finish);
376  }
377  }
378 
382  public function getFirstPageUrl()
383  {
384  return $this->getPageUrl(1);
385  }
386 
392  public function getPreviousPageUrl()
393  {
394  return $this->getPageUrl($this->getCollection()->getCurPage(-1));
395  }
396 
402  public function getNextPageUrl()
403  {
404  return $this->getPageUrl($this->getCollection()->getCurPage(+1));
405  }
406 
412  public function getLastPageUrl()
413  {
414  return $this->getPageUrl($this->getCollection()->getLastPageNumber());
415  }
416 
423  public function getPageUrl($page)
424  {
425  return $this->getPagerUrl([$this->getPageVarName() => $page]);
426  }
427 
432  public function getLimitUrl($limit)
433  {
434  return $this->getPagerUrl([$this->getLimitVarName() => $limit]);
435  }
436 
443  public function getPagerUrl($params = [])
444  {
445  $urlParams = [];
446  $urlParams['_current'] = true;
447  $urlParams['_escape'] = true;
448  $urlParams['_use_rewrite'] = true;
449  $urlParams['_fragment'] = $this->getFragment();
450  $urlParams['_query'] = $params;
451 
452  return $this->getUrl($this->getPath(), $urlParams);
453  }
454 
458  protected function getPath()
459  {
460  return $this->_getData('path') ?: '*/*/*';
461  }
462 
468  public function getFrameStart()
469  {
470  $this->_initFrame();
471  return $this->_frameStart;
472  }
473 
479  public function getFrameEnd()
480  {
481  $this->_initFrame();
482  return $this->_frameEnd;
483  }
484 
490  public function getFramePages()
491  {
492  $start = $this->getFrameStart();
493  $end = $this->getFrameEnd();
494  return range($start, $end);
495  }
496 
502  public function getPreviousJumpPage()
503  {
504  if (!$this->getJump()) {
505  return null;
506  }
507 
508  $frameStart = $this->getFrameStart();
509  if ($frameStart - 1 > 1) {
510  return max(2, $frameStart - $this->getJump());
511  }
512 
513  return null;
514  }
515 
521  public function getPreviousJumpUrl()
522  {
523  return $this->getPageUrl($this->getPreviousJumpPage());
524  }
525 
531  public function getNextJumpPage()
532  {
533  if (!$this->getJump()) {
534  return null;
535  }
536 
537  $frameEnd = $this->getFrameEnd();
538  if ($this->getLastPageNum() - $frameEnd > 1) {
539  return min($this->getLastPageNum() - 1, $frameEnd + $this->getJump());
540  }
541 
542  return null;
543  }
544 
550  public function getNextJumpUrl()
551  {
552  return $this->getPageUrl($this->getNextJumpPage());
553  }
554 
560  public function getFrameLength()
561  {
562  return $this->_frameLength;
563  }
564 
570  public function getJump()
571  {
572  return $this->_jump;
573  }
574 
581  public function setFrameLength($frame)
582  {
583  $frame = abs(intval($frame));
584  if ($frame == 0) {
585  $frame = $this->_frameLength;
586  }
587  if ($this->getFrameLength() != $frame) {
588  $this->_setFrameInitialized(false);
589  $this->_frameLength = $frame;
590  }
591 
592  return $this;
593  }
594 
601  public function setJump($jump)
602  {
603  $jump = abs(intval($jump));
604  if ($this->getJump() != $jump) {
605  $this->_setFrameInitialized(false);
606  $this->_jump = $jump;
607  }
608 
609  return $this;
610  }
611 
617  public function canShowFirst()
618  {
619  return $this->getJump() > 1 && $this->getFrameStart() > 1;
620  }
621 
627  public function canShowLast()
628  {
629  return $this->getJump() > 1 && $this->getFrameEnd() < $this->getLastPageNum();
630  }
631 
637  public function canShowPreviousJump()
638  {
639  return $this->getPreviousJumpPage() !== null;
640  }
641 
647  public function canShowNextJump()
648  {
649  return $this->getNextJumpPage() !== null;
650  }
651 
657  protected function _initFrame()
658  {
659  if (!$this->isFrameInitialized()) {
660  $start = 0;
661  $end = 0;
662 
663  $collection = $this->getCollection();
664  if ($collection->getLastPageNumber() <= $this->getFrameLength()) {
665  $start = 1;
666  $end = $collection->getLastPageNumber();
667  } else {
668  $half = ceil($this->getFrameLength() / 2);
669  if ($collection->getCurPage() >= $half &&
670  $collection->getCurPage() <= $collection->getLastPageNumber() - $half
671  ) {
672  $start = $collection->getCurPage() - $half + 1;
673  $end = $start + $this->getFrameLength() - 1;
674  } elseif ($collection->getCurPage() < $half) {
675  $start = 1;
676  $end = $this->getFrameLength();
677  } elseif ($collection->getCurPage() > $collection->getLastPageNumber() - $half) {
678  $end = $collection->getLastPageNumber();
679  $start = $end - $this->getFrameLength() + 1;
680  }
681  }
682  $this->_frameStart = $start;
683  $this->_frameEnd = $end;
684 
685  $this->_setFrameInitialized(true);
686  }
687 
688  return $this;
689  }
690 
697  protected function _setFrameInitialized($flag)
698  {
699  $this->_frameInitialized = (bool)$flag;
700  return $this;
701  }
702 
708  public function isFrameInitialized()
709  {
711  }
712 
718  public function getAnchorTextForPrevious()
719  {
720  return $this->_scopeConfig->getValue(
721  'design/pagination/anchor_text_for_previous',
722  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
723  );
724  }
725 
731  public function getAnchorTextForNext()
732  {
733  return $this->_scopeConfig->getValue(
734  'design/pagination/anchor_text_for_next',
735  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
736  );
737  }
738 
745  public function setIsOutputRequired($isRequired)
746  {
747  $this->_outputRequired = (bool)$isRequired;
748  return $this;
749  }
750 
756  protected function _toHtml()
757  {
758  if ($this->_outputRequired || $this->getTotalNum() > $this->getLimit()) {
759  return parent::_toHtml();
760  }
761  return '';
762  }
763 
769  public function getFragment()
770  {
771  return $this->_fragment;
772  }
773 
780  public function setFragment($fragment)
781  {
782  $this->_fragment = $fragment;
783  return $this;
784  }
785 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setAvailableLimit(array $limits)
Definition: Pager.php:261
setCollection($collection)
Definition: Pager.php:173
$start
Definition: listing.phtml:18
setIsOutputRequired($isRequired)
Definition: Pager.php:745
$this _collection
Definition: coupons.php:7
$page
Definition: pages.php:8
setData($key, $value=null)
Definition: DataObject.php:72
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18