/** * Take a Mage_Catalog_Model_Category object and build a category tree from * the child leaf to the root of the category (root > inner child > inner most child) * @param Mage_Catalog_Model_Category $category the inner most child * @return string */ protected function _buildCategoryTree(Mage_Catalog_Model_Category $category) { $collecton = $this->_getCategoriesByIds(explode('/', $category->getPath())); $categories = array(); foreach ($collecton as $cat) { $categories[] = $cat->getName(); } return implode(' > ', array_filter($categories)); }
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); } } }
/** * Get category path as "-" separated string. * * @param Mage_Catalog_Model_Category $category * @return string */ public function getCategoryPathAsString(Mage_Catalog_Model_Category $category) { // Extract the categry IDs from the category path $categoryPathIds = explode('/', $category->getPath()); // Exclude the root category array_shift($categoryPathIds); // Get the name of each category in the category path $categoryNames = array(); foreach ($categoryPathIds as $categoryId) { $categoryNames[] = Mage::getModel('catalog/category')->load($categoryId)->getName(); } // Implode the categories, separated by a " - ", e.g. "Category X - Subcategory Y - Subcategory Z" $categoryPath = implode(' - ', $categoryNames); return $categoryPath; }
/** * Load nodes by parent id * * @param Mage_Catalog_Model_Category|int $parentNode * @param integer $recursionLevel * @param integer $storeId * * @return Mage_Catalog_Model_Resource_Category_Flat */ protected function _fastIndexerLoadNodes($parentNode = null, $recursionLevel = 0, $storeId = 0) { $_conn = $this->_getReadAdapter(); $startLevel = 1; $parentPath = ''; if ($parentNode instanceof Mage_Catalog_Model_Category) { $parentPath = $parentNode->getPath(); $startLevel = $parentNode->getLevel(); } elseif (is_numeric($parentNode)) { $selectParent = $_conn->select()->from($this->getMainStoreTable($storeId))->where('entity_id = ?', $parentNode)->where('store_id = ?', $storeId); $parentNode = $_conn->fetchRow($selectParent); if ($parentNode) { $parentPath = $parentNode['path']; $startLevel = $parentNode['level']; } } $select = $_conn->select()->from(array('main_table' => $this->getMainStoreTable($storeId)), array('entity_id', new Zend_Db_Expr('main_table.' . $_conn->quoteIdentifier('name')), new Zend_Db_Expr('main_table.' . $_conn->quoteIdentifier('path')), 'is_active', 'is_anchor', 'main_table.url_path as request_path'))->where('main_table.is_active = ?', '1')->where('main_table.include_in_menu = ?', '1')->order('main_table.position'); if ($parentPath) { $select->where($_conn->quoteInto("main_table.path like ?", "{$parentPath}/%")); } if ($recursionLevel != 0) { $levelField = $_conn->quoteIdentifier('level'); $select->where($levelField . ' <= ?', $startLevel + $recursionLevel); } $inactiveCategories = $this->getInactiveCategoryIds(); if (!empty($inactiveCategories)) { $select->where('main_table.entity_id NOT IN (?)', $inactiveCategories); } // Allow extensions to modify select (e.g. add custom category attributes to select) Mage::dispatchEvent('catalog_category_flat_loadnodes_before', array('select' => $select)); $arrNodes = $_conn->fetchAll($select); $nodes = array(); foreach ($arrNodes as $node) { $node['id'] = $node['entity_id']; $nodes[$node['id']] = Mage::getModel('catalog/category')->setData($node); } return $nodes; }
/** * Return children ids of category * * @param Mage_Catalog_Model_Category $category * @param boolean $recursive * @return array */ public function getChildren($category, $recursive = true) { $attributeId = (int) $this->_getIsActiveAttributeId(); $backendTable = $this->getTable(array($this->getEntityTablePrefix(), 'int')); $adapter = $this->_getReadAdapter(); $checkSql = $adapter->getCheckSql('c.value_id > 0', 'c.value', 'd.value'); $bind = array('attribute_id' => $attributeId, 'store_id' => $category->getStoreId(), 'scope' => 1, 'c_path' => $category->getPath() . '/%'); $select = $this->_getReadAdapter()->select()->from(array('m' => $this->getEntityTable()), 'entity_id')->joinLeft(array('d' => $backendTable), 'd.attribute_id = :attribute_id AND d.store_id = 0 AND d.entity_id = m.entity_id', array())->joinLeft(array('c' => $backendTable), 'c.attribute_id = :attribute_id AND c.store_id = :store_id AND c.entity_id = m.entity_id', array())->where($checkSql . ' = :scope')->where($adapter->quoteIdentifier('path') . ' LIKE :c_path'); if (!$recursive) { $select->where($adapter->quoteIdentifier('level') . ' <= :c_level'); $bind['c_level'] = $category->getLevel() + 1; } return $adapter->fetchCol($select, $bind); }
/** * Return children ids of category * * @param Mage_Catalog_Model_Category $category * @param integer $level * @return array */ public function getChildren($category, $recursive = true, $isActive = true) { $select = $this->_getReadAdapter()->select()->from($this->getMainStoreTable($category->getStoreId()), 'entity_id')->where('path LIKE ?', "{$category->getPath()}/%"); if (!$recursive) { $select->where('level <= ?', $category->getLevel() + 1); } if ($isActive) { $select->where('is_active = ?', '1'); } $_categories = $this->_getReadAdapter()->fetchAll($select); $categoriesIds = array(); foreach ($_categories as $_category) { $categoriesIds[] = $_category['entity_id']; } return $categoriesIds; }
/** * Check whether specified category is allowed * * @param Mage_Catalog_Model_Category $category * @return bool */ protected function _isCategoryAllowed($category) { if (!$category->getId()) { return false; } $categoryPath = $category->getPath(); foreach ($this->_role->getAllowedRootCategories() as $rootPath) { if ($categoryPath === $rootPath || 0 === strpos($categoryPath, "{$rootPath}/")) { return true; } } return false; }
/** * Get count of active/not active children categories * * @param Mage_Catalog_Model_Category $category * @param bool $isActiveFlag * @return int */ public function getChildrenAmount($category, $isActiveFlag = true) { $storeId = Mage::app()->getStore()->getId(); $attributeId = $this->_getIsActiveAttributeId(); $table = $this->getTable(array($this->getEntityTablePrefix(), 'int')); $adapter = $this->_getReadAdapter(); $checkSql = $adapter->getCheckSql('c.value_id > 0', 'c.value', 'd.value'); $bind = array('attribute_id' => $attributeId, 'store_id' => $storeId, 'active_flag' => $isActiveFlag, 'c_path' => $category->getPath() . '/%'); $select = $adapter->select()->from(array('m' => $this->getEntityTable()), array('COUNT(m.entity_id)'))->joinLeft(array('d' => $table), 'd.attribute_id = :attribute_id AND d.store_id = 0 AND d.entity_id = m.entity_id', array())->joinLeft(array('c' => $table), "c.attribute_id = :attribute_id AND c.store_id = :store_id AND c.entity_id = m.entity_id", array())->where('m.path LIKE :c_path')->where($checkSql . ' = :active_flag'); return $this->_getReadAdapter()->fetchOne($select, $bind); }
/** * Return children ids of category * * @param Mage_Catalog_Model_Category $category * @param boolean $recursive * @return array */ public function getChildren($category, $recursive = true) { $attributeId = $this->_getIsActiveAttributeId(); $select = $this->_getReadAdapter()->select()->from(array('m' => $this->getEntityTable()), 'entity_id')->joinLeft(array('d' => $this->getEntityTable() . '_int'), "d.attribute_id = '{$attributeId}' AND d.store_id = 0 AND d.entity_id = m.entity_id", array())->joinLeft(array('c' => $this->getEntityTable() . '_int'), "c.attribute_id = '{$attributeId}' AND c.store_id = '{$category->getStoreId()}' AND c.entity_id = m.entity_id", array())->where('(IFNULL(c.value, d.value) = ?)', '1')->where('path LIKE ?', "{$category->getPath()}/%"); if (!$recursive) { $select->where('level <= ?', $category->getLevel() + 1); } $_categories = $this->_getReadAdapter()->fetchAll($select); $categoriesIds = array(); foreach ($_categories as $_category) { $categoriesIds[] = $_category['entity_id']; } return $categoriesIds; // $this->_getTree()->load(); // return $this->_getTree()->getChildren($category->getId(), false); }
/** * Get count of active/not active children categories * * @param Mage_Catalog_Model_Category $category * @param bool $isActiveFlag * @return int */ public function getChildrenAmount($category, $isActiveFlag = true) { $storeId = Mage::app()->getStore()->getId(); $attributeId = $this->_getIsActiveAttributeId(); $table = Mage::getSingleton('core/resource')->getTableName('catalog/category') . '_int'; $select = $this->_getReadAdapter()->select()->from(array('m' => $this->getEntityTable()), array('COUNT(m.entity_id)'))->joinLeft(array('d' => $table), "d.attribute_id = '{$attributeId}' AND d.store_id = 0 AND d.entity_id = m.entity_id", array())->joinLeft(array('c' => $table), "c.attribute_id = '{$attributeId}' AND c.store_id = '{$storeId}' AND c.entity_id = m.entity_id", array())->where('m.path like ?', $category->getPath() . '/%')->where('(IFNULL(c.value, d.value) = ?)', $isActiveFlag); return $this->_getReadAdapter()->fetchOne($select); }
/** * Apply event to category * * @param Varien_Data_Tree_Node|Mage_Catalog_Model_Category $category * @param Varien_Data_Collection $eventCollection * @return Enterprise_CatalogEvent_Model_Observer */ protected function _applyEventToCategory($category, Varien_Data_Collection $eventCollection) { foreach (array_reverse($this->_parseCategoryPath($category->getPath())) as $categoryId) { // Walk through category path, search event for category $event = $eventCollection->getItemByColumnValue('category_id', $categoryId); if ($event) { $category->setEvent($event); return $this; } } return $this; }
/** * Validate catalog category save * * @param Mage_Catalog_Model_Category $model */ public function catalogCategorySaveBefore($model) { if (!$model->getId()) { return; } // No saving to wrong stores if (!$this->_role->hasStoreAccess($model->getStoreIds())) { $this->_throwSave(); } // No saving under disallowed root categories $categoryPath = $model->getPath(); $allowed = false; foreach ($this->_role->getAllowedRootCategories() as $rootPath) { if ($categoryPath != $rootPath) { if (0 === strpos($categoryPath, "{$rootPath}/")) { $allowed = true; } } else { if ($this->_role->hasExclusiveCategoryAccess($rootPath)) { $allowed = true; } } if ($allowed) { break; } } if (!$allowed) { $this->_throwSave(); } }
/** * Get a parent category for a (disabled) category * * @param Mage_Catalog_Model_Category $category * @return string Redirect url */ protected function getRedirectForCategory(Mage_Catalog_Model_Category $category) { $url = Mage::getBaseUrl(); $path = array_reverse(explode('/', $category->getPath())); $blacklist = explode(',', Mage::getStoreConfig(self::XML_PATH_REDIRECT_BLACKLIST_CATEGORY_IDS)); array_walk($blacklist, create_function('&$val', '$val = trim($val);')); $blacklist = array_filter($blacklist); $root_id = Mage::app()->getGroup()->getRootCategoryId(); foreach ($path as $id) { // When category root is reached return base url if ($id == $root_id) { return $url; } // Skip blacklisted categories if (in_array($id, $blacklist)) { continue; } $category = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('entity_id', $id)->addUrlRewriteToResult()->addIsActiveFilter()->setPageSize(1)->setCurPage(1)->getFirstItem(); if ($category->getEntityId()) { return $url . $category->getRequestPath(); } } return $url; }