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 class Mage_Catalog_Block_Product_List_Toolbar extends Mage_Page_Block_Html_Pager
00035 {
00036
00037
00038
00039
00040
00041 protected $_orderVarName = 'order';
00042
00043
00044
00045
00046
00047
00048 protected $_directionVarName = 'dir';
00049
00050
00051
00052
00053
00054
00055 protected $_modeVarName = 'mode';
00056
00057
00058
00059
00060
00061
00062 protected $_availableOrder = array();
00063
00064
00065
00066
00067
00068
00069 protected $_availableMode = array();
00070
00071
00072
00073
00074
00075
00076 protected $_enableViewSwitcher = true;
00077
00078
00079
00080
00081
00082
00083 protected $_isExpanded = true;
00084
00085
00086
00087
00088
00089
00090 protected $_orderField = null;
00091
00092
00093
00094
00095
00096
00097 protected $_direction = 'asc';
00098
00099
00100
00101
00102
00103
00104 protected $_viewMode = null;
00105
00106
00107
00108
00109
00110
00111 protected function _getConfig()
00112 {
00113 return Mage::getSingleton('catalog/config');
00114 }
00115
00116
00117
00118
00119
00120 protected function _construct()
00121 {
00122 parent::_construct();
00123 $this->_orderField = Mage::getStoreConfig(
00124 Mage_Catalog_Model_Config::XML_PATH_LIST_DEFAULT_SORT_BY
00125 );
00126
00127 $this->_availableOrder = $this->_getConfig()->getAttributeUsedForSortByArray();
00128
00129 switch (Mage::getStoreConfig('catalog/frontend/list_mode')) {
00130 case 'grid':
00131 $this->_availableMode = array('grid' => $this->__('Grid'));
00132 break;
00133
00134 case 'list':
00135 $this->_availableMode = array('list' => $this->__('List'));
00136 break;
00137
00138 case 'grid-list':
00139 $this->_availableMode = array('grid' => $this->__('Grid'), 'list' => $this->__('List'));
00140 break;
00141
00142 case 'list-grid':
00143 $this->_availableMode = array('list' => $this->__('List'), 'grid' => $this->__('Grid'));
00144 break;
00145 }
00146 $this->setTemplate('catalog/product/list/toolbar.phtml');
00147 }
00148
00149
00150
00151
00152
00153
00154
00155 public function setCollection($collection)
00156 {
00157 parent::setCollection($collection);
00158 if ($this->getCurrentOrder()) {
00159 $this->getCollection()->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
00160 }
00161 return $this;
00162 }
00163
00164
00165
00166
00167
00168
00169 public function getOrderVarName()
00170 {
00171 return $this->_orderVarName;
00172 }
00173
00174
00175
00176
00177
00178
00179 public function getDirectionVarName()
00180 {
00181 return $this->_directionVarName;
00182 }
00183
00184
00185
00186
00187
00188
00189 public function getModeVarName()
00190 {
00191 return $this->_modeVarName;
00192 }
00193
00194
00195
00196
00197
00198
00199 public function getCurrentOrder()
00200 {
00201 $order = $this->getRequest()->getParam($this->getOrderVarName());
00202 $orders = $this->getAvailableOrders();
00203 if ($order && isset($orders[$order])) {
00204 return $order;
00205 }
00206 if ($this->_orderField && isset($orders[$this->_orderField])) {
00207 return $this->_orderField;
00208 }
00209 $keys = array_keys($orders);
00210 return $keys[0];
00211 }
00212
00213
00214
00215
00216
00217
00218 public function getCurrentDirection()
00219 {
00220 if ($dir = (string) $this->getRequest()->getParam($this->getDirectionVarName())) {
00221 $dir = strtolower($dir);
00222 if (in_array($dir, array('asc', 'desc'))) {
00223 return $dir;
00224 }
00225 }
00226 return $this->_direction;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235 public function setDefaultOrder($field)
00236 {
00237 if (isset($this->_availableOrder[$field])) {
00238 $this->_orderField = $field;
00239 }
00240 return $this;
00241 }
00242
00243
00244
00245
00246
00247
00248
00249 public function setDefaultDirection($dir)
00250 {
00251 if (in_array(strtolower($dir), array('asc', 'desc'))) {
00252 $this->_direction = strtolower($dir);
00253 }
00254 return $this;
00255 }
00256
00257
00258
00259
00260
00261
00262 public function getAvailableOrders()
00263 {
00264 return $this->_availableOrder;
00265 }
00266
00267
00268
00269
00270
00271
00272
00273 public function setAvailableOrders($orders)
00274 {
00275 $this->_availableOrder = $orders;
00276 return $this;
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286 public function addOrderToAvailableOrders($order, $value)
00287 {
00288 $this->_availableOrder[$order] = $value;
00289 return $this;
00290 }
00291
00292
00293
00294
00295
00296
00297 public function removeOrderFromAvailableOrders($order)
00298 {
00299 if (isset($this->_availableOrder[$order])) {
00300 unset($this->_availableOrder[$order]);
00301 }
00302 return $this;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 public function isOrderCurrent($order)
00312 {
00313 return ($order == $this->getCurrentOrder());
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323 public function getOrderUrl($order, $direction)
00324 {
00325 if (is_null($order)) {
00326 $order = $this->getCurrentOrder() ? $this->getCurrentOrder() : $this->_availableOrder[0];
00327 }
00328 return $this->getPagerUrl(array(
00329 $this->getOrderVarName()=>$order,
00330 $this->getDirectionVarName()=>$direction,
00331 $this->getPageVarName() => null
00332 ));
00333 }
00334
00335
00336
00337
00338
00339
00340 public function getCurrentMode()
00341 {
00342 $mode = $this->getRequest()->getParam($this->getModeVarName());
00343 if ($mode) {
00344 Mage::getSingleton('catalog/session')->setDisplayMode($mode);
00345 }
00346 else {
00347 $mode = Mage::getSingleton('catalog/session')->getDisplayMode();
00348 }
00349
00350 if ($mode && isset($this->_availableMode[$mode])) {
00351 return $mode;
00352 }
00353 return current(array_keys($this->_availableMode));
00354 }
00355
00356
00357
00358
00359
00360
00361
00362 public function isModeActive($mode)
00363 {
00364 return $this->getCurrentMode() == $mode;
00365 }
00366
00367
00368
00369
00370
00371
00372 public function getModes()
00373 {
00374 return $this->_availableMode;
00375 }
00376
00377
00378
00379
00380
00381
00382
00383 public function setModes($modes)
00384 {
00385 if(!isset($this->_availableMode)){
00386 $this->_availableMode = $modes;
00387 }
00388 return $this;
00389 }
00390
00391
00392
00393
00394
00395
00396
00397 public function getModeUrl($mode)
00398 {
00399 return $this->getPagerUrl( array($this->getModeVarName()=>$mode, $this->getPageVarName() => null) );
00400 }
00401
00402
00403
00404
00405
00406
00407 public function disableViewSwitcher()
00408 {
00409 $this->_enableViewSwitcher = false;
00410 return $this;
00411 }
00412
00413
00414
00415
00416
00417
00418 public function enableViewSwitcher()
00419 {
00420 $this->_enableViewSwitcher = true;
00421 return $this;
00422 }
00423
00424
00425
00426
00427
00428
00429 public function isEnabledViewSwitcher()
00430 {
00431 return $this->_enableViewSwitcher;
00432 }
00433
00434
00435
00436
00437
00438
00439 public function disableExpanded()
00440 {
00441 $this->_isExpanded = false;
00442 return $this;
00443 }
00444
00445
00446
00447
00448
00449
00450 public function enableExpanded()
00451 {
00452 $this->_isExpanded = true;
00453 return $this;
00454 }
00455
00456
00457
00458
00459
00460
00461 public function isExpanded()
00462 {
00463 return $this->_isExpanded;
00464 }
00465
00466
00467
00468
00469
00470
00471 public function getDefaultPerPageValue()
00472 {
00473 if ($this->getCurrentMode() == 'list') {
00474 if ($default = $this->getDefaultListPerPage()) {
00475 return $default;
00476 }
00477 return Mage::getStoreConfig('catalog/frontend/list_per_page');
00478 }
00479 elseif ($this->getCurrentMode() == 'grid') {
00480 if ($default = $this->getDefaultGridPerPage()) {
00481 return $default;
00482 }
00483 return Mage::getStoreConfig('catalog/frontend/grid_per_page');
00484 }
00485 return 0;
00486 }
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 public function addPagerLimit($mode, $value, $label='')
00497 {
00498 if (!isset($this->_availableLimit[$mode])) {
00499 $this->_availableLimit[$mode] = array();
00500 }
00501 $this->_availableLimit[$mode][$value] = empty($label) ? $value : $label;
00502 return $this;
00503 }
00504
00505
00506
00507
00508
00509
00510 public function getAvailableLimit()
00511 {
00512 if ($this->getCurrentMode() == 'list') {
00513 if (isset($this->_availableLimit['list'])) {
00514 return $this->_availableLimit['list'];
00515 }
00516 $perPageValues = (string)Mage::getStoreConfig('catalog/frontend/list_per_page_values');
00517 $perPageValues = explode(',', $perPageValues);
00518 $perPageValues = array_combine($perPageValues, $perPageValues);
00519 return ($perPageValues + array('all'=>$this->__('All')));
00520 }
00521 elseif ($this->getCurrentMode() == 'grid') {
00522 if (isset($this->_availableLimit['grid'])) {
00523 return $this->_availableLimit['grid'];
00524 }
00525 $perPageValues = (string)Mage::getStoreConfig('catalog/frontend/grid_per_page_values');
00526 $perPageValues = explode(',', $perPageValues);
00527 $perPageValues = array_combine($perPageValues, $perPageValues);
00528 return ($perPageValues + array('all'=>$this->__('All')));
00529 }
00530 return parent::getAvailableLimit();
00531 }
00532
00533
00534
00535
00536
00537
00538 public function getLimit()
00539 {
00540 $limits = $this->getAvailableLimit();
00541 $limit = $this->getRequest()->getParam($this->getLimitVarName());
00542
00543 if ($limit && isset($limits[$limit])) {
00544 Mage::getSingleton('catalog/session')->setLimitPage($limit);
00545 } else {
00546 $limit = Mage::getSingleton('catalog/session')->getLimitPage();
00547 }
00548 if (isset($limits[$limit])) {
00549 return $limit;
00550 }
00551 if ($limit = $this->getDefaultPerPageValue()) {
00552 if (isset($limits[$limit])) {
00553 return $limit;
00554 }
00555 }
00556
00557 $limits = array_keys($limits);
00558 return $limits[0];
00559 }
00560
00561
00562
00563
00564
00565
00566
00567 public function getLimitUrl($limit)
00568 {
00569 return $this->getPagerUrl(array(
00570 $this->getLimitVarName() => $limit,
00571 $this->getPageVarName() => null
00572 ));
00573 }
00574 }