Fix bugs and add import count handle
modifié: core/lib/Thelia/Controller/Admin/ImportController.php modifié: core/lib/Thelia/Core/FileFormat/Formatting/Formatter/CSVFormatter.php modifié: core/lib/Thelia/ImportExport/Import/ImportHandler.php modifié: core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php modifié: core/lib/Thelia/Tests/FileFormat/Formatting/Formatter/CSVFormatterTest.php
This commit is contained in:
@@ -301,7 +301,12 @@ class ImportController extends BaseAdminController
|
||||
);
|
||||
}
|
||||
|
||||
return $this->getTranslator()->trans("Import successfully done");
|
||||
return $this->getTranslator()->trans(
|
||||
"Import successfully done, %numb row(s) have been imported",
|
||||
[
|
||||
"%numb" => $handler->getImportedRows(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,10 +12,8 @@
|
||||
|
||||
namespace Thelia\Core\FileFormat\Formatting\Formatter;
|
||||
use Thelia\Core\FileFormat\Formatting\AbstractFormatter;
|
||||
use Thelia\Core\FileFormat\Formatting\Exception\BadFormattedStringException;
|
||||
use Thelia\Core\FileFormat\Formatting\FormatterData;
|
||||
use Thelia\Core\FileFormat\FormatType;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
|
||||
/**
|
||||
* Class CSVFormatter
|
||||
@@ -25,7 +23,7 @@ use Thelia\Core\Translation\Translator;
|
||||
class CSVFormatter extends AbstractFormatter
|
||||
{
|
||||
public $delimiter = ";";
|
||||
public $lineReturn = "\r\n";
|
||||
public $lineReturn = "\n";
|
||||
public $stringDelimiter = "\"";
|
||||
|
||||
/**
|
||||
@@ -126,6 +124,7 @@ class CSVFormatter extends AbstractFormatter
|
||||
|
||||
if (count($raw) > 0) {
|
||||
$keys = explode($this->delimiter, array_shift($raw));
|
||||
$keysLength = count($keys);
|
||||
|
||||
foreach ($keys as &$key) {
|
||||
$key = trim($key, $this->stringDelimiter);
|
||||
@@ -137,18 +136,19 @@ class CSVFormatter extends AbstractFormatter
|
||||
$newRow = [];
|
||||
$row = explode($this->delimiter, $row);
|
||||
|
||||
for ($i = 0; $i < $columns; ++$i) {
|
||||
$value = trim($row[$i], $this->stringDelimiter);
|
||||
if (count($row) >= $keysLength) {
|
||||
for ($i = 0; $i < $columns; ++$i) {
|
||||
$value = trim($row[$i], $this->stringDelimiter);
|
||||
|
||||
if (false !== $unserialized = @unserialize($row[$i])) {
|
||||
$value = $unserialized;
|
||||
if (false !== $unserialized = @unserialize($row[$i])) {
|
||||
$value = $unserialized;
|
||||
}
|
||||
|
||||
$newRow[$keys[$i]] = $value;
|
||||
}
|
||||
|
||||
$newRow[$keys[$i]] = $value;
|
||||
$decoded[] = $newRow;
|
||||
}
|
||||
|
||||
$decoded[] = $newRow;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,13 @@ use Thelia\ImportExport\AbstractHandler;
|
||||
*/
|
||||
abstract class ImportHandler extends AbstractHandler
|
||||
{
|
||||
protected $importedRows = 0;
|
||||
|
||||
public function getImportedRows()
|
||||
{
|
||||
return $this->importedRows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Thelia\Core\FileFormat\Formatting\FormatterData
|
||||
* @return string|array error messages
|
||||
|
||||
@@ -25,6 +25,8 @@ use Thelia\Model\ProductSaleElementsQuery;
|
||||
*/
|
||||
class ProductStockImport extends ImportHandler
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param \Thelia\Core\FileFormat\Formatting\FormatterData
|
||||
* @return string|array error messages
|
||||
@@ -36,6 +38,7 @@ class ProductStockImport extends ImportHandler
|
||||
$errors = [];
|
||||
$translator = Translator::getInstance();
|
||||
|
||||
|
||||
while (null !== $row = $data->popRow()) {
|
||||
$obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]);
|
||||
|
||||
@@ -50,12 +53,15 @@ class ProductStockImport extends ImportHandler
|
||||
$errors[] = $errorMessage ;
|
||||
} else {
|
||||
$obj->setQuantity($row["stock"])->save();
|
||||
$this->importedRows++;
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return string|array
|
||||
*
|
||||
|
||||
@@ -35,7 +35,7 @@ class CSVFormatterTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testSimpleEncode()
|
||||
{
|
||||
$expected = "\"ref\";\"stock\"\r\n\"foo\";\"bar\"";
|
||||
$expected = "\"ref\";\"stock\"\n\"foo\";\"bar\"";
|
||||
|
||||
$data = [
|
||||
[
|
||||
@@ -86,7 +86,7 @@ class CSVFormatterTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testSimpleDecode()
|
||||
{
|
||||
$data = "\"ref\";\"stock\"\r\n\"foo\";\"bar\"";
|
||||
$data = "\"ref\";\"stock\"\n\"foo\";\"bar\"";
|
||||
|
||||
$expected = [
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user