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 category filter abstract model
29: *
30: * @category Mage
31: * @package Mage_Catalog
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_Catalog_Model_Layer_Filter_Abstract extends Varien_Object
35: {
36: /**
37: * Request variable name with filter value
38: *
39: * @var string
40: */
41: protected $_requestVar;
42:
43: /**
44: * Array of filter items
45: *
46: * @var array
47: */
48: protected $_items;
49:
50: /**
51: * Set request variable name which is used for apply filter
52: *
53: * @param string $varName
54: * @return Mage_Catalog_Model_Layer_Filter_Abstract
55: */
56: public function setRequestVar($varName)
57: {
58: $this->_requestVar = $varName;
59: return $this;
60: }
61:
62: /**
63: * Get request variable name which is used for apply filter
64: *
65: * @return string
66: */
67: public function getRequestVar()
68: {
69: return $this->_requestVar;
70: }
71:
72: /**
73: * Get filter value for reset current filter state
74: *
75: * @return mixed
76: */
77: public function getResetValue()
78: {
79: return null;
80: }
81:
82: /**
83: * Retrieve filter value for Clear All Items filter state
84: *
85: * @return mixed
86: */
87: public function getCleanValue()
88: {
89: return null;
90: }
91:
92: /**
93: * Apply filter to collection
94: *
95: * @param Zend_Controller_Request_Abstract $request
96: */
97: public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
98: {
99: return $this;
100: }
101:
102: /**
103: * Get fiter items count
104: *
105: * @return int
106: */
107: public function getItemsCount()
108: {
109: return count($this->getItems());
110: }
111:
112: /**
113: * Get all filter items
114: *
115: * @return array
116: */
117: public function getItems()
118: {
119: if (is_null($this->_items)) {
120: $this->_initItems();
121: }
122: return $this->_items;
123: }
124:
125: /**
126: * Get data array for building filter items
127: *
128: * result array should have next structure:
129: * array(
130: * $index => array(
131: * 'label' => $label,
132: * 'value' => $value,
133: * 'count' => $count
134: * )
135: * )
136: *
137: * @return array
138: */
139: protected function _getItemsData()
140: {
141: return array();
142: }
143:
144: /**
145: * Initialize filter items
146: *
147: * @return Mage_Catalog_Model_Layer_Filter_Abstract
148: */
149: protected function _initItems()
150: {
151: $data = $this->_getItemsData();
152: $items=array();
153: foreach ($data as $itemData) {
154: $items[] = $this->_createItem(
155: $itemData['label'],
156: $itemData['value'],
157: $itemData['count']
158: );
159: }
160: $this->_items = $items;
161: return $this;
162: }
163:
164:
165: /**
166: * Retrieve layer object
167: *
168: * @return Mage_Catalog_Model_Layer
169: */
170: public function getLayer()
171: {
172: $layer = $this->_getData('layer');
173: if (is_null($layer)) {
174: $layer = Mage::getSingleton('catalog/layer');
175: $this->setData('layer', $layer);
176: }
177: return $layer;
178: }
179:
180: /**
181: * Create filter item object
182: *
183: * @param string $label
184: * @param mixed $value
185: * @param int $count
186: * @return Mage_Catalog_Model_Layer_Filter_Item
187: */
188: protected function _createItem($label, $value, $count=0)
189: {
190: return Mage::getModel('catalog/layer_filter_item')
191: ->setFilter($this)
192: ->setLabel($label)
193: ->setValue($value)
194: ->setCount($count);
195: }
196:
197: /**
198: * Get all product ids from from collection with applied filters
199: *
200: * @return array
201: */
202: protected function _getFilterEntityIds()
203: {
204: return $this->getLayer()->getProductCollection()->getAllIdsCache();
205: }
206:
207: /**
208: * Get product collection select object with applied filters
209: *
210: * @return Varien_Db_Select
211: */
212: protected function _getBaseCollectionSql()
213: {
214: return $this->getLayer()->getProductCollection()->getSelect();
215: }
216:
217: /**
218: * Set attribute model to filter
219: *
220: * @param Mage_Eav_Model_Entity_Attribute $attribute
221: * @return Mage_Catalog_Model_Layer_Filter_Abstract
222: */
223: public function setAttributeModel($attribute)
224: {
225: $this->setRequestVar($attribute->getAttributeCode());
226: $this->setData('attribute_model', $attribute);
227: return $this;
228: }
229:
230: /**
231: * Get attribute model associated with filter
232: *
233: * @return Mage_Catalog_Model_Resource_Eav_Attribute
234: */
235: public function getAttributeModel()
236: {
237: $attribute = $this->getData('attribute_model');
238: if (is_null($attribute)) {
239: Mage::throwException(Mage::helper('catalog')->__('The attribute model is not defined'));
240: }
241: return $attribute;
242: }
243:
244: /**
245: * Get filter text label
246: *
247: * @return string
248: */
249: public function getName()
250: {
251: return $this->getAttributeModel()->getStoreLabel();
252: }
253:
254: /**
255: * Retrieve current store id scope
256: *
257: * @return int
258: */
259: public function getStoreId()
260: {
261: $storeId = $this->_getData('store_id');
262: if (is_null($storeId)) {
263: $storeId = Mage::app()->getStore()->getId();
264: }
265: return $storeId;
266: }
267:
268: /**
269: * Set store id scope
270: *
271: * @param int $storeId
272: * @return Mage_Catalog_Model_Layer_Filter_Abstract
273: */
274: public function setStoreId($storeId)
275: {
276: return $this->setData('store_id', $storeId);
277: }
278:
279: /**
280: * Retrieve Website ID scope
281: *
282: * @return int
283: */
284: public function getWebsiteId()
285: {
286: $websiteId = $this->_getData('website_id');
287: if (is_null($websiteId)) {
288: $websiteId = Mage::app()->getStore()->getWebsiteId();
289: }
290: return $websiteId;
291: }
292:
293: /**
294: * Set Website ID scope
295: *
296: * @param int $websiteId
297: * @return Mage_Catalog_Model_Layer_Filter_Abstract
298: */
299: public function setWebsiteId($websiteId)
300: {
301: return $this->setData('website_id', $websiteId);
302: }
303:
304: /**
305: * Clear current element link text, for example 'Clear Price'
306: *
307: * @return false|string
308: */
309: public function getClearLinkText()
310: {
311: return false;
312: }
313: }
314: