From 697b905a0f12e08651efecc77ac192169547eef7 Mon Sep 17 00:00:00 2001 From: Nicolas Barbey <223106@supinfo.com> Date: Fri, 6 Aug 2021 17:19:59 +0200 Subject: [PATCH] Thelia 2.5 compatibility --- Config/config.xml | 4 +-- Config/module.xml | 4 +-- Config/routing.xml | 4 +-- Controller/AdminController.php | 16 ++++++++- Controller/ExportController.php | 36 +++++++++++-------- Controller/ImportController.php | 14 ++++++-- Form/ExportForm.php | 16 ++++----- Form/ExtensionForm.php | 6 ++-- Translation.php | 11 +++++- .../default/Translation/translation.html | 4 +-- 10 files changed, 78 insertions(+), 37 deletions(-) diff --git a/Config/config.xml b/Config/config.xml index 7959a55..dda0257 100644 --- a/Config/config.xml +++ b/Config/config.xml @@ -9,12 +9,12 @@
- + diff --git a/Config/module.xml b/Config/module.xml index 95aea9c..b4b7fb9 100644 --- a/Config/module.xml +++ b/Config/module.xml @@ -20,7 +20,7 @@ en_US fr_FR - 1.0.5 + 2.0.0 Vincent Lopes-Vicente, Nicolas Barbey @@ -34,7 +34,7 @@ classic - 2.3.0 + 2.5.0 other 0 0 diff --git a/Config/routing.xml b/Config/routing.xml index a9c8c67..e920a27 100644 --- a/Config/routing.xml +++ b/Config/routing.xml @@ -4,7 +4,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + diff --git a/Controller/AdminController.php b/Controller/AdminController.php index 2c72c84..1e0a4c0 100644 --- a/Controller/AdminController.php +++ b/Controller/AdminController.php @@ -14,10 +14,18 @@ use Thelia\Tools\URL; use Translation\Form\ExtensionForm; use Translation\Translation; +use Symfony\Component\Routing\Annotation\Route; +/** + * @Route("/admin/module/translation", name="admin_translation") + */ class AdminController extends BaseAdminController { + + /** + * @Route("", name="_menu", methods="GET") + */ public function showPage() { // Is the module active ? @@ -26,9 +34,12 @@ public function showPage() return $this->render('Translation/translation', ['in_use' => $translationInUse]); } + /** + * @Route("/extension", name="_extension", methods="POST") + */ public function setExtensionAction() { - $form = new ExtensionForm($this->getRequest()); + $form = $this->createForm(ExtensionForm::getName()); $extensionForm = $this->validateForm($form); @@ -39,6 +50,9 @@ public function setExtensionAction() return $this->generateRedirect(URL::getInstance()->absoluteUrl('/admin/module/translation')); } + /** + * @Route("/revert", name="_revert", methods="GET") + */ public function revertAction() { $fs = new Filesystem(); diff --git a/Controller/ExportController.php b/Controller/ExportController.php index 1982737..3b18827 100644 --- a/Controller/ExportController.php +++ b/Controller/ExportController.php @@ -3,6 +3,9 @@ namespace Translation\Controller; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\Translation\Dumper\PhpFileDumper; use Symfony\Component\Translation\Dumper\XliffFileDumper; @@ -11,6 +14,7 @@ use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\Translation\TranslationEvent; use Thelia\Core\Template\TemplateDefinition; +use Thelia\Core\Translation\Translator; use Thelia\Model\Lang; use Thelia\Model\LangQuery; use Thelia\Model\Module; @@ -19,32 +23,38 @@ use Translation\Dumper\PoFileDumperWithComments; use Translation\Form\ExportForm; use Translation\Translation; +use Symfony\Component\Routing\Annotation\Route; - +/** + * @Route("/admin/module/translation/export", name="admin_translation_export") + */ class ExportController extends BaseAdminController { /** * @return \Symfony\Component\HttpFoundation\Response * @throws \Exception + * @Route("", name="", methods="POST") */ - public function exportAction() + public function exportAction(RequestStack $requestStack, EventDispatcherInterface $dispatcher) { $translationsArchiveDir = Translation::TRANSLATIONS_DIR . 'archives'; $translationsTempDir = Translation::TRANSLATIONS_DIR . 'tmp'; - if (! file_exists($translationsArchiveDir)){ - mkdir($translationsArchiveDir, 0777, true); + $fs = new Filesystem(); + + if (!file_exists($translationsArchiveDir)){ + $fs->mkdir($translationsArchiveDir); } if (! file_exists($translationsTempDir)){ - mkdir($translationsTempDir, 0777, true); + $fs->mkdir($translationsTempDir); } - $form = new ExportForm($this->getRequest()); + $form = $this->createForm(ExportForm::getName()); $exportForm = $this->validateForm($form); - $lang = LangQuery::create()->filterById($this->getRequest()->get('lang_id'))->findOne(); + $lang = LangQuery::create()->filterById($requestStack->getCurrentRequest()->get('lang_id'))->findOne(); $dirs = $exportForm->get('directories')->getData(); $ext = Translation::getConfigValue('extension'); @@ -60,7 +70,7 @@ public function exportAction() ]; } foreach ($dirs as $dir) { - $this->exportTranslations($dir, $ext, $lang, $translationsTempDir); + $this->exportTranslations($dir, $ext, $lang, $translationsTempDir, $requestStack, $dispatcher); } if (file_exists($translationsTempDir.DS.$ext)){ @@ -100,7 +110,7 @@ public function exportAction() } else { $this->setupFormErrorContext( 'No translation' , - $this->getTranslator()->trans('No translation found'), + Translator::getInstance()->trans('No translation found'), $form ); @@ -120,7 +130,7 @@ public function exportAction() * @param $tmpDir * @throws \Exception */ - protected function exportTranslations($dir, $ext, Lang $lang, $tmpDir) + protected function exportTranslations($dir, $ext, Lang $lang, $tmpDir, RequestStack $requestStack, EventDispatcherInterface $dispatcher) { $items = []; @@ -136,7 +146,7 @@ protected function exportTranslations($dir, $ext, Lang $lang, $tmpDir) case 'backOffice': case 'email': case 'pdf' : - $templateDir = $this->getRequest()->get($dir.'_directory_select'); + $templateDir = $requestStack->getCurrentRequest()->get($dir.'_directory_select'); $templateName = $this->camelCaseToUpperSnakeCase($dir); $template = new TemplateDefinition( $templateDir, @@ -188,7 +198,7 @@ protected function exportTranslations($dir, $ext, Lang $lang, $tmpDir) $keyMetaData = []; - $this->getDispatcher()->dispatch(TheliaEvents::TRANSLATION_GET_STRINGS, $event); + $dispatcher->dispatch($event, TheliaEvents::TRANSLATION_GET_STRINGS); if (null !== $translatableStrings = $event->getTranslatableStrings()) { $catalogue = new MessageCatalogue($lang->getLocale()); @@ -261,8 +271,6 @@ protected function getModulesDirectories() 'pdf' => 'Pdf' ]; - $domain = null; - foreach ($modulesNames as $moduleName){ if ($moduleName[0] !== '.'){ /** @var Module $module */ diff --git a/Controller/ImportController.php b/Controller/ImportController.php index 8504d69..a2b3e82 100644 --- a/Controller/ImportController.php +++ b/Controller/ImportController.php @@ -9,25 +9,35 @@ namespace Translation\Controller; +use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\File\UploadedFile; use Thelia\Controller\Admin\BaseAdminController; use Thelia\Tools\URL; use Translation\Translation; +use Symfony\Component\Routing\Annotation\Route; +/** + * @Route("/admin/module/translation/import", name="admin_translation_import") + */ class ImportController extends BaseAdminController { + /** + * @Route("", name="", methods="POST") + */ public function importAction() { $path = Translation::TRANSLATIONS_DIR . 'tmp'; + $fs = new Filesystem(); + if (!file_exists($path)) { - mkdir($path, 0777 , true); + $fs->mkdir($path); } $ext = Translation::getConfigValue('extension'); if (!file_exists(Translation::TRANSLATIONS_DIR . $ext)) { - mkdir(Translation::TRANSLATIONS_DIR . $ext, 0777 , true); + $fs->mkdir(Translation::TRANSLATIONS_DIR . $ext); } /** @var UploadedFile $importFile */ diff --git a/Form/ExportForm.php b/Form/ExportForm.php index f4ebcb6..969108f 100644 --- a/Form/ExportForm.php +++ b/Form/ExportForm.php @@ -27,19 +27,19 @@ protected function buildForm() 'required' => true, 'multiple' => true, 'choices' => [ - "all" => Translator::getInstance()->trans('Every translations', [], Translation::DOMAIN_NAME), - "core" => Translator::getInstance()->trans('Core Thelia', [], Translation::DOMAIN_NAME), - 'frontOffice' => Translator::getInstance()->trans('Front-office templates', [], Translation::DOMAIN_NAME), - "backOffice" => Translator::getInstance()->trans('Back-office templates', [], Translation::DOMAIN_NAME), - "pdf" => Translator::getInstance()->trans('PDF templates', [], Translation::DOMAIN_NAME), - "email" => Translator::getInstance()->trans('E-mail templates', [], Translation::DOMAIN_NAME), - "modules" => Translator::getInstance()->trans('Modules', [], Translation::DOMAIN_NAME) + Translator::getInstance()->trans('Every translations', [], Translation::DOMAIN_NAME) => "all", + Translator::getInstance()->trans('Core Thelia', [], Translation::DOMAIN_NAME) => "core", + Translator::getInstance()->trans('Front-office templates', [], Translation::DOMAIN_NAME) => "frontOffice", + Translator::getInstance()->trans('Back-office templates', [], Translation::DOMAIN_NAME) => "backOffice", + Translator::getInstance()->trans('PDF templates', [], Translation::DOMAIN_NAME) => "pdf", + Translator::getInstance()->trans('E-mail templates', [], Translation::DOMAIN_NAME) => "email", + Translator::getInstance()->trans('Modules', [], Translation::DOMAIN_NAME) => "modules" ] ] ); } - public function getName() + public static function getName() { return 'translation-export-form'; } diff --git a/Form/ExtensionForm.php b/Form/ExtensionForm.php index 8fa06dd..9c17202 100644 --- a/Form/ExtensionForm.php +++ b/Form/ExtensionForm.php @@ -18,14 +18,14 @@ protected function buildForm() [ 'data' => Translation::getConfigValue("extension"), 'choices' => [ - 'po' => '.po', - "xlf" => '.xlf' + '.po' => 'po', + ".xlf" => 'xlf' ] ] ); } - public function getName() + public static function getName() { return "translation-config-form"; } diff --git a/Translation.php b/Translation.php index 02e5982..93e3037 100644 --- a/Translation.php +++ b/Translation.php @@ -13,6 +13,7 @@ namespace Translation; use Propel\Runtime\Connection\ConnectionInterface; +use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator; use Symfony\Component\Filesystem\Filesystem; use Thelia\Module\BaseModule; @@ -23,7 +24,7 @@ class Translation extends BaseModule const TRANSLATIONS_DIR = THELIA_LOCAL_DIR . 'translations' . DS; - public function postActivation(ConnectionInterface $con = null) + public function postActivation(ConnectionInterface $con = null): void { if (null === self::getConfigValue('extension')){ self::setConfigValue('extension' , 'po'); @@ -34,4 +35,12 @@ public static function deleteTmp() { (new Filesystem())->remove(self::TRANSLATIONS_DIR . 'tmp'); } + + public static function configureServices(ServicesConfigurator $servicesConfigurator): void + { + $servicesConfigurator->load(self::getModuleCode().'\\', __DIR__) + ->exclude([THELIA_MODULE_DIR . ucfirst(self::getModuleCode()). "/I18n/*"]) + ->autowire(true) + ->autoconfigure(true); + } } diff --git a/templates/backOffice/default/Translation/translation.html b/templates/backOffice/default/Translation/translation.html index caf8245..77054e8 100644 --- a/templates/backOffice/default/Translation/translation.html +++ b/templates/backOffice/default/Translation/translation.html @@ -79,12 +79,12 @@ {/if} -
+