protected function _reindexCategoryUrlKey(Mage_Catalog_Model_Category $category, Mage_Core_Model_Store $store) { $requestPath = trim($category->getParentUrl(), '/'); if (Mage::getStoreConfig('catalog/seo/category_use_parent_category')) { $requestPath = (!empty($requestPath) ? $requestPath . '/' : '') . $category->getUrlKey(); } else { $requestPath = $category->getUrlKey(); } $requestPath = $this->_cutRequestPath($requestPath); $urlKeyValue = $this->_getUrlKeyAttributeValueId($category, $store); if (empty($urlKeyValue) && empty($urlKeyValue['value_id'])) { $category = $this->_setUrlKeyForDefaultStore($category, $store); $urlKeyValue = $this->_getUrlKeyAttributeValueId($category, $store); } $valueId = $urlKeyValue['value_id']; $rewriteRow = $this->_getRewrite($requestPath, $store->getId()); if (!$rewriteRow || $rewriteRow['value_id'] != $valueId) { $rewriteForValueId = $this->_getRewriteForValueId($store->getId(), $valueId); $suffix = trim(str_replace($requestPath, '', $category->getRequestPath()), '-'); $requestPathIncrement = (int) $this->_getRewriteRequestIncrement($requestPath, $store); if (!$rewriteForValueId || !preg_match('#^(\\d)+$#', $suffix) || $suffix > $requestPathIncrement) { if ($rewriteRow && $rewriteRow['value_id'] != $valueId) { $requestPath .= '-' . ++$requestPathIncrement; } $category = $this->_saveRewrite($category, $store, $requestPath, $valueId); } } $this->_indexedCategoryIds[$store->getId()][$category->getId()] = 1; return $category; }
/** * Check unique url_key value in catalog_category_entity_url_key table. * * @param Mage_Catalog_Model_Category $object * @return Mage_Catalog_Model_Category_Attribute_Backend_Urlkey * @throws Mage_Core_Exception */ protected function _validateEntityUrl($object) { $connection = $object->getResource()->getReadConnection(); $select = $connection->select()->from($this->getAttribute()->getBackendTable(), array('count' => new Zend_Db_Expr('COUNT(\'value_id\')')))->where($connection->quoteInto('entity_id <> ?', $object->getId()))->where($connection->quoteInto('value = ?', $object->getUrlKey())); $result = $connection->fetchOne($select); if ((int) $result) { throw new Mage_Core_Exception(Mage::helper('catalog')->__("Category with the '%s' url_key attribute already exists.", $object->getUrlKey())); } return $this; }
/** * Checks if a category matches criteria: active && url_key not null && included in menu if it has to */ protected static function isCategoryAcceptable(Mage_Catalog_Model_Category $category = null, $mustBeIncludedInNavigation = true) { if (!$category->getIsActive() || is_null($category->getUrlKey()) || $mustBeIncludedInNavigation && !$category->getIncludeInMenu()) { return false; } return true; }
private function getLayoutHandles() { $handlePrefix = 'CATEGORY'; //if we are taking a look at a category page we don't add handles if (in_array('catalog_product_view', $this->layout->getUpdate()->getHandles())) { $handlePrefix = 'PRODUCT_CATEGORY'; } //We add the category paths here, ex: CATEGORY_123 and CATEGORY_12_child $path = array_reverse(array_slice($this->category->getPathIds(), 1, -1)); foreach ($path as $id) { $elem = array_pop($path); $count = count($path); $handle = $handlePrefix . '_' . $elem; for ($i = 0; $i <= $count; $i++) { $handle .= '_child'; } $this->addHandle($handle); $this->addHandle(substr($handle, 0, strrpos($handle, 'child')) . $this->category->getUrlKey()); } }
protected function exportData(Mage_Catalog_Model_Category $category, $file, $depth = 0) { $data = array('id' => $category->getId(), 'parent_id' => $category->getParentId(), 'attribute_set_id' => $category->getAttributeSetId(), 'urlPath' => $category->getUrlPath(), 'urlKey' => $category->getUrlKey(), 'path' => $category->getPath(), 'position' => $category->getPosition(), 'page_layout' => $category->getPageLayout(), 'description' => $category->getDescription(), 'display_mode' => $category->getDisplayMode(), 'is_active' => $category->getIsActive(), 'is_anchor' => $category->getIsAnchor(), 'include_in_menu' => $category->getIncludeInMenu(), 'custom_design' => $category->getCustomDesign(), 'level' => $category->getLevel(), 'name' => $category->getName(), 'metaTitle' => $category->getMetaTitle(), 'metaKeywords' => $category->getMetaKeywords(), 'metaDescription' => $category->getMetaDescription()); echo str_repeat(' ', $depth); echo '* ' . $category->getName() . sprintf(' (%s products)', $category->getProductCount()) . PHP_EOL; fputcsv($file, $data); if ($category->hasChildren()) { $children = Mage::getModel('catalog/category')->getCategories($category->getId()); foreach ($children as $child) { $child = Mage::getModel('catalog/category')->load($child->getId()); $this->exportData($child, $file, $depth + 1); } } }
/** * Write category url_key into enterprise_url_rewrite table * * @param Mage_Catalog_Model_Category $category * @param Mage_Core_Model_Store $store * @return Mage_Catalog_Model_Category */ protected function _reindexCategoryUrlKey(Mage_Catalog_Model_Category $category, Mage_Core_Model_Store $store) { $requestPath = trim($category->getParentUrl(), '/'); $requestPath = (!empty($requestPath) ? $requestPath . '/' : '') . $category->getUrlKey(); $requestPath = $this->_cutRequestPath($requestPath); $urlKeyValue = $this->_getUrlKeyAttributeValueId($category, $store); /* * if this category created on store view level, we should write default url key for this category, * or this category will be unaccessible from frontend */ if (empty($urlKeyValue) && empty($urlKeyValue['value_id'])) { $category = $this->_setUrlKeyForDefaultStore($category, $store); //get url key value id from backend table after changes $urlKeyValue = $this->_getUrlKeyAttributeValueId($category, $store); } $valueId = $urlKeyValue['value_id']; /** * Check if we should insert rewrite into table */ $rewriteRow = $this->_getRewrite($requestPath, $store->getId()); if (!$rewriteRow || $rewriteRow['value_id'] != $valueId) { //get current url path from enterprise_url_rewrite $rewriteForValueId = $this->_getRewriteForValueId($store->getId(), $valueId); $suffix = trim(str_replace($requestPath, '', $category->getRequestPath()), '-'); /* * theoretically we may face with situation when several categories have url_key like: * id url_key request path * 1 abc abc * 2 abc abc-1 * 3 abc abc-2 * and we should reindex category with id 2, we can't be sure should we add prefix or not * so workaround with regexp cover most cases of this problem */ $requestPathIncrement = (int) $this->_getRewriteRequestIncrement($requestPath, $store); if (!$rewriteForValueId || !preg_match('#^(\\d)+$#', $suffix) || $suffix > $requestPathIncrement) { if ($rewriteRow && $rewriteRow['value_id'] != $valueId) { $requestPath .= '-' . ++$requestPathIncrement; } $category = $this->_saveRewrite($category, $store, $requestPath, $valueId); // clean full page cache for category } } // save id of already indexed category into list $this->_indexedCategoryIds[$store->getId()][$category->getId()] = 1; return $category; }
/** * Get url key for category data * * @param Mage_Catalog_Model_Category $category * @return string */ public function _getUrlKey($category) { if ($category instanceof Mage_Catalog_Model_Category) { $urlKey = $category->getUrlKey(); } else { $urlKey = $this->_getCategoryInstance()->setData($category->getData())->getUrlKey(); } return $urlKey; }