From afa904343eb3a0b57336a811a8af0d3129e9d960 Mon Sep 17 00:00:00 2001 From: Mike Bijnsdorp Date: Tue, 1 Mar 2016 12:34:01 +0100 Subject: [PATCH] Set correct category position if not included in data During initCategories, the position of the next child for every category is retrieved from the db and updated during the import. If the position is not in the import data, the next_child_position is used and updated. When an existing category is moved from one parent to another, it also gets a new position --- .../Model/Import/Entity/Category.php | 80 ++++++++++++++----- 1 file changed, 61 insertions(+), 19 deletions(-) diff --git a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category.php b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category.php index 7d47e885..a0b88294 100644 --- a/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category.php +++ b/src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Category.php @@ -193,6 +193,11 @@ class AvS_FastSimpleImport_Model_Import_Entity_Category extends Mage_ImportExpor protected $_defaultAttributeSetId = 0; + /** + * @var Mage_Catalog_Model_Resource_Category + */ + protected $_categoryResource; + public function setIgnoreDuplicates($ignore) { $this->_ignoreDuplicates = (boolean) $ignore; @@ -255,6 +260,11 @@ public function __construct() { parent::__construct(); + /* @var $categoryResource Mage_Catalog_Model_Resource_Category */ + $categoryResource = Mage::getModel('catalog/category')->getResource(); + $this->_categoryResource = $categoryResource; + $this->_entityTable = $categoryResource->getEntityTable(); + $this ->_initOnTabAttributes() ->_initWebsites() @@ -262,11 +272,6 @@ public function __construct() ->_initCategories() ->_initAttributes() ->_initAttributeSetId(); - - /* @var $categoryResource Mage_Catalog_Model_Resource_Category */ - $categoryResource = Mage::getModel('catalog/category')->getResource(); - $this->_entityTable = $categoryResource->getEntityTable(); - } /** @@ -375,11 +380,14 @@ protected function _initCategories() } $index = $this->_implodeEscaped('/', $path); - $this->_categoriesWithRoots[$rootCategoryName][$index] = array( - 'entity_id' => $category->getId(), - 'path' => $category->getPath(), - 'level' => $category->getLevel(), - 'position' => $category->getPosition() + $this->_categoriesWithRoots[$rootCategoryName][$index] = new Varien_Object( + array( + 'entity_id' => $category->getId(), + 'path' => $category->getPath(), + 'level' => $category->getLevel(), + 'position' => $category->getPosition(), + 'next_child_position' => $this->_getCategoryNextChildPosition($category->getPath()) + ) ); //allow importing by ids. @@ -394,6 +402,26 @@ protected function _initCategories() return $this; } + /** + * Get the position of a category's next child using the path of the category + * + * @param string $path + * + * @return int|string + */ + protected function _getCategoryNextChildPosition($path) + { + static $method; + if (is_null($method)) { + $class = new ReflectionClass('Mage_Catalog_Model_Resource_Category'); + $method = $class->getMethod('_getMaxPosition'); + $method->setAccessible(true); + } + + $position = $method->invoke($this->_categoryResource, $path); + return $position + 1; + } + /** * Initialize stores hash. * @@ -474,7 +502,6 @@ protected function _prepareRowForDb(array $rowData) if (self::SCOPE_DEFAULT == $this->getRowScope($rowData)) { $rowData['name'] = $this->_getCategoryName($rowData); - if (! isset($rowData['position'])) $rowData['position'] = 10000; // diglin - prevent warning message } return $rowData; @@ -548,16 +575,24 @@ protected function _saveCategories() 'level' => $parentCategory['level'] + 1, 'created_at' => empty($rowData['created_at']) ? now() : gmstrftime($strftimeFormat, strtotime($rowData['created_at'])), - 'updated_at' => now(), - 'position' => $rowData['position'] + 'updated_at' => now() ); if (isset($this->_categoriesWithRoots[$rowData[self::COL_ROOT]][$rowData[self::COL_CATEGORY]])) { //edit - $entityId = $this->_categoriesWithRoots[$rowData[self::COL_ROOT]][$rowData[self::COL_CATEGORY]]['entity_id']; + $path = $parentCategory['path'] .'/'.$entityId; + $position = $this->_categoriesWithRoots[$rowData[self::COL_ROOT]][self::COL_CATEGORY]['position']; + if ($path !== $this->_categoriesWithRoots[$rowData[self::COL_ROOT]][self::COL_CATEGORY]['path'] && empty($rowData['position'])) { + // category moved to a new parent + $position = $parentCategory['next_child_position']; + $parentCategory['next_child_position'] += 1; + $this->_categoriesWithRoots[$rowData[self::COL_ROOT]][self::COL_CATEGORY]['path'] = $path; + } + $entityRow['entity_id'] = $entityId; $entityRow['path'] = $parentCategory['path'] .'/'.$entityId; + $entityRow['position'] = !empty($rowData['position']) ? $rowData['position'] : $position; $entityRowsUp[] = $entityRow; $rowData['entity_id'] = $entityId; } else @@ -567,14 +602,21 @@ protected function _saveCategories() $entityRow['path'] = $parentCategory['path'] .'/'.$entityId; $entityRow['entity_type_id'] = $this->_entityTypeId; $entityRow['attribute_set_id'] = $this->_defaultAttributeSetId; + $entityRow['position'] = !empty($rowData['position']) ? $rowData['position'] : $parentCategory['next_child_position']; $entityRowsIn[] = $entityRow; - $this->_newCategory[$rowData[self::COL_ROOT]][$rowData[self::COL_CATEGORY]] = array( - 'entity_id' => $entityId, - 'path' => $entityRow['path'], - 'level' => $entityRow['level'] + $this->_newCategory[$rowData[self::COL_ROOT]][$rowData[self::COL_CATEGORY]] = new Varien_Object( + array( + 'entity_id' => $entityId, + 'path' => $entityRow['path'], + 'level' => $entityRow['level'], + 'next_child_position' => 1 + ) ); + if (empty($rowData['position'])) { + $parentCategory['next_child_position'] += 1; + } } } @@ -1288,7 +1330,7 @@ public function getEntityByCategory($root, $category) protected function _initOnTabAttributes() { if (Mage::helper('core')->isModuleEnabled('OnTap_Merchandiser')) { - $this->_particularAttributes = array_merge($this->_particularAttributes, array( + $this->_particularAttributes = array_merge($this->_particularAttributes, array( '_ontap_heroproducts', '_ontap_attribute', '_ontap_attribute_value',