From 069d2a07d93b684b740d4c972e69336c045011dc Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Thu, 17 Jul 2014 14:43:44 +0200 Subject: [PATCH] =?UTF-8?q?Refactor=20ImportExportController=20into=20two?= =?UTF-8?q?=20logical=20controllers=20=09modifi=C3=A9:=20=20=20=20=20=20?= =?UTF-8?q?=20=20=20core/lib/Thelia/Config/Resources/routing/admin.xml=20?= =?UTF-8?q?=09renomm=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Cont?= =?UTF-8?q?roller/Admin/ImportExportController.php=20->=20core/lib/Thelia/?= =?UTF-8?q?Controller/Admin/ExportController.php=20=09nouveau=20fichier:?= =?UTF-8?q?=20core/lib/Thelia/Controller/Admin/ImportController.php=20=09m?= =?UTF-8?q?odifi=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/Core/Dep?= =?UTF-8?q?endencyInjection/Loader/XmlFileLoader.php=20=09nouveau=20fichie?= =?UTF-8?q?r:=20core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderMana?= =?UTF-8?q?gerTrait.php=20=09nouveau=20fichier:=20core/lib/Thelia/Core/Fil?= =?UTF-8?q?eFormat/Formatting/FormatterManagerTrait.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Thelia/Config/Resources/routing/admin.xml | 43 +- .../Controller/Admin/ExportController.php | 265 ++++++++- .../Controller/Admin/ImportController.php | 402 +++++++++++++ .../Admin/ImportExportController.php | 554 ------------------ .../Loader/XmlFileLoader.php | 36 -- .../Archive/ArchiveBuilderManagerTrait.php | 31 + .../Formatting/FormatterManagerTrait.php | 31 + 7 files changed, 761 insertions(+), 601 deletions(-) create mode 100644 core/lib/Thelia/Controller/Admin/ImportController.php delete mode 100644 core/lib/Thelia/Controller/Admin/ImportExportController.php create mode 100644 core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderManagerTrait.php create mode 100644 core/lib/Thelia/Core/FileFormat/Formatting/FormatterManagerTrait.php diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index e10e4fbcb..4459a43f5 100644 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -1158,7 +1158,7 @@ Thelia\Controller\Admin\TranslationsController::updateAction - + Thelia\Controller\Admin\ExportController::indexAction @@ -1189,28 +1189,55 @@ - Thelia\Controller\Admin\ImportExportController::export + Thelia\Controller\Admin\ExportController::export \d+ - Thelia\Controller\Admin\ImportExportController::exportView + Thelia\Controller\Admin\ExportController::exportView \d+ + + + + Thelia\Controller\Admin\ImportController::indexAction + + + + Thelia\Controller\Admin\ImportController::changePosition + up|down + \d+ + + + + Thelia\Controller\Admin\ImportController::updatePosition + \d+ + \d+ + + + + Thelia\Controller\Admin\ImportController::changeCategoryPosition + up|down + \d+ + + + + Thelia\Controller\Admin\ImportController::updateCategoryPosition + \d+ + \d+ + + - Thelia\Controller\Admin\ImportExportController::import + Thelia\Controller\Admin\ImportController::import \d+ - Thelia\Controller\Admin\ImportExportController::importView + Thelia\Controller\Admin\ImportController::importView \d+ - - - diff --git a/core/lib/Thelia/Controller/Admin/ExportController.php b/core/lib/Thelia/Controller/Admin/ExportController.php index 7637f1978..adebe95f5 100644 --- a/core/lib/Thelia/Controller/Admin/ExportController.php +++ b/core/lib/Thelia/Controller/Admin/ExportController.php @@ -12,9 +12,22 @@ namespace Thelia\Controller\Admin; +use Thelia\Core\FileFormat\Archive\ArchiveBuilderManagerTrait; +use Thelia\Core\FileFormat\Formatting\FormatterManagerTrait; use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\Resource\AdminResources; -use Thelia\Core\Translation\Translator; +use Thelia\Core\Template\Element\LoopResult; +use Thelia\Core\Template\Loop\Export as ExportLoop; +use Thelia\Core\Event\ImportExport as ImportExportEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder; +use Thelia\Core\FileFormat\Formatting\AbstractFormatter; +use Thelia\Core\HttpFoundation\Response; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Form\ExportForm; +use Thelia\ImportExport\Export\DocumentsExportInterface; +use Thelia\ImportExport\Export\ExportHandler; +use Thelia\ImportExport\Export\ImagesExportInterface; use Thelia\Model\ExportCategoryQuery; use Thelia\Model\ExportQuery; @@ -25,6 +38,9 @@ use Thelia\Model\ExportQuery; */ class ExportController extends BaseAdminController { + use ArchiveBuilderManagerTrait; + use FormatterManagerTrait; + public function indexAction() { if (null !== $response = $this->checkAuth([AdminResources::EXPORT], [], [AccessManager::VIEW])) { @@ -36,6 +52,249 @@ class ExportController extends BaseAdminController return $this->render('export'); } + /** + * @param integer $id + * @return Response + * + * This method is called when the route /admin/export/{id} + * is called with a POST request. + */ + public function export($id) + { + if (null === $export = $this->getExport($id)) { + return $this->render("404"); + } + + /** + * Get needed services + */ + $archiveBuilderManager = $this->getArchiveBuilderManager($this->container); + $formatterManager = $this->getFormatterManager($this->container); + + /** + * Get the archive builders + */ + $archiveBuilders = []; + foreach ($archiveBuilderManager->getNames() as $archiveBuilder) { + $archiveBuilders[$archiveBuilder] = $archiveBuilder; + } + + /** + * Define and validate the form + */ + $form = new ExportForm($this->getRequest()); + $errorMessage = null; + + try { + $boundForm = $this->validateForm($form); + + $archiveBuilder = null; + + /** + * Get the formatter and the archive builder if we have to compress the file(s) + */ + + /** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */ + $formatter = $formatterManager->get( + $boundForm->get("formatter")->getData() + ); + + if ($boundForm->get("do_compress")->getData()) { + /** @var \Thelia\Core\FileFormat\Archive\ArchiveBuilderInterface $archiveBuilder */ + $archiveBuilder = $archiveBuilderManager->get( + $boundForm->get("archive_builder")->getData() + ); + } + + /** + * Return the generated Response + */ + + return $this->processExport( + $formatter, + $export->getHandleClassInstance($this->container), + $archiveBuilder, + $boundForm->get("images")->getData(), + $boundForm->get("documents")->getData() + ); + + } 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); + } + + /** + * @param AbstractFormatter $formatter + * @param ExportHandler $handler + * @param AbstractArchiveBuilder $archiveBuilder + * @param bool $includeImages + * @param bool $includeDocuments + * @return Response + * + * Processes an export by returning a response with the export's content. + */ + protected function processExport( + AbstractFormatter $formatter, + ExportHandler $handler, + AbstractArchiveBuilder $archiveBuilder = null, + $includeImages = false, + $includeDocuments = false + ) { + /** + * Build an event containing the formatter and the handler. + * Used for specific configuration (e.g: XML node names) + */ + $data = $handler->buildFormatterData(); + $event = new ImportExportEvent($formatter, $handler , $data); + + $filename = $formatter::FILENAME . "." . $formatter->getExtension(); + + if ($archiveBuilder === null) { + $this->dispatch(TheliaEvents::BEFORE_EXPORT, $event); + + $formattedContent = $formatter->encode($data); + + return new Response( + $formattedContent, + 200, + [ + "Content-Type" => $formatter->getMimeType(), + "Content-Disposition" => + "attachment; filename=\"" . $filename . "\"", + ] + ); + } else { + $event->setArchiveBuilder($archiveBuilder); + $this->dispatch(TheliaEvents::BEFORE_EXPORT, $event); + + $formattedContent = $formatter->encode($data); + + if ($includeImages && $handler instanceof ImagesExportInterface) { + $this->processExportImages($handler, $archiveBuilder); + } + + if ($includeDocuments && $handler instanceof DocumentsExportInterface) { + $this->processExportDocuments($handler, $archiveBuilder); + } + + $archiveBuilder->addFileFromString( + $formattedContent, $filename + ); + + return $archiveBuilder->buildArchiveResponse($formatter::FILENAME); + } + } + + /** + * @param ImagesExportInterface $handler + * @param AbstractArchiveBuilder $archiveBuilder + * + * Procedure that add images in the export's archive + */ + protected function processExportImages(ImagesExportInterface $handler, AbstractArchiveBuilder $archiveBuilder) + { + foreach ($handler->getImagesPaths() as $name => $documentPath) { + $archiveBuilder->addFile( + $documentPath, + $handler::IMAGES_DIRECTORY, + is_integer($name) ? null : $name + ); + } + } + + /** + * @param DocumentsExportInterface $handler + * @param AbstractArchiveBuilder $archiveBuilder + * + * Procedure that add documents in the export's archive + */ + protected function processExportDocuments(DocumentsExportInterface $handler, AbstractArchiveBuilder $archiveBuilder) + { + foreach ($handler->getDocumentsPaths() as $name => $documentPath) { + $archiveBuilder->addFile( + $documentPath, + $handler::DOCUMENTS_DIRECTORY, + is_integer($name) ? null : $name + ); + } + } + + /** + * @param integer $id + * @return Response + * + * This method is called when the route /admin/export/{id} + * is called with a GET request. + * + * It returns a modal view if the request is an AJAX one, + * otherwise it generates a "normal" back-office page + */ + public function exportView($id) + { + if (null === $export = $this->getExport($id)) { + return $this->render("404"); + } + + /** + * Use the loop to inject the same vars in Smarty + */ + $loop = new ExportLoop($this->container); + + $loop->initializeArgs([ + "export" => $export->getId() + ]); + + $query = $loop->buildModelCriteria(); + $result= $query->find(); + + $results = $loop->parseResults( + new LoopResult($result) + ); + + $parserContext = $this->getParserContext(); + + /** @var \Thelia\Core\Template\Element\LoopResultRow $row */ + foreach ($results as $row) { + foreach ($row->getVarVal() as $name=>$value) { + $parserContext->set($name, $value); + } + } + + /** + * Inject conditions in smarty, + * It is used to display or not the checkboxes "Include images" + * and "Include documents" + */ + $this->getParserContext() + ->set("HAS_IMAGES", $export->hasImages($this->container)) + ->set("HAS_DOCUMENTS", $export->hasDocuments($this->container)) + ; + + /** Then render the form */ + if ($this->getRequest()->isXmlHttpRequest()) { + return $this->render("ajax/export-modal"); + } else { + return $this->render("export-page"); + } + } + + public function changePosition($action, $id) { if (null !== $response = $this->checkAuth([AdminResources::EXPORT], [], [AccessManager::UPDATE])) { @@ -129,7 +388,7 @@ class ExportController extends BaseAdminController if (null === $export) { throw new \ErrorException( - Translator::getInstance()->trans( + $this->getTranslator()->trans( "There is no id \"%id\" in the exports", [ "%id" => $id @@ -147,7 +406,7 @@ class ExportController extends BaseAdminController if (null === $category) { throw new \ErrorException( - Translator::getInstance()->trans( + $this->getTranslator()->trans( "There is no id \"%id\" in the export categories", [ "%id" => $id diff --git a/core/lib/Thelia/Controller/Admin/ImportController.php b/core/lib/Thelia/Controller/Admin/ImportController.php new file mode 100644 index 000000000..7be132c01 --- /dev/null +++ b/core/lib/Thelia/Controller/Admin/ImportController.php @@ -0,0 +1,402 @@ + + */ +class ImportController extends BaseAdminController +{ + use FormatterManagerTrait; + use ArchiveBuilderManagerTrait; + + public function indexAction() + { + if (null !== $response = $this->checkAuth([AdminResources::IMPORT], [], [AccessManager::VIEW])) { + return $response; + } + + $this->setOrders(); + + return $this->render('import'); + } + + /** + * @param integer $id + * @return Response + * + * This method is called when the route /admin/import/{id} + * is called with a POST request. + */ + public function import($id) + { + if (null === $import = $this->getImport($id)) { + return $this->render("404"); + } + + $archiveBuilderManager = $this->getArchiveBuilderManager($this->container); + $formatterManager = $this->getFormatterManager($this->container); + + /** + * Get needed services + */ + $form = new ImportForm($this->getRequest()); + $errorMessage = null; + $successMessage = null; + + + try { + $boundForm = $this->validateForm($form); + + /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */ + $file = $boundForm->get("file_upload")->getData(); + + /** + * We have to check the extension manually because of composed file formats as tar.gz or tar.bz2 + */ + $name = $file->getClientOriginalName(); + $nameLength = strlen($name); + + + $handler = $import->getHandleClassInstance($this->container); + $types = $handler->getHandledTypes(); + + $formats = + $formatterManager->getExtensionsByTypes($types, true) + + $archiveBuilderManager->getExtensions(true) + ; + + $uploadFormat = null; + + /** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */ + $formatter = null; + + /** @var \Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder $archiveBuilder */ + $archiveBuilder = null; + + foreach ($formats as $format) { + $formatLength = strlen($format); + if ($nameLength >= $formatLength && substr($name, -$formatLength) === $formatLength) { + $uploadFormat = $format; + + $flip = array_flip($format); + + try { + $formatter = $formatterManager->get($flip[$format]); + } catch(\OutOfBoundsException $e) {} + + try { + $archiveBuilder = $archiveBuilderManager->get($flip[$format]); + } catch(\OutOfBoundsException $e) {} + + break; + } + } + + $splitName = explode(".", $name); + $ext = ""; + + if (1 < $limit = count($splitName)) { + $ext = "." . $splitName[$limit-1]; + } + + if ($uploadFormat === null) { + + + throw new FormValidationException( + $this->getTranslator()->trans( + "The extension \"%ext\" is not allowed", + [ + "%ext" => $ext + ] + ) + ); + } + + if ($archiveBuilder !== null) { + /** + * If the file is an archive + */ + $archiveBuilder->loadArchive($file->getPathname()); + $content = null; + + /** + * TODO: HANDLE + */ + + } elseif ($formatter !== null) { + /** + * If the file isn't + */ + + $content = file_get_contents($file->getPathname()); + + } else { + throw new \ErrorException( + $this->getTranslator()->trans( + "There's a problem, the extension \"%ext\" has been found, ". + "but has no formatters nor archive builder", + [ + "%ext" => $ext + ] + ) + ); + } + + $data = $formatter->decode($content); + + // Dispatch event + + $handler->retrieveFromFormatterData($data); + + $successMessage = $this->getTranslator()->trans("Import successfully done"); + + } catch(FormValidationException $e) { + $errorMessage = $this->createStandardFormValidationErrorMessage($e); + } catch(\Exception $e) { + $errorMessage = $e->getMessage(); + } + + if ($successMessage !== null) { + $this->getParserContext()->set("success_message", $successMessage); + } + + if ($errorMessage !== null) { + $form->setErrorMessage($errorMessage); + + $this->getParserContext() + ->addForm($form) + ->setGeneralError($errorMessage) + ; + } + + return $this->importView($id); + } + + + /** + * @param integer $id + * @return Response + * + * This method is called when the route /admin/import/{id} + * is called with a GET request. + * + * It returns a modal view if the request is an AJAX one, + * otherwise it generates a "normal" back-office page + */ + public function importView($id) + { + if (null === $import = $this->getImport($id)) { + return $this->render("404"); + } + + /** + * Use the loop to inject the same vars in Smarty + */ + $loop = new ImportLoop($this->container); + + $loop->initializeArgs([ + "export" => $import->getId() + ]); + + $query = $loop->buildModelCriteria(); + $result= $query->find(); + + $results = $loop->parseResults( + new LoopResult($result) + ); + + $parserContext = $this->getParserContext(); + + /** @var \Thelia\Core\Template\Element\LoopResultRow $row */ + foreach ($results as $row) { + foreach ($row->getVarVal() as $name=>$value) { + $parserContext->set($name, $value); + } + } + + /** + * Get allowed formats + */ + /** @var \Thelia\ImportExport\AbstractHandler $handler */ + $handler = $import->getHandleClassInstance($this->container); + $types = $handler->getHandledTypes(); + + $formatterManager = $this->getFormatterManager($this->container); + $archiveBuilderManager = $this->getArchiveBuilderManager($this->container); + + $formats = + $formatterManager->getExtensionsByTypes($types, true) + + $archiveBuilderManager->getExtensions(true) + ; + + /** + * Get allowed mime types (used for the "Search a file" window + */ + $mimeTypes = + $formatterManager->getMimeTypesByTypes($types) + + $archiveBuilderManager->getMimeTypes() + ; + + /** + * Inject them in smarty + */ + $parserContext + ->set( "ALLOWED_MIME_TYPES", implode(",", $mimeTypes)) + ->set("ALLOWED_EXTENSIONS", implode(", ", $formats)) + ; + + /** Then render the form */ + if ($this->getRequest()->isXmlHttpRequest()) { + return $this->render("ajax/import-modal"); + } else { + return $this->render("import-page"); + } + } + + protected function setOrders($category = null, $import = null) + { + if ($category === null) { + $category = $this->getRequest()->query->get("category_order", "manual"); + } + + if ($import === null) { + $import = $this->getRequest()->query->get("import_order", "manual"); + } + + $this->getParserContext() + ->set("category_order", $category) + ; + + $this->getParserContext() + ->set("import_order", $import) + ; + } + + public function changePosition($action, $id) + { + if (null !== $response = $this->checkAuth([AdminResources::IMPORT], [], [AccessManager::UPDATE])) { + return $response; + } + + $import = $this->getImport($id); + + if ($action === "up") { + $import->upPosition(); + } elseif ($action === "down") { + $import->downPosition(); + } + + $this->setOrders(null, "manual"); + + return $this->render('import'); + } + + public function updatePosition($id, $value) + { + if (null !== $response = $this->checkAuth([AdminResources::IMPORT], [], [AccessManager::UPDATE])) { + return $response; + } + + $import = $this->getImport($id); + + $import->updatePosition($value); + + $this->setOrders(null, "manual"); + + return $this->render('import'); + } + + public function changeCategoryPosition($action, $id) + { + if (null !== $response = $this->checkAuth([AdminResources::IMPORT], [], [AccessManager::UPDATE])) { + return $response; + } + + $category = $this->getCategory($id); + + if ($action === "up") { + $category->upPosition(); + } elseif ($action === "down") { + $category->downPosition(); + } + + $this->setOrders("manual"); + + return $this->render('import'); + } + + public function updateCategoryPosition($id, $value) + { + if (null !== $response = $this->checkAuth([AdminResources::IMPORT], [], [AccessManager::UPDATE])) { + return $response; + } + + $category = $this->getCategory($id); + + $category->updatePosition($value); + + $this->setOrders("manual"); + + return $this->render('import'); + } + + protected function getImport($id) + { + $import = ImportQuery::create()->findPk($id); + + if (null === $import) { + throw new \ErrorException( + $this->getTranslator()->trans( + "There is no id \"%id\" in the imports", + [ + "%id" => $id + ] + ) + ); + } + + return $import; + } + + protected function getCategory($id) + { + $category = ImportCategoryQuery::create()->findPk($id); + + if (null === $category) { + throw new \ErrorException( + $this->getTranslator()->trans( + "There is no id \"%id\" in the import categories", + [ + "%id" => $id + ] + ) + ); + } + + return $category; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Controller/Admin/ImportExportController.php b/core/lib/Thelia/Controller/Admin/ImportExportController.php deleted file mode 100644 index 685910200..000000000 --- a/core/lib/Thelia/Controller/Admin/ImportExportController.php +++ /dev/null @@ -1,554 +0,0 @@ - - */ -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"); - } - - /** - * @param integer $id - * @return Response - * - * This method is called when the route /admin/import/{id} - * is called with a POST request. - */ - public function import($id) - { - if (null === $import = $this->getImport($id)) { - return $this->render("404"); - } - - /** - * Get needed services - */ - $this->hydrate(); - - $form = new ImportForm($this->getRequest()); - $errorMessage = null; - $successMessage = null; - - - try { - $boundForm = $this->validateForm($form); - - /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */ - $file = $boundForm->get("file_upload")->getData(); - - /** - * We have to check the extension manually because of composed file formats as tar.gz or tar.bz2 - */ - $name = $file->getClientOriginalName(); - $nameLength = strlen($name); - - - $handler = $import->getHandleClassInstance($this->container); - $types = $handler->getHandledTypes(); - - $formats = - $this->formatterManager->getExtensionsByTypes($types, true) + - $this->archiveBuilderManager->getExtensions(true) - ; - - $uploadFormat = null; - - /** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */ - $formatter = null; - - /** @var \Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder $archiveBuilder */ - $archiveBuilder = null; - - foreach ($formats as $format) { - $formatLength = strlen($format); - if ($nameLength >= $formatLength && substr($name, -$formatLength) === $formatLength) { - $uploadFormat = $format; - - $flip = array_flip($format); - - try { - $formatter = $this->formatterManager->get($flip[$format]); - } catch(\OutOfBoundsException $e) {} - - try { - $archiveBuilder = $this->archiveBuilderManager->get($flip[$format]); - } catch(\OutOfBoundsException $e) {} - - break; - } - } - - $splitName = explode(".", $name); - $ext = ""; - - if (1 < $limit = count($splitName)) { - $ext = "." . $splitName[$limit-1]; - } - - if ($uploadFormat === null) { - - - throw new FormValidationException( - $this->getTranslator()->trans( - "The extension \"%ext\" is not allowed", - [ - "%ext" => $ext - ] - ) - ); - } - - if ($archiveBuilder !== null) { - /** - * If the file is an archive - */ - $archiveBuilder->loadArchive($file->getPathname()); - $content = null; - - /** - * TODO: HANDLE - */ - - } elseif ($formatter !== null) { - /** - * If the file isn't - */ - - $content = file_get_contents($file->getPathname()); - - } else { - throw new \ErrorException( - $this->getTranslator()->trans( - "There's a problem, the extension \"%ext\" has been found, ". - "but has no formatters nor archive builder", - [ - "%ext" => $ext - ] - ) - ); - } - - $data = $formatter->decode($content); - - // Dispatch event - - $handler->retrieveFromFormatterData($data); - - $successMessage = $this->getTranslator()->trans("Import successfully done"); - - } catch(FormValidationException $e) { - $errorMessage = $this->createStandardFormValidationErrorMessage($e); - } catch(\Exception $e) { - $errorMessage = $e->getMessage(); - } - - if ($successMessage !== null) { - $this->getParserContext()->set("success_message", $successMessage); - } - - if ($errorMessage !== null) { - $form->setErrorMessage($errorMessage); - - $this->getParserContext() - ->addForm($form) - ->setGeneralError($errorMessage) - ; - } - - return $this->importView($id); - } - - /** - * @param integer $id - * @return Response - * - * This method is called when the route /admin/export/{id} - * is called with a POST request. - */ - 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; - } - - /** - * Define and validate the form - */ - $form = new ExportForm($this->getRequest()); - $errorMessage = null; - - try { - $boundForm = $this->validateForm($form); - - $archiveBuilder = null; - - /** - * Get the formatter and the archive builder if we have to compress the file(s) - */ - - /** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */ - $formatter = $this->formatterManager->get( - $boundForm->get("formatter")->getData() - ); - - if ($boundForm->get("do_compress")->getData()) { - /** @var \Thelia\Core\FileFormat\Archive\ArchiveBuilderInterface $archiveBuilder */ - $archiveBuilder = $this->archiveBuilderManager->get( - $boundForm->get("archive_builder")->getData() - ); - } - - /** - * Return the generated Response - */ - - return $this->processExport( - $formatter, - $export->getHandleClassInstance($this->container), - $archiveBuilder, - $boundForm->get("images")->getData(), - $boundForm->get("documents")->getData() - ); - - } 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); - } - - /** - * @param AbstractFormatter $formatter - * @param ExportHandler $handler - * @param AbstractArchiveBuilder $archiveBuilder - * @param bool $includeImages - * @param bool $includeDocuments - * @return Response - * - * Processes an export by returning a response with the export's content. - */ - protected function processExport( - AbstractFormatter $formatter, - ExportHandler $handler, - AbstractArchiveBuilder $archiveBuilder = null, - $includeImages = false, - $includeDocuments = false - ) { - /** - * Build an event containing the formatter and the handler. - * Used for specific configuration (e.g: XML node names) - */ - $data = $handler->buildFormatterData(); - $event = new ImportExportEvent($formatter, $handler , $data); - - $filename = $formatter::FILENAME . "." . $formatter->getExtension(); - - if ($archiveBuilder === null) { - $this->dispatch(TheliaEvents::BEFORE_EXPORT, $event); - - $formattedContent = $formatter->encode($data); - - return new Response( - $formattedContent, - 200, - [ - "Content-Type" => $formatter->getMimeType(), - "Content-Disposition" => - "attachment; filename=\"" . $filename . "\"", - ] - ); - } else { - $event->setArchiveBuilder($archiveBuilder); - $this->dispatch(TheliaEvents::BEFORE_EXPORT, $event); - - $formattedContent = $formatter->encode($data); - - if ($includeImages && $handler instanceof ImagesExportInterface) { - $this->processExportImages($handler, $archiveBuilder); - } - - if ($includeDocuments && $handler instanceof DocumentsExportInterface) { - $this->processExportDocuments($handler, $archiveBuilder); - } - - $archiveBuilder->addFileFromString( - $formattedContent, $filename - ); - - return $archiveBuilder->buildArchiveResponse($formatter::FILENAME); - } - } - - /** - * @param ImagesExportInterface $handler - * @param AbstractArchiveBuilder $archiveBuilder - * - * Procedure that add images in the export's archive - */ - protected function processExportImages(ImagesExportInterface $handler, AbstractArchiveBuilder $archiveBuilder) - { - foreach ($handler->getImagesPaths() as $name => $documentPath) { - $archiveBuilder->addFile( - $documentPath, - $handler::IMAGES_DIRECTORY, - is_integer($name) ? null : $name - ); - } - } - - /** - * @param DocumentsExportInterface $handler - * @param AbstractArchiveBuilder $archiveBuilder - * - * Procedure that add documents in the export's archive - */ - protected function processExportDocuments(DocumentsExportInterface $handler, AbstractArchiveBuilder $archiveBuilder) - { - foreach ($handler->getDocumentsPaths() as $name => $documentPath) { - $archiveBuilder->addFile( - $documentPath, - $handler::DOCUMENTS_DIRECTORY, - is_integer($name) ? null : $name - ); - } - } - - /** - * @param integer $id - * @return Response - * - * This method is called when the route /admin/import/{id} - * is called with a GET request. - * - * It returns a modal view if the request is an AJAX one, - * otherwise it generates a "normal" back-office page - */ - public function importView($id) - { - if (null === $import = $this->getImport($id)) { - return $this->render("404"); - } - - /** - * Use the loop to inject the same vars in Smarty - */ - $loop = new ImportLoop($this->container); - - $loop->initializeArgs([ - "export" => $import->getId() - ]); - - $query = $loop->buildModelCriteria(); - $result= $query->find(); - - $results = $loop->parseResults( - new LoopResult($result) - ); - - $parserContext = $this->getParserContext(); - - /** @var \Thelia\Core\Template\Element\LoopResultRow $row */ - foreach ($results as $row) { - foreach ($row->getVarVal() as $name=>$value) { - $parserContext->set($name, $value); - } - } - - /** - * Get allowed formats - */ - /** @var \Thelia\ImportExport\AbstractHandler $handler */ - $this->hydrate(); - $handler = $import->getHandleClassInstance($this->container); - - $types = $handler->getHandledTypes(); - - $formats = - $this->formatterManager->getExtensionsByTypes($types, true) + - $this->archiveBuilderManager->getExtensions(true) - ; - - /** - * Get allowed mime types (used for the "Search a file" window - */ - $mimeTypes = - $this->formatterManager->getMimeTypesByTypes($types) + - $this->archiveBuilderManager->getMimeTypes() - ; - - /** - * Inject them in smarty - */ - $parserContext - ->set( "ALLOWED_MIME_TYPES", implode(",", $mimeTypes)) - ->set("ALLOWED_EXTENSIONS", implode(", ", $formats)) - ; - - /** Then render the form */ - if ($this->getRequest()->isXmlHttpRequest()) { - return $this->render("ajax/import-modal"); - } else { - return $this->render("import-page"); - } - } - - /** - * @param integer $id - * @return Response - * - * This method is called when the route /admin/export/{id} - * is called with a GET request. - * - * It returns a modal view if the request is an AJAX one, - * otherwise it generates a "normal" back-office page - */ - public function exportView($id) - { - if (null === $export = $this->getExport($id)) { - return $this->render("404"); - } - - /** - * Use the loop to inject the same vars in Smarty - */ - $loop = new ExportLoop($this->container); - - $loop->initializeArgs([ - "export" => $export->getId() - ]); - - $query = $loop->buildModelCriteria(); - $result= $query->find(); - - $results = $loop->parseResults( - new LoopResult($result) - ); - - $parserContext = $this->getParserContext(); - - /** @var \Thelia\Core\Template\Element\LoopResultRow $row */ - foreach ($results as $row) { - foreach ($row->getVarVal() as $name=>$value) { - $parserContext->set($name, $value); - } - } - - /** - * Inject conditions in smarty, - * It is used to display or not the checkboxes "Include images" - * and "Include documents" - */ - $this->getParserContext() - ->set("HAS_IMAGES", $export->hasImages($this->container)) - ->set("HAS_DOCUMENTS", $export->hasDocuments($this->container)) - ; - - /** Then render the form */ - if ($this->getRequest()->isXmlHttpRequest()) { - return $this->render("ajax/export-modal"); - } else { - return $this->render("export-page"); - } - } - - /** - * @param $id - * @return array|mixed|\Thelia\Model\Export - * - * This method is a shortcut to get an export model - */ - protected function getExport($id) - { - $export = ExportQuery::create() - ->findPk($id) - ; - - return $export; - } - - /** - * @param $id - * @return array|mixed|\Thelia\Model\Import - * - * This method is a shortcut to get an import model - */ - protected function getImport($id) - { - $export = ImportQuery::create() - ->findPk($id) - ; - - return $export; - } - -} diff --git a/core/lib/Thelia/Core/DependencyInjection/Loader/XmlFileLoader.php b/core/lib/Thelia/Core/DependencyInjection/Loader/XmlFileLoader.php index 2f8215827..6fb105bba 100644 --- a/core/lib/Thelia/Core/DependencyInjection/Loader/XmlFileLoader.php +++ b/core/lib/Thelia/Core/DependencyInjection/Loader/XmlFileLoader.php @@ -311,7 +311,6 @@ class XmlFileLoader extends FileLoader $con->beginTransaction(); try { - $refs = []; /** @var SimpleXMLElement $exportCategory */ foreach ($exportCategories as $exportCategory) { $id = (string) $exportCategory->getAttributeAsPhp("id"); @@ -338,16 +337,8 @@ class XmlFileLoader extends FileLoader ->save($con); ; } - - $refs[] = $id; } - ExportCategoryQuery::create() - ->filterByRef($refs, Criteria::NOT_IN) - ->find() - ->delete() - ; - $con->commit(); } catch (\Exception $e) { $con->rollBack(); @@ -366,7 +357,6 @@ class XmlFileLoader extends FileLoader $con->beginTransaction(); try { - $refs = []; /** @var SimpleXMLElement $export */ foreach ($exports as $export) { @@ -453,16 +443,8 @@ class XmlFileLoader extends FileLoader ->save($con) ; } - - $refs[] = $id; } - ExportQuery::create() - ->filterByRef($refs, Criteria::NOT_IN) - ->find() - ->delete() - ; - $con->commit(); } catch (\Exception $e) { $con->rollBack(); @@ -481,7 +463,6 @@ class XmlFileLoader extends FileLoader $con->beginTransaction(); try { - $refs = []; /** @var SimpleXMLElement $importCategory */ foreach ($importCategories as $importCategory) { @@ -510,15 +491,8 @@ class XmlFileLoader extends FileLoader ; } - $refs[] = $id; } - ImportCategoryQuery::create() - ->filterByRef($refs, Criteria::NOT_IN) - ->find() - ->delete() - ; - $con->commit(); } catch (\Exception $e) { $con->rollBack(); @@ -537,8 +511,6 @@ class XmlFileLoader extends FileLoader $con->beginTransaction(); try { - $refs = []; - /** @var SimpleXMLElement $import */ foreach ($imports as $import) { $id = (string) $import->getAttributeAsPhp("id"); @@ -624,16 +596,8 @@ class XmlFileLoader extends FileLoader ->save($con) ; } - - $refs[] = $id; } - ImportQuery::create() - ->filterByRef($refs, Criteria::NOT_IN) - ->find() - ->delete() - ; - $con->commit(); } catch (\Exception $e) { $con->rollBack(); diff --git a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderManagerTrait.php b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderManagerTrait.php new file mode 100644 index 000000000..616012db8 --- /dev/null +++ b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderManagerTrait.php @@ -0,0 +1,31 @@ + + */ +trait ArchiveBuilderManagerTrait +{ + /** + * @param ContainerInterface $container + * @return \Thelia\Core\FileFormat\Archive\ArchiveBuilderManager + */ + public function getArchiveBuilderManager(ContainerInterface $container) + { + return $container->get("thelia.manager.archive_builder_manager"); + } +} diff --git a/core/lib/Thelia/Core/FileFormat/Formatting/FormatterManagerTrait.php b/core/lib/Thelia/Core/FileFormat/Formatting/FormatterManagerTrait.php new file mode 100644 index 000000000..4957550b1 --- /dev/null +++ b/core/lib/Thelia/Core/FileFormat/Formatting/FormatterManagerTrait.php @@ -0,0 +1,31 @@ + + */ +trait FormatterManagerTrait +{ + /** + * @param ContainerInterface $container + * @return \Thelia\Core\FileFormat\Formatting\FormatterManager + */ + public function getFormatterManager(ContainerInterface $container) + { + return $container->get("thelia.manager.formatter_manager"); + } +} \ No newline at end of file