<?php
declare(strict_types=1);
namespace MadPreventAddressModification;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use MadPreventAddressModification\Core\Event\AddressChangeRequestEvent;
use MadPreventAddressModification\DependencyInjection\CompilerPass\BusinessEventCompilerPass;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Content\MailTemplate\MailTemplateActions;
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\Event\EventAction\EventActionEntity;
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;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class MadPreventAddressModification extends Plugin
{
public const TEMPLATE_TYPE_NAME = 'AddressChangeRequestType';
public const TEMPLATE_TYPE_TECHNICAL_NAME = 'address_change_request';
public const MAIL_TEMPLATE_NAME = 'AddressChangeRequest';
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' => [
'salesChannel' => 'sales_channel',
'customer' => 'customer',
]
]
];
//You can add translations with the matching ISO-Codes. You always have to provide a value vor the default system language
//Later you can change and add translations also in the administration
$mailTemplateId = Uuid::randomHex();
$mailTemplate = [
[
'id' => $mailTemplateId,
'mailTemplateTypeId' => $mailTemplateTypeId,
'subject' => [
'en-GB' => 'Address change request',
'de-DE' => 'Addressänderungsanfrage',
],
'senderName' => [
'en-GB' => '{{ salesChannel.name }}',
'de-DE' => '{{ salesChannel.name }}',
],
'contentPlain' => "PLACEHOLDER, please use twig-tempate in Theme",
'contentHtml' => 'PLACEHOLDER, please use twig-tempate in Theme',
]
];
try {
$mailTemplateTypeRepository->create($mailTemplateType, $installContext->getContext());
$mailTemplateRepository->create($mailTemplate, $installContext->getContext());
} catch (UniqueConstraintViolationException $exception) {
// No, we've already installed the fields, it's fine.
}
$eventActionRepository = $this->container->get('event_action.repository');
$event = [
[
'title' => 'Addressänderung angefragt',
'eventName' => AddressChangeRequestEvent::EVENT_NAME,
'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
'config' => [
'mail_template_id' => $mailTemplateId,
'mail_template_type_id' => $mailTemplateTypeId,
],
'active' => true,
]
];
try {
$eventActionRepository->create($event, $installContext->getContext());
} catch (UniqueConstraintViolationException $exception) {
// No, we've already installed the fields, it's fine.
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
//Keep UserData? Then do nothing here
if ($uninstallContext->keepUserData()) {
return;
}
//get the Templates and Associations added by this Plugin from the DB
/** @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();
//Get the Ids from the fetched Entities
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $mailTemplateIds);
//Delete the Templates which were added by this Plugin
$mailTemplateRepository->delete($ids, $uninstallContext->getContext());
//Delete the TemplateType which were added by this Plugin
$mailTemplateTypeRepository->delete([
['id' => $myCustomMailTemplateType->getId()]
], $uninstallContext->getContext());
/** @var EntityRepositoryInterface $eventActionRepository */
$eventActionRepository = $this->container->get('event_action.repository');
/* @var $changeAddressRequestEventAction \Shopware\Core\Framework\Event\EventAction\EventActionEntity */
$changeAddressRequestEventAction = $eventActionRepository->search(
(new Criteria())
->addFilter(new EqualsFilter('eventName', AddressChangeRequestEvent::EVENT_NAME)),
$uninstallContext
->getContext()
)->first();
//Delete the TemplateType which were added by this Plugin
if ($changeAddressRequestEventAction instanceof EventActionEntity) {
$eventActionRepository->delete([
['id' => $changeAddressRequestEventAction->getId()]
], $uninstallContext->getContext());
}
}
}