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: class Mage_Catalog_Helper_Category extends Mage_Core_Helper_Abstract
35: {
36: const XML_PATH_CATEGORY_URL_SUFFIX = 'catalog/seo/category_url_suffix';
37: const XML_PATH_USE_CATEGORY_CANONICAL_TAG = 'catalog/seo/category_canonical_tag';
38: const XML_PATH_CATEGORY_ROOT_ID = 'catalog/category/root_id';
39:
40: 41: 42: 43: 44:
45: protected $_storeCategories = array();
46:
47: 48: 49: 50: 51:
52: protected $_categoryUrlSuffix = array();
53:
54: 55: 56: 57: 58: 59: 60:
61: public function getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)
62: {
63: $parent = Mage::app()->getStore()->getRootCategoryId();
64: $cacheKey = sprintf('%d-%d-%d-%d', $parent, $sorted, $asCollection, $toLoad);
65: if (isset($this->_storeCategories[$cacheKey])) {
66: return $this->_storeCategories[$cacheKey];
67: }
68:
69: 70: 71:
72: $category = Mage::getModel('catalog/category');
73:
74: if (!$category->checkId($parent)) {
75: if ($asCollection) {
76: return new Varien_Data_Collection();
77: }
78: return array();
79: }
80:
81: $recursionLevel = max(0, (int) Mage::app()->getStore()->getConfig('catalog/navigation/max_depth'));
82: $storeCategories = $category->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
83:
84: $this->_storeCategories[$cacheKey] = $storeCategories;
85: return $storeCategories;
86: }
87:
88: 89: 90: 91: 92: 93:
94: public function getCategoryUrl($category)
95: {
96: if ($category instanceof Mage_Catalog_Model_Category) {
97: return $category->getUrl();
98: }
99: return Mage::getModel('catalog/category')
100: ->setData($category->getData())
101: ->getUrl();
102: }
103:
104: 105: 106: 107: 108: 109:
110: public function canShow($category)
111: {
112: if (is_int($category)) {
113: $category = Mage::getModel('catalog/category')->load($category);
114: }
115:
116: if (!$category->getId()) {
117: return false;
118: }
119:
120: if (!$category->getIsActive()) {
121: return false;
122: }
123: if (!$category->isInRootCategoryList()) {
124: return false;
125: }
126:
127: return true;
128: }
129:
130: 131: 132: 133: 134: 135:
136: public function getCategoryUrlSuffix($storeId = null)
137: {
138: if (is_null($storeId)) {
139: $storeId = Mage::app()->getStore()->getId();
140: }
141:
142: if (!isset($this->_categoryUrlSuffix[$storeId])) {
143: $this->_categoryUrlSuffix[$storeId] = Mage::getStoreConfig(self::XML_PATH_CATEGORY_URL_SUFFIX, $storeId);
144: }
145: return $this->_categoryUrlSuffix[$storeId];
146: }
147:
148: 149: 150: 151: 152: 153: 154: 155: 156:
157: public function getCategoryUrlPath($urlPath, $slash = false, $storeId = null)
158: {
159: if (!$this->getCategoryUrlSuffix($storeId)) {
160: return $urlPath;
161: }
162:
163: if ($slash) {
164: $regexp = '#('.preg_quote($this->getCategoryUrlSuffix($storeId), '#').')/$#i';
165: $replace = '/';
166: }
167: else {
168: $regexp = '#('.preg_quote($this->getCategoryUrlSuffix($storeId), '#').')$#i';
169: $replace = '';
170: }
171:
172: return preg_replace($regexp, $replace, $urlPath);
173: }
174:
175: 176: 177: 178: 179: 180:
181: public function canUseCanonicalTag($store = null)
182: {
183: return Mage::getStoreConfig(self::XML_PATH_USE_CATEGORY_CANONICAL_TAG, $store);
184: }
185: }
186: