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 class Mage_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_defaultToolbarBlock = 'catalog/product_list_toolbar';
00043
00044
00045
00046
00047
00048
00049 protected $_productCollection;
00050
00051
00052
00053
00054
00055
00056 protected $_defaultColumnCount = 3;
00057
00058
00059
00060
00061
00062
00063
00064 protected $_columnCountLayoutDepend = array();
00065
00066
00067
00068
00069
00070
00071
00072 protected function _getProductCollection()
00073 {
00074 if (is_null($this->_productCollection)) {
00075 $layer = Mage::getSingleton('catalog/layer');
00076
00077 if ($this->getShowRootCategory()) {
00078 $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
00079 }
00080
00081
00082 if (Mage::registry('product')) {
00083
00084 $categories = Mage::registry('product')->getCategoryCollection()
00085 ->setPage(1, 1)
00086 ->load();
00087
00088 if ($categories->count()) {
00089
00090 $this->setCategoryId(current($categories->getIterator()));
00091 }
00092 }
00093
00094 $origCategory = null;
00095 if ($this->getCategoryId()) {
00096 $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
00097 if ($category->getId()) {
00098 $origCategory = $layer->getCurrentCategory();
00099 $layer->setCurrentCategory($category);
00100 }
00101 }
00102 $this->_productCollection = $layer->getProductCollection();
00103
00104 $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
00105
00106 if ($origCategory) {
00107 $layer->setCurrentCategory($origCategory);
00108 }
00109 }
00110 return $this->_productCollection;
00111 }
00112
00113
00114
00115
00116
00117
00118 public function getLoadedProductCollection()
00119 {
00120 return $this->_getProductCollection();
00121 }
00122
00123
00124
00125
00126
00127
00128 public function getMode()
00129 {
00130 return $this->getChild('toolbar')->getCurrentMode();
00131 }
00132
00133
00134
00135
00136
00137 protected function _beforeToHtml()
00138 {
00139
00140
00141
00142
00143 $toolbar = $this->getToolbarBlock();
00144
00145
00146 $collection = $this->_getProductCollection();
00147
00148
00149 if ($orders = $this->getAvailableOrders()) {
00150 $toolbar->setAvailableOrders($orders);
00151 }
00152 if ($sort = $this->getSortBy()) {
00153 $toolbar->setDefaultOrder($sort);
00154 }
00155 if ($modes = $this->getModes()) {
00156 $toolbar->setModes($modes);
00157 }
00158
00159
00160 $toolbar->setCollection($collection);
00161
00162 $this->setChild('toolbar', $toolbar);
00163 Mage::dispatchEvent('catalog_block_product_list_collection', array(
00164 'collection'=>$this->_getProductCollection(),
00165 ));
00166
00167 $this->_getProductCollection()->load();
00168 Mage::getModel('review/review')->appendSummary($this->_getProductCollection());
00169 return parent::_beforeToHtml();
00170 }
00171
00172
00173
00174
00175
00176
00177 public function getToolbarBlock()
00178 {
00179 if ($blockName = $this->getToolbarBlockName()) {
00180 if ($block = $this->getLayout()->getBlock($blockName)) {
00181 return $block;
00182 }
00183 }
00184 $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
00185 return $block;
00186 }
00187
00188
00189
00190
00191
00192
00193 public function getToolbarHtml()
00194 {
00195 return $this->getChildHtml('toolbar');
00196 }
00197
00198 public function setCollection($collection)
00199 {
00200 $this->_productCollection = $collection;
00201 return $this;
00202 }
00203
00204 public function addAttribute($code)
00205 {
00206 $this->_getProductCollection()->addAttributeToSelect($code);
00207 return $this;
00208 }
00209
00210 public function getPriceBlockTemplate()
00211 {
00212 return $this->_getData('price_block_template');
00213 }
00214
00215
00216
00217
00218
00219
00220 protected function _getConfig()
00221 {
00222 return Mage::getSingleton('catalog/config');
00223 }
00224
00225
00226
00227
00228
00229
00230
00231 public function prepareSortableFieldsByCategory($category) {
00232 if (!$this->getAvailableOrders()) {
00233 $this->setAvailableOrders($category->getAvailableSortByOptions());
00234 }
00235 $availableOrders = $this->getAvailableOrders();
00236 if (!$this->getSortBy()) {
00237 if ($categorySortBy = $category->getDefaultSortBy()) {
00238 if (!$availableOrders) {
00239 $availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
00240 }
00241 if (isset($availableOrders[$categorySortBy])) {
00242 $this->setSortBy($categorySortBy);
00243 }
00244 }
00245 }
00246
00247
00248 return $this;
00249 }
00250
00251
00252
00253
00254
00255
00256 public function getColumnCount()
00257 {
00258 if (!$this->_getData('column_count')) {
00259 $pageLayout = $this->getPageLayout();
00260 if ($pageLayout && $this->getColumnCountLayoutDepend($pageLayout->getCode())) {
00261 $this->setData(
00262 'column_count',
00263 $this->getColumnCountLayoutDepend($pageLayout->getCode())
00264 );
00265 } else {
00266 $this->setData('column_count', $this->_defaultColumnCount);
00267 }
00268 }
00269
00270 return (int) $this->_getData('column_count');
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280 public function addColumnCountLayoutDepend($pageLayout, $columnCount)
00281 {
00282 $this->_columnCountLayoutDepend[$pageLayout] = $columnCount;
00283 return $this;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 public function removeColumnCountLayoutDepend($pageLayout)
00293 {
00294 if (isset($this->_columnCountLayoutDepend[$pageLayout])) {
00295 unset($this->_columnCountLayoutDepend[$pageLayout]);
00296 }
00297
00298 return $this;
00299 }
00300
00301
00302
00303
00304
00305
00306
00307 public function getColumnCountLayoutDepend($pageLayout)
00308 {
00309 if (isset($this->_columnCountLayoutDepend[$pageLayout])) {
00310 return $this->_columnCountLayoutDepend[$pageLayout];
00311 }
00312
00313 return false;
00314 }
00315
00316
00317
00318
00319
00320
00321 public function getPageLayout()
00322 {
00323 return $this->helper('page/layout')->getCurrentPageLayout();
00324 }
00325 }