*/ class Mage_Catalog_Helper_Category extends Mage_Core_Helper_Abstract { const XML_PATH_CATEGORY_URL_SUFFIX = 'catalog/seo/category_url_suffix'; const XML_PATH_USE_CATEGORY_CANONICAL_TAG = 'catalog/seo/category_canonical_tag'; const XML_PATH_CATEGORY_ROOT_ID = 'catalog/category/root_id'; /** * Store categories cache * * @var array */ protected $_storeCategories = array(); /** * Cache for category rewrite suffix * * @var array */ protected $_categoryUrlSuffix = array(); /** * Retrieve current store categories * * @param boolean|string $sorted * @param boolean $asCollection * @return Varien_Data_Tree_Node_Collection|Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection|array */ public function getStoreCategories($sorted=false, $asCollection=false, $toLoad=true) { $parent = Mage::app()->getStore()->getRootCategoryId(); $cacheKey = sprintf('%d-%d-%d-%d', $parent, $sorted, $asCollection, $toLoad); if (isset($this->_storeCategories[$cacheKey])) { return $this->_storeCategories[$cacheKey]; } /** * Check if parent node of the store still exists */ $category = Mage::getModel('catalog/category'); /* @var $category Mage_Catalog_Model_Category */ if (!$category->checkId($parent)) { if ($asCollection) { return new Varien_Data_Collection(); } return array(); } $recursionLevel = max(0, (int) Mage::app()->getStore()->getConfig('catalog/navigation/max_depth')); $storeCategories = $category->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad); $this->_storeCategories[$cacheKey] = $storeCategories; return $storeCategories; } /** * Retrieve category url * * @param Mage_Catalog_Model_Category $category * @return string */ public function getCategoryUrl($category) { if ($category instanceof Mage_Catalog_Model_Category) { return $category->getUrl(); } return Mage::getModel('catalog/category') ->setData($category->getData()) ->getUrl(); } /** * Check if a category can be shown * * @param Mage_Catalog_Model_Category|int $category * @return boolean */ public function canShow($category) { if (is_int($category)) { $category = Mage::getModel('catalog/category')->load($category); } if (!$category->getId()) { return false; } if (!$category->getIsActive()) { return false; } if (!$category->isInRootCategoryList()) { return false; } return true; } /** * Retrieve category rewrite sufix for store * * @param int $storeId * @return string */ public function getCategoryUrlSuffix($storeId = null) { if (is_null($storeId)) { $storeId = Mage::app()->getStore()->getId(); } if (!isset($this->_categoryUrlSuffix[$storeId])) { $this->_categoryUrlSuffix[$storeId] = Mage::getStoreConfig(self::XML_PATH_CATEGORY_URL_SUFFIX, $storeId); } return $this->_categoryUrlSuffix[$storeId]; } /** * Retrieve clear url for category as parrent * * @param string $url * @param bool $slash * @param int $storeId * * @return string */ public function getCategoryUrlPath($urlPath, $slash = false, $storeId = null) { if (!$this->getCategoryUrlSuffix($storeId)) { return $urlPath; } if ($slash) { $regexp = '#('.preg_quote($this->getCategoryUrlSuffix($storeId), '#').')/$#i'; $replace = '/'; } else { $regexp = '#('.preg_quote($this->getCategoryUrlSuffix($storeId), '#').')$#i'; $replace = ''; } return preg_replace($regexp, $replace, $urlPath); } /** * Check if can be used for category * * @param $store * @return bool */ public function canUseCanonicalTag($store = null) { return Mage::getStoreConfig(self::XML_PATH_USE_CATEGORY_CANONICAL_TAG, $store); } }