custom/static-plugins/MadPreventAddressModification/src/MadPreventAddressModification.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadPreventAddressModification;
  4. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  5. use MadPreventAddressModification\Core\Event\AddressChangeRequestEvent;
  6. use MadPreventAddressModification\DependencyInjection\CompilerPass\BusinessEventCompilerPass;
  7. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  8. use Shopware\Core\Content\MailTemplate\MailTemplateActions;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Event\EventAction\EventActionEntity;
  13. use Shopware\Core\Framework\Plugin;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. class MadPreventAddressModification extends Plugin
  19. {
  20.     public const TEMPLATE_TYPE_NAME 'AddressChangeRequestType';
  21.     public const TEMPLATE_TYPE_TECHNICAL_NAME 'address_change_request';
  22.     public const MAIL_TEMPLATE_NAME 'AddressChangeRequest';
  23.     public function install(InstallContext $installContext): void
  24.     {
  25.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  26.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  27.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  28.         $mailTemplateRepository $this->container->get('mail_template.repository');
  29.         $mailTemplateTypeId Uuid::randomHex();
  30.         $mailTemplateType = [
  31.             [
  32.                 'id' => $mailTemplateTypeId,
  33.                 'name' => self::TEMPLATE_TYPE_NAME,
  34.                 'technicalName' => self::TEMPLATE_TYPE_TECHNICAL_NAME,
  35.                 'availableEntities' => [
  36.                     'salesChannel' => 'sales_channel',
  37.                     'customer' => 'customer',
  38.                 ]
  39.             ]
  40.         ];
  41.         //You can add translations with the matching ISO-Codes. You always have to provide a value vor the default system language
  42.         //Later you can change and add translations also in the administration
  43.         $mailTemplateId Uuid::randomHex();
  44.         $mailTemplate = [
  45.             [
  46.                 'id' => $mailTemplateId,
  47.                 'mailTemplateTypeId' => $mailTemplateTypeId,
  48.                 'subject' => [
  49.                     'en-GB' => 'Address change request',
  50.                     'de-DE' => 'Addressänderungsanfrage',
  51.                 ],
  52.                 'senderName' => [
  53.                     'en-GB' => '{{ salesChannel.name }}',
  54.                     'de-DE' => '{{ salesChannel.name }}',
  55.                 ],
  56.                 'contentPlain' => "PLACEHOLDER, please use twig-tempate in Theme",
  57.                 'contentHtml' => 'PLACEHOLDER, please use twig-tempate in Theme',
  58.             ]
  59.         ];
  60.         try {
  61.             $mailTemplateTypeRepository->create($mailTemplateType$installContext->getContext());
  62.             $mailTemplateRepository->create($mailTemplate$installContext->getContext());
  63.         } catch (UniqueConstraintViolationException $exception) {
  64.             // No, we've already installed the fields, it's fine.
  65.         }
  66.         $eventActionRepository $this->container->get('event_action.repository');
  67.         $event = [
  68.             [
  69.                 'title' => 'Addressänderung angefragt',
  70.                 'eventName' => AddressChangeRequestEvent::EVENT_NAME,
  71.                 'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
  72.                 'config' => [
  73.                     'mail_template_id' => $mailTemplateId,
  74.                     'mail_template_type_id' => $mailTemplateTypeId,
  75.                 ],
  76.                 'active' => true,
  77.             ]
  78.         ];
  79.         try {
  80.             $eventActionRepository->create($event$installContext->getContext());
  81.         } catch (UniqueConstraintViolationException $exception) {
  82.             // No, we've already installed the fields, it's fine.
  83.         }
  84.     }
  85.     public function uninstall(UninstallContext $uninstallContext): void
  86.     {
  87.         //Keep UserData? Then do nothing here
  88.         if ($uninstallContext->keepUserData()) {
  89.             return;
  90.         }
  91.         //get the Templates and Associations added by this Plugin from the DB
  92.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  93.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  94.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  95.         $mailTemplateRepository $this->container->get('mail_template.repository');
  96.         /** @var MailTemplateTypeEntity $myCustomMailTemplateType */
  97.         $myCustomMailTemplateType $mailTemplateTypeRepository->search(
  98.             (new Criteria())
  99.                 ->addFilter(new EqualsFilter('technicalName'self::TEMPLATE_TYPE_TECHNICAL_NAME)),
  100.             $uninstallContext
  101.                 ->getContext()
  102.         )->first();
  103.         $mailTemplateIds $mailTemplateRepository->searchIds(
  104.             (new Criteria())
  105.                 ->addFilter(new EqualsFilter('mailTemplateTypeId'$myCustomMailTemplateType->getId())),
  106.             $uninstallContext
  107.                 ->getContext()
  108.         )->getIds();
  109.         //Get the Ids from the fetched Entities
  110.         $ids array_map(static function ($id) {
  111.             return ['id' => $id];
  112.         }, $mailTemplateIds);
  113.         //Delete the Templates which were added by this Plugin
  114.         $mailTemplateRepository->delete($ids$uninstallContext->getContext());
  115.         //Delete the TemplateType which were added by this Plugin
  116.         $mailTemplateTypeRepository->delete([
  117.             ['id' => $myCustomMailTemplateType->getId()]
  118.         ], $uninstallContext->getContext());
  119.         /** @var EntityRepositoryInterface $eventActionRepository */
  120.         $eventActionRepository $this->container->get('event_action.repository');
  121.         /* @var $changeAddressRequestEventAction \Shopware\Core\Framework\Event\EventAction\EventActionEntity */
  122.         $changeAddressRequestEventAction $eventActionRepository->search(
  123.             (new Criteria())
  124.                 ->addFilter(new EqualsFilter('eventName'AddressChangeRequestEvent::EVENT_NAME)),
  125.             $uninstallContext
  126.                 ->getContext()
  127.         )->first();
  128.         //Delete the TemplateType which were added by this Plugin
  129.         if ($changeAddressRequestEventAction instanceof EventActionEntity) {
  130.             $eventActionRepository->delete([
  131.                 ['id' => $changeAddressRequestEventAction->getId()]
  132.             ], $uninstallContext->getContext());
  133.         }
  134.     }
  135. }