00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Catalog_Model_Layer_Filter_Price extends Mage_Catalog_Model_Layer_Filter_Abstract
00035 {
00036 const MIN_RANGE_POWER = 10;
00037
00038
00039
00040
00041
00042 public function __construct()
00043 {
00044 parent::__construct();
00045 $this->_requestVar = 'price';
00046 }
00047
00048
00049
00050
00051
00052
00053 public function getPriceRange()
00054 {
00055 $range = $this->getData('price_range');
00056 if (is_null($range)) {
00057 $maxPrice = $this->getMaxPriceInt();
00058 $index = 1;
00059 do {
00060 $range = pow(10, (strlen(floor($maxPrice))-$index));
00061 $items = $this->getRangeItemCounts($range);
00062 $index++;
00063 }
00064 while($range>self::MIN_RANGE_POWER && count($items)<2);
00065
00066 $this->setData('price_range', $range);
00067 }
00068 return $range;
00069 }
00070
00071
00072
00073
00074
00075
00076 public function getMaxPriceInt()
00077 {
00078 $maxPrice = $this->getData('max_price_int');
00079 if (is_null($maxPrice)) {
00080 $maxPrice = Mage::getSingleton('catalogindex/price')->getMaxValue(
00081 $this->getAttributeModel(),
00082 $this->_getBaseCollectionSql()
00083 );
00084
00085 $maxPrice = floor($maxPrice);
00086 $this->setData('max_price_int', $maxPrice);
00087 }
00088 return $maxPrice;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 public function getRangeItemCounts($range)
00098 {
00099 $items = $this->getData('range_item_counts_'.$range);
00100 if (is_null($items)) {
00101 $items = Mage::getSingleton('catalogindex/price')->getCount(
00102 $this->getAttributeModel(),
00103 $range,
00104 $this->_getBaseCollectionSql()
00105 );
00106 $this->setData('range_item_counts_'.$range, $items);
00107 }
00108 return $items;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118 protected function _renderItemLabel($range, $value)
00119 {
00120 $store = Mage::app()->getStore();
00121 $fromPrice = $store->formatPrice(($value-1)*$range);
00122 $toPrice = $store->formatPrice($value*$range);
00123 return Mage::helper('catalog')->__('%s - %s', $fromPrice, $toPrice);
00124 }
00125
00126
00127
00128
00129
00130
00131 protected function _getCacheKey()
00132 {
00133 $key = $this->getLayer()->getStateKey()
00134 . '_PRICES_GRP_' . Mage::getSingleton('customer/session')->getCustomerGroupId()
00135 . '_CURR_' . Mage::app()->getStore()->getCurrentCurrencyCode()
00136 . '_LOC_'
00137 ;
00138 $taxReq = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false);
00139 $key.= $taxReq->__toString(array(), '_');
00140 return $key;
00141 }
00142
00143
00144
00145
00146
00147
00148 protected function _getItemsData()
00149 {
00150 $key = $this->_getCacheKey();
00151
00152 $data = $this->getLayer()->getAggregator()->getCacheData($key);
00153 if ($data === null) {
00154 $range = $this->getPriceRange();
00155 $dbRanges = $this->getRangeItemCounts($range);
00156 $data = array();
00157
00158 foreach ($dbRanges as $index=>$count) {
00159 $data[] = array(
00160 'label' => $this->_renderItemLabel($range, $index),
00161 'value' => $index . ',' . $range,
00162 'count' => $count,
00163 );
00164 }
00165
00166 $tags = array(
00167 Mage_Catalog_Model_Product_Type_Price::CACHE_TAG,
00168 );
00169 $tags = $this->getLayer()->getStateTags($tags);
00170 $this->getLayer()->getAggregator()->saveCacheData($data, $key, $tags);
00171 }
00172 return $data;
00173 }
00174
00175
00176
00177
00178
00179
00180 public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
00181 {
00182
00183
00184
00185 $filter = $request->getParam($this->getRequestVar());
00186 if (!$filter) {
00187 return $this;
00188 }
00189
00190 $filter = explode(',', $filter);
00191 if (count($filter) != 2) {
00192 return $this;
00193 }
00194
00195 list($index, $range) = $filter;
00196
00197 if ((int)$index && (int)$range) {
00198 $this->setPriceRange((int)$range);
00199
00200 Mage::getSingleton('catalogindex/price')->applyFilterToCollection(
00201 $this->getLayer()->getProductCollection(),
00202 $this->getAttributeModel(),
00203 $range,
00204 $index
00205 );
00206
00207 $this->getLayer()->getState()->addFilter(
00208 $this->_createItem($this->_renderItemLabel($range, $index), $filter)
00209 );
00210
00211 $this->_items = array();
00212 }
00213 return $this;
00214 }
00215 }