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;
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,6 +136,7 @@ class CSVFormatter extends AbstractFormatter
$newRow = [];
$row = explode($this->delimiter, $row);
if (count($row) >= $keysLength) {
for ($i = 0; $i < $columns; ++$i) {
$value = trim($row[$i], $this->stringDelimiter);
@@ -148,7 +148,7 @@ class CSVFormatter extends AbstractFormatter
}
$decoded[] = $newRow;
}
}
}

View File

@@ -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

View File

@@ -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
*

View File

@@ -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 = [
[