00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 class Mage_Page_Block_Html_Pager extends Mage_Core_Block_Template
00037 {
00038 protected $_collection = null;
00039 protected $_pageVarName = 'p';
00040 protected $_limitVarName = 'limit';
00041 protected $_availableLimit = array(10=>10,20=>20,50=>50);
00042 protected $_dispersion = 3;
00043 protected $_displayPages = 5;
00044 protected $_showPerPage = true;
00045
00046 protected function _construct()
00047 {
00048 parent::_construct();
00049 $this->setTemplate('page/html/pager.phtml');
00050 }
00051
00052 public function getCurrentPage()
00053 {
00054 if ($page = (int) $this->getRequest()->getParam($this->getPageVarName())) {
00055 return $page;
00056 }
00057 return 1;
00058 }
00059
00060 public function getLimit()
00061 {
00062 $limits = $this->getAvailableLimit();
00063 if ($limit = $this->getRequest()->getParam($this->getLimitVarName())) {
00064 if (isset($limits[$limit])) {
00065 return $limit;
00066 }
00067 }
00068 $limits = array_keys($limits);
00069 return $limits[0];
00070 }
00071
00072 public function setCollection($collection)
00073 {
00074 $this->_collection = $collection
00075 ->setCurPage($this->getCurrentPage());
00076
00077 if ((int) $this->getLimit()) {
00078 $this->_collection->setPageSize($this->getLimit());
00079 }
00080
00081 return $this;
00082 }
00083
00084
00085
00086
00087 public function getCollection()
00088 {
00089 return $this->_collection;
00090 }
00091
00092 public function setPageVarName($varName)
00093 {
00094 $this->_pageVarName = $varName;
00095 return $this;
00096 }
00097
00098 public function getPageVarName()
00099 {
00100 return $this->_pageVarName;
00101 }
00102
00103 public function setShowPerPage($varName)
00104 {
00105 $this->_showPerPage=$varName;
00106 }
00107
00108 public function getShowPerPage()
00109 {
00110 if(sizeof($this->getAvailableLimit())<=1) {
00111 return false;
00112 }
00113 return $this->_showPerPage;
00114 }
00115
00116 public function setLimitVarName($varName)
00117 {
00118 $this->_limitVarName = $varName;
00119 return $this;
00120 }
00121
00122 public function getLimitVarName()
00123 {
00124 return $this->_limitVarName;
00125 }
00126
00127 public function setAvailableLimit(array $limits)
00128 {
00129 $this->_availableLimit = $limits;
00130 }
00131
00132 public function getAvailableLimit()
00133 {
00134 return $this->_availableLimit;
00135 }
00136
00137 public function getFirstNum()
00138 {
00139 $collection = $this->getCollection();
00140 return $collection->getPageSize()*($collection->getCurPage()-1)+1;
00141 }
00142
00143 public function getLastNum()
00144 {
00145 $collection = $this->getCollection();
00146 return $collection->getPageSize()*($collection->getCurPage()-1)+$collection->count();
00147 }
00148
00149 public function getTotalNum()
00150 {
00151 return $this->getCollection()->getSize();
00152 }
00153
00154 public function isFirstPage()
00155 {
00156 return $this->getCollection()->getCurPage() == 1;
00157 }
00158
00159 public function getLastPageNum()
00160 {
00161 return $this->getCollection()->getLastPageNumber();
00162 }
00163
00164 public function isLastPage()
00165 {
00166 return $this->getCollection()->getCurPage() >= $this->getLastPageNum();
00167 }
00168
00169 public function isLimitCurrent($limit)
00170 {
00171 return $limit == $this->getLimit();
00172 }
00173
00174 public function isPageCurrent($page)
00175 {
00176 return $page == $this->getCurrentPage();
00177 }
00178
00179 public function getPages()
00180 {
00181 $collection = $this->getCollection();
00182
00183 $pages = array();
00184 if ($collection->getLastPageNumber() <= $this->_displayPages) {
00185 $pages = range(1, $collection->getLastPageNumber());
00186 }
00187 else {
00188 $half = ceil($this->_displayPages / 2);
00189 if ($collection->getCurPage() >= $half && $collection->getCurPage() <= $collection->getLastPageNumber() - $half) {
00190 $start = ($collection->getCurPage() - $half) + 1;
00191 $finish = ($start + $this->_displayPages) - 1;
00192 }
00193 elseif ($collection->getCurPage() < $half) {
00194 $start = 1;
00195 $finish = $this->_displayPages;
00196 }
00197 elseif ($collection->getCurPage() > ($collection->getLastPageNumber() - $half)) {
00198 $finish = $collection->getLastPageNumber();
00199 $start = $finish - $this->_displayPages + 1;
00200 }
00201
00202 $pages = range($start, $finish);
00203 }
00204 return $pages;
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 }
00215
00216 public function getFirstPageUrl()
00217 {
00218 return $this->getPageUrl(1);
00219 }
00220
00221 public function getPreviousPageUrl()
00222 {
00223 return $this->getPageUrl($this->getCollection()->getCurPage(-1));
00224 }
00225
00226 public function getNextPageUrl()
00227 {
00228 return $this->getPageUrl($this->getCollection()->getCurPage(+1));
00229 }
00230
00231 public function getLastPageUrl()
00232 {
00233 return $this->getPageUrl($this->getCollection()->getLastPageNumber());
00234 }
00235
00236 public function getPageUrl($page)
00237 {
00238 return $this->getPagerUrl(array($this->getPageVarName()=>$page));
00239 }
00240
00241 public function getLimitUrl($limit)
00242 {
00243 return $this->getPagerUrl(array($this->getLimitVarName()=>$limit));
00244 }
00245
00246 public function getPagerUrl($params=array())
00247 {
00248 $urlParams = array();
00249 $urlParams['_current'] = true;
00250 $urlParams['_escape'] = true;
00251 $urlParams['_use_rewrite'] = true;
00252 $urlParams['_query'] = $params;
00253 return $this->getUrl('*/*/*', $urlParams);
00254 }
00255 }
00256