custom/plugins/MojeBambinoComplaintForm/src/MojeBambinoComplaintForm.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MojeBambino\ComplaintForm;
  3. use Shopware\Core\Framework\Plugin;
  4. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  5. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Uuid\Uuid;
  12. class MojeBambinoComplaintForm extends Plugin
  13. {
  14.     public const TEMPLATE_TYPE_NAME 'ComplaintType';
  15.     public const TEMPLATE_TYPE_TECHNICAL_NAME 'complaint_type';
  16.     public const MAIL_TEMPLATE_NAME 'ComplaintMailTemplate';
  17.     
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  21.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  22.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  23.         $mailTemplateRepository $this->container->get('mail_template.repository');
  24.         $mailTemplateTypeId Uuid::randomHex();
  25.         $mailTemplateType = [
  26.                 [
  27.                         'id' => $mailTemplateTypeId,
  28.                         'name' => self::TEMPLATE_TYPE_NAME,
  29.                         'technicalName' => self::TEMPLATE_TYPE_TECHNICAL_NAME,
  30.                         'availableEntities' => [
  31.                                 'complaintForm' => 'complaint_form',
  32.                                 'salesChannel' => 'sales_channel'
  33.                         ]
  34.                 ]
  35.         ];
  36.         
  37.         //You can add translations with the matching ISO-Codes. You always have to provide a value vor the default system language
  38.         //Later you can change and add translations also in the administration
  39.         $mailTemplate = [
  40.                 [
  41.                         'id' => Uuid::randomHex(),
  42.                         'mailTemplateTypeId' => $mailTemplateTypeId,
  43.                         'subject' => [
  44.                                 'en-GB' => 'Subject of my custom mail template',
  45.                                 'de-DE' => 'Betreff meiner eigenen Mailvorlage',
  46.                                 'pl-PL' => 'Moje Bambino - reklamacja złożona online'
  47.                         ],
  48.                         'contentPlain' => "Hello,\nthis is the content in plain text for my custom mail template\n\nKind Regards,\nYours",
  49.                         'contentHtml' => 'Hello,<br>this is the content in html for my custom mail template<br/><br/>Kind Regards,<br/>Yours',
  50.                 ]
  51.         ];
  52.         
  53.         try {
  54.             $mailTemplateTypeRepository->create($mailTemplateType$installContext->getContext());
  55.             $mailTemplateRepository->create($mailTemplate$installContext->getContext());
  56.         } catch (UniqueConstraintViolationException $exception) {
  57.             // No, we've already installed the fields, it's fine.
  58.         }
  59.     }
  60.     
  61.     public function uninstall(UninstallContext $uninstallContext): void
  62.     {
  63.         //Keep UserData? Then do nothing here
  64.         if ($uninstallContext->keepUserData()) {
  65.             return;
  66.         }
  67.         
  68.         //get the Templates and Associations added by this Plugin from the DB
  69.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  70.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  71.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  72.         $mailTemplateRepository $this->container->get('mail_template.repository');
  73.         
  74.         /** @var MailTemplateTypeEntity $myCustomMailTemplateType */
  75.         $myCustomMailTemplateType $mailTemplateTypeRepository->search(
  76.                 (new Criteria())
  77.                     ->addFilter(new EqualsFilter('technicalName'self::TEMPLATE_TYPE_TECHNICAL_NAME)),
  78.                 $uninstallContext
  79.                     ->getContext()
  80.                 )->first();
  81.                 
  82.                 $mailTemplateIds $mailTemplateRepository->searchIds(
  83.                         (new Criteria())
  84.                             ->addFilter(new EqualsFilter('mailTemplateTypeId'$myCustomMailTemplateType->getId())),
  85.                         $uninstallContext
  86.                             ->getContext()
  87.                         )->getIds();
  88.                         
  89.                         //Get the Ids from the fetched Entities
  90.                         $ids array_map(static function ($id) {
  91.                             return ['id' => $id];
  92.                         }, $mailTemplateIds);
  93.                             
  94.                             //Delete the Templates which were added by this Plugin
  95.                             $mailTemplateRepository->delete($ids$uninstallContext->getContext());
  96.                             
  97.                             //Delete the TemplateType which were added by this Plugin
  98.                             $mailTemplateTypeRepository->delete([
  99.                                     ['id' => $myCustomMailTemplateType->getId()]
  100.                             ], $uninstallContext->getContext());
  101.     }
  102. }