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_Category extends Mage_Catalog_Model_Layer_Filter_Abstract
00035 {
00036
00037
00038
00039
00040
00041 protected $_categoryId;
00042
00043
00044
00045
00046
00047
00048 protected $_appliedCategory = null;
00049
00050
00051
00052
00053 public function __construct()
00054 {
00055 parent::__construct();
00056 $this->_requestVar = 'cat';
00057 }
00058
00059
00060
00061
00062
00063
00064 public function getResetValue()
00065 {
00066 if ($this->_appliedCategory) {
00067
00068
00069
00070 $pathIds = array_reverse($this->_appliedCategory->getPathIds());
00071 $curCategoryId = $this->getLayer()->getCurrentCategory()->getId();
00072 if (isset($pathIds[1]) && $pathIds[1] != $curCategoryId) {
00073 return $pathIds[1];
00074 }
00075 }
00076 return null;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086 public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
00087 {
00088 $filter = (int) $request->getParam($this->getRequestVar());
00089 $this->_categoryId = $filter;
00090 $this->_appliedCategory = Mage::getModel('catalog/category')
00091 ->setStoreId(Mage::app()->getStore()->getId())
00092 ->load($filter);
00093
00094 if ($this->_isValidCategory($this->_appliedCategory)) {
00095 $this->getLayer()->getProductCollection()
00096 ->addCategoryFilter($this->_appliedCategory);
00097
00098 $this->getLayer()->getState()->addFilter(
00099 $this->_createItem($this->_appliedCategory->getName(), $filter)
00100 );
00101 }
00102
00103 return $this;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 protected function _isValidCategory($category)
00113 {
00114 return $category->getId();
00115 }
00116
00117
00118
00119
00120
00121
00122 public function getName()
00123 {
00124 return Mage::helper('catalog')->__('Category');
00125 }
00126
00127
00128
00129
00130
00131
00132 public function getCategory()
00133 {
00134 if (!is_null($this->_categoryId)) {
00135 $category = Mage::getModel('catalog/category')
00136 ->load($this->_categoryId);
00137 if ($category->getId()) {
00138 return $category;
00139 }
00140 }
00141 return $this->getLayer()->getCurrentCategory();
00142 }
00143
00144
00145
00146
00147
00148
00149 protected function _getItemsData()
00150 {
00151 $key = $this->getLayer()->getStateKey().'_SUBCATEGORIES';
00152 $data = $this->getLayer()->getAggregator()->getCacheData($key);
00153
00154 if ($data === null) {
00155 $categoty = $this->getCategory();
00156
00157 $categories = $categoty->getChildrenCategories();
00158
00159 $this->getLayer()->getProductCollection()
00160 ->addCountToCategories($categories);
00161
00162 $data = array();
00163 foreach ($categories as $category) {
00164 if ($category->getIsActive() && $category->getProductCount()) {
00165 $data[] = array(
00166 'label' => $category->getName(),
00167 'value' => $category->getId(),
00168 'count' => $category->getProductCount(),
00169 );
00170 }
00171 }
00172 $tags = $this->getLayer()->getStateTags();
00173 $this->getLayer()->getAggregator()->saveCacheData($data, $key, $tags);
00174 }
00175 return $data;
00176 }
00177 }