From f1a66869c9826e8da1b8733ba2ca388a1bf6270c Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Wed, 23 Jun 2021 17:06:49 +0200 Subject: [PATCH 1/7] WIP added import command to import files into database --- src/Command/TemplateImport.php | 184 ++++++++++++++++++++++++++++++ src/Resources/config/services.xml | 18 +++ 2 files changed, 202 insertions(+) create mode 100644 src/Command/TemplateImport.php create mode 100644 src/Resources/config/services.xml diff --git a/src/Command/TemplateImport.php b/src/Command/TemplateImport.php new file mode 100644 index 0000000..0a27002 --- /dev/null +++ b/src/Command/TemplateImport.php @@ -0,0 +1,184 @@ +container = $container; + $this->languageRepository = $languageRepository; + $this->mailTemplateRepository = $mailTemplateRepository; + $this->mailTemplateTranslationRepository = $mailTemplateTranslationRepository; + $this->mailTemplateTypeRepository = $mailTemplateTypeRepository; + $this->mailTemplateDefinition = $mailTemplateDefinition; + } + + /** + * Configure the input and outputs + */ + protected function configure() : void + { + $this + ->addOption('languageName', 'l', InputOption::VALUE_OPTIONAL, 'Language name', 'Nederlands') + ->addOption('languageCode', 'c', InputOption::VALUE_OPTIONAL, 'Language code', 'nl-NL') + ->addOption('overwrite', 'o', InputOption::VALUE_OPTIONAL, 'Overwrite existing templates', false); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + * @throws Exception + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $overwrite = !!$input->getOption('overwrite'); + $languageName = $input->getOption('languageName'); + $languageCode = $input->getOption('languageCode'); + + // Find language based on language name + $criteria = new Criteria(); + $criteria->addFilter(new EqualsFilter('name', $languageName)); + $context = Context::createDefaultContext(); + /** @var LanguageEntity $language */ + try { + $language = $this->languageRepository->search($criteria, $context)->first(); + } catch (\Exception $e) { + $output->writeln(sprintf('Could not find mail language with name %s', $languageName)); + return 1; + } + + // Filter out non-directories or relatives + $this->basePath = __DIR__ . '/../Resources/views/email/' . $languageCode . '/'; + $mailTypes = array_filter(scandir($this->basePath), function ($input) { + return strlen($input) > 2 && is_dir($this->basePath . $input); + }); + + // Loop through mailtypes to add the templates + foreach ($mailTypes as $mailTypeTechnicalName) { + $mailTemplateTypeCriteria = new Criteria(); + $mailTemplateTypeCriteria->addFilter(new EqualsFilter('technicalName', $mailTypeTechnicalName)); + try { + $mailType = $this->mailTemplateTypeRepository->search($mailTemplateTypeCriteria, $context)->first(); + $mailTemplateCriteria = new Criteria(); + $mailTemplateCriteria->addFilter(new EqualsFilter('mailTemplateTypeId', $mailType->getId())); + $mailTemplate = $this->mailTemplateRepository->search($mailTemplateCriteria, $context)->first(); + if (!$mailTemplate) { + $mailTemplateId = Uuid::randomHex(); + } else { + $mailTemplateId = $mailTemplate->getId(); + } + + $mailTemplate = $this->getMailTemplateContent($mailTemplateId, $mailTypeTechnicalName, $mailType->getId(), $languageName, $languageCode); + if (!$mailTemplate) { + $output->writeln(sprintf('No HTML and/or text content for %s (%s) found. Skipping.', $mailTypeTechnicalName, $languageName)); + continue; + } + if ($overwrite) { + try { + $this->mailTemplateTranslationRepository->upsert([$mailTemplate], $context); + $output->writeln(sprintf('Succesfully upserted mail template for %s.', $mailTypeTechnicalName)); + } catch (\Exception $e) { + $output->writeln(sprintf('Could not upsert mail template for %s; %s.', $mailTypeTechnicalName, $e->getMessage())); + } + } else { + try { + $this->mailTemplateTranslationRepository->create([$mailTemplate], $context); + $output->writeln(sprintf('Succesfully inserted mail template for %s.', $mailTypeTechnicalName)); + } catch (\Exception $e) { + $output->writeln(sprintf('Could not insert mail template for %s; %s.', $mailTypeTechnicalName, $e->getMessage())); + } + } + } catch (\Exception $e) { + $output->writeln(sprintf('Could not find mail template type for %s; %s', $mailTypeTechnicalName, $e->getMessage())); + } + } + + return 0; + } + + private function getMailTemplateContent($mailTemplateId, string $mailTypeTechnicalName, $mailTypeId, string $languageName, string $languageCode) + { + $contentHtml = @file_get_contents($this->basePath . $mailTypeTechnicalName . '/html.twig'); + $contentText = @file_get_contents($this->basePath . $mailTypeTechnicalName . '/plain.twig'); + $subject = @file_get_contents($this->basePath . $mailTypeTechnicalName . '/subject.twig'); + + if (!$contentHtml || !$contentText) { + return false; + } + + return [ + 'id' => $mailTemplateId, + 'description' => $mailTypeTechnicalName . ' (' . $languageName . ')', + 'systemDefault' => true, + 'senderName' => '{{ salesChannel.name }}', + 'subject' => $subject ? trim($subject) : $mailTypeTechnicalName, + 'contentHtml' => $contentHtml, + 'contentPlain' => $contentText, + 'mailTemplateTypeId' => $mailTypeId, + ]; + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml new file mode 100644 index 0000000..6758208 --- /dev/null +++ b/src/Resources/config/services.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + From 00ed61e0dfd313bf53a50ac6b7baa9d07e31d684 Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Thu, 24 Jun 2021 11:20:08 +0200 Subject: [PATCH 2/7] Refactored console command, added type hints, removed overwrite option --- src/Command/TemplateImport.php | 119 +++++++++++++-------------------- 1 file changed, 46 insertions(+), 73 deletions(-) diff --git a/src/Command/TemplateImport.php b/src/Command/TemplateImport.php index 0a27002..f0e6cf3 100644 --- a/src/Command/TemplateImport.php +++ b/src/Command/TemplateImport.php @@ -2,23 +2,17 @@ namespace ElgentosDutchEmailTemplates\Command; -use League\Csv\Exception; -use League\Csv\Reader; -use League\Csv\Statement; -use League\Csv\TabularDataReader; +use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity; use Shopware\Core\Content\MailTemplate\MailTemplateDefinition; -use Shopware\Core\Defaults; +use Shopware\Core\Content\MailTemplate\MailTemplateEntity; use Shopware\Core\Framework\Context; -use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; -use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter; +use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; -use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteContext; use Shopware\Core\Framework\Uuid\Uuid; use Shopware\Core\System\Language\LanguageEntity; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -30,30 +24,16 @@ */ class TemplateImport extends Command { - /** - * @var ContainerInterface - */ public ContainerInterface $container; - /** - * @var EntityRepositoryInterface - */ + public EntityRepositoryInterface $languageRepository; - /** - * @var EntityRepositoryInterface - */ + public EntityRepositoryInterface $mailTemplateRepository; - /** - * @var EntityRepositoryInterface - */ + public EntityRepositoryInterface $mailTemplateTranslationRepository; - /** - * @var EntityRepositoryInterface - */ + public EntityRepositoryInterface $mailTemplateTypeRepository; - /** - * @var MailTemplateDefinition - */ public MailTemplateDefinition $mailTemplateDefinition; public string $basePath = ''; @@ -66,7 +46,8 @@ public function __construct( EntityRepositoryInterface $mailTemplateTypeRepository, MailTemplateDefinition $mailTemplateDefinition, string $name = null - ) { + ) + { parent::__construct($name); $this->container = $container; $this->languageRepository = $languageRepository; @@ -76,26 +57,15 @@ public function __construct( $this->mailTemplateDefinition = $mailTemplateDefinition; } - /** - * Configure the input and outputs - */ - protected function configure() : void + protected function configure(): void { $this ->addOption('languageName', 'l', InputOption::VALUE_OPTIONAL, 'Language name', 'Nederlands') - ->addOption('languageCode', 'c', InputOption::VALUE_OPTIONAL, 'Language code', 'nl-NL') - ->addOption('overwrite', 'o', InputOption::VALUE_OPTIONAL, 'Overwrite existing templates', false); + ->addOption('languageCode', 'c', InputOption::VALUE_OPTIONAL, 'Language code', 'nl-NL'); } - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int - * @throws Exception - */ protected function execute(InputInterface $input, OutputInterface $output): int { - $overwrite = !!$input->getOption('overwrite'); $languageName = $input->getOption('languageName'); $languageCode = $input->getOption('languageCode'); @@ -119,38 +89,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int // Loop through mailtypes to add the templates foreach ($mailTypes as $mailTypeTechnicalName) { - $mailTemplateTypeCriteria = new Criteria(); - $mailTemplateTypeCriteria->addFilter(new EqualsFilter('technicalName', $mailTypeTechnicalName)); try { - $mailType = $this->mailTemplateTypeRepository->search($mailTemplateTypeCriteria, $context)->first(); - $mailTemplateCriteria = new Criteria(); - $mailTemplateCriteria->addFilter(new EqualsFilter('mailTemplateTypeId', $mailType->getId())); - $mailTemplate = $this->mailTemplateRepository->search($mailTemplateCriteria, $context)->first(); - if (!$mailTemplate) { - $mailTemplateId = Uuid::randomHex(); - } else { - $mailTemplateId = $mailTemplate->getId(); - } - - $mailTemplate = $this->getMailTemplateContent($mailTemplateId, $mailTypeTechnicalName, $mailType->getId(), $languageName, $languageCode); - if (!$mailTemplate) { + $mailTemplateType = $this->getMailTemplateTypeByTechnicalName($mailTypeTechnicalName, $context); + $mailTemplate = $mailTemplateType ? $this->getMailTemplateByMailTemplateTypeId($mailTemplateType, $context) : null; + $mailTemplateContent = $this->getMailTemplateContent($mailTemplate, $mailTypeTechnicalName, $mailTemplateType->getId(), $languageName); + if (empty($mailTemplateContent)) { $output->writeln(sprintf('No HTML and/or text content for %s (%s) found. Skipping.', $mailTypeTechnicalName, $languageName)); continue; } - if ($overwrite) { - try { - $this->mailTemplateTranslationRepository->upsert([$mailTemplate], $context); - $output->writeln(sprintf('Succesfully upserted mail template for %s.', $mailTypeTechnicalName)); - } catch (\Exception $e) { - $output->writeln(sprintf('Could not upsert mail template for %s; %s.', $mailTypeTechnicalName, $e->getMessage())); - } - } else { - try { - $this->mailTemplateTranslationRepository->create([$mailTemplate], $context); - $output->writeln(sprintf('Succesfully inserted mail template for %s.', $mailTypeTechnicalName)); - } catch (\Exception $e) { - $output->writeln(sprintf('Could not insert mail template for %s; %s.', $mailTypeTechnicalName, $e->getMessage())); - } + try { + $this->mailTemplateTranslationRepository->upsert([$mailTemplateContent], $context); + $output->writeln(sprintf('Succesfully upserted mail template for %s.', $mailTypeTechnicalName)); + } catch (\Exception $e) { + $output->writeln(sprintf('Could not upsert mail template for %s; %s.', $mailTypeTechnicalName, $e->getMessage())); } } catch (\Exception $e) { $output->writeln(sprintf('Could not find mail template type for %s; %s', $mailTypeTechnicalName, $e->getMessage())); @@ -160,18 +111,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - private function getMailTemplateContent($mailTemplateId, string $mailTypeTechnicalName, $mailTypeId, string $languageName, string $languageCode) + private function getMailTemplateContent(?MailTemplateEntity $mailTemplate, string $mailTypeTechnicalName, string $mailTypeId, string $languageName): array { $contentHtml = @file_get_contents($this->basePath . $mailTypeTechnicalName . '/html.twig'); $contentText = @file_get_contents($this->basePath . $mailTypeTechnicalName . '/plain.twig'); $subject = @file_get_contents($this->basePath . $mailTypeTechnicalName . '/subject.twig'); if (!$contentHtml || !$contentText) { - return false; + return []; } - return [ - 'id' => $mailTemplateId, + $data = [ 'description' => $mailTypeTechnicalName . ' (' . $languageName . ')', 'systemDefault' => true, 'senderName' => '{{ salesChannel.name }}', @@ -180,5 +130,28 @@ private function getMailTemplateContent($mailTemplateId, string $mailTypeTechnic 'contentPlain' => $contentText, 'mailTemplateTypeId' => $mailTypeId, ]; + + // If the mail template already exists, pass along the mail template ID, otherwise create a new one + if ($mailTemplate) { + $data['mailTemplateId'] = $mailTemplate->getId(); + } else { + $data['id'] = Uuid::randomHex(); + } + + return $data; + } + + protected function getMailTemplateTypeByTechnicalName(string $mailTypeTechnicalName, Context $context): ?MailTemplateTypeEntity + { + $mailTemplateTypeCriteria = new Criteria(); + $mailTemplateTypeCriteria->addFilter(new EqualsFilter('technicalName', $mailTypeTechnicalName)); + return $this->mailTemplateTypeRepository->search($mailTemplateTypeCriteria, $context)->first(); + } + + protected function getMailTemplateByMailTemplateTypeId(MailTemplateTypeEntity $mailTemplateType, Context $context): ?MailTemplateEntity + { + $mailTemplateCriteria = new Criteria(); + $mailTemplateCriteria->addFilter(new EqualsFilter('mailTemplateTypeId', $mailTemplateType->getId())); + return $this->mailTemplateRepository->search($mailTemplateCriteria, $context)->first(); } } From 6d440d2145b46ffa66181c11b5b08ce8d8697291 Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Thu, 24 Jun 2021 11:20:32 +0200 Subject: [PATCH 3/7] Renamed incorrectly named files --- .../nl-NL/product_stock_warning/{plain.html.twig => plain.twig} | 0 .../email/nl-NL/sepa_confirmation/{plain.html.twig => plain.twig} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/Resources/views/email/nl-NL/product_stock_warning/{plain.html.twig => plain.twig} (100%) rename src/Resources/views/email/nl-NL/sepa_confirmation/{plain.html.twig => plain.twig} (100%) diff --git a/src/Resources/views/email/nl-NL/product_stock_warning/plain.html.twig b/src/Resources/views/email/nl-NL/product_stock_warning/plain.twig similarity index 100% rename from src/Resources/views/email/nl-NL/product_stock_warning/plain.html.twig rename to src/Resources/views/email/nl-NL/product_stock_warning/plain.twig diff --git a/src/Resources/views/email/nl-NL/sepa_confirmation/plain.html.twig b/src/Resources/views/email/nl-NL/sepa_confirmation/plain.twig similarity index 100% rename from src/Resources/views/email/nl-NL/sepa_confirmation/plain.html.twig rename to src/Resources/views/email/nl-NL/sepa_confirmation/plain.twig From d59e156771d3e37aab994d5224b8105adef675db Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Thu, 24 Jun 2021 11:30:07 +0200 Subject: [PATCH 4/7] Removed the need for the language code input --- src/Command/TemplateImport.php | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Command/TemplateImport.php b/src/Command/TemplateImport.php index f0e6cf3..72ac102 100644 --- a/src/Command/TemplateImport.php +++ b/src/Command/TemplateImport.php @@ -8,7 +8,6 @@ use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; -use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; use Shopware\Core\Framework\Uuid\Uuid; use Shopware\Core\System\Language\LanguageEntity; @@ -59,18 +58,16 @@ public function __construct( protected function configure(): void { - $this - ->addOption('languageName', 'l', InputOption::VALUE_OPTIONAL, 'Language name', 'Nederlands') - ->addOption('languageCode', 'c', InputOption::VALUE_OPTIONAL, 'Language code', 'nl-NL'); + $this->addOption('languageName', 'l', InputOption::VALUE_OPTIONAL, 'Language name', 'Nederlands'); } protected function execute(InputInterface $input, OutputInterface $output): int { $languageName = $input->getOption('languageName'); - $languageCode = $input->getOption('languageCode'); // Find language based on language name $criteria = new Criteria(); + $criteria->addAssociation('locale'); $criteria->addFilter(new EqualsFilter('name', $languageName)); $context = Context::createDefaultContext(); /** @var LanguageEntity $language */ @@ -81,13 +78,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } + $languageCode = $language->getLocale() ? $language->getLocale()->getCode() : null; + if (!$languageCode) { + $output->writeln(sprintf('Could not find mail language locale with name %s', $languageName)); + return 1; + } + // Filter out non-directories or relatives $this->basePath = __DIR__ . '/../Resources/views/email/' . $languageCode . '/'; $mailTypes = array_filter(scandir($this->basePath), function ($input) { return strlen($input) > 2 && is_dir($this->basePath . $input); }); - // Loop through mailtypes to add the templates + // Loop through mail types to add the templates foreach ($mailTypes as $mailTypeTechnicalName) { try { $mailTemplateType = $this->getMailTemplateTypeByTechnicalName($mailTypeTechnicalName, $context); @@ -98,8 +101,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int continue; } try { - $this->mailTemplateTranslationRepository->upsert([$mailTemplateContent], $context); - $output->writeln(sprintf('Succesfully upserted mail template for %s.', $mailTypeTechnicalName)); + // If the 'id' field is set, create a new mail template. If the 'mailTemplateId' is set, create a new mail translation template + if (isset($mailTemplateContent['id'])) { + $this->mailTemplateRepository->upsert([$mailTemplateContent], $context); + $output->writeln(sprintf('Succesfully upserted mail template for %s.', $mailTypeTechnicalName)); + } elseif (isset($mailTemplateContent['mailTemplateId'])) { + $this->mailTemplateTranslationRepository->upsert([$mailTemplateContent], $context); + $output->writeln(sprintf('Succesfully upserted mail template translation for %s.', $mailTypeTechnicalName)); + } } catch (\Exception $e) { $output->writeln(sprintf('Could not upsert mail template for %s; %s.', $mailTypeTechnicalName, $e->getMessage())); } @@ -131,7 +140,7 @@ private function getMailTemplateContent(?MailTemplateEntity $mailTemplate, strin 'mailTemplateTypeId' => $mailTypeId, ]; - // If the mail template already exists, pass along the mail template ID, otherwise create a new one + // If the mail template already exists, pass along the mail template ID, otherwise create a new UUID if ($mailTemplate) { $data['mailTemplateId'] = $mailTemplate->getId(); } else { From a52887771a808cb465b946d87b775258751222ed Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Thu, 24 Jun 2021 11:31:25 +0200 Subject: [PATCH 5/7] Updated readme --- README.md | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e640987..24ae504 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,44 @@ # Shopware 6 Dutch transactional email templates -Tip: use this with [frosh/frosh-platform-template-mail](https://github.com/FriendsOfShopware/FroshTemplateMail) - Since I couldn't find translations anywhere, here's a community project. If you find any typo's, please create a PR :) +## Import templates +A console command is available to import the templates into your database. *Be aware that this overwrites your existing mail templates!* + +``` +$ bin/console elgentos-dutch-email-templates:import +Succesfully upserted mail template translation for contact_form. +Succesfully upserted mail template translation for customer.group.registration.accepted. +Succesfully upserted mail template translation for customer.group.registration.declined. +Succesfully upserted mail template translation for customer.recovery.request. +Succesfully upserted mail template translation for customer_group_change_accept. +Succesfully upserted mail template translation for customer_group_change_reject. +Succesfully upserted mail template translation for customer_register. +Succesfully upserted mail template translation for customer_register.double_opt_in. +Succesfully upserted mail template translation for guest_order.double_opt_in. +Succesfully upserted mail template translation for newsletterDoubleOptIn. +Succesfully upserted mail template translation for newsletterRegister. +Succesfully upserted mail template translation for order.state.cancelled. +Succesfully upserted mail template translation for order.state.completed. +Succesfully upserted mail template translation for order.state.in_progress. +Succesfully upserted mail template translation for order.state.open. +Succesfully upserted mail template translation for order_confirmation_mail. +Succesfully upserted mail template translation for order_delivery.state.cancelled. +Succesfully upserted mail template translation for order_delivery.state.returned. +Succesfully upserted mail template translation for order_delivery.state.returned_partially. +Succesfully upserted mail template translation for order_delivery.state.shipped. +Succesfully upserted mail template translation for order_delivery.state.shipped_partially. +Succesfully upserted mail template translation for order_transaction.state.cancelled. +Succesfully upserted mail template translation for order_transaction.state.open. +Succesfully upserted mail template translation for order_transaction.state.paid. +Succesfully upserted mail template translation for order_transaction.state.paid_partially. +Succesfully upserted mail template translation for order_transaction.state.refunded. +Succesfully upserted mail template translation for order_transaction.state.refunded_partially. +Succesfully upserted mail template translation for order_transaction.state.reminded. +Succesfully upserted mail template translation for password_change. +Succesfully upserted mail template translation for product_stock_warning. +Succesfully upserted mail template translation for sepa_confirmation. +Succesfully upserted mail template translation for user.recovery.request. +``` + Huge thanks to @MelvinAchterhuis for providing a large number of these :) From 0f08c7e90052ba23ac86d4558ef9def8e13fac40 Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Thu, 24 Jun 2021 12:09:51 +0200 Subject: [PATCH 6/7] Set languageId when creating translation template --- src/Command/TemplateImport.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Command/TemplateImport.php b/src/Command/TemplateImport.php index 72ac102..d074280 100644 --- a/src/Command/TemplateImport.php +++ b/src/Command/TemplateImport.php @@ -95,7 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int try { $mailTemplateType = $this->getMailTemplateTypeByTechnicalName($mailTypeTechnicalName, $context); $mailTemplate = $mailTemplateType ? $this->getMailTemplateByMailTemplateTypeId($mailTemplateType, $context) : null; - $mailTemplateContent = $this->getMailTemplateContent($mailTemplate, $mailTypeTechnicalName, $mailTemplateType->getId(), $languageName); + $mailTemplateContent = $this->getMailTemplateContent($mailTemplate, $mailTypeTechnicalName, $mailTemplateType->getId(), $language); if (empty($mailTemplateContent)) { $output->writeln(sprintf('No HTML and/or text content for %s (%s) found. Skipping.', $mailTypeTechnicalName, $languageName)); continue; @@ -106,6 +106,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->mailTemplateRepository->upsert([$mailTemplateContent], $context); $output->writeln(sprintf('Succesfully upserted mail template for %s.', $mailTypeTechnicalName)); } elseif (isset($mailTemplateContent['mailTemplateId'])) { + $mailTemplateContent['languageId'] = $language->getId(); $this->mailTemplateTranslationRepository->upsert([$mailTemplateContent], $context); $output->writeln(sprintf('Succesfully upserted mail template translation for %s.', $mailTypeTechnicalName)); } @@ -120,7 +121,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - private function getMailTemplateContent(?MailTemplateEntity $mailTemplate, string $mailTypeTechnicalName, string $mailTypeId, string $languageName): array + private function getMailTemplateContent(?MailTemplateEntity $mailTemplate, string $mailTypeTechnicalName, string $mailTypeId, LanguageEntity $language): array { $contentHtml = @file_get_contents($this->basePath . $mailTypeTechnicalName . '/html.twig'); $contentText = @file_get_contents($this->basePath . $mailTypeTechnicalName . '/plain.twig'); @@ -131,7 +132,7 @@ private function getMailTemplateContent(?MailTemplateEntity $mailTemplate, strin } $data = [ - 'description' => $mailTypeTechnicalName . ' (' . $languageName . ')', + 'description' => $mailTypeTechnicalName . ' (' . $language->getName() . ')', 'systemDefault' => true, 'senderName' => '{{ salesChannel.name }}', 'subject' => $subject ? trim($subject) : $mailTypeTechnicalName, From f4079c92934e5e8f56315607b9eab3edaa908558 Mon Sep 17 00:00:00 2001 From: Peter Jaap Blaakmeer Date: Thu, 24 Jun 2021 12:32:10 +0200 Subject: [PATCH 7/7] Removed unncessary class injections --- src/Command/TemplateImport.php | 10 ---------- src/Resources/config/services.xml | 2 -- 2 files changed, 12 deletions(-) diff --git a/src/Command/TemplateImport.php b/src/Command/TemplateImport.php index d074280..219ea21 100644 --- a/src/Command/TemplateImport.php +++ b/src/Command/TemplateImport.php @@ -3,7 +3,6 @@ namespace ElgentosDutchEmailTemplates\Command; use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity; -use Shopware\Core\Content\MailTemplate\MailTemplateDefinition; use Shopware\Core\Content\MailTemplate\MailTemplateEntity; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; @@ -15,7 +14,6 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class ImageImport @@ -23,8 +21,6 @@ */ class TemplateImport extends Command { - public ContainerInterface $container; - public EntityRepositoryInterface $languageRepository; public EntityRepositoryInterface $mailTemplateRepository; @@ -33,27 +29,21 @@ class TemplateImport extends Command public EntityRepositoryInterface $mailTemplateTypeRepository; - public MailTemplateDefinition $mailTemplateDefinition; - public string $basePath = ''; public function __construct( - ContainerInterface $container, EntityRepositoryInterface $languageRepository, EntityRepositoryInterface $mailTemplateRepository, EntityRepositoryInterface $mailTemplateTranslationRepository, EntityRepositoryInterface $mailTemplateTypeRepository, - MailTemplateDefinition $mailTemplateDefinition, string $name = null ) { parent::__construct($name); - $this->container = $container; $this->languageRepository = $languageRepository; $this->mailTemplateRepository = $mailTemplateRepository; $this->mailTemplateTranslationRepository = $mailTemplateTranslationRepository; $this->mailTemplateTypeRepository = $mailTemplateTypeRepository; - $this->mailTemplateDefinition = $mailTemplateDefinition; } protected function configure(): void diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 6758208..3ede10f 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -7,12 +7,10 @@ - -