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: class Mage_Catalog_Block_Product_List_Toolbar extends Mage_Core_Block_Template
36: {
37: 38: 39: 40: 41:
42: protected $_collection = null;
43:
44: 45: 46: 47: 48:
49: protected $_pageVarName = 'p';
50:
51: 52: 53: 54: 55:
56: protected $_orderVarName = 'order';
57:
58: 59: 60: 61: 62:
63: protected $_directionVarName = 'dir';
64:
65: 66: 67: 68: 69:
70: protected $_modeVarName = 'mode';
71:
72: 73: 74: 75: 76:
77: protected $_limitVarName = 'limit';
78:
79: 80: 81: 82: 83:
84: protected $_availableOrder = array();
85:
86: 87: 88: 89: 90:
91: protected $_availableMode = array();
92:
93: 94: 95: 96: 97:
98: protected $_enableViewSwitcher = true;
99:
100: 101: 102: 103: 104:
105: protected $_isExpanded = true;
106:
107: 108: 109: 110: 111:
112: protected $_orderField = null;
113:
114: 115: 116: 117: 118:
119: protected $_direction = 'asc';
120:
121: 122: 123: 124: 125:
126: protected $_viewMode = null;
127:
128: 129: 130: 131: 132:
133: protected $_availableLimit = array();
134:
135: 136: 137: 138: 139:
140: protected $_defaultAvailableLimit = array(10=>10,20=>20,50=>50);
141:
142: 143: 144:
145: protected $_paramsMemorizeAllowed = true;
146:
147: 148: 149: 150: 151:
152: protected function _getConfig()
153: {
154: return Mage::getSingleton('catalog/config');
155: }
156:
157: 158: 159: 160:
161: protected function _construct()
162: {
163: parent::_construct();
164: $this->_orderField = Mage::getStoreConfig(
165: Mage_Catalog_Model_Config::XML_PATH_LIST_DEFAULT_SORT_BY
166: );
167:
168: $this->_availableOrder = $this->_getConfig()->getAttributeUsedForSortByArray();
169:
170: switch (Mage::getStoreConfig('catalog/frontend/list_mode')) {
171: case 'grid':
172: $this->_availableMode = array('grid' => $this->__('Grid'));
173: break;
174:
175: case 'list':
176: $this->_availableMode = array('list' => $this->__('List'));
177: break;
178:
179: case 'grid-list':
180: $this->_availableMode = array('grid' => $this->__('Grid'), 'list' => $this->__('List'));
181: break;
182:
183: case 'list-grid':
184: $this->_availableMode = array('list' => $this->__('List'), 'grid' => $this->__('Grid'));
185: break;
186: }
187: $this->setTemplate('catalog/product/list/toolbar.phtml');
188: }
189:
190: 191: 192:
193: public function disableParamsMemorizing()
194: {
195: $this->_paramsMemorizeAllowed = false;
196: return $this;
197: }
198:
199: 200: 201: 202: 203: 204: 205:
206: protected function _memorizeParam($param, $value)
207: {
208: $session = Mage::getSingleton('catalog/session');
209: if ($this->_paramsMemorizeAllowed && !$session->getParamsMemorizeDisabled()) {
210: $session->setData($param, $value);
211: }
212: return $this;
213: }
214:
215: 216: 217: 218: 219: 220:
221: public function setCollection($collection)
222: {
223: $this->_collection = $collection;
224:
225: $this->_collection->setCurPage($this->getCurrentPage());
226:
227:
228: $limit = (int)$this->getLimit();
229: if ($limit) {
230: $this->_collection->setPageSize($limit);
231: }
232: if ($this->getCurrentOrder()) {
233: $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
234: }
235: return $this;
236: }
237:
238: 239: 240: 241: 242:
243: public function getCollection()
244: {
245: return $this->_collection;
246: }
247:
248: 249: 250: 251: 252:
253: public function getPageVarName()
254: {
255: return $this->_pageVarName;
256: }
257:
258: 259: 260: 261: 262:
263: public function getOrderVarName()
264: {
265: return $this->_orderVarName;
266: }
267:
268: 269: 270: 271: 272:
273: public function getDirectionVarName()
274: {
275: return $this->_directionVarName;
276: }
277:
278: 279: 280: 281: 282:
283: public function getModeVarName()
284: {
285: return $this->_modeVarName;
286: }
287:
288: 289: 290: 291: 292:
293: public function getLimitVarName()
294: {
295: return $this->_limitVarName;
296: }
297:
298: 299: 300: 301: 302:
303: public function getCurrentPage()
304: {
305: if ($page = (int) $this->getRequest()->getParam($this->getPageVarName())) {
306: return $page;
307: }
308: return 1;
309: }
310:
311: 312: 313: 314: 315:
316: public function getCurrentOrder()
317: {
318: $order = $this->_getData('_current_grid_order');
319: if ($order) {
320: return $order;
321: }
322:
323: $orders = $this->getAvailableOrders();
324: $defaultOrder = $this->_orderField;
325:
326: if (!isset($orders[$defaultOrder])) {
327: $keys = array_keys($orders);
328: $defaultOrder = $keys[0];
329: }
330:
331: $order = $this->getRequest()->getParam($this->getOrderVarName());
332: if ($order && isset($orders[$order])) {
333: if ($order == $defaultOrder) {
334: Mage::getSingleton('catalog/session')->unsSortOrder();
335: } else {
336: $this->_memorizeParam('sort_order', $order);
337: }
338: } else {
339: $order = Mage::getSingleton('catalog/session')->getSortOrder();
340: }
341:
342: if (!$order || !isset($orders[$order])) {
343: $order = $defaultOrder;
344: }
345: $this->setData('_current_grid_order', $order);
346: return $order;
347: }
348:
349: 350: 351: 352: 353:
354: public function getCurrentDirection()
355: {
356: $dir = $this->_getData('_current_grid_direction');
357: if ($dir) {
358: return $dir;
359: }
360:
361: $directions = array('asc', 'desc');
362: $dir = strtolower($this->getRequest()->getParam($this->getDirectionVarName()));
363: if ($dir && in_array($dir, $directions)) {
364: if ($dir == $this->_direction) {
365: Mage::getSingleton('catalog/session')->unsSortDirection();
366: } else {
367: $this->_memorizeParam('sort_direction', $dir);
368: }
369: } else {
370: $dir = Mage::getSingleton('catalog/session')->getSortDirection();
371: }
372:
373: if (!$dir || !in_array($dir, $directions)) {
374: $dir = $this->_direction;
375: }
376: $this->setData('_current_grid_direction', $dir);
377: return $dir;
378: }
379:
380: 381: 382: 383: 384: 385:
386: public function setDefaultOrder($field)
387: {
388: if (isset($this->_availableOrder[$field])) {
389: $this->_orderField = $field;
390: }
391: return $this;
392: }
393:
394: 395: 396: 397: 398: 399:
400: public function setDefaultDirection($dir)
401: {
402: if (in_array(strtolower($dir), array('asc', 'desc'))) {
403: $this->_direction = strtolower($dir);
404: }
405: return $this;
406: }
407:
408: 409: 410: 411: 412:
413: public function getAvailableOrders()
414: {
415: return $this->_availableOrder;
416: }
417:
418: 419: 420: 421: 422: 423:
424: public function setAvailableOrders($orders)
425: {
426: $this->_availableOrder = $orders;
427: return $this;
428: }
429:
430: 431: 432: 433: 434: 435: 436:
437: public function addOrderToAvailableOrders($order, $value)
438: {
439: $this->_availableOrder[$order] = $value;
440: return $this;
441: }
442: 443: 444: 445: 446: 447:
448: public function removeOrderFromAvailableOrders($order)
449: {
450: if (isset($this->_availableOrder[$order])) {
451: unset($this->_availableOrder[$order]);
452: }
453: return $this;
454: }
455:
456: 457: 458: 459: 460: 461:
462: public function isOrderCurrent($order)
463: {
464: return ($order == $this->getCurrentOrder());
465: }
466:
467: 468: 469: 470: 471: 472: 473:
474: public function getOrderUrl($order, $direction)
475: {
476: if (is_null($order)) {
477: $order = $this->getCurrentOrder() ? $this->getCurrentOrder() : $this->_availableOrder[0];
478: }
479: return $this->getPagerUrl(array(
480: $this->getOrderVarName()=>$order,
481: $this->getDirectionVarName()=>$direction,
482: $this->getPageVarName() => null
483: ));
484: }
485:
486: 487: 488: 489: 490: 491:
492: public function ($params=array())
493: {
494: $urlParams = array();
495: $urlParams['_current'] = true;
496: $urlParams['_escape'] = true;
497: $urlParams['_use_rewrite'] = true;
498: $urlParams['_query'] = $params;
499: return $this->getUrl('*/*/*', $urlParams);
500: }
501:
502: 503: 504: 505: 506:
507: public function getCurrentMode()
508: {
509: $mode = $this->_getData('_current_grid_mode');
510: if ($mode) {
511: return $mode;
512: }
513: $modes = array_keys($this->_availableMode);
514: $defaultMode = current($modes);
515: $mode = $this->getRequest()->getParam($this->getModeVarName());
516: if ($mode) {
517: if ($mode == $defaultMode) {
518: Mage::getSingleton('catalog/session')->unsDisplayMode();
519: } else {
520: $this->_memorizeParam('display_mode', $mode);
521: }
522: } else {
523: $mode = Mage::getSingleton('catalog/session')->getDisplayMode();
524: }
525:
526: if (!$mode || !isset($this->_availableMode[$mode])) {
527: $mode = $defaultMode;
528: }
529: $this->setData('_current_grid_mode', $mode);
530: return $mode;
531: }
532:
533: 534: 535: 536: 537: 538:
539: public function isModeActive($mode)
540: {
541: return $this->getCurrentMode() == $mode;
542: }
543:
544: 545: 546: 547: 548:
549: public function getModes()
550: {
551: return $this->_availableMode;
552: }
553:
554: 555: 556: 557: 558: 559:
560: public function setModes($modes)
561: {
562: if(!isset($this->_availableMode)){
563: $this->_availableMode = $modes;
564: }
565: return $this;
566: }
567:
568: 569: 570: 571: 572: 573:
574: public function getModeUrl($mode)
575: {
576: return $this->getPagerUrl( array($this->getModeVarName()=>$mode, $this->getPageVarName() => null) );
577: }
578:
579: 580: 581: 582: 583:
584: public function disableViewSwitcher()
585: {
586: $this->_enableViewSwitcher = false;
587: return $this;
588: }
589:
590: 591: 592: 593: 594:
595: public function enableViewSwitcher()
596: {
597: $this->_enableViewSwitcher = true;
598: return $this;
599: }
600:
601: 602: 603: 604: 605:
606: public function isEnabledViewSwitcher()
607: {
608: return $this->_enableViewSwitcher;
609: }
610:
611: 612: 613: 614: 615:
616: public function disableExpanded()
617: {
618: $this->_isExpanded = false;
619: return $this;
620: }
621:
622: 623: 624: 625: 626:
627: public function enableExpanded()
628: {
629: $this->_isExpanded = true;
630: return $this;
631: }
632:
633: 634: 635: 636: 637:
638: public function isExpanded()
639: {
640: return $this->_isExpanded;
641: }
642:
643: 644: 645: 646: 647:
648: public function getDefaultPerPageValue()
649: {
650: if ($this->getCurrentMode() == 'list') {
651: if ($default = $this->getDefaultListPerPage()) {
652: return $default;
653: }
654: return Mage::getStoreConfig('catalog/frontend/list_per_page');
655: }
656: elseif ($this->getCurrentMode() == 'grid') {
657: if ($default = $this->getDefaultGridPerPage()) {
658: return $default;
659: }
660: return Mage::getStoreConfig('catalog/frontend/grid_per_page');
661: }
662: return 0;
663: }
664:
665: 666: 667: 668: 669: 670: 671: 672:
673: public function ($mode, $value, $label='')
674: {
675: if (!isset($this->_availableLimit[$mode])) {
676: $this->_availableLimit[$mode] = array();
677: }
678: $this->_availableLimit[$mode][$value] = empty($label) ? $value : $label;
679: return $this;
680: }
681:
682: 683: 684: 685: 686:
687: public function getAvailableLimit()
688: {
689: $currentMode = $this->getCurrentMode();
690: if (in_array($currentMode, array('list', 'grid'))) {
691: return $this->_getAvailableLimit($currentMode);
692: } else {
693: return $this->_defaultAvailableLimit;
694: }
695: }
696:
697: 698: 699: 700: 701:
702: protected function _getAvailableLimit($mode)
703: {
704: if (isset($this->_availableLimit[$mode])) {
705: return $this->_availableLimit[$mode];
706: }
707: $perPageConfigKey = 'catalog/frontend/' . $mode . '_per_page_values';
708: $perPageValues = (string)Mage::getStoreConfig($perPageConfigKey);
709: $perPageValues = explode(',', $perPageValues);
710: $perPageValues = array_combine($perPageValues, $perPageValues);
711: if (Mage::getStoreConfigFlag('catalog/frontend/list_allow_all')) {
712: return ($perPageValues + array('all'=>$this->__('All')));
713: } else {
714: return $perPageValues;
715: }
716: }
717:
718: 719: 720: 721: 722:
723: public function getLimit()
724: {
725: $limit = $this->_getData('_current_limit');
726: if ($limit) {
727: return $limit;
728: }
729:
730: $limits = $this->getAvailableLimit();
731: $defaultLimit = $this->getDefaultPerPageValue();
732: if (!$defaultLimit || !isset($limits[$defaultLimit])) {
733: $keys = array_keys($limits);
734: $defaultLimit = $keys[0];
735: }
736:
737: $limit = $this->getRequest()->getParam($this->getLimitVarName());
738: if ($limit && isset($limits[$limit])) {
739: if ($limit == $defaultLimit) {
740: Mage::getSingleton('catalog/session')->unsLimitPage();
741: } else {
742: $this->_memorizeParam('limit_page', $limit);
743: }
744: } else {
745: $limit = Mage::getSingleton('catalog/session')->getLimitPage();
746: }
747: if (!$limit || !isset($limits[$limit])) {
748: $limit = $defaultLimit;
749: }
750:
751: $this->setData('_current_limit', $limit);
752: return $limit;
753: }
754:
755: 756: 757: 758: 759: 760:
761: public function getLimitUrl($limit)
762: {
763: return $this->getPagerUrl(array(
764: $this->getLimitVarName() => $limit,
765: $this->getPageVarName() => null
766: ));
767: }
768:
769: public function isLimitCurrent($limit)
770: {
771: return $limit == $this->getLimit();
772: }
773:
774: public function getFirstNum()
775: {
776: $collection = $this->getCollection();
777: return $collection->getPageSize()*($collection->getCurPage()-1)+1;
778: }
779:
780: public function getLastNum()
781: {
782: $collection = $this->getCollection();
783: return $collection->getPageSize()*($collection->getCurPage()-1)+$collection->count();
784: }
785:
786: public function getTotalNum()
787: {
788: return $this->getCollection()->getSize();
789: }
790:
791: public function isFirstPage()
792: {
793: return $this->getCollection()->getCurPage() == 1;
794: }
795:
796: public function getLastPageNum()
797: {
798: return $this->getCollection()->getLastPageNumber();
799: }
800:
801: 802: 803: 804: 805:
806: public function ()
807: {
808: $pagerBlock = $this->getChild('product_list_toolbar_pager');
809:
810: if ($pagerBlock instanceof Varien_Object) {
811:
812:
813: $pagerBlock->setAvailableLimit($this->getAvailableLimit());
814:
815: $pagerBlock->setUseContainer(false)
816: ->setShowPerPage(false)
817: ->setShowAmounts(false)
818: ->setLimitVarName($this->getLimitVarName())
819: ->setPageVarName($this->getPageVarName())
820: ->setLimit($this->getLimit())
821: ->setFrameLength(Mage::getStoreConfig('design/pagination/pagination_frame'))
822: ->setJump(Mage::getStoreConfig('design/pagination/pagination_frame_skip'))
823: ->setCollection($this->getCollection());
824:
825: return $pagerBlock->toHtml();
826: }
827:
828: return '';
829: }
830: }
831: