1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Catalog
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Product list
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_Abstract
36: {
37: /**
38: * Default toolbar block name
39: *
40: * @var string
41: */
42: protected $_defaultToolbarBlock = 'catalog/product_list_toolbar';
43:
44: /**
45: * Product Collection
46: *
47: * @var Mage_Eav_Model_Entity_Collection_Abstract
48: */
49: protected $_productCollection;
50:
51: /**
52: * Retrieve loaded category collection
53: *
54: * @return Mage_Eav_Model_Entity_Collection_Abstract
55: */
56: protected function _getProductCollection()
57: {
58: if (is_null($this->_productCollection)) {
59: $layer = $this->getLayer();
60: /* @var $layer Mage_Catalog_Model_Layer */
61: if ($this->getShowRootCategory()) {
62: $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
63: }
64:
65: // if this is a product view page
66: if (Mage::registry('product')) {
67: // get collection of categories this product is associated with
68: $categories = Mage::registry('product')->getCategoryCollection()
69: ->setPage(1, 1)
70: ->load();
71: // if the product is associated with any category
72: if ($categories->count()) {
73: // show products from this category
74: $this->setCategoryId(current($categories->getIterator()));
75: }
76: }
77:
78: $origCategory = null;
79: if ($this->getCategoryId()) {
80: $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
81: if ($category->getId()) {
82: $origCategory = $layer->getCurrentCategory();
83: $layer->setCurrentCategory($category);
84: }
85: }
86: $this->_productCollection = $layer->getProductCollection();
87:
88: $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
89:
90: if ($origCategory) {
91: $layer->setCurrentCategory($origCategory);
92: }
93: }
94:
95: return $this->_productCollection;
96: }
97:
98: /**
99: * Get catalog layer model
100: *
101: * @return Mage_Catalog_Model_Layer
102: */
103: public function getLayer()
104: {
105: $layer = Mage::registry('current_layer');
106: if ($layer) {
107: return $layer;
108: }
109: return Mage::getSingleton('catalog/layer');
110: }
111:
112: /**
113: * Retrieve loaded category collection
114: *
115: * @return Mage_Eav_Model_Entity_Collection_Abstract
116: */
117: public function getLoadedProductCollection()
118: {
119: return $this->_getProductCollection();
120: }
121:
122: /**
123: * Retrieve current view mode
124: *
125: * @return string
126: */
127: public function getMode()
128: {
129: return $this->getChild('toolbar')->getCurrentMode();
130: }
131:
132: /**
133: * Need use as _prepareLayout - but problem in declaring collection from
134: * another block (was problem with search result)
135: */
136: protected function _beforeToHtml()
137: {
138: $toolbar = $this->getToolbarBlock();
139:
140: // called prepare sortable parameters
141: $collection = $this->_getProductCollection();
142:
143: // use sortable parameters
144: if ($orders = $this->getAvailableOrders()) {
145: $toolbar->setAvailableOrders($orders);
146: }
147: if ($sort = $this->getSortBy()) {
148: $toolbar->setDefaultOrder($sort);
149: }
150: if ($dir = $this->getDefaultDirection()) {
151: $toolbar->setDefaultDirection($dir);
152: }
153: if ($modes = $this->getModes()) {
154: $toolbar->setModes($modes);
155: }
156:
157: // set collection to toolbar and apply sort
158: $toolbar->setCollection($collection);
159:
160: $this->setChild('toolbar', $toolbar);
161: Mage::dispatchEvent('catalog_block_product_list_collection', array(
162: 'collection' => $this->_getProductCollection()
163: ));
164:
165: $this->_getProductCollection()->load();
166:
167: return parent::_beforeToHtml();
168: }
169:
170: /**
171: * Retrieve Toolbar block
172: *
173: * @return Mage_Catalog_Block_Product_List_Toolbar
174: */
175: public function getToolbarBlock()
176: {
177: if ($blockName = $this->getToolbarBlockName()) {
178: if ($block = $this->getLayout()->getBlock($blockName)) {
179: return $block;
180: }
181: }
182: $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
183: return $block;
184: }
185:
186: /**
187: * Retrieve additional blocks html
188: *
189: * @return string
190: */
191: public function getAdditionalHtml()
192: {
193: return $this->getChildHtml('additional');
194: }
195:
196: /**
197: * Retrieve list toolbar HTML
198: *
199: * @return string
200: */
201: public function getToolbarHtml()
202: {
203: return $this->getChildHtml('toolbar');
204: }
205:
206: public function setCollection($collection)
207: {
208: $this->_productCollection = $collection;
209: return $this;
210: }
211:
212: public function addAttribute($code)
213: {
214: $this->_getProductCollection()->addAttributeToSelect($code);
215: return $this;
216: }
217:
218: public function getPriceBlockTemplate()
219: {
220: return $this->_getData('price_block_template');
221: }
222:
223: /**
224: * Retrieve Catalog Config object
225: *
226: * @return Mage_Catalog_Model_Config
227: */
228: protected function _getConfig()
229: {
230: return Mage::getSingleton('catalog/config');
231: }
232:
233: /**
234: * Prepare Sort By fields from Category Data
235: *
236: * @param Mage_Catalog_Model_Category $category
237: * @return Mage_Catalog_Block_Product_List
238: */
239: public function prepareSortableFieldsByCategory($category) {
240: if (!$this->getAvailableOrders()) {
241: $this->setAvailableOrders($category->getAvailableSortByOptions());
242: }
243: $availableOrders = $this->getAvailableOrders();
244: if (!$this->getSortBy()) {
245: if ($categorySortBy = $category->getDefaultSortBy()) {
246: if (!$availableOrders) {
247: $availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
248: }
249: if (isset($availableOrders[$categorySortBy])) {
250: $this->setSortBy($categorySortBy);
251: }
252: }
253: }
254:
255: return $this;
256: }
257: }
258: