<?php declare(strict_types=1);
namespace Zeobv\GetNotified;
use Composer\InstalledVersions;
use Doctrine\DBAL\DBALException;
use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Zeobv\GetNotified\Compatibility\DependencyLoader;
use Zeobv\GetNotified\Content\StockSubscriber\Aggregate\StockSubscriberProductDefinition;
use Zeobv\GetNotified\Content\StockSubscriber\StockSubscriberDefinition;
class ZeobvGetNotified extends Plugin
{
const BACK_IN_STOCK_MAIL_TEMPLATE_NAME = 'get_notified.stock_notification';
const NEW_SUBSCRIBER_MAIL_TEMPLATE_NAME = 'get_notified.new_subscriber_notification';
const DOUBLE_OPT_IN_MAIL_TEMPLATE_NAME = 'get_notified.double_opt_in_notification';
/**
* @param ContainerBuilder $container
*
* @return void
* @throws \Exception
*/
public function build(ContainerBuilder $container): void
{
parent::build($container);
$this->container = $container;
# load the dependencies that are compatible
# with our current shopware version
$loader = new DependencyLoader($container);
$loader->loadServices();
}
/**
* @param UninstallContext $uninstallContext
*
* @throws DBALException
* @throws InconsistentCriteriaIdsException
*/
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
return;
}
$connection = $this->container->get('Doctrine\DBAL\Connection');
$stockSubscriberProductsTable = StockSubscriberProductDefinition::ENTITY_NAME;
$stockSubscriberTable = StockSubscriberDefinition::ENTITY_NAME;
$connection->executeStatement("DROP TABLE IF EXISTS $stockSubscriberProductsTable");
$connection->executeStatement("DROP TABLE IF EXISTS $stockSubscriberTable");
$mailTemplateRepository = $this->container->get('mail_template.repository');
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
$criteria = new Criteria();
$criteria->addAssociation('mailTemplateType');
$criteria->addFilter(new EqualsAnyFilter(
'mailTemplateType.technicalName', [
self::BACK_IN_STOCK_MAIL_TEMPLATE_NAME,
self::NEW_SUBSCRIBER_MAIL_TEMPLATE_NAME,
self::DOUBLE_OPT_IN_MAIL_TEMPLATE_NAME,
]
));
$templates = $mailTemplateRepository->search($criteria, $uninstallContext->getContext());
if ($templates->count() <= 0) {
return;
}
$mailTemplateIds = [];
$mailTemplateTypeIds = [];
/** @var MailTemplateEntity $mailTemplate */
foreach ($templates->getElements() as $mailTemplate) {
$mailTemplateIds[] = ['id' => $mailTemplate->getId()];
if (!in_array($mailTemplate->getMailTemplateTypeId(), $mailTemplateTypeIds)) {
$mailTemplateTypeIds[] = ['id' => $mailTemplate->getMailTemplateTypeId()];
}
}
if (!empty($mailTemplateIds)) {
$mailTemplateRepository->delete($mailTemplateIds, $uninstallContext->getContext());
}
if (!empty($mailTemplateTypeIds)) {
$mailTemplateTypeRepository->delete($mailTemplateTypeIds, $uninstallContext->getContext());
}
$conn = $this->container->get('Doctrine\DBAL\Connection');
$customFields = [
'zeobvGetNotifiedNeverNotify',
'zeobvGetNotifiedMinStockToNotify'
];
$query = "";
foreach ($customFields as $customField) {
$query .= "UPDATE `product_translation` SET `custom_fields` = JSON_REMOVE(`product_translation`.`custom_fields`,'$.$customField');";
}
$conn->executeStatement($query);
}
}