Toolbar.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright © 2015 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Block\Post\PostList;
  9. /**
  10. * Blog posts list toolbar
  11. */
  12. class Toolbar extends \Magento\Framework\View\Element\Template
  13. {
  14. /**
  15. * Page GET parameter name
  16. */
  17. const PAGE_PARM_NAME = 'page';
  18. /**
  19. * Products collection
  20. *
  21. * @var \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection
  22. */
  23. protected $_collection = null;
  24. /**
  25. * Default block template
  26. * @var string
  27. */
  28. protected $_template = 'post/list/toolbar.phtml';
  29. /**
  30. * Set collection to pager
  31. *
  32. * @param \Magento\Framework\Data\Collection $collection
  33. * @return $this
  34. */
  35. public function setCollection($collection)
  36. {
  37. $this->_collection = $collection;
  38. $this->_collection->setCurPage($this->getCurrentPage());
  39. // we need to set pagination only if passed value integer and more that 0
  40. $limit = (int)$this->getLimit();
  41. if ($limit) {
  42. $this->_collection->setPageSize($limit);
  43. }
  44. if ($this->getCurrentOrder()) {
  45. $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
  46. }
  47. return $this;
  48. }
  49. /**
  50. * Return products collection instance
  51. *
  52. * @return \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection
  53. */
  54. public function getCollection()
  55. {
  56. return $this->_collection;
  57. }
  58. /**
  59. * Get specified posts limit display per page
  60. *
  61. * @return string
  62. */
  63. public function getLimit()
  64. {
  65. return $this->_scopeConfig->getValue(
  66. 'mfblog/post_list/posts_per_page',
  67. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  68. );
  69. }
  70. /**
  71. * Return current page from request
  72. *
  73. * @return int
  74. */
  75. public function getCurrentPage()
  76. {
  77. $page = (int) $this->_request->getParam(self::PAGE_PARM_NAME);
  78. return $page ? $page : 1;
  79. }
  80. /**
  81. * Render pagination HTML
  82. *
  83. * @return string
  84. */
  85. public function getPagerHtml()
  86. {
  87. $pagerBlock = $this->getChildBlock('post_list_toolbar_pager');
  88. if ($pagerBlock instanceof \Magento\Framework\DataObject) {
  89. /* @var $pagerBlock \Magento\Theme\Block\Html\Pager */
  90. $pagerBlock->setUseContainer(
  91. false
  92. )->setShowPerPage(
  93. false
  94. )->setShowAmounts(
  95. false
  96. )->setPageVarName(
  97. 'page'
  98. )->setFrameLength(
  99. $this->_scopeConfig->getValue(
  100. 'design/pagination/pagination_frame',
  101. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  102. )
  103. )->setJump(
  104. $this->_scopeConfig->getValue(
  105. 'design/pagination/pagination_frame_skip',
  106. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  107. )
  108. )->setLimit(
  109. $this->getLimit()
  110. )->setCollection(
  111. $this->getCollection()
  112. );
  113. return $pagerBlock->toHtml();
  114. }
  115. return '';
  116. }
  117. }