custom/plugins/MadCreateBuisnessMail/src/MadCreateBuisnessMail.php line 20

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