Skip to content

Commit

Permalink
Merge branch 'develop' into fix-category-position
Browse files Browse the repository at this point in the history
  • Loading branch information
mbijnsdorp authored Jun 7, 2017
2 parents afa9043 + 0d16aca commit a458252
Show file tree
Hide file tree
Showing 15 changed files with 407 additions and 109 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "magento-module",
"description": "Wrapper for Magento ImportExport functionality, which imports products and customers from arrays",
"homepage": "https://github.com/avstudnitz/AvS_FastSimpleImport",
"require": {
"suggest": {
"magento-hackathon/magento-composer-installer": "*"
}
}
Expand Down
3 changes: 2 additions & 1 deletion modman
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src/app/code/community/AvS/FastSimpleImport app/code/community/AvS/FastSimpleImport
src/app/etc/modules/AvS_FastSimpleImport.xml app/etc/modules/AvS_FastSimpleImport.xml
src/app/etc/modules/AvS_FastSimpleImport.xml app/etc/modules/AvS_FastSimpleImport.xml
src/shell/import.php shell/import.php
21 changes: 20 additions & 1 deletion src/app/code/community/AvS/FastSimpleImport/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,23 @@
*/

class AvS_FastSimpleImport_Helper_Data extends Mage_Core_Helper_Abstract
{}
{

/**
* Returns str with all alphabetic characters converted to lowercase.
* Uses the multibyte function variants if available.
*
* @param string $str the string being lowercased
*
* @return string str with all alphabetic characters converted to lowercase
*/
public function strtolower($str)
{
if (function_exists('mb_strtolower') && function_exists('mb_detect_encoding')) {
return mb_strtolower($str, mb_detect_encoding($str));
}

return strtolower($str);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AvS_FastSimpleImport_Model_Import_Entity_Category extends Mage_ImportExpor
* Size of bunch - part of entities to save in one step.
*/
const BUNCH_SIZE = 20;
protected $_useConfigAttributes = array('available_sort_by', 'default_sort_by', 'filter_price_range');

/**
* Data row scopes.
Expand Down Expand Up @@ -305,7 +306,6 @@ public function getCategoriesWithRoots() {
return $this->_categoriesWithRoots;
}


protected function _explodeEscaped($delimiter = '/', $string)
{
$exploded = explode($delimiter, $string);
Expand Down Expand Up @@ -502,11 +502,34 @@ 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 @@ -653,9 +676,9 @@ protected function _saveCategories()
$storeIds = array(0);

if ('select' == $attrParams['type']) {
if (isset($attrParams['options'][strtolower($attrValue)]))
if (isset($attrParams['options'][Mage::helper('fastsimpleimport')->strtolower($attrValue)]))
{
$attrValue = $attrParams['options'][strtolower($attrValue)];
$attrValue = $attrParams['options'][Mage::helper('fastsimpleimport')->strtolower($attrValue)];
}
} elseif ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
$attrValue = gmstrftime($strftimeFormat, strtotime($attrValue));
Expand Down Expand Up @@ -876,6 +899,21 @@ protected function _getCategoryName($rowData)
return end($categoryParts);
}

protected function isAttributeUsingConfig($attrCode, $rowData)
{
if (!in_array($attrCode, $this->_useConfigAttributes)) {
return false;
}

$key = 'use_config_' . $attrCode;

if (!empty($rowData[$key])) {
return true;
}

return false;
}

/**
* Validate data row.
*
Expand Down Expand Up @@ -962,6 +1000,9 @@ public function validateRow(array $rowData, $rowNum)
foreach ($this->_attributes as $attrCode => $attrParams) {
if (isset($rowData[$attrCode]) && strlen($rowData[$attrCode])) {
$this->isAttributeValid($attrCode, $attrParams, $rowData, $rowNum);
} elseif ($attrParams['is_required'] && in_array($attrCode, $this->_indexValueAttributes)) {
// empty value is allowed for these "required attributes" using the tickbox
continue;
} elseif ($attrParams['is_required'] && !isset($this->_categoriesWithRoots[$root][$category])) {
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNum, $attrCode);
}
Expand Down Expand Up @@ -1033,7 +1074,7 @@ public function isAttributeValid($attrCode, array $attrParams, array $rowData, $
break;
case 'select':
case 'multiselect':
$valid = isset($attrParams['options'][strtolower($rowData[$attrCode])]);
$valid = isset($attrParams['options'][Mage::helper('fastsimpleimport')->strtolower($rowData[$attrCode])]);
$message = 'Possible options are: ' . implode(', ', array_keys($attrParams['options'])) . '. Your input: ' . $rowData[$attrCode];
break;
case 'int':
Expand Down Expand Up @@ -1202,11 +1243,12 @@ protected function _reindexUpdatedCategories()
protected function _getProcessedCategoryIds()
{
$categoryIds = array();
$source = $this->getSource();
$coreHelper = Mage::helper("core");
$source = $this->_getSource();

$source->rewind();
while ($source->valid()) {
$current = $source->current();
$current = $coreHelper->unEscapeCSVData($source->current());
if (isset($this->_newCategory[$current[self::COL_ROOT]][$current[self::COL_CATEGORY]])) {
$categoryIds[] = $this->_newCategory[$current[self::COL_ROOT]][$current[self::COL_CATEGORY]];
} elseif (isset($this->_categoriesWithRoots[$current[self::COL_ROOT]][$current[self::COL_CATEGORY]])) {
Expand Down Expand Up @@ -1249,6 +1291,7 @@ protected function _filterRowData(&$rowData)
*/
protected function _saveValidatedBunches()
{
$coreHelper = Mage::helper("core");
$source = $this->_getSource();
$bunchRows = array();
$startNewBunch = false;
Expand All @@ -1273,7 +1316,7 @@ protected function _saveValidatedBunches()
if ($this->_errorsCount >= $this->_errorsLimit) { // errors limit check
return $this;
}
$rowData = $source->current();
$rowData = $coreHelper->unEscapeCSVData($source->current());

$this->_processedRowsCount++;

Expand Down Expand Up @@ -1460,4 +1503,24 @@ protected function _saveOnTab()
}
return $this;
}

/**
* New categories data.
*
* @return array
*/
public function getNewCategory()
{
return $this->_newCategory;
}

/**
* Existing categories getter.
*
* @return array
*/
public function getOldCategory()
{
return $this->getCategoriesWithRoots();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,6 @@ protected function _initWorkBunch(array $bunch)
}
}

if ($this->getBehavior() == Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE && count($categoryIds) > 0) {
$this->getConnection()->query(
$this->getConnection()->quoteInto(
"DELETE FROM `{$this->_entityTable}` WHERE `category_id` IN (?)", $categoryIds
)
);
}

if (count($skus) > 0) {
/** @var Varien_Db_Statement_Pdo_Mysql $result */
$result = $this->getConnection()->query(
Expand All @@ -306,6 +298,14 @@ protected function _initWorkBunch(array $bunch)
$this->_skuEntityIds[$row['sku']] = (int)$row['entity_id'];
}
}

if ($this->getBehavior() == Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE && count($this->_skuEntityIds) > 0) {
$this->getConnection()->query(
$this->getConnection()->quoteInto(
"DELETE FROM `{$this->_entityTable}` WHERE `product_id` IN (?)", array_values($this->_skuEntityIds)
)
);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ protected function _saveWishlists() {
if (self::SCOPE_DEFAULT == $this->getRowScope($rowData)) {
$wishlist = array();

$emailToLower = strtolower($rowData[self::COL_EMAIL]);
$emailToLower = Mage::helper('fastsimpleimport')->strtolower($rowData[self::COL_EMAIL]);
if (isset($oldCustomersToLower[$emailToLower][$rowData[self::COL_WEBSITE]])) {
$wishlist['customer_id'] = $oldCustomersToLower[$emailToLower][$rowData[self::COL_WEBSITE]];
} elseif (isset($newCustomersToLower[$emailToLower][$rowData[self::COL_WEBSITE]])) {
Expand Down Expand Up @@ -311,7 +311,7 @@ protected function _saveCustomers()
'updated_at' => now()
);

$emailToLower = strtolower($rowData[self::COL_EMAIL]);
$emailToLower = Mage::helper('fastsimpleimport')->strtolower($rowData[self::COL_EMAIL]);
if (isset($oldCustomersToLower[$emailToLower][$rowData[self::COL_WEBSITE]])) { // edit
$entityId = $oldCustomersToLower[$emailToLower][$rowData[self::COL_WEBSITE]];
$entityRow['entity_id'] = $entityId;
Expand All @@ -337,8 +337,8 @@ protected function _saveCustomers()
$attrParams = $this->_attributes[$attrCode];

if ('select' == $attrParams['type']) {
if (isset($attrParams['options'][strtolower($value)])) {
$value = $attrParams['options'][strtolower($value)];
if (isset($attrParams['options'][Mage::helper('fastsimpleimport')->strtolower($value)])) {
$value = $attrParams['options'][Mage::helper('fastsimpleimport')->strtolower($value)];
}
} elseif ('datetime' == $attrParams['type']) {
$value = gmstrftime($strftimeFormat, strtotime($value));
Expand Down Expand Up @@ -412,7 +412,7 @@ public function isAttributeValid($attrCode, array $attrParams, array $rowData, $
$message = 'Decimal value expected. Your Input: '.$rowData[$attrCode];
break;
case 'select':
$valid = isset($attrParams['options'][strtolower($rowData[$attrCode])]);
$valid = isset($attrParams['options'][Mage::helper('fastsimpleimport')->strtolower($rowData[$attrCode])]);
$message = 'Possible options are: ' . implode(', ', array_keys($attrParams['options'])) . '. Your input: ' . $rowData[$attrCode];
break;
case 'int':
Expand Down Expand Up @@ -485,7 +485,7 @@ public function validateRow(array $rowData, $rowNum)
$this->addRowError(self::ERROR_EMAIL_SITE_NOT_FOUND, $rowNum);
}
} elseif (self::SCOPE_DEFAULT == $rowScope) { // row is SCOPE_DEFAULT = new customer block begins
$email = strtolower($rowData[self::COL_EMAIL]);
$email = Mage::helper('fastsimpleimport')->strtolower($rowData[self::COL_EMAIL]);
$website = $rowData[self::COL_WEBSITE];

if (!Zend_Validate::is($email, 'EmailAddress')) {
Expand Down Expand Up @@ -683,4 +683,24 @@ public function getEntityByEmail($email)
}
return false;
}

/**
* New customers data.
*
* @return array
*/
public function getNewCustomers()
{
return $this->_newCustomers;
}

/**
* Existing customers getter.
*
* @return array
*/
public function getOldCustomers()
{
return $this->_oldCustomers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function _importData()
foreach ($this->_attributes as $attrAlias => $attrParams) {
if (isset($rowData[$attrAlias]) && strlen($rowData[$attrAlias])) {
if ('select' == $attrParams['type']) {
$value = $attrParams['options'][strtolower($rowData[$attrAlias])];
$value = $attrParams['options'][Mage::helper('fastsimpleimport')->strtolower($rowData[$attrAlias])];
} elseif ('datetime' == $attrParams['type']) {
$value = gmstrftime($strftimeFormat, strtotime($rowData[$attrAlias]));
} else {
Expand Down Expand Up @@ -105,8 +105,8 @@ protected function _importData()
}
// let's try to find region ID
if (!empty($rowData[$regionColName])) {
$countryNormalized = strtolower($rowData[$countryColName]);
$regionNormalized = strtolower($rowData[$regionColName]);
$countryNormalized = Mage::helper('fastsimpleimport')->strtolower($rowData[$countryColName]);
$regionNormalized = Mage::helper('fastsimpleimport')->strtolower($rowData[$regionColName]);

if (isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
$regionId = $this->_countryRegions[$countryNormalized][$regionNormalized];
Expand Down
Loading

0 comments on commit a458252

Please sign in to comment.