custom/plugins/MadFrontendContactForm/src/MadFrontendContactForm.php line 21

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