diff --git a/core/lib/Thelia/Controller/Admin/ExportController.php b/core/lib/Thelia/Controller/Admin/ExportController.php index adebe95f5..73c5cbf1a 100644 --- a/core/lib/Thelia/Controller/Admin/ExportController.php +++ b/core/lib/Thelia/Controller/Admin/ExportController.php @@ -166,12 +166,14 @@ class ExportController extends BaseAdminController $filename = $formatter::FILENAME . "." . $formatter->getExtension(); if ($archiveBuilder === null) { - $this->dispatch(TheliaEvents::BEFORE_EXPORT, $event); + $this->dispatch(TheliaEvents::EXPORT_BEFORE_ENCODE, $event); $formattedContent = $formatter->encode($data); + $this->dispatch(TheliaEvents::EXPORT_AFTER_ENCODE, $event->setContent($formattedContent)); + return new Response( - $formattedContent, + $event->getContent(), 200, [ "Content-Type" => $formatter->getMimeType(), @@ -181,10 +183,12 @@ class ExportController extends BaseAdminController ); } else { $event->setArchiveBuilder($archiveBuilder); - $this->dispatch(TheliaEvents::BEFORE_EXPORT, $event); + $this->dispatch(TheliaEvents::EXPORT_BEFORE_ENCODE, $event); $formattedContent = $formatter->encode($data); + $this->dispatch(TheliaEvents::EXPORT_AFTER_ENCODE, $event->setContent($formattedContent)); + if ($includeImages && $handler instanceof ImagesExportInterface) { $this->processExportImages($handler, $archiveBuilder); } @@ -194,7 +198,7 @@ class ExportController extends BaseAdminController } $archiveBuilder->addFileFromString( - $formattedContent, $filename + $event->getContent(), $filename ); return $archiveBuilder->buildArchiveResponse($formatter::FILENAME); diff --git a/core/lib/Thelia/Controller/Admin/ImportController.php b/core/lib/Thelia/Controller/Admin/ImportController.php index 7be132c01..2ee8958ae 100644 --- a/core/lib/Thelia/Controller/Admin/ImportController.php +++ b/core/lib/Thelia/Controller/Admin/ImportController.php @@ -11,6 +11,8 @@ /*************************************************************************************/ namespace Thelia\Controller\Admin; +use Thelia\Core\Event\ImportExport as ImportExportEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Core\FileFormat\Archive\ArchiveBuilderManagerTrait; use Thelia\Core\FileFormat\Formatting\FormatterManagerTrait; use Thelia\Core\HttpFoundation\Response; @@ -97,19 +99,20 @@ class ImportController extends BaseAdminController /** @var \Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder $archiveBuilder */ $archiveBuilder = null; - foreach ($formats as $format) { + foreach ($formats as $objectName => $format) { $formatLength = strlen($format); - if ($nameLength >= $formatLength && substr($name, -$formatLength) === $formatLength) { + $formatExtension = substr($name, -$formatLength); + + if ($nameLength >= $formatLength && $formatExtension === $format) { $uploadFormat = $format; - $flip = array_flip($format); try { - $formatter = $formatterManager->get($flip[$format]); + $formatter = $formatterManager->get($objectName); } catch(\OutOfBoundsException $e) {} try { - $archiveBuilder = $archiveBuilderManager->get($flip[$format]); + $archiveBuilder = $archiveBuilderManager->get($objectName); } catch(\OutOfBoundsException $e) {} break; @@ -124,8 +127,6 @@ class ImportController extends BaseAdminController } if ($uploadFormat === null) { - - throw new FormValidationException( $this->getTranslator()->trans( "The extension \"%ext\" is not allowed", @@ -144,14 +145,35 @@ class ImportController extends BaseAdminController $content = null; /** - * TODO: HANDLE + * Check expected file names for each formatter */ + $fileNames = []; + /** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */ + foreach ($formatterManager->getFormattersByTypes($types) as $formatter) { + $fileName = $formatter::FILENAME . "." . $formatter->getExtension(); + $fileNames[] = $fileName; + + if ($archiveBuilder->hasFile($fileName)) { + $content = $archiveBuilder->getFileContent($fileName); + break; + } + } + + if ($content === null) { + throw new \ErrorException( + $this->getTranslator()->trans( + "Your archive must contain one of these file and doesn't: %files", + [ + "%files" => implode(", ", $fileNames), + ] + ) + ); + } } elseif ($formatter !== null) { /** - * If the file isn't + * If the file isn't an archive */ - $content = file_get_contents($file->getPathname()); } else { @@ -166,11 +188,28 @@ class ImportController extends BaseAdminController ); } + $event = new ImportExportEvent($formatter, $handler, null, $archiveBuilder); + $event->setContent($content); + + $this->dispatch(TheliaEvents::IMPORT_AFTER_DECODE, $event); + $data = $formatter->decode($content); - // Dispatch event + $event->setContent(null)->setData($data); + $this->dispatch(TheliaEvents::IMPORT_AFTER_DECODE, $event); - $handler->retrieveFromFormatterData($data); + $errors = $handler->retrieveFromFormatterData($data); + + if (!empty($errors)) { + throw new \Exception( + $this->getTranslator()->trans( + "Errors occurred while importing the file: %errors", + [ + "%errors" => implode(", ", $errors), + ] + ) + ); + } $successMessage = $this->getTranslator()->trans("Import successfully done"); diff --git a/core/lib/Thelia/Core/Event/ImportExport.php b/core/lib/Thelia/Core/Event/ImportExport.php index 1c867ca69..b3b0d47d9 100644 --- a/core/lib/Thelia/Core/Event/ImportExport.php +++ b/core/lib/Thelia/Core/Event/ImportExport.php @@ -11,10 +11,10 @@ /*************************************************************************************/ namespace Thelia\Core\Event; -use Thelia\Core\Event\ActionEvent; use Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder; use Thelia\Core\FileFormat\Formatting\AbstractFormatter; use Thelia\Core\FileFormat\Formatting\FormatterData; +use Thelia\ImportExport\AbstractHandler; use Thelia\ImportExport\Export\ExportHandler; /** @@ -24,7 +24,7 @@ use Thelia\ImportExport\Export\ExportHandler; */ class ImportExport extends ActionEvent { - /** @var \Thelia\ImportExport\Export\ExportHandler */ + /** @var \Thelia\ImportExport\AbstractHandler */ protected $handler; /** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter */ @@ -36,10 +36,13 @@ class ImportExport extends ActionEvent /** @var \Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder */ protected $archiveBuilder; + /** @var mixed */ + protected $content; + public function __construct( - AbstractFormatter $formatter, - \Thelia\ImportExport\Export\ExportHandler $handler, - FormatterData $data, + AbstractFormatter $formatter = null, + AbstractHandler $handler = null, + FormatterData $data = null, AbstractArchiveBuilder $archiveBuilder = null ) { $this->archiveBuilder = $archiveBuilder; @@ -123,4 +126,25 @@ class ImportExport extends ActionEvent { return $this->data; } + + /** + * @param $content + * @return $this + */ + public function setContent($content) + { + $this->content = $content; + + return $this; + } + + /** + * @return mixed + */ + public function getContent() + { + return $this->content; + } + + } diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 40d0e758c..ef86b62c6 100644 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -769,5 +769,9 @@ final class TheliaEvents // -- Export ---------------------------------------------- - const BEFORE_EXPORT = "Thelia.before.export"; + const EXPORT_BEFORE_ENCODE = "Thelia.export.encode.before"; + const EXPORT_AFTER_ENCODE = "Thelia.export.encode.after"; + + const IMPORT_BEFORE_DECODE = "Thelia.import.decode.before"; + const IMPORT_AFTER_DECODE = "Thelia.import.decode.after"; } diff --git a/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php b/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php index 4e7a2add8..7ac923956 100644 --- a/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php +++ b/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php @@ -22,7 +22,7 @@ use Thelia\Log\Tlog; */ abstract class AbstractFormatter implements FormatInterface, FormatterInterface { - const FILENAME = "export"; + const FILENAME = "data"; /** @var \Thelia\Core\Translation\Translator */ protected $translator; diff --git a/core/lib/Thelia/Core/FileFormat/Formatting/Formatter/JsonFormatter.php b/core/lib/Thelia/Core/FileFormat/Formatting/Formatter/JsonFormatter.php index 9e5213937..2744fb60b 100644 --- a/core/lib/Thelia/Core/FileFormat/Formatting/Formatter/JsonFormatter.php +++ b/core/lib/Thelia/Core/FileFormat/Formatting/Formatter/JsonFormatter.php @@ -83,7 +83,7 @@ class JsonFormatter extends AbstractFormatter */ public function decode($rawData) { - return (new FormatterData($this->getAliases()))->setData( + return (new FormatterData())->setData( json_decode($rawData, true) ); } diff --git a/core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php b/core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php index d09338f45..a4f494775 100644 --- a/core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php +++ b/core/lib/Thelia/Core/FileFormat/Formatting/FormatterData.php @@ -172,13 +172,9 @@ class FormatterData * @param int $index * @return array|bool */ - public function popRow($index = 0) + public function popRow() { - $row = $this->getRow($index); - - if (false !== $row) { - unset($this->data[$index]); - } + $row = array_pop($this->data); return $row; } diff --git a/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php b/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php index b495bac0b..44bc7dfc9 100644 --- a/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php +++ b/core/lib/Thelia/ImportExport/Import/Type/ProductStockImport.php @@ -35,27 +35,24 @@ class ProductStockImport extends ImportHandler { $errors = []; $translator = Translator::getInstance(); - $collection = new ObjectCollection(); - while (false !== $row = $data->popRow()) { + while (null !== $row = $data->popRow()) { $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]); if ($obj === null) { - $errors += [ - $translator->trans( - "The product sale elements reference %ref doesn't exist", - [ - "%ref" => $row["ref"] - ] - ) - ]; + $errorMessage = $translator->trans( + "The product sale element reference %ref doesn't exist", + [ + "%ref" => $row["ref"] + ] + ); + + $errors[] = $errorMessage ; } else { - $collection->append($obj->setQuantity($row["stock"])); + $obj->setQuantity($row["stock"])->save(); } } - $collection->save(); - return $errors; } diff --git a/templates/backOffice/default/ajax/import-modal.html b/templates/backOffice/default/ajax/import-modal.html index dfbdded49..d0e73bbea 100644 --- a/templates/backOffice/default/ajax/import-modal.html +++ b/templates/backOffice/default/ajax/import-modal.html @@ -1,5 +1,6 @@ {form name="thelia.import"}