1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Model_Layer_Filter_Decimal extends Mage_Catalog_Model_Layer_Filter_Abstract
36: {
37: const MIN_RANGE_POWER = 10;
38:
39: 40: 41: 42: 43:
44: protected $_resource;
45:
46: 47: 48: 49:
50: public function __construct()
51: {
52: parent::__construct();
53: $this->_requestVar = 'decimal';
54: }
55:
56: 57: 58: 59: 60:
61: protected function _getResource()
62: {
63: if (is_null($this->_resource)) {
64: $this->_resource = Mage::getResourceModel('catalog/layer_filter_decimal');
65: }
66: return $this->_resource;
67: }
68:
69: 70: 71: 72: 73: 74: 75:
76: public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
77: {
78: parent::apply($request, $filterBlock);
79:
80: 81: 82:
83: $filter = $request->getParam($this->getRequestVar());
84: if (!$filter) {
85: return $this;
86: }
87:
88: $filter = explode(',', $filter);
89: if (count($filter) != 2) {
90: return $this;
91: }
92:
93: list($index, $range) = $filter;
94: if ((int)$index && (int)$range) {
95: $this->setRange((int)$range);
96:
97: $this->_getResource()->applyFilterToCollection($this, $range, $index);
98: $this->getLayer()->getState()->addFilter(
99: $this->_createItem($this->_renderItemLabel($range, $index), $filter)
100: );
101:
102: $this->_items = array();
103: }
104:
105: return $this;
106: }
107:
108: 109: 110: 111: 112:
113: protected function _getCacheKey()
114: {
115: $key = $this->getLayer()->getStateKey()
116: . '_ATTR_' . $this->getAttributeModel()->getAttributeCode();
117: return $key;
118: }
119:
120: 121: 122: 123: 124: 125: 126:
127: protected function _renderItemLabel($range, $value)
128: {
129: $from = Mage::app()->getStore()->formatPrice(($value - 1) * $range, false);
130: $to = Mage::app()->getStore()->formatPrice($value * $range, false);
131: return Mage::helper('catalog')->__('%s - %s', $from, $to);
132: }
133:
134: 135: 136: 137: 138:
139: public function getMaxValue()
140: {
141: $max = $this->getData('max_value');
142: if (is_null($max)) {
143: list($min, $max) = $this->_getResource()->getMinMax($this);
144: $this->setData('max_value', $max);
145: $this->setData('min_value', $min);
146: }
147: return $max;
148: }
149:
150: 151: 152: 153: 154:
155: public function getMinValue()
156: {
157: $min = $this->getData('min_value');
158: if (is_null($min)) {
159: list($min, $max) = $this->_getResource()->getMinMax($this);
160: $this->setData('max_value', $max);
161: $this->setData('min_value', $min);
162: }
163: return $min;
164: }
165:
166: 167: 168: 169: 170:
171: public function getRange()
172: {
173: $range = $this->getData('range');
174: if (!$range) {
175: $maxValue = $this->getMaxValue();
176: $index = 1;
177: do {
178: $range = pow(10, (strlen(floor($maxValue)) - $index));
179: $items = $this->getRangeItemCounts($range);
180: $index++;
181: }
182: while ($range > self::MIN_RANGE_POWER && count($items) < 2);
183: $this->setData('range', $range);
184: }
185:
186: return $range;
187: }
188:
189: 190: 191: 192: 193: 194:
195: public function getRangeItemCounts($range)
196: {
197: $rangeKey = 'range_item_counts_' . $range;
198: $items = $this->getData($rangeKey);
199: if (is_null($items)) {
200: $items = $this->_getResource()->getCount($this, $range);
201: $this->setData($rangeKey, $items);
202: }
203: return $items;
204: }
205:
206: 207: 208: 209: 210:
211: protected function _getItemsData()
212: {
213: $key = $this->_getCacheKey();
214:
215: $data = $this->getLayer()->getAggregator()->getCacheData($key);
216: if ($data === null) {
217: $data = array();
218: $range = $this->getRange();
219: $dbRanges = $this->getRangeItemCounts($range);
220:
221: foreach ($dbRanges as $index => $count) {
222: $data[] = array(
223: 'label' => $this->_renderItemLabel($range, $index),
224: 'value' => $index . ',' . $range,
225: 'count' => $count,
226: );
227: }
228:
229:
230: }
231: return $data;
232: }
233: }
234: