Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi behavior patch #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
90 changes: 48 additions & 42 deletions code/Model/Import/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,32 @@ public function importEntities($entities, $entityType = null, $behavior = null)
* Import attributes and put them in attribute sets and attribute group
*
* @param array $data
* @param string $behavior
* @param string $behavior comma-separated list of behaviors
*
* @return true
*/
public function importAttributes(array $data, $behavior = null)
{
if (null === $behavior) {
$behavior = Mage_ImportExport_Model_Import::BEHAVIOR_APPEND;
}
// default behavior is BEHAVIOR_APPEND, convert comma separated behavior list to array
$behaviors = is_null ( $behavior ) ? array (Mage_ImportExport_Model_Import::BEHAVIOR_APPEND ) : explode ( ',', $behavior );

$this->_init();

if (Danslo_ApiImport_Model_Import::BEHAVIOR_DELETE_IF_NOT_EXIST === $behavior) {
$this->_pruneAttributes($data);
} else {
foreach ($data as $attribute) {
if (isset($attribute['attribute_id'])) {
$attributeCode = $attribute['attribute_id'];
unset($attribute['attribute_id']);

if (Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE === $behavior
|| Mage_ImportExport_Model_Import::BEHAVIOR_APPEND === $behavior) {
$this->_setup->addAttribute($this->_catalogProductEntityTypeId, $attributeCode, $attribute);
} elseif (Mage_ImportExport_Model_Import::BEHAVIOR_DELETE === $behavior) {
$this->_setup->removeAttribute($this->_catalogProductEntityTypeId, $attributeCode);

foreach ($behaviors as $behavior) {

if (Danslo_ApiImport_Model_Import::BEHAVIOR_DELETE_IF_NOT_EXIST === $behavior) {
$this->_pruneAttributes($data);
} else {
foreach ($data as $attribute) {
if (isset($attribute['attribute_id'])) {
$attributeCode = $attribute['attribute_id'];
unset($attribute['attribute_id']);

if (Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE === $behavior|| Mage_ImportExport_Model_Import::BEHAVIOR_APPEND === $behavior) {
$this->_setup->addAttribute($this->_catalogProductEntityTypeId, $attributeCode, $attribute);
} elseif (Mage_ImportExport_Model_Import::BEHAVIOR_DELETE === $behavior) {
$this->_setup->removeAttribute($this->_catalogProductEntityTypeId, $attributeCode);
}
}
}
}
Expand All @@ -117,24 +119,26 @@ public function importAttributes(array $data, $behavior = null)
* Compute attributes sets and their groups and import them
*
* @param array $data
* @param string $behavior
* @param string $behavior comma-separated list of behaviors
*
* @return true
*/
public function importAttributeSets(array $data, $behavior = null)
{
if (null === $behavior) {
$behavior = Mage_ImportExport_Model_Import::BEHAVIOR_APPEND;
}
// default behavior is BEHAVIOR_APPEND, convert comma separated behavior list to array
$behaviors = is_null ( $behavior ) ? array (Mage_ImportExport_Model_Import::BEHAVIOR_APPEND ) : explode ( ',', $behavior );

$this->_init();

if (Mage_ImportExport_Model_Import::BEHAVIOR_DELETE === $behavior) {
$this->_removeAttributeSets($data);
} elseif (Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE === $behavior
|| Mage_ImportExport_Model_Import::BEHAVIOR_APPEND === $behavior) {
$this->_updateAttributeSets($data);
} elseif (Danslo_ApiImport_Model_Import::BEHAVIOR_DELETE_IF_NOT_EXIST === $behavior) {
$this->_pruneAttributeSets($data);

foreach ($behaviors as $behavior) {

if (Mage_ImportExport_Model_Import::BEHAVIOR_DELETE === $behavior) {
$this->_removeAttributeSets($data);
} elseif (Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE === $behavior|| Mage_ImportExport_Model_Import::BEHAVIOR_APPEND === $behavior) {
$this->_updateAttributeSets($data);
} elseif (Danslo_ApiImport_Model_Import::BEHAVIOR_DELETE_IF_NOT_EXIST === $behavior) {
$this->_pruneAttributeSets($data);
}
}

return true;
Expand All @@ -144,24 +148,26 @@ public function importAttributeSets(array $data, $behavior = null)
* Links attributes to attributes group and attribute sets
*
* @param array $data
* @param string $behavior
* @param string $behavior comma-separated list of behaviors
*
* @return bool
*/
public function importAttributeAssociations(array $data, $behavior = null)
{
if (null === $behavior) {
$behavior = Mage_ImportExport_Model_Import::BEHAVIOR_APPEND;
}
// default behavior is BEHAVIOR_APPEND, convert comma separated behavior list to array
$behaviors = is_null ( $behavior ) ? array (Mage_ImportExport_Model_Import::BEHAVIOR_APPEND ) : explode ( ',', $behavior );

$this->_init();

if (Mage_ImportExport_Model_Import::BEHAVIOR_DELETE === $behavior) {
$this->_removeAttributeFromGroup($data);
} elseif (Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE === $behavior
|| Mage_ImportExport_Model_Import::BEHAVIOR_APPEND === $behavior) {
$this->_updateAttributeAssociations($data);
} elseif (Danslo_ApiImport_Model_Import::BEHAVIOR_DELETE_IF_NOT_EXIST === $behavior) {
$this->_pruneAttributesFromAttributeSets($data);

foreach ($behaviors as $behavior) {

if (Mage_ImportExport_Model_Import::BEHAVIOR_DELETE === $behavior) {
$this->_removeAttributeFromGroup($data);
} elseif (Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE === $behavior|| Mage_ImportExport_Model_Import::BEHAVIOR_APPEND === $behavior) {
$this->_updateAttributeAssociations($data);
} elseif (Danslo_ApiImport_Model_Import::BEHAVIOR_DELETE_IF_NOT_EXIST === $behavior) {
$this->_pruneAttributesFromAttributeSets($data);
}
}

return true;
Expand Down