PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Catalog/Block/Product/Widget/Html/Pager.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 279 lines | 147 code | 23 blank | 109 comment | 21 complexity | fd7e5c23c252ba53ab8ad6e4e7f4c923 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * New products widget pager block
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Catalog\Block\Product\Widget\Html;
  12. class Pager extends \Magento\Theme\Block\Html\Pager
  13. {
  14. /**
  15. * Collection size
  16. *
  17. * Size of collection which may has a manual limitation
  18. *
  19. * @var int
  20. */
  21. protected $_collectionSize;
  22. /**
  23. * Current page
  24. *
  25. * @var int
  26. */
  27. protected $_currentPage;
  28. /**
  29. * Last page
  30. *
  31. * @var int
  32. */
  33. protected $_lastPage;
  34. /**
  35. * Return collection size
  36. *
  37. * It may be limited by manual
  38. *
  39. * @return int
  40. */
  41. public function getCollectionSize()
  42. {
  43. if (null === $this->_collectionSize) {
  44. $this->_collectionSize = $this->getCollection()->getSize();
  45. if ($this->getTotalLimit() && $this->_collectionSize > $this->getTotalLimit()) {
  46. $this->_collectionSize = $this->getTotalLimit();
  47. }
  48. }
  49. return $this->_collectionSize;
  50. }
  51. /**
  52. * Return number of current page
  53. *
  54. * If current page is grate then total count of page current page will be equals total count of page
  55. *
  56. * @return int
  57. */
  58. public function getCurrentPage()
  59. {
  60. if (null === $this->_currentPage) {
  61. $page = abs((int)$this->getRequest()->getParam($this->getPageVarName()));
  62. if ($page > $this->getLastPageNum()) {
  63. $this->_currentPage = $this->getLastPageNum();
  64. } elseif ($page > 0) {
  65. $this->_currentPage = $page;
  66. } else {
  67. $this->_currentPage = 1;
  68. }
  69. }
  70. return $this->_currentPage;
  71. }
  72. /**
  73. * Return items count per page
  74. *
  75. * @return int
  76. */
  77. public function getLimit()
  78. {
  79. if ($this->_limit > 0) {
  80. return $this->_limit;
  81. }
  82. $limit = $this->getRequest()->getParam($this->getLimitVarName());
  83. $limits = $this->getAvailableLimit();
  84. if ($limit && isset($limits[$limit])) {
  85. return $limit;
  86. }
  87. $limits = array_keys($limits);
  88. return current($limits);
  89. }
  90. /**
  91. * Connect collection to paging
  92. *
  93. * @param \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection $collection
  94. * @return \Magento\Catalog\Block\Product\Widget\Html\Pager
  95. */
  96. public function setCollection($collection)
  97. {
  98. $this->_collection = $collection;
  99. $this->_collection->setPageSize(null)->setCurPage(null);
  100. $collectionOffset = $this->getFirstNum() - 1;
  101. $collectionLimit = $collectionOffset + $this->getLimit() >
  102. $this->getTotalNum() ? $this->getTotalNum() - $collectionOffset : $this->getLimit();
  103. $this->_collection->getSelect()->limit($collectionLimit, $collectionOffset);
  104. $this->_setFrameInitialized(false);
  105. return $this;
  106. }
  107. /**
  108. * Return position number in collection for first item on current page
  109. *
  110. * @return int
  111. */
  112. public function getFirstNum()
  113. {
  114. return $this->getLimit() * ($this->getCurrentPage() - 1) + 1;
  115. }
  116. /**
  117. * Return position number in collection for last item on current page
  118. *
  119. * @return int
  120. */
  121. public function getLastNum()
  122. {
  123. $collection = $this->getCollection();
  124. return $this->getLimit() * ($this->getCurrentPage() - 1) + $collection->count();
  125. }
  126. /**
  127. * Return total number of collection
  128. *
  129. * It may be limited by manual
  130. *
  131. * @return int
  132. */
  133. public function getTotalNum()
  134. {
  135. return $this->getCollectionSize();
  136. }
  137. /**
  138. * Return number of last page
  139. *
  140. * @return bool
  141. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  142. */
  143. public function getLastPageNum()
  144. {
  145. if (null === $this->_lastPage) {
  146. $this->_lastPage = ceil($this->getCollectionSize() / $this->getLimit());
  147. if ($this->_lastPage <= 0) {
  148. $this->_lastPage = 1;
  149. }
  150. }
  151. return $this->_lastPage;
  152. }
  153. /**
  154. * Checks if current page is the first page
  155. *
  156. * @return bool
  157. */
  158. public function isFirstPage()
  159. {
  160. return $this->getCurrentPage() == 1;
  161. }
  162. /**
  163. * Checks if current page is the last page
  164. *
  165. * @return bool
  166. */
  167. public function isLastPage()
  168. {
  169. return $this->getCurrentPage() >= $this->getLastPageNum();
  170. }
  171. /**
  172. * Return array of pages
  173. *
  174. * @return array
  175. */
  176. public function getPages()
  177. {
  178. $pages = [];
  179. if ($this->getLastPageNum() <= $this->_displayPages) {
  180. $pages = range(1, $this->getLastPageNum());
  181. } else {
  182. $half = ceil($this->_displayPages / 2);
  183. if ($this->getCurrentPage() >= $half && $this->getCurrentPage() <= $this->getLastPageNum() - $half) {
  184. $start = $this->getCurrentPage() - $half + 1;
  185. $finish = $start + $this->_displayPages - 1;
  186. } elseif ($this->getCurrentPage() < $half) {
  187. $start = 1;
  188. $finish = $this->_displayPages;
  189. } elseif ($this->getCurrentPage() > $this->getLastPageNum() - $half) {
  190. $finish = $this->getLastPageNum();
  191. $start = $finish - $this->_displayPages + 1;
  192. }
  193. $pages = range($start, $finish);
  194. }
  195. return $pages;
  196. }
  197. /**
  198. * Retrieve url for previous page
  199. *
  200. * @return string
  201. */
  202. public function getPreviousPageUrl()
  203. {
  204. return $this->getPageUrl($this->getCurrentPage() - 1);
  205. }
  206. /**
  207. * Retrieve url for next page
  208. *
  209. * @return string
  210. */
  211. public function getNextPageUrl()
  212. {
  213. return $this->getPageUrl($this->getCurrentPage() + 1);
  214. }
  215. /**
  216. * Retrieve url for last page
  217. *
  218. * @return string
  219. */
  220. public function getLastPageUrl()
  221. {
  222. return $this->getPageUrl($this->getLastPageNum());
  223. }
  224. /**
  225. * Initialize frame data, such as frame start, frame start etc.
  226. *
  227. * @return \Magento\Catalog\Block\Product\Widget\Html\Pager
  228. */
  229. protected function _initFrame()
  230. {
  231. if (!$this->isFrameInitialized()) {
  232. $start = 0;
  233. $end = 0;
  234. if ($this->getLastPageNum() <= $this->getFrameLength()) {
  235. $start = 1;
  236. $end = $this->getLastPageNum();
  237. } else {
  238. $half = ceil($this->getFrameLength() / 2);
  239. if ($this->getCurrentPage() >= $half && $this->getCurrentPage() <= $this->getLastPageNum() - $half) {
  240. $start = $this->getCurrentPage() - $half + 1;
  241. $end = $start + $this->getFrameLength() - 1;
  242. } elseif ($this->getCurrentPage() < $half) {
  243. $start = 1;
  244. $end = $this->getFrameLength();
  245. } elseif ($this->getCurrentPage() > $this->getLastPageNum() - $half) {
  246. $end = $this->getLastPageNum();
  247. $start = $end - $this->getFrameLength() + 1;
  248. }
  249. }
  250. $this->_frameStart = $start;
  251. $this->_frameEnd = $end;
  252. $this->_setFrameInitialized(true);
  253. }
  254. return $this;
  255. }
  256. }