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_Helper_Category extends Mage_Core_Helper_Abstract
00035 {
00036 const XML_PATH_CATEGORY_URL_SUFFIX = 'catalog/seo/category_url_suffix';
00037
00038
00039
00040
00041
00042
00043 protected $_storeCategories = array();
00044
00045
00046
00047
00048
00049
00050 protected $_categoryUrlSuffix = array();
00051
00052
00053
00054
00055
00056
00057
00058
00059 public function getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)
00060 {
00061 $parent = Mage::app()->getStore()->getRootCategoryId();
00062 $cacheKey = sprintf('%d-%d-%d-%d', $parent, $sorted, $asCollection, $toLoad);
00063 if (isset($this->_storeCategories[$cacheKey])) {
00064 return $this->_storeCategories[$cacheKey];
00065 }
00066
00067
00068
00069
00070 $category = Mage::getModel('catalog/category');
00071
00072 if (!$category->checkId($parent)) {
00073 if ($asCollection) {
00074 return new Varien_Data_Collection();
00075 }
00076 return array();
00077 }
00078
00079 $recursionLevel = max(0, (int) Mage::app()->getStore()->getConfig('catalog/navigation/max_depth'));
00080 $storeCategories = $category->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
00081
00082 $this->_storeCategories[$cacheKey] = $storeCategories;
00083 return $storeCategories;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 public function getCategoryUrl($category)
00093 {
00094 if ($category instanceof Mage_Catalog_Model_Category) {
00095 return $category->getUrl();
00096 }
00097 return Mage::getModel('catalog/category')
00098 ->setData($category->getData())
00099 ->getUrl();
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 public function canShow($category)
00109 {
00110 if (is_int($category)) {
00111 $category = Mage::getModel('catalog/category')->load($category);
00112 }
00113
00114 if (!$category->getId()) {
00115 return false;
00116 }
00117
00118 if (!$category->getIsActive()) {
00119 return false;
00120 }
00121 if (!$category->isInRootCategoryList()) {
00122 return false;
00123 }
00124
00125 return true;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134 public function getCategoryUrlSuffix($storeId = null)
00135 {
00136 if (is_null($storeId)) {
00137 $storeId = Mage::app()->getStore()->getId();
00138 }
00139
00140 if (!isset($this->_categoryUrlSuffix[$storeId])) {
00141 $this->_categoryUrlSuffix[$storeId] = Mage::getStoreConfig(self::XML_PATH_CATEGORY_URL_SUFFIX, $storeId);
00142 }
00143 return $this->_categoryUrlSuffix[$storeId];
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 public function getCategoryUrlPath($urlPath, $slash = false, $storeId = null)
00156 {
00157 if (!$this->getCategoryUrlSuffix($storeId)) {
00158 return $urlPath;
00159 }
00160
00161 if ($slash) {
00162 $regexp = '#('.preg_quote($this->getCategoryUrlSuffix($storeId), '#').')/$#i';
00163 $replace = '/';
00164 }
00165 else {
00166 $regexp = '#('.preg_quote($this->getCategoryUrlSuffix($storeId), '#').')$#i';
00167 $replace = '';
00168 }
00169
00170 return preg_replace($regexp, $replace, $urlPath);
00171 }
00172 }