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