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: * Catalog layered navigation view block
29: *
30: * @category Mage
31: * @package Mage_Catalog
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Catalog_Block_Layer_View extends Mage_Core_Block_Template
35: {
36: /**
37: * State block name
38: *
39: * @var string
40: */
41: protected $_stateBlockName;
42:
43: /**
44: * Category Block Name
45: *
46: * @var string
47: */
48: protected $_categoryBlockName;
49:
50: /**
51: * Attribute Filter Block Name
52: *
53: * @var string
54: */
55: protected $_attributeFilterBlockName;
56:
57: /**
58: * Price Filter Block Name
59: *
60: * @var string
61: */
62: protected $_priceFilterBlockName;
63:
64: /**
65: * Decimal Filter Block Name
66: *
67: * @var string
68: */
69: protected $_decimalFilterBlockName;
70:
71: /**
72: * Internal constructor
73: */
74: protected function _construct()
75: {
76: parent::_construct();
77:
78: $this->_initBlocks();
79: }
80:
81: /**
82: * Initialize blocks names
83: */
84: protected function _initBlocks()
85: {
86: $this->_stateBlockName = 'catalog/layer_state';
87: $this->_categoryBlockName = 'catalog/layer_filter_category';
88: $this->_attributeFilterBlockName = 'catalog/layer_filter_attribute';
89: $this->_priceFilterBlockName = 'catalog/layer_filter_price';
90: $this->_decimalFilterBlockName = 'catalog/layer_filter_decimal';
91: }
92:
93: /**
94: * Get attribute filter block name
95: *
96: * @deprecated after 1.4.1.0
97: *
98: * @return string
99: */
100: protected function _getAttributeFilterBlockName()
101: {
102: return 'catalog/layer_filter_attribute';
103: }
104:
105: /**
106: * Prepare child blocks
107: *
108: * @return Mage_Catalog_Block_Layer_View
109: */
110: protected function _prepareLayout()
111: {
112: $stateBlock = $this->getLayout()->createBlock($this->_stateBlockName)
113: ->setLayer($this->getLayer());
114:
115: $categoryBlock = $this->getLayout()->createBlock($this->_categoryBlockName)
116: ->setLayer($this->getLayer())
117: ->init();
118:
119: $this->setChild('layer_state', $stateBlock);
120: $this->setChild('category_filter', $categoryBlock);
121:
122: $filterableAttributes = $this->_getFilterableAttributes();
123: foreach ($filterableAttributes as $attribute) {
124: if ($attribute->getAttributeCode() == 'price') {
125: $filterBlockName = $this->_priceFilterBlockName;
126: } elseif ($attribute->getBackendType() == 'decimal') {
127: $filterBlockName = $this->_decimalFilterBlockName;
128: } else {
129: $filterBlockName = $this->_attributeFilterBlockName;
130: }
131:
132: $this->setChild($attribute->getAttributeCode() . '_filter',
133: $this->getLayout()->createBlock($filterBlockName)
134: ->setLayer($this->getLayer())
135: ->setAttributeModel($attribute)
136: ->init());
137: }
138:
139: $this->getLayer()->apply();
140:
141: return parent::_prepareLayout();
142: }
143:
144: /**
145: * Get layer object
146: *
147: * @return Mage_Catalog_Model_Layer
148: */
149: public function getLayer()
150: {
151: return Mage::getSingleton('catalog/layer');
152: }
153:
154: /**
155: * Get all fiterable attributes of current category
156: *
157: * @return array
158: */
159: protected function _getFilterableAttributes()
160: {
161: $attributes = $this->getData('_filterable_attributes');
162: if (is_null($attributes)) {
163: $attributes = $this->getLayer()->getFilterableAttributes();
164: $this->setData('_filterable_attributes', $attributes);
165: }
166:
167: return $attributes;
168: }
169:
170: /**
171: * Get layered navigation state html
172: *
173: * @return string
174: */
175: public function getStateHtml()
176: {
177: return $this->getChildHtml('layer_state');
178: }
179:
180: /**
181: * Get all layer filters
182: *
183: * @return array
184: */
185: public function getFilters()
186: {
187: $filters = array();
188: if ($categoryFilter = $this->_getCategoryFilter()) {
189: $filters[] = $categoryFilter;
190: }
191:
192: $filterableAttributes = $this->_getFilterableAttributes();
193: foreach ($filterableAttributes as $attribute) {
194: $filters[] = $this->getChild($attribute->getAttributeCode() . '_filter');
195: }
196:
197: return $filters;
198: }
199:
200: /**
201: * Get category filter block
202: *
203: * @return Mage_Catalog_Block_Layer_Filter_Category
204: */
205: protected function _getCategoryFilter()
206: {
207: return $this->getChild('category_filter');
208: }
209:
210: /**
211: * Check availability display layer options
212: *
213: * @return bool
214: */
215: public function canShowOptions()
216: {
217: foreach ($this->getFilters() as $filter) {
218: if ($filter->getItemsCount()) {
219: return true;
220: }
221: }
222:
223: return false;
224: }
225:
226: /**
227: * Check availability display layer block
228: *
229: * @return bool
230: */
231: public function canShowBlock()
232: {
233: return $this->canShowOptions() || count($this->getLayer()->getState()->getFilters());
234: }
235:
236: /**
237: * Retrieve Price Filter block
238: *
239: * @return Mage_Catalog_Block_Layer_Filter_Price
240: */
241: protected function _getPriceFilter()
242: {
243: return $this->getChild('_price_filter');
244: }
245:
246: /**
247: * Get url for 'Clear All' link
248: *
249: * @return string
250: */
251: public function getClearUrl()
252: {
253: return $this->getChild('layer_state')->getClearUrl();
254: }
255: }
256: