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); } } }
/** * Recursively apply custom design settings to category if it's option * custom_use_parent_settings is setted to 1 while parent option is not * * @deprecated after 1.4.2.0-beta1, functionality moved to Mage_Catalog_Model_Design * @param Mage_Catalog_Model_Category $category * @param Mage_Core_Model_Layout_Update $update * * @return Mage_Catalog_CategoryController */ protected function _applyCustomDesignSettings($category, $update) { if ($category->getCustomUseParentSettings() && $category->getLevel() > 1) { $parentCategory = $category->getParentCategory(); if ($parentCategory && $parentCategory->getId()) { return $this->_applyCustomDesignSettings($parentCategory, $update); } } $validityDate = $category->getCustomDesignDate(); if (array_key_exists('from', $validityDate) && array_key_exists('to', $validityDate) && Mage::app()->getLocale()->isStoreDateInInterval(null, $validityDate['from'], $validityDate['to'])) { if ($category->getPageLayout()) { $this->getLayout()->helper('page/layout')->applyHandle($category->getPageLayout()); } $update->addUpdate($category->getCustomLayoutUpdate()); } return $this; }
/** * Recursively returns a value / label array of all active categories * @param Mage_Catalog_Model_Category $category * @param String $parentname * @return array */ private function getChildCategories($category, $parentname = '') { //category not active - skip it if (!$category->getIsActive()) { return ''; } //array containing all the categories to return $ret = array(); /* Add the current category to return array * Root categories shouldn't be selected */ if ($category->getLevel() > 1) { $ret[$category->getID()] = $category->getName() . $parentname; } // get all children if (Mage::helper('catalog/category_flat')->isEnabled()) { $children = (array) $category->getChildrenNodes(); $childrenCount = count($children); } else { $children = $category->getChildrenCategories(); $childrenCount = $children->count(); } $hasChildren = $children && $childrenCount; // select active children $activeChildren = array(); foreach ($children as $child) { if ($child->getIsActive()) { $activeChildren[] = $child; } } $activeChildrenCount = count($activeChildren); $hasActiveChildren = $activeChildrenCount > 0; /** * Use recursion to include all children categories too */ foreach ($activeChildren as $child) { $childarray = $this->getChildCategories($child, " / " . $category->getName() . $parentname); foreach ($childarray as $k => $v) { $ret[$k] = $v; } } return $ret; }
/** * 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; }
/** * Recursively returns a value / label array of all active categories * * @param Mage_Catalog_Model_Category $category * @param String $parentname * @return array */ private function getChildCategories($category, $parentname = '') { //category not active - skip it if (!$category->getIsActive()) { return ''; } //array containing all the categories to return $ret = array(); /* Add the current category to return array * Root categories shouldn't be selected */ if ($category->getLevel() > 1) { $ret[$category->getID()] = ltrim($parentname . " / " . $category->getName(), " / "); } // get all children $children = $category->getChildrenCategories(); $childrenCount = $children->count(); $hasChildren = $children && $childrenCount; // select active children $activeChildren = array(); foreach ($children as $child) { if ($child->getIsActive()) { $activeChildren[] = $child; } } $activeChildrenCount = count($activeChildren); $hasActiveChildren = $activeChildrenCount > 0; /** * Use recursion to include all children categories too */ foreach ($activeChildren as $child) { $childarray = $this->getChildCategories($child, $parentname . " / " . $category->getName()); foreach ($childarray as $k => $v) { $ret[$k] = ltrim($v, " / "); } } return $ret; }
/** * 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 level of indent * * @param Mage_Catalog_Model_Category $item * @param integer $delta * @return integer */ public function getLevel($item, $delta = 1) { return (int) ($item->getLevel() - $this->_storeRootCategoryLevel - 1) * $delta; }
/** * 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; }
public function testGetLevel() { $this->assertEquals(0, $this->_model->getLevel()); $this->_model->setData('level', 1); $this->assertEquals(1, $this->_model->getLevel()); }
/** * 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); }
public function getObject(Mage_Catalog_Model_Category $category) { /** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */ $productCollection = $category->getProductCollection(); $category->setProductCount($productCollection->addMinimalPrice()->count()); $transport = new Varien_Object(); Mage::dispatchEvent('algolia_category_index_before', array('category' => $category, 'custom_data' => $transport)); $customData = $transport->getData(); $storeId = $category->getStoreId(); $category->getUrlInstance()->setStore($storeId); $path = ''; foreach ($category->getPathIds() as $categoryId) { if ($path != '') { $path .= ' / '; } $path .= $this->getCategoryName($categoryId, $storeId); } $image_url = NULL; try { $image_url = $category->getImageUrl(); } catch (Exception $e) { /* no image, no default: not fatal */ } $data = array('objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), '_tags' => array('category'), 'popularity' => 1, 'product_count' => $category->getProductCount()); if (!empty($image_url)) { $data['image_url'] = $image_url; } foreach ($this->config->getCategoryAdditionalAttributes($storeId) as $attribute) { $value = $category->getData($attribute['attribute']); $attribute_ressource = $category->getResource()->getAttribute($attribute['attribute']); if ($attribute_ressource) { $value = $attribute_ressource->getFrontend()->getValue($category); } if (isset($data[$attribute['attribute']])) { $value = $data[$attribute['attribute']]; } if ($value) { $data[$attribute['attribute']] = $value; } } $data = array_merge($data, $customData); foreach ($data as &$data0) { $data0 = $this->try_cast($data0); } return $data; }