Skip to content
This repository was archived by the owner on Sep 12, 2023. It is now read-only.

Commit 3f6ec35

Browse files
committed
Improved error messages, added method to continue import after errors
1 parent 49f2576 commit 3f6ec35

File tree

4 files changed

+165
-21
lines changed

4 files changed

+165
-21
lines changed

src/app/code/community/AvS/FastSimpleImport/Model/Import.php

+41-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* @method string getBehavior()
1414
* @method AvS_FastSimpleImport_Model_Import setPartialIndexing(boolean $value)
1515
* @method boolean getPartialIndexing()
16+
* @method AvS_FastSimpleImport_Model_Import setContinueAfterErrors(boolean $value)
17+
* @method boolean getContinueAfterErrors()
1618
* @method array getDropdownAttributes()
1719
*/
1820
class AvS_FastSimpleImport_Model_Import extends Mage_ImportExport_Model_Import
@@ -21,6 +23,7 @@ protected function _construct()
2123
{
2224
$this->setBehavior(self::BEHAVIOR_REPLACE);
2325
$this->setPartialIndexing(false);
26+
$this->setContinueAfterErrors(false);
2427
}
2528

2629
/**
@@ -47,24 +50,23 @@ public function processProductImport($data, $behavior = null)
4750
$validationResult = $this->validateSource($data);
4851
if ($this->getProcessedRowsCount() > 0) {
4952
if (!$validationResult) {
50-
$message = sprintf("Input Data contains %s corrupt records (from a total of %s)",
51-
$this->getInvalidRowsCount(), $this->getProcessedRowsCount()
52-
);
53-
foreach ($this->getErrors() as $type => $lines) {
54-
$message .= "\n:::: " . $type . " ::::\nIn Line(s) " . implode(", ", $lines) . "\n";
53+
if (!$this->getContinueAfterErrors()) {
54+
55+
Mage::throwException($this->getErrorMessage());
5556
}
56-
Mage::throwException($message);
5757
}
5858

59-
if ($this->getPartialIndexing()) {
59+
if ($this->getProcessedRowsCount() > $this->getInvalidRowsCount()) {
60+
if ($this->getPartialIndexing()) {
6061

61-
$this->_prepareDeletedProductsReindex();
62-
$this->importSource();
63-
$this->reindexImportedProducts();
64-
} else {
62+
$this->_prepareDeletedProductsReindex();
63+
$this->importSource();
64+
$this->reindexImportedProducts();
65+
} else {
6566

66-
$this->importSource();
67-
$this->invalidateIndex();
67+
$this->importSource();
68+
$this->invalidateIndex();
69+
}
6870
}
6971
}
7072

@@ -93,15 +95,16 @@ public function processCustomerImport($data, $behavior = null)
9395
$validationResult = $this->validateSource($data);
9496
if ($this->getProcessedRowsCount() > 0) {
9597
if (!$validationResult) {
96-
$message = sprintf("Input Data contains %s corrupt records (from a total of %s)",
97-
$this->getInvalidRowsCount(), $this->getProcessedRowsCount()
98-
);
99-
foreach ($this->getErrors() as $type => $lines) {
100-
$message .= "\n:::: " . $type . " ::::\nIn Line(s) " . implode(", ", $lines) . "\n";
98+
if (!$this->getContinueAfterErrors()) {
99+
100+
Mage::throwException($this->getErrorMessage());
101101
}
102-
Mage::throwException($message);
103102
}
104-
$this->importSource();
103+
104+
if ($this->getProcessedRowsCount() > $this->getInvalidRowsCount()) {
105+
106+
$this->importSource();
107+
}
105108
}
106109

107110
return $this;
@@ -136,6 +139,24 @@ public function getEntityAdapter()
136139
}
137140

138141
/**
142+
* Get single error message as string
143+
*
144+
* @return string
145+
*/
146+
public function getErrorMessage()
147+
{
148+
$message = sprintf("Input Data contains %s corrupt records (from a total of %s)",
149+
$this->getInvalidRowsCount(), $this->getProcessedRowsCount()
150+
);
151+
foreach ($this->getErrors() as $type => $lines) {
152+
$message .= "\n:::: " . $type . " ::::\nIn Line(s) " . implode(", ", $lines) . "\n";
153+
}
154+
return $message;
155+
}
156+
157+
/**
158+
* Get error messages which information in which rows the errors occured
159+
*
139160
* @return array
140161
*/
141162
public function getErrorMessages()

src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Customer.php

+61
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,65 @@ public function setBehavior($behavior)
3232
{
3333
$this->_parameters['behavior'] = $behavior;
3434
}
35+
36+
/**
37+
* Check one attribute. Can be overridden in child.
38+
*
39+
* @param string $attrCode Attribute code
40+
* @param array $attrParams Attribute params
41+
* @param array $rowData Row data
42+
* @param int $rowNum
43+
* @return boolean
44+
*/
45+
public function isAttributeValid($attrCode, array $attrParams, array $rowData, $rowNum)
46+
{
47+
$message = '';
48+
switch ($attrParams['type']) {
49+
case 'varchar':
50+
$val = Mage::helper('core/string')->cleanString($rowData[$attrCode]);
51+
$valid = Mage::helper('core/string')->strlen($val) < self::DB_MAX_VARCHAR_LENGTH;
52+
$message = 'String is too long, only ' . self::DB_MAX_VARCHAR_LENGTH . ' characters allowed.';
53+
break;
54+
case 'decimal':
55+
$val = trim($rowData[$attrCode]);
56+
$valid = (float)$val == $val;
57+
$message = 'Decimal value expected.';
58+
break;
59+
case 'select':
60+
case 'multiselect':
61+
$valid = isset($attrParams['options'][strtolower($rowData[$attrCode])]);
62+
$message = 'Possible options are: ' . implode(', ', array_keys($attrParams['options']));
63+
break;
64+
case 'int':
65+
$val = trim($rowData[$attrCode]);
66+
$valid = (int)$val == $val;
67+
$message = 'Integer value expected.';
68+
break;
69+
case 'datetime':
70+
$val = trim($rowData[$attrCode]);
71+
$valid = strtotime($val) !== false
72+
|| preg_match('/^\d{2}.\d{2}.\d{2,4}(?:\s+\d{1,2}.\d{1,2}(?:.\d{1,2})?)?$/', $val);
73+
$message = 'Datetime value expected.';
74+
break;
75+
case 'text':
76+
$val = Mage::helper('core/string')->cleanString($rowData[$attrCode]);
77+
$valid = Mage::helper('core/string')->strlen($val) < self::DB_MAX_TEXT_LENGTH;
78+
$message = 'String is too long, only ' . self::DB_MAX_TEXT_LENGTH . ' characters allowed.';
79+
break;
80+
default:
81+
$valid = true;
82+
break;
83+
}
84+
85+
if (!$valid) {
86+
$this->addRowError(Mage::helper('importexport')->__("Invalid value for '%s'") . '. ' . $message, $rowNum, $attrCode);
87+
} elseif (!empty($attrParams['is_unique'])) {
88+
if (isset($this->_uniqueAttributes[$attrCode][$rowData[$attrCode]])) {
89+
$this->addRowError(Mage::helper('importexport')->__("Duplicate Unique Attribute for '%s'"), $rowNum, $attrCode);
90+
return false;
91+
}
92+
$this->_uniqueAttributes[$attrCode][$rowData[$attrCode]] = true;
93+
}
94+
return (bool) $valid;
95+
}
3596
}

src/app/code/community/AvS/FastSimpleImport/Model/Import/Entity/Product.php

+62
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,66 @@ public function getDropdownAttributes()
432432
{
433433
return $this->_dropdownAttributes;
434434
}
435+
436+
437+
/**
438+
* Check one attribute. Can be overridden in child.
439+
*
440+
* @param string $attrCode Attribute code
441+
* @param array $attrParams Attribute params
442+
* @param array $rowData Row data
443+
* @param int $rowNum
444+
* @return boolean
445+
*/
446+
public function isAttributeValid($attrCode, array $attrParams, array $rowData, $rowNum)
447+
{
448+
$message = '';
449+
switch ($attrParams['type']) {
450+
case 'varchar':
451+
$val = Mage::helper('core/string')->cleanString($rowData[$attrCode]);
452+
$valid = Mage::helper('core/string')->strlen($val) < self::DB_MAX_VARCHAR_LENGTH;
453+
$message = 'String is too long, only ' . self::DB_MAX_VARCHAR_LENGTH . ' characters allowed.';
454+
break;
455+
case 'decimal':
456+
$val = trim($rowData[$attrCode]);
457+
$valid = (float)$val == $val;
458+
$message = 'Decimal value expected.';
459+
break;
460+
case 'select':
461+
case 'multiselect':
462+
$valid = isset($attrParams['options'][strtolower($rowData[$attrCode])]);
463+
$message = 'Possible options are: ' . implode(', ', array_keys($attrParams['options']));
464+
break;
465+
case 'int':
466+
$val = trim($rowData[$attrCode]);
467+
$valid = (int)$val == $val;
468+
$message = 'Integer value expected.';
469+
break;
470+
case 'datetime':
471+
$val = trim($rowData[$attrCode]);
472+
$valid = strtotime($val) !== false
473+
|| preg_match('/^\d{2}.\d{2}.\d{2,4}(?:\s+\d{1,2}.\d{1,2}(?:.\d{1,2})?)?$/', $val);
474+
$message = 'Datetime value expected.';
475+
break;
476+
case 'text':
477+
$val = Mage::helper('core/string')->cleanString($rowData[$attrCode]);
478+
$valid = Mage::helper('core/string')->strlen($val) < self::DB_MAX_TEXT_LENGTH;
479+
$message = 'String is too long, only ' . self::DB_MAX_TEXT_LENGTH . ' characters allowed.';
480+
break;
481+
default:
482+
$valid = true;
483+
break;
484+
}
485+
486+
if (!$valid) {
487+
$this->addRowError(Mage::helper('importexport')->__("Invalid value for '%s'") . '. ' . $message, $rowNum, $attrCode);
488+
} elseif (!empty($attrParams['is_unique'])) {
489+
if (isset($this->_uniqueAttributes[$attrCode][$rowData[$attrCode]])) {
490+
$this->addRowError(Mage::helper('importexport')->__("Duplicate Unique Attribute for '%s'"), $rowNum, $attrCode);
491+
return false;
492+
}
493+
$this->_uniqueAttributes[$attrCode][$rowData[$attrCode]] = true;
494+
}
495+
return (bool) $valid;
496+
}
435497
}

test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function getUniqueCode($length = "")
3434

3535
// Create/Update products
3636
$data = array();
37-
for ($i = 1; $i <= 10; $i++) {
37+
for ($i = 1; $i <= 6000; $i++) {
3838

3939
$randomString = getUniqueCode(20);
4040
$data[] = array(

0 commit comments

Comments
 (0)