diff --git a/core/lib/Thelia/Controller/Admin/ImportExportController.php b/core/lib/Thelia/Controller/Admin/ImportExportController.php index de39423b3..f1dcb1765 100644 --- a/core/lib/Thelia/Controller/Admin/ImportExportController.php +++ b/core/lib/Thelia/Controller/Admin/ImportExportController.php @@ -11,9 +11,12 @@ /*************************************************************************************/ namespace Thelia\Controller\Admin; +use Thelia\Core\HttpFoundation\Response; use Thelia\Core\Template\Element\LoopResult; use Thelia\Core\Template\Loop\Export as ExportLoop; use Thelia\Core\Template\Loop\Import as ImportLoop; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Form\ExportForm; use Thelia\Model\ExportQuery; /** @@ -23,6 +26,19 @@ use Thelia\Model\ExportQuery; */ class ImportExportController extends BaseAdminController { + /** @var \Thelia\Core\FileFormat\Archive\ArchiveBuilderManager */ + protected $archiveBuilderManager; + + /** @var \Thelia\Core\FileFormat\Formatting\FormatterManager */ + protected $formatterManager; + + public function hydrate() + { + + $this->archiveBuilderManager = $this->container->get("thelia.manager.archive_builder_manager"); + $this->formatterManager = $this->container->get("thelia.manager.formatter_manager"); + } + public function import($id) { @@ -30,7 +46,93 @@ class ImportExportController extends BaseAdminController public function export($id) { + if (null === $export = $this->getExport($id)) { + return $this->render("404"); + } + /** + * Get needed services + */ + $this->hydrate(); + + /** + * Get the archive builders + */ + $archiveBuilders = []; + foreach ($this->archiveBuilderManager->getNames() as $archiveBuilder) { + $archiveBuilders[$archiveBuilder] = $archiveBuilder; + } + + + /** + * Get the allowed formatters to inject them into the form + */ + $handler = $export->getHandleClassInstance($this->container); + + $types = $handler->getHandledType(); + + if (!is_array($types)) { + $types = [$types]; + } + + $formatters = []; + /** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */ + foreach ($this->formatterManager->getAll() as $formatter) { + if (in_array($formatter->getExportType(), $types)) { + $formatters[$formatter->getName()] = $formatter->getName(); + } + } + + /** + * Define and validate the form + */ + $form = new ExportForm($this->getRequest()); + $form->setArchiveBuilderNames($archiveBuilders); + $form->setFormatters($formatters); + $errorMessage = null; + + try { + $boundForm = $this->validateForm($form); + + $data = $handler->buildFormatterData(); + + $formatter = $this->formatterManager->get( + $boundForm->get("formatter")->getData() + ); + + $formattedContent = $formatter->encode($data); + + if (!$boundForm->get("do_compress")->getData()) + { + return new Response( + $formattedContent, + 200, + [ + "Content-Type" => $formatter->getMimeType(), + "Content-Disposition" => $formatter::FILENAME . "." . $formatter->getExtension(), + ] + ); + } + + } catch(FormValidationException $e) { + $errorMessage = $this->createStandardFormValidationErrorMessage($e); + } catch(\Exception $e) { + $errorMessage = $e->getMessage(); + } + + /** + * If has an error, display it + */ + if (null !== $errorMessage) { + $form->setErrorMessage($errorMessage); + + $this->getParserContext() + ->addForm($form) + ->setGeneralError($errorMessage) + ; + } + + return $this->exportView($id); } public function importView($id) diff --git a/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php b/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php index 33bbd63bf..4e7a2add8 100644 --- a/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php +++ b/core/lib/Thelia/Core/FileFormat/Formatting/AbstractFormatter.php @@ -22,31 +22,18 @@ use Thelia\Log\Tlog; */ abstract class AbstractFormatter implements FormatInterface, FormatterInterface { + const FILENAME = "export"; + /** @var \Thelia\Core\Translation\Translator */ protected $translator; /** @var \Thelia\Log\Tlog */ protected $logger; - /** @var array */ - protected $aliases = array(); - public function __construct() { $this->translator = Translator::getInstance(); $this->logger = Tlog::getInstance(); } - - public function setAliases(array $aliases) - { - $this->aliases = $aliases; - - return $this; - } - - public function getAliases() - { - return $this->aliases; - } } diff --git a/templates/backOffice/default/ajax/export-modal.html b/templates/backOffice/default/ajax/export-modal.html index b32a08bd4..7b10a6250 100644 --- a/templates/backOffice/default/ajax/export-modal.html +++ b/templates/backOffice/default/ajax/export-modal.html @@ -1,5 +1,6 @@ {form name="thelia.export"}