From 2a56864f94e4c201366547a16116288a0ee92e8a Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Mon, 21 Jul 2014 16:39:17 +0200 Subject: [PATCH] =?UTF-8?q?Add=20mandatory=20columns=20for=20imports=20=09?= =?UTF-8?q?modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/ImportE?= =?UTF-8?q?xport/Import/ImportHandler.php=20=09modifi=C3=A9:=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20core/lib/Thelia/ImportExport/Import/Type/Product?= =?UTF-8?q?PricesImport.php=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20c?= =?UTF-8?q?ore/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ImportExport/Import/ImportHandler.php | 34 ++++++- .../Import/Type/ProductPricesImport.php | 90 ++++++++++--------- .../Import/Type/ProductStockImport.php | 23 +++-- 3 files changed, 98 insertions(+), 49 deletions(-) diff --git a/core/lib/Thelia/ImportExport/Import/ImportHandler.php b/core/lib/Thelia/ImportExport/Import/ImportHandler.php index fc375c2d5..a1193a7eb 100644 --- a/core/lib/Thelia/ImportExport/Import/ImportHandler.php +++ b/core/lib/Thelia/ImportExport/Import/ImportHandler.php @@ -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 @@ -35,4 +67,4 @@ abstract class ImportHandler extends AbstractHandler * The method does the import routine from a FormatterData */ abstract public function retrieveFromFormatterData(FormatterData $data); -} \ No newline at end of file +} \ No newline at end of file diff --git a/core/lib/Thelia/ImportExport/Import/Type/ProductPricesImport.php b/core/lib/Thelia/ImportExport/Import/Type/ProductPricesImport.php index ea056fa9d..f3c2f8dcc 100644 --- a/core/lib/Thelia/ImportExport/Import/Type/ProductPricesImport.php +++ b/core/lib/Thelia/ImportExport/Import/Type/ProductPricesImport.php @@ -63,57 +63,67 @@ class ProductPricesImport extends ImportHandler $translator = Translator::getInstance(); while (null !== $row = $data->popRow()) { - if (count($row) > 1) { - $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]); - if ($obj === null) { - $errorMessage = $translator->trans( - "The product sale element reference %ref doesn't exist", - [ - "%ref" => $row["ref"] - ] - ); + $this->checkMandatoryColumns($row); - $errors[] = $errorMessage ; - } else { + $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]); - $currency = null; + if ($obj === null) { + $errorMessage = $translator->trans( + "The product sale element reference %ref doesn't exist", + [ + "%ref" => $row["ref"] + ] + ); - if (isset($row["currency"])) { - $currency = CurrencyQuery::create()->findOneByCode($row["currency"]); - } + $errors[] = $errorMessage ; + } else { - if ($currency === null) { - $currency = Currency::getDefaultCurrency(); - } + $currency = null; - $price = ProductPriceQuery::create() - ->filterByProductSaleElementsId($obj->getId()) - ->findOneByCurrencyId($currency->getId()) - ; - - if ($price === null) { - $price = new ProductPrice(); - - $price - ->setProductSaleElements($obj) - ->setCurrency($currency) - ; - } - - $price->setPrice($row["price"]); - - if (isset($row["promo_price"])) { - $price->setPromoPrice($row["promo_price"]); - } - - $price->save(); - $this->importedRows++; + if (isset($row["currency"])) { + $currency = CurrencyQuery::create()->findOneByCode($row["currency"]); } + + if ($currency === null) { + $currency = Currency::getDefaultCurrency(); + } + + $price = ProductPriceQuery::create() + ->filterByProductSaleElementsId($obj->getId()) + ->findOneByCurrencyId($currency->getId()) + ; + + if ($price === null) { + $price = new ProductPrice(); + + $price + ->setProductSaleElements($obj) + ->setCurrency($currency) + ; + } + + $price->setPrice($row["price"]); + + if (isset($row["promo_price"])) { + $price->setPromoPrice($row["promo_price"]); + } + + $price->save(); + $this->importedRows++; } } return $errors; } + /** + * @return array The mandatory columns to have for import + */ + protected function getMandatoryColumns() + { + return ["ref", "price"]; + } + + } diff --git a/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php b/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php index 65ff5e419..6e9d179f0 100644 --- a/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php +++ b/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php @@ -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