custom/plugins/ZeobvGetNotified/src/ZeobvGetNotified.php line 20

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