-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from elgentos/add-import-command
Add import command
- Loading branch information
Showing
5 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ElgentosDutchEmailTemplates\Command; | ||
|
||
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity; | ||
use Shopware\Core\Content\MailTemplate\MailTemplateEntity; | ||
use Shopware\Core\Framework\Context; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; | ||
use Shopware\Core\Framework\Uuid\Uuid; | ||
use Shopware\Core\System\Language\LanguageEntity; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Class ImageImport | ||
* @package ElgentosDutchEmailTemplates\Command | ||
*/ | ||
class TemplateImport extends Command | ||
{ | ||
public EntityRepositoryInterface $languageRepository; | ||
|
||
public EntityRepositoryInterface $mailTemplateRepository; | ||
|
||
public EntityRepositoryInterface $mailTemplateTranslationRepository; | ||
|
||
public EntityRepositoryInterface $mailTemplateTypeRepository; | ||
|
||
public string $basePath = ''; | ||
|
||
public function __construct( | ||
EntityRepositoryInterface $languageRepository, | ||
EntityRepositoryInterface $mailTemplateRepository, | ||
EntityRepositoryInterface $mailTemplateTranslationRepository, | ||
EntityRepositoryInterface $mailTemplateTypeRepository, | ||
string $name = null | ||
) | ||
{ | ||
parent::__construct($name); | ||
$this->languageRepository = $languageRepository; | ||
$this->mailTemplateRepository = $mailTemplateRepository; | ||
$this->mailTemplateTranslationRepository = $mailTemplateTranslationRepository; | ||
$this->mailTemplateTypeRepository = $mailTemplateTypeRepository; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->addOption('languageName', 'l', InputOption::VALUE_OPTIONAL, 'Language name', 'Nederlands'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$languageName = $input->getOption('languageName'); | ||
|
||
// Find language based on language name | ||
$criteria = new Criteria(); | ||
$criteria->addAssociation('locale'); | ||
$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('<error>Could not find mail language with name %s</error>', $languageName)); | ||
return 1; | ||
} | ||
|
||
$languageCode = $language->getLocale() ? $language->getLocale()->getCode() : null; | ||
if (!$languageCode) { | ||
$output->writeln(sprintf('<error>Could not find mail language locale with name %s</error>', $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 mail types to add the templates | ||
foreach ($mailTypes as $mailTypeTechnicalName) { | ||
try { | ||
$mailTemplateType = $this->getMailTemplateTypeByTechnicalName($mailTypeTechnicalName, $context); | ||
$mailTemplate = $mailTemplateType ? $this->getMailTemplateByMailTemplateTypeId($mailTemplateType, $context) : null; | ||
$mailTemplateContent = $this->getMailTemplateContent($mailTemplate, $mailTypeTechnicalName, $mailTemplateType->getId(), $language); | ||
if (empty($mailTemplateContent)) { | ||
$output->writeln(sprintf('<comment>No HTML and/or text content for %s (%s) found. Skipping.</comment>', $mailTypeTechnicalName, $languageName)); | ||
continue; | ||
} | ||
try { | ||
// 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('<info>Succesfully upserted mail template for %s.</info>', $mailTypeTechnicalName)); | ||
} elseif (isset($mailTemplateContent['mailTemplateId'])) { | ||
$mailTemplateContent['languageId'] = $language->getId(); | ||
$this->mailTemplateTranslationRepository->upsert([$mailTemplateContent], $context); | ||
$output->writeln(sprintf('<info>Succesfully upserted mail template translation for %s.</info>', $mailTypeTechnicalName)); | ||
} | ||
} catch (\Exception $e) { | ||
$output->writeln(sprintf('<error>Could not upsert mail template for %s; %s.</error>', $mailTypeTechnicalName, $e->getMessage())); | ||
} | ||
} catch (\Exception $e) { | ||
$output->writeln(sprintf('<error>Could not find mail template type for %s; %s</error>', $mailTypeTechnicalName, $e->getMessage())); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
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'); | ||
$subject = @file_get_contents($this->basePath . $mailTypeTechnicalName . '/subject.twig'); | ||
|
||
if (!$contentHtml || !$contentText) { | ||
return []; | ||
} | ||
|
||
$data = [ | ||
'description' => $mailTypeTechnicalName . ' (' . $language->getName() . ')', | ||
'systemDefault' => true, | ||
'senderName' => '{{ salesChannel.name }}', | ||
'subject' => $subject ? trim($subject) : $mailTypeTechnicalName, | ||
'contentHtml' => $contentHtml, | ||
'contentPlain' => $contentText, | ||
'mailTemplateTypeId' => $mailTypeId, | ||
]; | ||
|
||
// If the mail template already exists, pass along the mail template ID, otherwise create a new UUID | ||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="ElgentosDutchEmailTemplates\Command\TemplateImport" autowire="true"> | ||
<tag name="console.command" command="elgentos-dutch-email-templates:import" /> | ||
<argument type="service" id="language.repository"/> | ||
<argument type="service" id="mail_template.repository" /> | ||
<argument type="service" id="mail_template_translation.repository" /> | ||
<argument type="service" id="mail_template_type.repository" /> | ||
</service> | ||
</services> | ||
</container> |
File renamed without changes.
File renamed without changes.