1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33: 34: 35:
36: class extends Mage_Core_Block_Template
37: {
38: protected $_collection = null;
39: protected $_pageVarName = 'p';
40: protected $_limitVarName = 'limit';
41: protected $_availableLimit = array(10=>10,20=>20,50=>50);
42: protected $_dispersion = 3;
43: protected $_displayPages = 5;
44: protected $_showPerPage = true;
45: protected $_limit = null;
46: protected $_outputRequired = true;
47:
48: 49: 50: 51:
52: protected $_frameLength = 5;
53:
54: 55: 56: 57:
58: protected $_jump = 5;
59:
60: 61: 62: 63:
64: protected $_frameInitialized = false;
65:
66: 67: 68: 69:
70: protected $_frameStart;
71:
72: 73: 74: 75:
76: protected $_frameEnd;
77:
78: protected function _construct()
79: {
80: parent::_construct();
81: $this->setData('show_amounts', true);
82: $this->setData('use_container', true);
83: $this->setTemplate('page/html/pager.phtml');
84: }
85:
86: public function getCurrentPage()
87: {
88: if ($page = (int) $this->getRequest()->getParam($this->getPageVarName())) {
89: return $page;
90: }
91: return 1;
92: }
93:
94: public function getLimit()
95: {
96: if ($this->_limit !== null) {
97: return $this->_limit;
98: }
99: $limits = $this->getAvailableLimit();
100: if ($limit = $this->getRequest()->getParam($this->getLimitVarName())) {
101: if (isset($limits[$limit])) {
102: return $limit;
103: }
104: }
105: $limits = array_keys($limits);
106: return $limits[0];
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function setLimit($limit)
116: {
117: $this->_limit = $limit;
118: return $this;
119: }
120:
121: public function setCollection($collection)
122: {
123: $this->_collection = $collection
124: ->setCurPage($this->getCurrentPage());
125:
126: if ((int) $this->getLimit()) {
127: $this->_collection->setPageSize($this->getLimit());
128: }
129:
130: $this->_setFrameInitialized(false);
131:
132: return $this;
133: }
134:
135: 136: 137:
138: public function getCollection()
139: {
140: return $this->_collection;
141: }
142:
143: public function setPageVarName($varName)
144: {
145: $this->_pageVarName = $varName;
146: return $this;
147: }
148:
149: public function getPageVarName()
150: {
151: return $this->_pageVarName;
152: }
153:
154: public function setShowPerPage($varName)
155: {
156: $this->_showPerPage=$varName;
157: return $this;
158: }
159:
160: public function getShowPerPage()
161: {
162: if(sizeof($this->getAvailableLimit())<=1) {
163: return false;
164: }
165: return $this->_showPerPage;
166: }
167:
168: public function setLimitVarName($varName)
169: {
170: $this->_limitVarName = $varName;
171: return $this;
172: }
173:
174: public function getLimitVarName()
175: {
176: return $this->_limitVarName;
177: }
178:
179: public function setAvailableLimit(array $limits)
180: {
181: $this->_availableLimit = $limits;
182: }
183:
184: public function getAvailableLimit()
185: {
186: return $this->_availableLimit;
187: }
188:
189: public function getFirstNum()
190: {
191: $collection = $this->getCollection();
192: return $collection->getPageSize()*($collection->getCurPage()-1)+1;
193: }
194:
195: public function getLastNum()
196: {
197: $collection = $this->getCollection();
198: return $collection->getPageSize()*($collection->getCurPage()-1)+$collection->count();
199: }
200:
201: public function getTotalNum()
202: {
203: return $this->getCollection()->getSize();
204: }
205:
206: public function isFirstPage()
207: {
208: return $this->getCollection()->getCurPage() == 1;
209: }
210:
211: public function getLastPageNum()
212: {
213: return $this->getCollection()->getLastPageNumber();
214: }
215:
216: public function isLastPage()
217: {
218: return $this->getCollection()->getCurPage() >= $this->getLastPageNum();
219: }
220:
221: public function isLimitCurrent($limit)
222: {
223: return $limit == $this->getLimit();
224: }
225:
226: public function isPageCurrent($page)
227: {
228: return $page == $this->getCurrentPage();
229: }
230:
231: public function getPages()
232: {
233: $collection = $this->getCollection();
234:
235: $pages = array();
236: if ($collection->getLastPageNumber() <= $this->_displayPages) {
237: $pages = range(1, $collection->getLastPageNumber());
238: }
239: else {
240: $half = ceil($this->_displayPages / 2);
241: if ($collection->getCurPage() >= $half && $collection->getCurPage() <= $collection->getLastPageNumber() - $half) {
242: $start = ($collection->getCurPage() - $half) + 1;
243: $finish = ($start + $this->_displayPages) - 1;
244: }
245: elseif ($collection->getCurPage() < $half) {
246: $start = 1;
247: $finish = $this->_displayPages;
248: }
249: elseif ($collection->getCurPage() > ($collection->getLastPageNumber() - $half)) {
250: $finish = $collection->getLastPageNumber();
251: $start = $finish - $this->_displayPages + 1;
252: }
253:
254: $pages = range($start, $finish);
255: }
256: return $pages;
257: }
258:
259: public function getFirstPageUrl()
260: {
261: return $this->getPageUrl(1);
262: }
263:
264: public function getPreviousPageUrl()
265: {
266: return $this->getPageUrl($this->getCollection()->getCurPage(-1));
267: }
268:
269: public function getNextPageUrl()
270: {
271: return $this->getPageUrl($this->getCollection()->getCurPage(+1));
272: }
273:
274: public function getLastPageUrl()
275: {
276: return $this->getPageUrl($this->getCollection()->getLastPageNumber());
277: }
278:
279: public function getPageUrl($page)
280: {
281: return $this->getPagerUrl(array($this->getPageVarName()=>$page));
282: }
283:
284: public function getLimitUrl($limit)
285: {
286: return $this->getPagerUrl(array($this->getLimitVarName()=>$limit));
287: }
288:
289: public function ($params=array())
290: {
291: $urlParams = array();
292: $urlParams['_current'] = true;
293: $urlParams['_escape'] = true;
294: $urlParams['_use_rewrite'] = true;
295: $urlParams['_query'] = $params;
296: return $this->getUrl('*/*/*', $urlParams);
297: }
298:
299: 300: 301: 302: 303:
304: public function getFrameStart()
305: {
306: $this->_initFrame();
307: return $this->_frameStart;
308: }
309:
310: 311: 312: 313: 314:
315: public function getFrameEnd()
316: {
317: $this->_initFrame();
318: return $this->_frameEnd;
319: }
320:
321: 322: 323: 324: 325:
326: public function getFramePages()
327: {
328: $start = $this->getFrameStart();
329: $end = $this->getFrameEnd();
330: return range($start, $end);
331: }
332:
333: 334: 335: 336: 337:
338: public function getPreviousJumpPage()
339: {
340: if (!$this->getJump()) {
341: return null;
342: }
343: $frameStart = $this->getFrameStart();
344: if ($frameStart - 1 > 1) {
345: return max(2, $frameStart - $this->getJump());
346: }
347:
348: return null;
349: }
350:
351: 352: 353: 354: 355:
356: public function getPreviousJumpUrl()
357: {
358: return $this->getPageUrl($this->getPreviousJumpPage());
359: }
360:
361: 362: 363: 364: 365:
366: public function getNextJumpPage()
367: {
368: if (!$this->getJump()) {
369: return null;
370: }
371: $frameEnd = $this->getFrameEnd();
372: if ($this->getLastPageNum() - $frameEnd > 1) {
373: return min($this->getLastPageNum() - 1, $frameEnd + $this->getJump());
374: }
375:
376: return null;
377: }
378:
379: 380: 381: 382: 383:
384: public function getNextJumpUrl()
385: {
386: return $this->getPageUrl($this->getNextJumpPage());
387: }
388:
389: 390: 391: 392: 393:
394: public function getFrameLength()
395: {
396: return $this->_frameLength;
397: }
398:
399: 400: 401: 402: 403:
404: public function getJump()
405: {
406: return $this->_jump;
407: }
408:
409: 410: 411: 412: 413: 414:
415: public function setFrameLength($frame)
416: {
417: $frame = abs(intval($frame));
418: if ($frame == 0) {
419: $frame = $this->_frameLength;
420: }
421: if ($this->getFrameLength() != $frame) {
422: $this->_setFrameInitialized(false);
423: $this->_frameLength = $frame;
424: }
425:
426: return $this;
427: }
428:
429: 430: 431: 432: 433: 434:
435: public function setJump($jump)
436: {
437: $jump = abs(intval($jump));
438: if ($this->getJump() != $jump) {
439: $this->_setFrameInitialized(false);
440: $this->_jump = $jump;
441: }
442:
443: return $this;
444: }
445:
446: 447: 448: 449: 450:
451: public function canShowFirst()
452: {
453: return $this->getJump() > 1 && $this->getFrameStart() > 1;
454: }
455:
456: 457: 458: 459: 460:
461: public function canShowLast()
462: {
463: return $this->getJump() > 1 && $this->getFrameEnd() < $this->getLastPageNum();
464: }
465:
466: 467: 468: 469: 470:
471: public function canShowPreviousJump()
472: {
473: return $this->getPreviousJumpPage() !== null;
474: }
475:
476: 477: 478: 479: 480:
481: public function canShowNextJump()
482: {
483: return $this->getNextJumpPage() !== null;
484: }
485:
486: 487: 488: 489: 490:
491: protected function _initFrame()
492: {
493: if (!$this->isFrameInitialized()) {
494: $start = 0;
495: $end = 0;
496:
497: $collection = $this->getCollection();
498: if ($collection->getLastPageNumber() <= $this->getFrameLength()) {
499: $start = 1;
500: $end = $collection->getLastPageNumber();
501: }
502: else {
503: $half = ceil($this->getFrameLength() / 2);
504: if ($collection->getCurPage() >= $half && $collection->getCurPage() <= $collection->getLastPageNumber() - $half) {
505: $start = ($collection->getCurPage() - $half) + 1;
506: $end = ($start + $this->getFrameLength()) - 1;
507: }
508: elseif ($collection->getCurPage() < $half) {
509: $start = 1;
510: $end = $this->getFrameLength();
511: }
512: elseif ($collection->getCurPage() > ($collection->getLastPageNumber() - $half)) {
513: $end = $collection->getLastPageNumber();
514: $start = $end - $this->getFrameLength() + 1;
515: }
516: }
517: $this->_frameStart = $start;
518: $this->_frameEnd = $end;
519:
520: $this->_setFrameInitialized(true);
521: }
522:
523: return $this;
524: }
525:
526: 527: 528: 529: 530: 531:
532: protected function _setFrameInitialized($flag)
533: {
534: $this->_frameInitialized = (bool)$flag;
535: return $this;
536: }
537:
538: 539: 540: 541: 542:
543: public function isFrameInitialized()
544: {
545: return $this->_frameInitialized;
546: }
547:
548: 549: 550: 551: 552:
553: public function getAnchorTextForPrevious()
554: {
555: return Mage::getStoreConfig('design/pagination/anchor_text_for_previous');
556: }
557:
558: 559: 560: 561: 562:
563: public function getAnchorTextForNext()
564: {
565: return Mage::getStoreConfig('design/pagination/anchor_text_for_next');
566: }
567:
568: 569: 570: 571: 572: 573:
574: public function setIsOutputRequired($isRequired)
575: {
576: $this->_outputRequired = (bool)$isRequired;
577: return $this;
578: }
579:
580: 581: 582: 583: 584:
585: protected function _toHtml()
586: {
587: if ($this->_outputRequired || $this->getTotalNum() > $this->getLimit()) {
588: return parent::_toHtml();
589: }
590: return '';
591: }
592: }
593: