<?php declare(strict_types=1);
namespace K3n\K3nApplicationCard;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
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\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
class K3nApplicationCard extends Plugin
{
public const TEMPLATE_TYPE_NAME = 'Application card';
public const TEMPLATE_TYPE_TECHNICAL_NAME = 'application_card';
public const MAIL_TEMPLATE_NAME = 'ApplicationCardMailTemplate';
public function install(InstallContext $installContext): void
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
$mailTemplateTypeId = Uuid::randomHex();
$mailTemplateType = [
[
'id' => $mailTemplateTypeId,
'name' => self::TEMPLATE_TYPE_NAME,
'technicalName' => self::TEMPLATE_TYPE_TECHNICAL_NAME,
'availableEntities' => [
'product' => 'product',
'salesChannel' => 'sales_channel'
]
]
];
$mailTemplate = [
[
'id' => Uuid::randomHex(),
'mailTemplateTypeId' => $mailTemplateTypeId,
'subject' => [
'en-GB' => 'Agrility Reservation',
'de-DE' => 'LG Seeds - Bestätigung Agrility Reservierung'
],
'senderName' => '{{ salesChannel.name }}',
'contentPlain' => "
Hallo {{ userName }},\n\n
Durch Ihre Reservierung in unserem Online-Portal haben Sie die Möglichkeit, kostenfrei Applikationskarten zur teilflächenspezifischen Maisaussaat 2021 zur erhalten. Entsprechend Ihrer reservierten Maismenge, erhalten Sie ein Flächenkontingent (ha), für das Sie Applikationskarten Anfragen können.\n
Anfang 2021 erhalten Sie von uns den Code sowie alle weiteren Informationen, um die Applikationskarten für Ihre Maisflächen kostenfrei zu bestellen.\n
Für weitere Informationen zu unserem Service der teilflächenspezifischen Maisaussaat erhalten Sie hier:\n
www.lgseeds.de/agrility
",
'contentHtml' => '
<div style="font-family:arial; font-size:12px;">
<p>Hallo {{ userName }},<br/><br/>
<p>Durch Ihre Reservierung in unserem Online-Portal haben Sie die Möglichkeit, kostenfrei Applikationskarten zur teilflächenspezifischen Maisaussaat 2021 zur erhalten. Entsprechend Ihrer reservierten Maismenge, erhalten Sie ein Flächenkontingent (ha), für das Sie Applikationskarten Anfragen können.</p>
<p>Anfang 2021 erhalten Sie von uns den Code sowie alle weiteren Informationen, um die Applikationskarten für Ihre Maisflächen kostenfrei zu bestellen.</p>
<p>Für weitere Informationen zu unserem Service der teilflächenspezifischen Maisaussaat erhalten Sie hier: <a href="http://www.lgseeds.de/agrility">www.lgseeds.de/agrility</a></p>
</p>
</div>
',
]
];
try {
$mailTemplateTypeRepository->create($mailTemplateType, $installContext->getContext());
$mailTemplateRepository->create($mailTemplate, $installContext->getContext());
} catch (UniqueConstraintViolationException $exception) {
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
return;
}
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
/** @var MailTemplateTypeEntity $myCustomMailTemplateType */
$myCustomMailTemplateType = $mailTemplateTypeRepository->search(
(new Criteria())
->addFilter(new EqualsFilter('technicalName', self::TEMPLATE_TYPE_TECHNICAL_NAME)),
$uninstallContext
->getContext()
)->first();
$mailTemplateIds = $mailTemplateRepository->searchIds(
(new Criteria())
->addFilter(new EqualsFilter('mailTemplateTypeId', $myCustomMailTemplateType->getId())),
$uninstallContext
->getContext()
)->getIds();
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $mailTemplateIds);
$mailTemplateRepository->delete($ids, $uninstallContext->getContext());
//Delete the TemplateType which were added by this Plugin
$mailTemplateTypeRepository->delete([
['id' => $myCustomMailTemplateType->getId()]
], $uninstallContext->getContext());
}
}