PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 271 lines | 138 code | 29 blank | 104 comment | 19 complexity | 37b2ef7921f5bcdea08faa7dfa24525c 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.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. $this->addModelTags($category);
  79. }
  80. }
  81. $this->_productCollection = $layer->getProductCollection();
  82. $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
  83. if ($origCategory) {
  84. $layer->setCurrentCategory($origCategory);
  85. }
  86. }
  87. return $this->_productCollection;
  88. }
  89. /**
  90. * Get catalog layer model
  91. *
  92. * @return Mage_Catalog_Model_Layer
  93. */
  94. public function getLayer()
  95. {
  96. $layer = Mage::registry('current_layer');
  97. if ($layer) {
  98. return $layer;
  99. }
  100. return Mage::getSingleton('catalog/layer');
  101. }
  102. /**
  103. * Retrieve loaded category collection
  104. *
  105. * @return Mage_Eav_Model_Entity_Collection_Abstract
  106. */
  107. public function getLoadedProductCollection()
  108. {
  109. return $this->_getProductCollection();
  110. }
  111. /**
  112. * Retrieve current view mode
  113. *
  114. * @return string
  115. */
  116. public function getMode()
  117. {
  118. return $this->getChild('toolbar')->getCurrentMode();
  119. }
  120. /**
  121. * Need use as _prepareLayout - but problem in declaring collection from
  122. * another block (was problem with search result)
  123. */
  124. protected function _beforeToHtml()
  125. {
  126. $toolbar = $this->getToolbarBlock();
  127. // called prepare sortable parameters
  128. $collection = $this->_getProductCollection();
  129. // use sortable parameters
  130. if ($orders = $this->getAvailableOrders()) {
  131. $toolbar->setAvailableOrders($orders);
  132. }
  133. if ($sort = $this->getSortBy()) {
  134. $toolbar->setDefaultOrder($sort);
  135. }
  136. if ($dir = $this->getDefaultDirection()) {
  137. $toolbar->setDefaultDirection($dir);
  138. }
  139. if ($modes = $this->getModes()) {
  140. $toolbar->setModes($modes);
  141. }
  142. // set collection to toolbar and apply sort
  143. $toolbar->setCollection($collection);
  144. $this->setChild('toolbar', $toolbar);
  145. Mage::dispatchEvent('catalog_block_product_list_collection', array(
  146. 'collection' => $this->_getProductCollection()
  147. ));
  148. $this->_getProductCollection()->load();
  149. return parent::_beforeToHtml();
  150. }
  151. /**
  152. * Retrieve Toolbar block
  153. *
  154. * @return Mage_Catalog_Block_Product_List_Toolbar
  155. */
  156. public function getToolbarBlock()
  157. {
  158. if ($blockName = $this->getToolbarBlockName()) {
  159. if ($block = $this->getLayout()->getBlock($blockName)) {
  160. return $block;
  161. }
  162. }
  163. $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
  164. return $block;
  165. }
  166. /**
  167. * Retrieve additional blocks html
  168. *
  169. * @return string
  170. */
  171. public function getAdditionalHtml()
  172. {
  173. return $this->getChildHtml('additional');
  174. }
  175. /**
  176. * Retrieve list toolbar HTML
  177. *
  178. * @return string
  179. */
  180. public function getToolbarHtml()
  181. {
  182. return $this->getChildHtml('toolbar');
  183. }
  184. public function setCollection($collection)
  185. {
  186. $this->_productCollection = $collection;
  187. return $this;
  188. }
  189. public function addAttribute($code)
  190. {
  191. $this->_getProductCollection()->addAttributeToSelect($code);
  192. return $this;
  193. }
  194. public function getPriceBlockTemplate()
  195. {
  196. return $this->_getData('price_block_template');
  197. }
  198. /**
  199. * Retrieve Catalog Config object
  200. *
  201. * @return Mage_Catalog_Model_Config
  202. */
  203. protected function _getConfig()
  204. {
  205. return Mage::getSingleton('catalog/config');
  206. }
  207. /**
  208. * Prepare Sort By fields from Category Data
  209. *
  210. * @param Mage_Catalog_Model_Category $category
  211. * @return Mage_Catalog_Block_Product_List
  212. */
  213. public function prepareSortableFieldsByCategory($category) {
  214. if (!$this->getAvailableOrders()) {
  215. $this->setAvailableOrders($category->getAvailableSortByOptions());
  216. }
  217. $availableOrders = $this->getAvailableOrders();
  218. if (!$this->getSortBy()) {
  219. if ($categorySortBy = $category->getDefaultSortBy()) {
  220. if (!$availableOrders) {
  221. $availableOrders = $this->_getConfig()->getAttributeUsedForSortByArray();
  222. }
  223. if (isset($availableOrders[$categorySortBy])) {
  224. $this->setSortBy($categorySortBy);
  225. }
  226. }
  227. }
  228. return $this;
  229. }
  230. /**
  231. * Retrieve block cache tags based on product collection
  232. *
  233. * @return array
  234. */
  235. public function getCacheTags()
  236. {
  237. return array_merge(
  238. parent::getCacheTags(),
  239. $this->getItemsTags($this->_getProductCollection())
  240. );
  241. }
  242. }