custom/plugins/CioImports/src/CioImports.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CioImports;
  3. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  4. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  5. use Shopware\Core\Framework\Context;
  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\Event\EventAction\EventActionEntity;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. class CioImports extends Plugin
  15. {
  16.     public function install(InstallContext $installContext): void
  17.     {
  18.         parent::install($installContext);
  19.         $mailTemplateTypeId Uuid::randomHex();
  20.         $this->createEmailTypeWithTemplates([
  21.             'id' => $mailTemplateTypeId,
  22.             'name' => 'ImportStockMissingProductNotificationType',
  23.             'technicalName' => 'import_stock_missing_product_notification_type',
  24.             'availableEntities' => [
  25.                 'product' => 'product'
  26.             ]
  27.         ],
  28.         [
  29.             [
  30.                 'id' => Uuid::randomHex(),
  31.                 'mailTemplateTypeId' => $mailTemplateTypeId,
  32.                 'subject' => [
  33.                     'en-GB' => 'Product is missing in stock import file',
  34.                     'de-DE' => 'Produkt fehlt beim Import des Lagerbestands'
  35.                 ],
  36.                 'contentPlain' => "Hello,\nthis is the content in plain text for my custom mail template\n\nKind Regards,\nYours",
  37.                 'contentHtml' => "<p>Hello,<br>this is the content in plain text for my custom mail template<br><br>Kind Regards,<br>Yours</p>",
  38.             ]
  39.         ], $installContext->getContext());
  40.         $eventActionId Uuid::randomHex();
  41.         $this->createEventAction([
  42.                 'id' => $eventActionId,
  43.                 'title' => 'Lagerbestand Import Benachrichtigung über fehlendes Produkt',
  44.                 'eventName' => 'import_notification_stock_missing',
  45.                 'actionName' => 'action.mail.send',
  46.                 'config' => [
  47.                     'mail_template_type_id' => $mailTemplateTypeId
  48.                 ],
  49.                 'active' => true,
  50.                 'createdAt' => new \DateTime()
  51.         ], $installContext->getContext());
  52.     }
  53.     public function uninstall(UninstallContext $uninstallContext): void
  54.     {
  55.         parent::uninstall($uninstallContext);
  56.         //Keep UserData? Then do nothing here
  57.         if ($uninstallContext->keepUserData()) {
  58.             return;
  59.         }
  60.         $this->removeEventAction('import_notification_stock_missing'$uninstallContext->getContext());
  61.         $this->removeEmailTypeWithTemplates('import_stock_missing_product_notification_type'$uninstallContext->getContext());
  62.     }
  63.     protected function createEmailTypeWithTemplates(array $type, array $templatesContext $context)
  64.     {
  65.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  66.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  67.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  68.         $mailTemplateRepository $this->container->get('mail_template.repository');
  69.         try {
  70.             $mailTemplateTypeRepository->create([$type], $context);
  71.             $mailTemplateRepository->create($templates$context);
  72.         } catch (UniqueConstraintViolationException $exception) {
  73.             // No, we've already installed the fields, it's fine.
  74.         }
  75.     }
  76.     protected function createEventAction(array $eventActionContext $context)
  77.     {
  78.         /** @var EntityRepositoryInterface $eventActionRepository */
  79.         $eventActionRepository $this->container->get('event_action.repository');
  80.         // prevent duplicates of the event
  81.         if ($eventActionRepository->search((new Criteria())->addFilter(new EqualsFilter('eventName'$eventAction['eventName'])), $context)->first() instanceof EventActionEntity) {
  82.             return;
  83.         }
  84.         try {
  85.             $eventActionRepository->create([$eventAction], $context);
  86.         } catch (UniqueConstraintViolationException $exception) {
  87.             // No, we've already installed the fields, it's fine.
  88.         }
  89.     }
  90.     protected function removeEventAction(string $eventActionNameContext $context)
  91.     {
  92.         /** @var EntityRepositoryInterface $eventActionRepository */
  93.         $eventActionRepository $this->container->get('event_action.repository');
  94.         $eventAction $eventActionRepository->search(
  95.             (new Criteria())->addFilter(new EqualsFilter('eventName'$eventActionName)), $context
  96.         )->first();
  97.         $eventActionRepository->delete([
  98.             ['id' => $eventAction->getId()]
  99.         ], $context);
  100.     }
  101.     protected function removeEmailTypeWithTemplates(string $typeNameContext $context)
  102.     {
  103.         /** @var EntityRepositoryInterface $mailTemplateTypeRepository */
  104.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  105.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  106.         $mailTemplateRepository $this->container->get('mail_template.repository');
  107.         /** @var MailTemplateTypeEntity $myCustomMailTemplateType */
  108.         $myCustomMailTemplateType $mailTemplateTypeRepository->search(
  109.             (new Criteria())->addFilter(new EqualsFilter('technicalName'$typeName)), $context
  110.         )->first();
  111.         $ids = [];
  112.         if ($myCustomMailTemplateType instanceof MailTemplateTypeEntity) {
  113.             $mailTemplateIds $mailTemplateRepository->searchIds(
  114.                 (new Criteria())->addFilter(new EqualsFilter('mailTemplateTypeId'$myCustomMailTemplateType->getId())), $context
  115.             )->getIds();
  116.             //Get the Ids from the fetched Entities
  117.             $ids array_map(static function ($id) {
  118.                 return ['id' => $id];
  119.             }, $mailTemplateIds);
  120.         }
  121.         $mailTemplateTypeRepository->delete([
  122.             ['id' => $myCustomMailTemplateType->getId()]
  123.         ], $context);
  124.         $mailTemplateRepository->delete($ids$context);
  125.     }
  126. }