Add mandatory columns for imports
modifié: core/lib/Thelia/ImportExport/Import/ImportHandler.php modifié: core/lib/Thelia/ImportExport/Import/Type/ProductPricesImport.php modifié: core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php
This commit is contained in:
@@ -11,7 +11,9 @@
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\ImportExport\Import;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Thelia\Core\FileFormat\Formatting\FormatterData;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\ImportExport\AbstractHandler;
|
||||
|
||||
/**
|
||||
@@ -23,11 +25,41 @@ abstract class ImportHandler extends AbstractHandler
|
||||
{
|
||||
protected $importedRows = 0;
|
||||
|
||||
/** @var Translator */
|
||||
protected $translator;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->translator = Translator::getInstance();
|
||||
|
||||
parent::__construct($container);
|
||||
}
|
||||
|
||||
public function getImportedRows()
|
||||
{
|
||||
return $this->importedRows;
|
||||
}
|
||||
|
||||
protected function checkMandatoryColumns(array $row)
|
||||
{
|
||||
$mandatoryColumns = $this->getMandatoryColumns();
|
||||
if ($mandatoryColumns != $keys = array_keys($row)) {
|
||||
throw new \UnexpectedValueException(
|
||||
$this->translator->trans(
|
||||
"The following columns are missing: %columns",
|
||||
[
|
||||
"%columns" => implode(", ", array_diff($keys, $mandatoryColumns)),
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array The mandatory columns to have for import
|
||||
*/
|
||||
abstract protected function getMandatoryColumns();
|
||||
|
||||
/**
|
||||
* @param \Thelia\Core\FileFormat\Formatting\FormatterData
|
||||
* @return string|array error messages
|
||||
|
||||
@@ -63,7 +63,9 @@ class ProductPricesImport extends ImportHandler
|
||||
$translator = Translator::getInstance();
|
||||
|
||||
while (null !== $row = $data->popRow()) {
|
||||
if (count($row) > 1) {
|
||||
|
||||
$this->checkMandatoryColumns($row);
|
||||
|
||||
$obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]);
|
||||
|
||||
if ($obj === null) {
|
||||
@@ -111,9 +113,17 @@ class ProductPricesImport extends ImportHandler
|
||||
$this->importedRows++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array The mandatory columns to have for import
|
||||
*/
|
||||
protected function getMandatoryColumns()
|
||||
{
|
||||
return ["ref", "price"];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\ImportExport\Import\Type;
|
||||
use Propel\Runtime\Collection\ObjectCollection;
|
||||
use Thelia\Core\FileFormat\Formatting\FormatterData;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Core\FileFormat\FormatType;
|
||||
@@ -36,23 +35,28 @@ class ProductStockImport extends ImportHandler
|
||||
public function retrieveFromFormatterData(FormatterData $data)
|
||||
{
|
||||
$errors = [];
|
||||
$translator = Translator::getInstance();
|
||||
|
||||
|
||||
while (null !== $row = $data->popRow()) {
|
||||
/**
|
||||
* Check for mandatory columns
|
||||
*/
|
||||
$this->checkMandatoryColumns($row);
|
||||
|
||||
$obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]);
|
||||
|
||||
if ($obj === null) {
|
||||
$errorMessage = $translator->trans(
|
||||
$errors[] = $this->translator->trans(
|
||||
"The product sale element reference %ref doesn't exist",
|
||||
[
|
||||
"%ref" => $row["ref"]
|
||||
]
|
||||
);
|
||||
|
||||
$errors[] = $errorMessage ;
|
||||
} else {
|
||||
$obj->setQuantity($row["stock"])->save();
|
||||
$obj
|
||||
->setQuantity($row["stock"])
|
||||
->setEanCode($row["ean"])
|
||||
->save()
|
||||
;
|
||||
$this->importedRows++;
|
||||
}
|
||||
}
|
||||
@@ -60,7 +64,10 @@ class ProductStockImport extends ImportHandler
|
||||
return $errors;
|
||||
}
|
||||
|
||||
|
||||
protected function getMandatoryColumns()
|
||||
{
|
||||
return ["ref", "stock", "ean"];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array
|
||||
|
||||
Reference in New Issue
Block a user