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:
Benjamin Perche
2014-07-21 11:47:47 +02:00
parent 4e29830bde
commit 79a8390817
5 changed files with 32 additions and 14 deletions

View File

@@ -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(),
]
);
} }
/** /**

View File

@@ -12,10 +12,8 @@
namespace Thelia\Core\FileFormat\Formatting\Formatter; namespace Thelia\Core\FileFormat\Formatting\Formatter;
use Thelia\Core\FileFormat\Formatting\AbstractFormatter; use Thelia\Core\FileFormat\Formatting\AbstractFormatter;
use Thelia\Core\FileFormat\Formatting\Exception\BadFormattedStringException;
use Thelia\Core\FileFormat\Formatting\FormatterData; use Thelia\Core\FileFormat\Formatting\FormatterData;
use Thelia\Core\FileFormat\FormatType; use Thelia\Core\FileFormat\FormatType;
use Thelia\Core\Translation\Translator;
/** /**
* Class CSVFormatter * Class CSVFormatter
@@ -25,7 +23,7 @@ use Thelia\Core\Translation\Translator;
class CSVFormatter extends AbstractFormatter class CSVFormatter extends AbstractFormatter
{ {
public $delimiter = ";"; public $delimiter = ";";
public $lineReturn = "\r\n"; public $lineReturn = "\n";
public $stringDelimiter = "\""; public $stringDelimiter = "\"";
/** /**
@@ -126,6 +124,7 @@ class CSVFormatter extends AbstractFormatter
if (count($raw) > 0) { if (count($raw) > 0) {
$keys = explode($this->delimiter, array_shift($raw)); $keys = explode($this->delimiter, array_shift($raw));
$keysLength = count($keys);
foreach ($keys as &$key) { foreach ($keys as &$key) {
$key = trim($key, $this->stringDelimiter); $key = trim($key, $this->stringDelimiter);
@@ -137,18 +136,19 @@ class CSVFormatter extends AbstractFormatter
$newRow = []; $newRow = [];
$row = explode($this->delimiter, $row); $row = explode($this->delimiter, $row);
for ($i = 0; $i < $columns; ++$i) { if (count($row) >= $keysLength) {
$value = trim($row[$i], $this->stringDelimiter); for ($i = 0; $i < $columns; ++$i) {
$value = trim($row[$i], $this->stringDelimiter);
if (false !== $unserialized = @unserialize($row[$i])) { if (false !== $unserialized = @unserialize($row[$i])) {
$value = $unserialized; $value = $unserialized;
}
$newRow[$keys[$i]] = $value;
} }
$newRow[$keys[$i]] = $value; $decoded[] = $newRow;
} }
$decoded[] = $newRow;
} }
} }

View File

@@ -21,6 +21,13 @@ use Thelia\ImportExport\AbstractHandler;
*/ */
abstract class ImportHandler extends AbstractHandler abstract class ImportHandler extends AbstractHandler
{ {
protected $importedRows = 0;
public function getImportedRows()
{
return $this->importedRows;
}
/** /**
* @param \Thelia\Core\FileFormat\Formatting\FormatterData * @param \Thelia\Core\FileFormat\Formatting\FormatterData
* @return string|array error messages * @return string|array error messages

View File

@@ -25,6 +25,8 @@ use Thelia\Model\ProductSaleElementsQuery;
*/ */
class ProductStockImport extends ImportHandler class ProductStockImport extends ImportHandler
{ {
/** /**
* @param \Thelia\Core\FileFormat\Formatting\FormatterData * @param \Thelia\Core\FileFormat\Formatting\FormatterData
* @return string|array error messages * @return string|array error messages
@@ -36,6 +38,7 @@ class ProductStockImport extends ImportHandler
$errors = []; $errors = [];
$translator = Translator::getInstance(); $translator = Translator::getInstance();
while (null !== $row = $data->popRow()) { while (null !== $row = $data->popRow()) {
$obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]); $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]);
@@ -50,12 +53,15 @@ class ProductStockImport extends ImportHandler
$errors[] = $errorMessage ; $errors[] = $errorMessage ;
} else { } else {
$obj->setQuantity($row["stock"])->save(); $obj->setQuantity($row["stock"])->save();
$this->importedRows++;
} }
} }
return $errors; return $errors;
} }
/** /**
* @return string|array * @return string|array
* *

View File

@@ -35,7 +35,7 @@ class CSVFormatterTest extends \PHPUnit_Framework_TestCase
public function testSimpleEncode() public function testSimpleEncode()
{ {
$expected = "\"ref\";\"stock\"\r\n\"foo\";\"bar\""; $expected = "\"ref\";\"stock\"\n\"foo\";\"bar\"";
$data = [ $data = [
[ [
@@ -86,7 +86,7 @@ class CSVFormatterTest extends \PHPUnit_Framework_TestCase
public function testSimpleDecode() public function testSimpleDecode()
{ {
$data = "\"ref\";\"stock\"\r\n\"foo\";\"bar\""; $data = "\"ref\";\"stock\"\n\"foo\";\"bar\"";
$expected = [ $expected = [
[ [