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: /**
28: * Layer attribute filter
29: *
30: * @category Mage
31: * @package Mage_Catalog
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Catalog_Model_Layer_Filter_Attribute extends Mage_Catalog_Model_Layer_Filter_Abstract
35: {
36: const OPTIONS_ONLY_WITH_RESULTS = 1;
37:
38: /**
39: * Resource instance
40: *
41: * @var Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute
42: */
43: protected $_resource;
44:
45: /**
46: * Construct attribute filter
47: *
48: */
49: public function __construct()
50: {
51: parent::__construct();
52: $this->_requestVar = 'attribute';
53: }
54:
55: /**
56: * Retrieve resource instance
57: *
58: * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute
59: */
60: protected function _getResource()
61: {
62: if (is_null($this->_resource)) {
63: $this->_resource = Mage::getResourceModel('catalog/layer_filter_attribute');
64: }
65: return $this->_resource;
66: }
67:
68: /**
69: * Get option text from frontend model by option id
70: *
71: * @param int $optionId
72: * @return string|bool
73: */
74: protected function _getOptionText($optionId)
75: {
76: return $this->getAttributeModel()->getFrontend()->getOption($optionId);
77: }
78:
79: /**
80: * Apply attribute option filter to product collection
81: *
82: * @param Zend_Controller_Request_Abstract $request
83: * @param Varien_Object $filterBlock
84: * @return Mage_Catalog_Model_Layer_Filter_Attribute
85: */
86: public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
87: {
88: $filter = $request->getParam($this->_requestVar);
89: if (is_array($filter)) {
90: return $this;
91: }
92: $text = $this->_getOptionText($filter);
93: if ($filter && strlen($text)) {
94: $this->_getResource()->applyFilterToCollection($this, $filter);
95: $this->getLayer()->getState()->addFilter($this->_createItem($text, $filter));
96: $this->_items = array();
97: }
98: return $this;
99: }
100:
101: /**
102: * Check whether specified attribute can be used in LN
103: *
104: * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
105: * @return bool
106: */
107: protected function _getIsFilterableAttribute($attribute)
108: {
109: return $attribute->getIsFilterable();
110: }
111:
112: /**
113: * Get data array for building attribute filter items
114: *
115: * @return array
116: */
117: protected function _getItemsData()
118: {
119: $attribute = $this->getAttributeModel();
120: $this->_requestVar = $attribute->getAttributeCode();
121:
122: $key = $this->getLayer()->getStateKey().'_'.$this->_requestVar;
123: $data = $this->getLayer()->getAggregator()->getCacheData($key);
124:
125: if ($data === null) {
126: $options = $attribute->getFrontend()->getSelectOptions();
127: $optionsCount = $this->_getResource()->getCount($this);
128: $data = array();
129: foreach ($options as $option) {
130: if (is_array($option['value'])) {
131: continue;
132: }
133: if (Mage::helper('core/string')->strlen($option['value'])) {
134: // Check filter type
135: if ($this->_getIsFilterableAttribute($attribute) == self::OPTIONS_ONLY_WITH_RESULTS) {
136: if (!empty($optionsCount[$option['value']])) {
137: $data[] = array(
138: 'label' => $option['label'],
139: 'value' => $option['value'],
140: 'count' => $optionsCount[$option['value']],
141: );
142: }
143: }
144: else {
145: $data[] = array(
146: 'label' => $option['label'],
147: 'value' => $option['value'],
148: 'count' => isset($optionsCount[$option['value']]) ? $optionsCount[$option['value']] : 0,
149: );
150: }
151: }
152: }
153:
154: $tags = array(
155: Mage_Eav_Model_Entity_Attribute::CACHE_TAG.':'.$attribute->getId()
156: );
157:
158: $tags = $this->getLayer()->getStateTags($tags);
159: $this->getLayer()->getAggregator()->saveCacheData($data, $key, $tags);
160: }
161: return $data;
162: }
163: }
164: