From 79a839081748179b38335d9915bf0652633a682d Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Mon, 21 Jul 2014 11:47:47 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20bugs=20and=20add=20import=20count=20handl?= =?UTF-8?q?e=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/?= =?UTF-8?q?Controller/Admin/ImportController.php=20=09modifi=C3=A9:=20=20?= =?UTF-8?q?=20=20=20=20=20=20=20core/lib/Thelia/Core/FileFormat/Formatting?= =?UTF-8?q?/Formatter/CSVFormatter.php=20=09modifi=C3=A9:=20=20=20=20=20?= =?UTF-8?q?=20=20=20=20core/lib/Thelia/ImportExport/Import/ImportHandler.p?= =?UTF-8?q?hp=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia?= =?UTF-8?q?/ImportExport/Import/Type/ProductStockImport.php=20=09modifi?= =?UTF-8?q?=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Tests/FileFor?= =?UTF-8?q?mat/Formatting/Formatter/CSVFormatterTest.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/Admin/ImportController.php | 7 +++++- .../Formatting/Formatter/CSVFormatter.php | 22 +++++++++---------- .../ImportExport/Import/ImportHandler.php | 7 ++++++ .../Import/Type/ProductStockImport.php | 6 +++++ .../Formatting/Formatter/CSVFormatterTest.php | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/core/lib/Thelia/Controller/Admin/ImportController.php b/core/lib/Thelia/Controller/Admin/ImportController.php index 5bbb69107..f4fa11f28 100644 --- a/core/lib/Thelia/Controller/Admin/ImportController.php +++ b/core/lib/Thelia/Controller/Admin/ImportController.php @@ -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(), + ] + ); } /** diff --git a/core/lib/Thelia/Core/FileFormat/Formatting/Formatter/CSVFormatter.php b/core/lib/Thelia/Core/FileFormat/Formatting/Formatter/CSVFormatter.php index f0b759c1e..56de24c26 100644 --- a/core/lib/Thelia/Core/FileFormat/Formatting/Formatter/CSVFormatter.php +++ b/core/lib/Thelia/Core/FileFormat/Formatting/Formatter/CSVFormatter.php @@ -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; - } } diff --git a/core/lib/Thelia/ImportExport/Import/ImportHandler.php b/core/lib/Thelia/ImportExport/Import/ImportHandler.php index f9c0638a3..fc375c2d5 100644 --- a/core/lib/Thelia/ImportExport/Import/ImportHandler.php +++ b/core/lib/Thelia/ImportExport/Import/ImportHandler.php @@ -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 diff --git a/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php b/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php index de5e38999..65ff5e419 100644 --- a/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php +++ b/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php @@ -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 * diff --git a/core/lib/Thelia/Tests/FileFormat/Formatting/Formatter/CSVFormatterTest.php b/core/lib/Thelia/Tests/FileFormat/Formatting/Formatter/CSVFormatterTest.php index ccbd2fef6..e79a9b28c 100644 --- a/core/lib/Thelia/Tests/FileFormat/Formatting/Formatter/CSVFormatterTest.php +++ b/core/lib/Thelia/Tests/FileFormat/Formatting/Formatter/CSVFormatterTest.php @@ -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 = [ [