Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,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;
Expand Down Expand Up @@ -256,6 +261,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()
Expand All @@ -264,10 +274,6 @@ public function __construct()
->_initAttributes()
->_initAttributeSetId();

/* @var $categoryResource Mage_Catalog_Model_Resource_Category */
$categoryResource = Mage::getModel('catalog/category')->getResource();
$this->_entityTable = $categoryResource->getEntityTable();

}

/**
Expand Down Expand Up @@ -375,12 +381,19 @@ 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.
if (!isset($this->_categoriesWithRoots[$structure[1]])) {
Expand All @@ -394,6 +407,30 @@ protected function _initCategories()
return $this;
}

/**
* Get the position of a category's next child using the path of the
* category
*
* @param $path
* @return int
*/
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.
*
Expand Down Expand Up @@ -474,35 +511,11 @@ protected function _prepareRowForDb(array $rowData)

if (self::SCOPE_DEFAULT == $this->getRowScope($rowData)) {
$rowData['name'] = $this->_getCategoryName($rowData);
$rowData['position'] = $this->_getCategoryPosition($rowData);
}

return $rowData;
}

/**
* Populate position to prevent warning
* try to use existing value to prevent reordering during update
*
* @param $rowData
* @return int
*/
protected function _getCategoryPosition($rowData)
{
if (isset($rowData['position'])) {
return $rowData['position'];
}

$position = 10000;
if (isset($rowData['_root']) && isset($rowData['_category'])
&& isset($this->_categoriesWithRoots[$rowData['_root']][$rowData['_category']])
) {
$position = $this->_categoriesWithRoots[$rowData['_root']][$rowData['_category']]['position'];
}
return $position;
}


/**
* Save category attributes.
*
Expand Down Expand Up @@ -570,16 +583,28 @@ 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]][$rowData[self::COL_CATEGORY]]['position'];

if ($path !== $this->_categoriesWithRoots[$rowData[self::COL_ROOT]][$rowData[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]][$rowData[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
Expand All @@ -589,14 +614,25 @@ protected function _saveCategories()
$entityRow['path'] = $parentCategory['path'] .'/'.$entityId;
$entityRow['entity_type_id'] = $this->_entityTypeId;
$entityRow['attribute_set_id'] = $this->_defaultAttributeSetId;
$entityRowsIn[] = $entityRow;

$this->_newCategory[$rowData[self::COL_ROOT]][$rowData[self::COL_CATEGORY]] = array(
'entity_id' => $entityId,
'path' => $entityRow['path'],
'level' => $entityRow['level']
);
if (!empty($rowData['position'])) {
$entityRow['position'] = $rowData['position'];
} else {
$entityRow['position'] = $parentCategory['next_child_position'];
$parentCategory['next_child_position'] += 1;
}

$entityRowsIn[] = $entityRow;

$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
)
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public function createCategoryTree($values)
$this->assertNotNull($baseCategory);
$this->assertEquals(count($values), $baseCategory->getChildrenCount());

// Assert positions
/** @var Mage_Catalog_Model_Category $categoryModel */
$categoryModel = Mage::getModel('catalog/category');
$topLevel = $categoryModel->loadByAttribute('name', 'Test2');
$firstChild = $categoryModel->loadByAttribute('name', 'TestTest');
$secondChild = $categoryModel->loadByAttribute(
'name',
'Test4 / WithSlash'
);

$this->assertEquals(1, $topLevel->getPosition());
$this->assertEquals(1, $firstChild->getPosition());
$this->assertEquals(2, $secondChild->getPosition());
}

/**
Expand Down