custom/plugins/MadMyWerb/src/MyWerb.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Mad\MyWerb;
  4. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  5. use Mad\MyWerb\DependencyInjection\CompilerPass\BusinessEventCompilerPass;
  6. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Event\EventAction\EventActionEntity;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. class MyWerb extends Plugin
  17. {
  18.     public const TEMPLATE_TYPE_NAME 'MyWerb';
  19.     public const TEMPLATE_TYPE_TECHNICAL_NAME 'order.my_werb_notification';
  20.     public const MAIL_TEMPLATE_NAME 'MyWerbMailTemplate';
  21.     public const EVENT_ACTION_NAME self::TEMPLATE_TYPE_TECHNICAL_NAME;
  22.     public const EVENT_ACTION_TITLE 'MyWerb Bestell-Info an Shopbetreiber';
  23.     /**
  24.      * @deprecated
  25.      */
  26.     public const TEMPLATE_TYPE_TECHNICAL_NAME_OLD 'my_werb';
  27.     public function build(ContainerBuilder $container): void
  28.     {
  29.         parent::build($container);
  30.     }
  31.     public function update(UpdateContext $updateContext): void
  32.     {
  33.         if (\version_compare($updateContext->getCurrentPluginVersion(), '1.1.0''<')) {
  34.             $this->renameMailTemplateType($updateContext);
  35.             $this->addEventAction($updateContext);
  36.         }
  37.     }
  38.     public function install(InstallContext $installContext): void
  39.     {
  40.         $context $installContext->getContext();
  41.         $mailTemplateTypeRepository $this->getMailTemplateTypeRepository();
  42.         $mailTemplateRepository $this->getMailTemplateRepository();
  43.         $mailTemplateTypeId $mailTemplateTypeRepository->searchIds(
  44.             (new Criteria())
  45.                 ->addFilter(
  46.                     new EqualsFilter('technicalName'self::TEMPLATE_TYPE_TECHNICAL_NAME)
  47.                 ),
  48.         $context)->firstId();
  49.         if ($mailTemplateTypeId === null) {
  50.             $mailTemplateTypeId Uuid::randomHex();
  51.         }
  52.         $mailTemplateType = [
  53.             [
  54.                 'id' => $mailTemplateTypeId,
  55.                 'name' => self::TEMPLATE_TYPE_NAME,
  56.                 'technicalName' => self::TEMPLATE_TYPE_TECHNICAL_NAME,
  57.                 'availableEntities' => [
  58.                     'product' => 'product',
  59.                     'salesChannel' => 'sales_channel',
  60.                     'order'=>'order'
  61.                 ]
  62.             ]
  63.         ];
  64.         $mailTemplateId $mailTemplateRepository->searchIds(
  65.             (new Criteria())
  66.             ->addFilter(new EqualsFilter('mailTemplateTypeId'$mailTemplateTypeId)),
  67.         $context)->firstId();
  68.         if ($mailTemplateId === null) {
  69.             $mailTemplateId Uuid::randomHex();
  70.         }
  71.         $mailTemplate = [
  72.             [
  73.                 'id' => $mailTemplateId,
  74.                 'mailTemplateTypeId' => $mailTemplateTypeId,
  75.                 'subject' => [
  76.                     'en-GB' => 'New MyWerb Order',
  77.                     'de-DE' => 'Neue MyWerb Bestellung'
  78.                 ],
  79.                 'contentPlain' => "text",
  80.                 'contentHtml' => 'html',
  81.             ]
  82.         ];
  83.         try {
  84.             $mailTemplateTypeRepository->upsert($mailTemplateType$installContext->getContext());
  85.             $mailTemplateRepository->upsert($mailTemplate$installContext->getContext());
  86.             $this->addEventAction($installContext);
  87.         } catch (UniqueConstraintViolationException $exception) {
  88.         }
  89.     }
  90.     /**
  91.      * @since 1.1.0
  92.      */
  93.     private function renameMailTemplateType(UpdateContext $updateContext): void
  94.     {
  95.         $context $updateContext->getContext();
  96.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  97.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  98.         $mailTemplateType $mailTemplateTypeRepository->search(
  99.             (new Criteria())
  100.                 ->addFilter(
  101.                     new EqualsFilter('technicalName'self::TEMPLATE_TYPE_TECHNICAL_NAME_OLD)
  102.                 ),
  103.             $context
  104.         )->first();
  105.         if ($mailTemplateType instanceof MailTemplateTypeEntity) {
  106.             $mailTemplateTypeRepository->update([
  107.                 [
  108.                     'id' => $mailTemplateType->getId(),
  109.                     'technicalName' => self::TEMPLATE_TYPE_TECHNICAL_NAME,
  110.                 ]
  111.             ], $context);
  112.         }
  113.     }
  114.     private function getMailTemplateTypeRepository(): EntityRepositoryInterface
  115.     {
  116.         return $this->container->get('mail_template_type.repository');
  117.     }
  118.     private function getMailTemplateRepository(): EntityRepositoryInterface
  119.     {
  120.         return $this->container->get('mail_template.repository');
  121.     }
  122. }
  123. //docker-compose exec app_server bin/console cache:clear