PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 183 lines | 85 code | 17 blank | 81 comment | 11 complexity | 7fdfcc712efa16a0decc3786f5f28d83 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. * Layer category filter
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Layer_Filter_Category extends Mage_Catalog_Model_Layer_Filter_Abstract
  34. {
  35. /**
  36. * Active Category Id
  37. *
  38. * @var int
  39. */
  40. protected $_categoryId;
  41. /**
  42. * Applied Category
  43. *
  44. * @var Mage_Catalog_Model_Category
  45. */
  46. protected $_appliedCategory = null;
  47. /**
  48. * Class constructor
  49. */
  50. public function __construct()
  51. {
  52. parent::__construct();
  53. $this->_requestVar = 'cat';
  54. }
  55. /**
  56. * Get filter value for reset current filter state
  57. *
  58. * @return mixed
  59. */
  60. public function getResetValue()
  61. {
  62. if ($this->_appliedCategory) {
  63. /**
  64. * Revert path ids
  65. */
  66. $pathIds = array_reverse($this->_appliedCategory->getPathIds());
  67. $curCategoryId = $this->getLayer()->getCurrentCategory()->getId();
  68. if (isset($pathIds[1]) && $pathIds[1] != $curCategoryId) {
  69. return $pathIds[1];
  70. }
  71. }
  72. return null;
  73. }
  74. /**
  75. * Apply category filter to layer
  76. *
  77. * @param Zend_Controller_Request_Abstract $request
  78. * @param Mage_Core_Block_Abstract $filterBlock
  79. * @return Mage_Catalog_Model_Layer_Filter_Category
  80. */
  81. public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
  82. {
  83. $filter = (int) $request->getParam($this->getRequestVar());
  84. if (!$filter) {
  85. return $this;
  86. }
  87. $this->_categoryId = $filter;
  88. Mage::register('current_category_filter', $this->getCategory(), true);
  89. $this->_appliedCategory = Mage::getModel('catalog/category')
  90. ->setStoreId(Mage::app()->getStore()->getId())
  91. ->load($filter);
  92. if ($this->_isValidCategory($this->_appliedCategory)) {
  93. $this->getLayer()->getProductCollection()
  94. ->addCategoryFilter($this->_appliedCategory);
  95. $this->getLayer()->getState()->addFilter(
  96. $this->_createItem($this->_appliedCategory->getName(), $filter)
  97. );
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Validate category for be using as filter
  103. *
  104. * @param Mage_Catalog_Model_Category $category
  105. * @return unknown
  106. */
  107. protected function _isValidCategory($category)
  108. {
  109. return $category->getId();
  110. }
  111. /**
  112. * Get filter name
  113. *
  114. * @return string
  115. */
  116. public function getName()
  117. {
  118. return Mage::helper('catalog')->__('Category');
  119. }
  120. /**
  121. * Get selected category object
  122. *
  123. * @return Mage_Catalog_Model_Category
  124. */
  125. public function getCategory()
  126. {
  127. if (!is_null($this->_categoryId)) {
  128. $category = Mage::getModel('catalog/category')
  129. ->load($this->_categoryId);
  130. if ($category->getId()) {
  131. return $category;
  132. }
  133. }
  134. return $this->getLayer()->getCurrentCategory();
  135. }
  136. /**
  137. * Get data array for building category filter items
  138. *
  139. * @return array
  140. */
  141. protected function _getItemsData()
  142. {
  143. $key = $this->getLayer()->getStateKey().'_SUBCATEGORIES';
  144. $data = $this->getLayer()->getAggregator()->getCacheData($key);
  145. if ($data === null) {
  146. $categoty = $this->getCategory();
  147. /** @var $categoty Mage_Catalog_Model_Categeory */
  148. $categories = $categoty->getChildrenCategories();
  149. $this->getLayer()->getProductCollection()
  150. ->addCountToCategories($categories);
  151. $data = array();
  152. foreach ($categories as $category) {
  153. if ($category->getIsActive() && $category->getProductCount()) {
  154. $data[] = array(
  155. 'label' => Mage::helper('core')->escapeHtml($category->getName()),
  156. 'value' => $category->getId(),
  157. 'count' => $category->getProductCount(),
  158. );
  159. }
  160. }
  161. $tags = $this->getLayer()->getStateTags();
  162. $this->getLayer()->getAggregator()->saveCacheData($data, $key, $tags);
  163. }
  164. return $data;
  165. }
  166. }