/app/code/core/Mage/Catalog/Block/Product/List.php

https://bitbucket.org/spenna/alexoo_produzione · PHP · 257 lines · 130 code · 28 blank · 99 comment · 19 complexity · 9f67650ef1273df2959c839f2c09c84a MD5 · raw file

  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. * Product list
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_Abstract
  34. {
  35. /**
  36. * Default toolbar block name
  37. *
  38. * @var string
  39. */
  40. protected $_defaultToolbarBlock = 'catalog/product_list_toolbar';
  41. /**
  42. * Product Collection
  43. *
  44. * @var Mage_Eav_Model_Entity_Collection_Abstract
  45. */
  46. protected $_productCollection;
  47. /**
  48. * Retrieve loaded category collection
  49. *
  50. * @return Mage_Eav_Model_Entity_Collection_Abstract
  51. */
  52. protected function _getProductCollection()
  53. {
  54. if (is_null($this->_productCollection)) {
  55. $layer = $this->getLayer();
  56. /* @var $layer Mage_Catalog_Model_Layer */
  57. if ($this->getShowRootCategory()) {
  58. $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
  59. }
  60. // if this is a product view page
  61. if (Mage::registry('product')) {
  62. // get collection of categories this product is associated with
  63. $categories = Mage::registry('product')->getCategoryCollection()
  64. ->setPage(1, 1)
  65. ->load();
  66. // if the product is associated with any category
  67. if ($categories->count()) {
  68. // show products from this category
  69. $this->setCategoryId(current($categories->getIterator()));
  70. }
  71. }
  72. $origCategory = null;
  73. if ($this->getCategoryId()) {
  74. $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
  75. if ($category->getId()) {
  76. $origCategory = $layer->getCurrentCategory();
  77. $layer->setCurrentCategory($category);
  78. }
  79. }
  80. $this->_productCollection = $layer->getProductCollection();
  81. $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
  82. if ($origCategory) {
  83. $layer->setCurrentCategory($origCategory);
  84. }
  85. }
  86. return $this->_productCollection;
  87. }
  88. /**
  89. * Get catalog layer model
  90. *
  91. * @return Mage_Catalog_Model_Layer
  92. */
  93. public function getLayer()
  94. {
  95. $layer = Mage::registry('current_layer');
  96. if ($layer) {
  97. return $layer;
  98. }
  99. return Mage::getSingleton('catalog/layer');
  100. }
  101. /**
  102. * Retrieve loaded category collection
  103. *
  104. * @return Mage_Eav_Model_Entity_Collection_Abstract
  105. */
  106. public function getLoadedProductCollection()
  107. {
  108. return $this->_getProductCollection();
  109. }
  110. /**
  111. * Retrieve current view mode
  112. *
  113. * @return string
  114. */
  115. public function getMode()
  116. {
  117. return $this->getChild('toolbar')->getCurrentMode();
  118. }
  119. /**
  120. * Need use as _prepareLayout - but problem in declaring collection from
  121. * another block (was problem with search result)
  122. */
  123. protected function _beforeToHtml()
  124. {
  125. $toolbar = $this->getToolbarBlock();
  126. // called prepare sortable parameters
  127. $collection = $this->_getProductCollection();
  128. // use sortable parameters
  129. if ($orders = $this->getAvailableOrders()) {
  130. $toolbar->setAvailableOrders($orders);
  131. }
  132. if ($sort = $this->getSortBy()) {
  133. $toolbar->setDefaultOrder($sort);
  134. }
  135. if ($dir = $this->getDefaultDirection()) {
  136. $toolbar->setDefaultDirection($dir);
  137. }
  138. if ($modes = $this->getModes()) {
  139. $toolbar->setModes($modes);
  140. }
  141. // set collection to toolbar and apply sort
  142. $toolbar->setCollection($collection);
  143. $this->setChild('toolbar', $toolbar);
  144. Mage::dispatchEvent('catalog_block_product_list_collection', array(
  145. 'collection' => $this->_getProductCollection()
  146. ));
  147. $this->_getProductCollection()->load();
  148. return parent::_beforeToHtml();
  149. }
  150. /**
  151. * Retrieve Toolbar block
  152. *
  153. * @return Mage_Catalog_Block_Product_List_Toolbar
  154. */
  155. public function getToolbarBlock()
  156. {
  157. if ($blockName = $this->getToolbarBlockName()) {
  158. if ($block = $this->getLayout()->getBlock($blockName)) {
  159. return $block;
  160. }
  161. }
  162. $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
  163. return $block;
  164. }
  165. /**
  166. * Retrieve additional blocks html
  167. *
  168. * @return string
  169. */
  170. public function getAdditionalHtml()
  171. {
  172. return $this->getChildHtml('additional');
  173. }
  174. /**
  175. * Retrieve list toolbar HTML
  176. *
  177. * @return string
  178. */
  179. public function getToolbarHtml()
  180. {
  181. return $this->getChildHtml('toolbar');
  182. }
  183. public function setCollection($collection)
  184. {
  185. $this->_productCollection = $collection;
  186. return $this;
  187. }
  188. public function addAttribute($code)
  189. {
  190. $this->_getProductCollection()->addAttributeToSelect($code);
  191. return $this;
  192. }
  193. public function getPriceBlockTemplate()
  194. {
  195. return $this->_getData('price_block_template');
  196. }
  197. /**
  198. * Retrieve Catalog Config object
  199. *
  200. * @return Mage_Catalog_Model_Config
  201. */
  202. protected function _getConfig()
  203. {
  204. return Mage::getSingleton('catalog/config');
  205. }
  206. /**
  207. * Prepare Sort By fields from Category Data
  208. *
  209. * @param Mage_Catalog_Model_Category $category
  210. * @return Mage_Catalog_Block_Product_List
  211. */
  212. public function prepareSortableFieldsByCategory($category) {
  213. if (!$this->getAvailableOrders()) {
  214. $this->setAvailableOrders($category->getAvailableSortByOptions());
  215. }
  216. $availableOrders = $this->getAvailableOrders();
  217. if (!$this->getSortBy()) {
  218. if ($categorySortBy = $category->getDefaultSortBy()) {
  219. if (!$availableOrders) {
  220. $availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
  221. }
  222. if (isset($availableOrders[$categorySortBy])) {
  223. $this->setSortBy($categorySortBy);
  224. }
  225. }
  226. }
  227. return $this;
  228. }
  229. }