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

https://bitbucket.org/jokusafet/magento2 · PHP · 152 lines · 72 code · 10 blank · 70 comment · 10 complexity · f774583369448963bb7fd50b0bb5fba2 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 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Layer attribute 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_Attribute extends Mage_Catalog_Model_Layer_Filter_Abstract
  34. {
  35. const OPTIONS_ONLY_WITH_RESULTS = 1;
  36. /**
  37. * Resource instance
  38. *
  39. * @var Mage_Catalog_Model_Resource_Layer_Filter_Attribute
  40. */
  41. protected $_resource;
  42. /**
  43. * Construct attribute filter
  44. *
  45. */
  46. public function __construct()
  47. {
  48. parent::__construct();
  49. $this->_requestVar = 'attribute';
  50. }
  51. /**
  52. * Retrieve resource instance
  53. *
  54. * @return Mage_Catalog_Model_Resource_Layer_Filter_Attribute
  55. */
  56. protected function _getResource()
  57. {
  58. if (is_null($this->_resource)) {
  59. $this->_resource = Mage::getResourceModel('Mage_Catalog_Model_Resource_Layer_Filter_Attribute');
  60. }
  61. return $this->_resource;
  62. }
  63. /**
  64. * Get option text from frontend model by option id
  65. *
  66. * @param int $optionId
  67. * @return string|bool
  68. */
  69. protected function _getOptionText($optionId)
  70. {
  71. return $this->getAttributeModel()->getFrontend()->getOption($optionId);
  72. }
  73. /**
  74. * Apply attribute option filter to product collection
  75. *
  76. * @param Zend_Controller_Request_Abstract $request
  77. * @param Varien_Object $filterBlock
  78. * @return Mage_Catalog_Model_Layer_Filter_Attribute
  79. */
  80. public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
  81. {
  82. $filter = $request->getParam($this->_requestVar);
  83. if (is_array($filter)) {
  84. return $this;
  85. }
  86. $text = $this->_getOptionText($filter);
  87. if ($filter && strlen($text)) {
  88. $this->_getResource()->applyFilterToCollection($this, $filter);
  89. $this->getLayer()->getState()->addFilter($this->_createItem($text, $filter));
  90. $this->_items = array();
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Check whether specified attribute can be used in LN
  96. *
  97. * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
  98. * @return bool
  99. */
  100. protected function _getIsFilterableAttribute($attribute)
  101. {
  102. return $attribute->getIsFilterable();
  103. }
  104. /**
  105. * Get data array for building attribute filter items
  106. *
  107. * @return array
  108. */
  109. protected function _getItemsData()
  110. {
  111. $attribute = $this->getAttributeModel();
  112. $this->_requestVar = $attribute->getAttributeCode();
  113. $options = $attribute->getFrontend()->getSelectOptions();
  114. $optionsCount = $this->_getResource()->getCount($this);
  115. $data = array();
  116. foreach ($options as $option) {
  117. if (is_array($option['value'])) {
  118. continue;
  119. }
  120. if (Mage::helper('Mage_Core_Helper_String')->strlen($option['value'])) {
  121. // Check filter type
  122. if ($this->_getIsFilterableAttribute($attribute) == self::OPTIONS_ONLY_WITH_RESULTS) {
  123. if (!empty($optionsCount[$option['value']])) {
  124. $data[] = array(
  125. 'label' => $option['label'],
  126. 'value' => $option['value'],
  127. 'count' => $optionsCount[$option['value']],
  128. );
  129. }
  130. }
  131. else {
  132. $data[] = array(
  133. 'label' => $option['label'],
  134. 'value' => $option['value'],
  135. 'count' => isset($optionsCount[$option['value']]) ? $optionsCount[$option['value']] : 0,
  136. );
  137. }
  138. }
  139. }
  140. return $data;
  141. }
  142. }