/xampp/htdocs/magento/app/code/core/Mage/Page/Block/Html/Pager.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site · PHP · 256 lines · 176 code · 35 blank · 45 comment · 12 complexity · 7eb35a355febe6fe7278be8e4a3a0af4 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Page
  23. * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Html page block
  28. *
  29. * @category Mage
  30. * @package Mage_Page
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. *
  33. * @todo separate order, mode and pager
  34. */
  35. class Mage_Page_Block_Html_Pager extends Mage_Core_Block_Template
  36. {
  37. protected $_collection = null;
  38. protected $_pageVarName = 'p';
  39. protected $_limitVarName = 'limit';
  40. protected $_availableLimit = array(10=>10,20=>20,50=>50);
  41. protected $_dispersion = 3;
  42. protected $_displayPages = 5;
  43. protected $_showPerPage = true;
  44. protected function _construct()
  45. {
  46. parent::_construct();
  47. $this->setTemplate('page/html/pager.phtml');
  48. }
  49. public function getCurrentPage()
  50. {
  51. if ($page = (int) $this->getRequest()->getParam($this->getPageVarName())) {
  52. return $page;
  53. }
  54. return 1;
  55. }
  56. public function getLimit()
  57. {
  58. $limits = $this->getAvailableLimit();
  59. if ($limit = $this->getRequest()->getParam($this->getLimitVarName())) {
  60. if (isset($limits[$limit])) {
  61. return $limit;
  62. }
  63. }
  64. $limits = array_keys($limits);
  65. return $limits[0];
  66. }
  67. public function setCollection($collection)
  68. {
  69. $this->_collection = $collection
  70. ->setCurPage($this->getCurrentPage());
  71. // If not int - then not limit
  72. if ((int) $this->getLimit()) {
  73. $this->_collection->setPageSize($this->getLimit());
  74. }
  75. return $this;
  76. }
  77. /**
  78. * @return Mage_Core_Model_Mysql4_Collection_Abstract
  79. */
  80. public function getCollection()
  81. {
  82. return $this->_collection;
  83. }
  84. public function setPageVarName($varName)
  85. {
  86. $this->_pageVarName = $varName;
  87. return $this;
  88. }
  89. public function getPageVarName()
  90. {
  91. return $this->_pageVarName;
  92. }
  93. public function setShowPerPage($varName)
  94. {
  95. $this->_showPerPage=$varName;
  96. }
  97. public function getShowPerPage()
  98. {
  99. if(sizeof($this->getAvailableLimit())<=1) {
  100. return false;
  101. }
  102. return $this->_showPerPage;
  103. }
  104. public function setLimitVarName($varName)
  105. {
  106. $this->_limitVarName = $varName;
  107. return $this;
  108. }
  109. public function getLimitVarName()
  110. {
  111. return $this->_limitVarName;
  112. }
  113. public function setAvailableLimit(array $limits)
  114. {
  115. $this->_availableLimit = $limits;
  116. }
  117. public function getAvailableLimit()
  118. {
  119. return $this->_availableLimit;
  120. }
  121. public function getFirstNum()
  122. {
  123. $collection = $this->getCollection();
  124. return $collection->getPageSize()*($collection->getCurPage()-1)+1;
  125. }
  126. public function getLastNum()
  127. {
  128. $collection = $this->getCollection();
  129. return $collection->getPageSize()*($collection->getCurPage()-1)+$collection->count();
  130. }
  131. public function getTotalNum()
  132. {
  133. return $this->getCollection()->getSize();
  134. }
  135. public function isFirstPage()
  136. {
  137. return $this->getCollection()->getCurPage() == 1;
  138. }
  139. public function getLastPageNum()
  140. {
  141. return $this->getCollection()->getLastPageNumber();
  142. }
  143. public function isLastPage()
  144. {
  145. return $this->getCollection()->getCurPage() >= $this->getLastPageNum();
  146. }
  147. public function isLimitCurrent($limit)
  148. {
  149. return $limit == $this->getLimit();
  150. }
  151. public function isPageCurrent($page)
  152. {
  153. return $page == $this->getCurrentPage();
  154. }
  155. public function getPages()
  156. {
  157. $collection = $this->getCollection();
  158. $pages = array();
  159. if ($collection->getLastPageNumber() <= $this->_displayPages) {
  160. $pages = range(1, $collection->getLastPageNumber());
  161. }
  162. else {
  163. $half = ceil($this->_displayPages / 2);
  164. if ($collection->getCurPage() >= $half && $collection->getCurPage() <= $collection->getLastPageNumber() - $half) {
  165. $start = ($collection->getCurPage() - $half) + 1;
  166. $finish = ($start + $this->_displayPages) - 1;
  167. }
  168. elseif ($collection->getCurPage() < $half) {
  169. $start = 1;
  170. $finish = $this->_displayPages;
  171. }
  172. elseif ($collection->getCurPage() > ($collection->getLastPageNumber() - $half)) {
  173. $finish = $collection->getLastPageNumber();
  174. $start = $finish - $this->_displayPages + 1;
  175. }
  176. $pages = range($start, $finish);
  177. }
  178. return $pages;
  179. // $pages = array();
  180. // for ($i=$this->getCollection()->getCurPage(-$this->_dispersion); $i <= $this->getCollection()->getCurPage(+($this->_dispersion-1)); $i++)
  181. // {
  182. //
  183. // $pages[] = $i;
  184. // }
  185. //
  186. // return $pages;
  187. }
  188. public function getFirstPageUrl()
  189. {
  190. return $this->getPageUrl(1);
  191. }
  192. public function getPreviousPageUrl()
  193. {
  194. return $this->getPageUrl($this->getCollection()->getCurPage(-1));
  195. }
  196. public function getNextPageUrl()
  197. {
  198. return $this->getPageUrl($this->getCollection()->getCurPage(+1));
  199. }
  200. public function getLastPageUrl()
  201. {
  202. return $this->getPageUrl($this->getCollection()->getLastPageNumber());
  203. }
  204. public function getPageUrl($page)
  205. {
  206. return $this->getPagerUrl(array($this->getPageVarName()=>$page));
  207. }
  208. public function getLimitUrl($limit)
  209. {
  210. return $this->getPagerUrl(array($this->getLimitVarName()=>$limit));
  211. }
  212. public function getPagerUrl($params=array())
  213. {
  214. $urlParams = array();
  215. $urlParams['_current'] = true;
  216. $urlParams['_escape'] = true;
  217. $urlParams['_use_rewrite'] = true;
  218. $urlParams['_query'] = $params;
  219. return $this->getUrl('*/*/*', $urlParams);
  220. }
  221. }