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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Twigel\GetNotified;
  3. use Composer\InstalledVersions;
  4. use Doctrine\DBAL\DBALException;
  5. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Twigel\GetNotified\Compatibility\DependencyLoader;
  15. use Twigel\GetNotified\Content\StockSubscriber\Aggregate\StockSubscriberProductDefinition;
  16. use Twigel\GetNotified\Content\StockSubscriber\StockSubscriberDefinition;
  17. use Twigel\GetNotified\Migration\Migration1572162797StockSubscriber;
  18. class TwigelGetNotified extends Plugin
  19. {
  20.     const BACK_IN_STOCK_MAIL_TEMPLATE_NAME 'get_notified.stock_notification';
  21.     const NEW_SUBSCRIBER_MAIL_TEMPLATE_NAME 'get_notified.new_subscriber_notification';
  22.     const DOUBLE_OPT_IN_MAIL_TEMPLATE_NAME 'get_notified.double_opt_in_notification';
  23.     /**
  24.      * @param ContainerBuilder $container
  25.      *
  26.      * @return void
  27.      * @throws \Exception
  28.      */
  29.     public function build(ContainerBuilder $container): void
  30.     {
  31.         parent::build($container);
  32.         $this->container $container;
  33.         # load the dependencies that are compatible
  34.         # with our current shopware version
  35.         $loader = new DependencyLoader($container);
  36.         $loader->loadServices();
  37.     }
  38.     public function install(InstallContext $installContext): void
  39.     {
  40.         parent::install($installContext);
  41.         (new Migration1572162797StockSubscriber())->update($this->container->get("Doctrine\DBAL\Connection"));
  42.     }
  43.     /**
  44.      * @param UninstallContext $uninstallContext
  45.      *
  46.      * @throws DBALException
  47.      * @throws InconsistentCriteriaIdsException
  48.      */
  49.     public function uninstall(UninstallContext $uninstallContext): void
  50.     {
  51.         if ($uninstallContext->keepUserData()) {
  52.             return;
  53.         }
  54.         $connection $this->container->get('Doctrine\DBAL\Connection');
  55.         $stockSubscriberProductsTable StockSubscriberProductDefinition::ENTITY_NAME;
  56.         $stockSubscriberTable StockSubscriberDefinition::ENTITY_NAME;
  57.         $connection->executeStatement("DROP TABLE IF EXISTS $stockSubscriberProductsTable");
  58.         $connection->executeStatement("DROP TABLE IF EXISTS $stockSubscriberTable");
  59.         $mailTemplateRepository $this->container->get('mail_template.repository');
  60.         $mailTemplateTypeRepository $this->container->get('mail_template_type.repository');
  61.         $criteria = new Criteria();
  62.         $criteria->addAssociation('mailTemplateType');
  63.         $criteria->addFilter(new EqualsAnyFilter(
  64.             'mailTemplateType.technicalName', [
  65.                 self::BACK_IN_STOCK_MAIL_TEMPLATE_NAME,
  66.                 self::NEW_SUBSCRIBER_MAIL_TEMPLATE_NAME,
  67.                 self::DOUBLE_OPT_IN_MAIL_TEMPLATE_NAME,
  68.             ]
  69.         ));
  70.         $templates $mailTemplateRepository->search($criteria$uninstallContext->getContext());
  71.         if ($templates->count() <= 0) {
  72.             return;
  73.         }
  74.         $mailTemplateIds = [];
  75.         $mailTemplateTypeIds = [];
  76.         /** @var MailTemplateEntity $mailTemplate */
  77.         foreach ($templates->getElements() as $mailTemplate) {
  78.             $mailTemplateIds[] = ['id' => $mailTemplate->getId()];
  79.             if (!in_array($mailTemplate->getMailTemplateTypeId(), $mailTemplateTypeIds)) {
  80.                 $mailTemplateTypeIds[] = ['id' => $mailTemplate->getMailTemplateTypeId()];
  81.             }
  82.         }
  83.         if (!empty($mailTemplateIds)) {
  84.             $mailTemplateRepository->delete($mailTemplateIds$uninstallContext->getContext());
  85.         }
  86.         if (!empty($mailTemplateTypeIds)) {
  87.             $mailTemplateTypeRepository->delete($mailTemplateTypeIds$uninstallContext->getContext());
  88.         }
  89.         $conn $this->container->get('Doctrine\DBAL\Connection');
  90.         $customFields = [
  91.             'zeobvGetNotifiedNeverNotify',
  92.             'zeobvGetNotifiedMinStockToNotify'
  93.         ];
  94.         $query "";
  95.         foreach ($customFields as $customField) {
  96.             $query .= "UPDATE `product_translation` SET `custom_fields` = JSON_REMOVE(`product_translation`.`custom_fields`,'$.$customField');";
  97.         }
  98.         $conn->executeStatement($query);
  99.     }
  100. }