Guest User

list.php

a guest
Dec 30th, 2014
532
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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) 2014 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. $this->addModelTags($category);
  85. }
  86. }
  87. $this->_productCollection = $layer->getProductCollection();
  88. $this->_productCollection->joinField('rating_summary', 'review_entity_summary', 'rating_summary', 'entity_pk_value=entity_id', array('entity_type'=>1, 'store_id'=> Mage::app()->getStore()->getId()), 'left');
  89.  
  90.  
  91. $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
  92.  
  93. if ($origCategory) {
  94. $layer->setCurrentCategory($origCategory);
  95. }
  96. }
  97.  
  98. return $this->_productCollection;
  99. }
  100.  
  101. /**
  102. * Get catalog layer model
  103. *
  104. * @return Mage_Catalog_Model_Layer
  105. */
  106. public function getLayer()
  107. {
  108. $layer = Mage::registry('current_layer');
  109. if ($layer) {
  110. return $layer;
  111. }
  112. return Mage::getSingleton('catalog/layer');
  113. }
  114.  
  115. /**
  116. * Retrieve loaded category collection
  117. *
  118. * @return Mage_Eav_Model_Entity_Collection_Abstract
  119. */
  120. public function getLoadedProductCollection()
  121. {
  122. return $this->_getProductCollection();
  123. }
  124.  
  125. /**
  126. * Retrieve current view mode
  127. *
  128. * @return string
  129. */
  130. public function getMode()
  131. {
  132. return $this->getChild('toolbar')->getCurrentMode();
  133. }
  134.  
  135. /**
  136. * Need use as _prepareLayout - but problem in declaring collection from
  137. * another block (was problem with search result)
  138. */
  139. protected function _beforeToHtml()
  140. {
  141. $toolbar = $this->getToolbarBlock();
  142.  
  143. // called prepare sortable parameters
  144. $collection = $this->_getProductCollection();
  145.  
  146. // use sortable parameters
  147. if ($orders = $this->getAvailableOrders()) {
  148. $toolbar->setAvailableOrders($orders);
  149. }
  150. if ($sort = $this->getSortBy()) {
  151. $toolbar->setDefaultOrder($sort);
  152. }
  153. if ($dir = $this->getDefaultDirection()) {
  154. $toolbar->setDefaultDirection($dir);
  155. }
  156. if ($modes = $this->getModes()) {
  157. $toolbar->setModes($modes);
  158. }
  159.  
  160. // set collection to toolbar and apply sort
  161. $toolbar->setCollection($collection);
  162.  
  163. $this->setChild('toolbar', $toolbar);
  164. Mage::dispatchEvent('catalog_block_product_list_collection', array(
  165. 'collection' => $this->_getProductCollection()
  166. ));
  167.  
  168. $this->_getProductCollection()->load();
  169.  
  170. return parent::_beforeToHtml();
  171. }
  172.  
  173. /**
  174. * Retrieve Toolbar block
  175. *
  176. * @return Mage_Catalog_Block_Product_List_Toolbar
  177. */
  178. public function getToolbarBlock()
  179. {
  180. if ($blockName = $this->getToolbarBlockName()) {
  181. if ($block = $this->getLayout()->getBlock($blockName)) {
  182. return $block;
  183. }
  184. }
  185. $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
  186. return $block;
  187. }
  188.  
  189. /**
  190. * Retrieve additional blocks html
  191. *
  192. * @return string
  193. */
  194. public function getAdditionalHtml()
  195. {
  196. return $this->getChildHtml('additional');
  197. }
  198.  
  199. /**
  200. * Retrieve list toolbar HTML
  201. *
  202. * @return string
  203. */
  204. public function getToolbarHtml()
  205. {
  206. return $this->getChildHtml('toolbar');
  207. }
  208.  
  209. public function setCollection($collection)
  210. {
  211. $this->_productCollection = $collection;
  212. return $this;
  213. }
  214.  
  215. public function addAttribute($code)
  216. {
  217. $this->_getProductCollection()->addAttributeToSelect($code);
  218. return $this;
  219. }
  220.  
  221. public function getPriceBlockTemplate()
  222. {
  223. return $this->_getData('price_block_template');
  224. }
  225.  
  226. /**
  227. * Retrieve Catalog Config object
  228. *
  229. * @return Mage_Catalog_Model_Config
  230. */
  231. protected function _getConfig()
  232. {
  233. return Mage::getSingleton('catalog/config');
  234. }
  235.  
  236. /**
  237. * Prepare Sort By fields from Category Data
  238. *
  239. * @param Mage_Catalog_Model_Category $category
  240. * @return Mage_Catalog_Block_Product_List
  241. */
  242. public function prepareSortableFieldsByCategory($category) {
  243. if (!$this->getAvailableOrders()) {
  244. $this->setAvailableOrders($category->getAvailableSortByOptions());
  245. }
  246. $availableOrders = $this->getAvailableOrders();
  247. if (!$this->getSortBy()) {
  248. if ($categorySortBy = $category->getDefaultSortBy()) {
  249. if (!$availableOrders) {
  250. $availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
  251. }
  252. if (isset($availableOrders[$categorySortBy])) {
  253. $this->setSortBy($categorySortBy);
  254. }
  255. }
  256. }
  257.  
  258. return $this;
  259. }
  260.  
  261. /**
  262. * Retrieve block cache tags based on product collection
  263. *
  264. * @return array
  265. */
  266. public function getCacheTags()
  267. {
  268. return array_merge(
  269. parent::getCacheTags(),
  270. $this->getItemsTags($this->_getProductCollection())
  271. );
  272. }
  273. }
RAW Paste Data