/** * Retrieve categories objects * Either $categoryIds or $path (with ending slash) must be specified * * @param int|array $categoryIds * @param int $storeId * @param string $path * @return array */ protected function _getCategories($categoryIds, $storeId = null, $path = null) { if ($this->_helper()->HideDisabledCategories($storeId)) { $categories = parent::_getCategories($categoryIds, $storeId, $path); if ($categories) { $category = end($categories); $attributes = $this->_getCategoryAttribute('is_active', array_keys($categories), $category->getStoreId()); unset($category); foreach ($attributes as $categoryId => $attributeValue) { if ($attributeValue == 0) { unset($categories[$categoryId]); } } unset($attributes); } return $categories; } return parent::_getCategories($categoryIds, $storeId, $path); }
/** * Retrieve categories objects * Either $categoryIds or $path (with ending slash) must be specified * * @param int|array $categoryIds * @param int $storeId * @param string $path * * @return array */ protected function _getCategories($categoryIds, $storeId = null, $path = null) { if (false === $this->_getHelper()->excludeDisabledCategories($storeId)) { return parent::_getCategories($categoryIds, $storeId, $path); } /** @var Mage_Catalog_Model_Resource_Eav_Attribute $isActiveAttribute */ $isActiveAttribute = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Category::ENTITY, 'is_active'); $categories = array(); $adapter = $this->_getReadAdapter(); if (!is_array($categoryIds)) { $categoryIds = array($categoryIds); } // the method parent::_getCategories has a bug in getCheckSql $isActiveExpr = $adapter->getCheckSql('IFNULL(c.value_id,0) > 0', 'c.value', 'd.value'); $select = $adapter->select()->from(array('main_table' => $this->getTable('catalog/category')), array('main_table.entity_id', 'main_table.parent_id', 'main_table.level', 'is_active' => $isActiveExpr, 'main_table.path')); // Prepare variables for checking whether categories belong to store if ($path === null) { $select->where('main_table.entity_id IN(?)', $categoryIds); } else { // Ensure that path ends with '/', otherwise we can get wrong results - e.g. $path = '1/2' will get '1/20' if (substr($path, -1) != '/') { $path .= '/'; } $select->where('main_table.path LIKE ?', $path . '%')->order('main_table.path'); } $table = $this->getTable(array('catalog/category', 'int')); $select->joinLeft(array('d' => $table), 'd.attribute_id = :attribute_id AND d.store_id = 0 AND d.entity_id = main_table.entity_id', array())->joinLeft(array('c' => $table), 'c.attribute_id = :attribute_id AND c.store_id = :store_id AND c.entity_id = main_table.entity_id', array()); if (true === $this->_getHelper()->excludeDisabledCategories($storeId)) { $select->where($isActiveExpr . '=1'); } if ($storeId !== null) { $rootCategoryPath = $this->getStores($storeId)->getRootCategoryPath(); $rootCategoryPathLength = strlen($rootCategoryPath); } $bind = array('attribute_id' => (int) $isActiveAttribute->getId(), 'store_id' => (int) $storeId); $this->_addCategoryAttributeToSelect($select, 'name', $storeId); $this->_addCategoryAttributeToSelect($select, 'url_key', $storeId); $this->_addCategoryAttributeToSelect($select, 'url_path', $storeId); Mage::dispatchEvent('fastindexer_get_categories_select', array('model' => $this, 'select' => $select, 'store_id' => $storeId)); $rowSet = $adapter->fetchAll($select, $bind); foreach ($rowSet as $row) { if ($storeId !== null) { // Check the category to be either store's root or its descendant // First - check that category's start is the same as root category if (substr($row['path'], 0, $rootCategoryPathLength) !== $rootCategoryPath) { continue; } // Second - check non-root category - that it's really a descendant, not a simple string match if (strlen($row['path']) > $rootCategoryPathLength && $row['path'][$rootCategoryPathLength] !== '/') { continue; } } $category = new Varien_Object($row); $category->setIdFieldName('entity_id'); $category->setStoreId($storeId); $this->_prepareCategoryParentId($category); $categories[$category->getId()] = $category; } unset($rowSet); return $categories; }