<?php declare(strict_types=1);
namespace Acris\ProductInfoText;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Uuid\Uuid;
class AcrisProductInfoTextCS extends Plugin
{
const ACRIS_ORDER_CONFIRMATION_PRODUCT_INFO_TEXT = 'acrisOrderConfirmationProductInfoText';
public function activate(ActivateContext $context): void
{
$this->insertDefaultMailTemplate($context->getContext());
}
public function postUpdate(UpdateContext $updateContext): void
{
if (version_compare($updateContext->getCurrentPluginVersion(), '3.1.0', '<') && $updateContext->getPlugin()->isActive() === true) {
$this->insertDefaultMailTemplate($updateContext->getContext());
}
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
$connection->executeUpdate('DROP TABLE IF EXISTS `acris_info_text_translation`');
$connection->executeUpdate('DROP TABLE IF EXISTS `acris_info_text_sales_channel`');
$connection->executeUpdate('DROP TABLE IF EXISTS `acris_info_text_stream`');
$this->removeInheritance($connection, 'rule', 'acrisProductInfoText');
$connection->executeStatement('DROP TABLE IF EXISTS `acris_info_text_rule`');
$connection->executeUpdate('DROP TABLE IF EXISTS `acris_info_text`');
$connection->executeStatement('DROP TABLE IF EXISTS `acris_info_text_rule`');
$this->removeInheritance($connection, 'product_stream', 'productInfoText');
$this->removeInheritance($connection, 'sales_channel', 'productInfoText');
$this->removeInheritance($connection, 'rule', 'productInfoText');
$this->removeDefaultMailTemplate($context->getContext());
}
protected function removeInheritance(Connection $connection, string $entity, string $propertyName): void
{
$sql = str_replace(
['#table#', '#column#'],
[$entity, $propertyName],
'ALTER TABLE `#table#` DROP IF EXISTS `#column#`'
);
$connection->executeUpdate($sql);
}
private function insertDefaultMailTemplate(Context $context)
{
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
// ACRIS order confirmation
/** @var IdSearchResult $mailTemplateTypeOrderConfirmationSearchResult */
$mailTemplateTypeOrderConfirmationSearchResult = $mailTemplateTypeRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('technicalName', self::ACRIS_ORDER_CONFIRMATION_PRODUCT_INFO_TEXT)), $context);
if (empty($mailTemplateTypeOrderConfirmationSearchResult->firstId())) {
$mailTemplateTypeOrderConfirmationId = Uuid::randomHex();
$mailTemplateOrderConfirmationId = Uuid::randomHex();
$mailTemplateTypOrderConfirmationData = [
'id' => $mailTemplateTypeOrderConfirmationId,
'technicalName' => self::ACRIS_ORDER_CONFIRMATION_PRODUCT_INFO_TEXT,
'availableEntities' => ['order' => 'order', 'salesChannel' => 'sales_channel', 'editOrderUrl' => null],
'translations' => [
'de-DE' => [
'name' => 'ACRIS Bestellbestätigung mit Produkt Info Texten'
],
'en-GB' => [
'name' => 'ACRIS order confirmation with product info texts'
]
],
'name' => 'ACRIS order confirmation with with product info texts',
'mailTemplates' => [
[
'id' => $mailTemplateOrderConfirmationId,
'systemDefault' => true,
'translations' => [
'de-DE' => [
'senderName' => '{{ salesChannel.name }}',
'subject' => 'Ihre Bestellung bei {{ salesChannel.name }} (#{{ order.orderNumber }})',
'description' => 'ACRIS Mail Template mit Produkt Info Texten',
'contentHtml' => file_get_contents($this->path . '/Resources/mail-template/html/de-DE/order-confirmation-product-info-texts.html.twig'),
'contentPlain' => file_get_contents($this->path . '/Resources/mail-template/plain/de-DE/order-confirmation-product-info-texts.html.twig')
],
'en-GB' => [
'senderName' => '{{ salesChannel.name }}',
'subject' => 'Your order at {{ salesChannel.name }} (#{{ order.orderNumber }})',
'description' => 'ACRIS Mail Template with product info texts',
'contentHtml' => file_get_contents($this->path . '/Resources/mail-template/html/en-GB/order-confirmation-product-info-texts.html.twig'),
'contentPlain' => file_get_contents($this->path . '/Resources/mail-template/plain/en-GB/order-confirmation-product-info-texts.html.twig')
],
],
'senderName' => '{{ salesChannel.name }}',
'subject' => 'Your order at {{ salesChannel.name }} (#{{ order.orderNumber }})',
'description' => 'ACRIS Mail Template with product info texts',
'contentHtml' => file_get_contents($this->path . '/Resources/mail-template/html/en-GB/order-confirmation-product-info-texts.html.twig'),
'contentPlain' => file_get_contents($this->path . '/Resources/mail-template/plain/en-GB/order-confirmation-product-info-texts.html.twig')
]
]
];
$mailTemplateTypeRepository->upsert([$mailTemplateTypOrderConfirmationData], $context);
}
}
private function removeDefaultMailTemplate(Context $context)
{
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var IdSearchResult $mailTemplateTypeOrderConfirmationSearchResult */
$mailTemplateTypeOrderConfirmationSearchResult = $mailTemplateTypeRepository->searchIds((new Criteria())->addFilter(new EqualsAnyFilter('technicalName', [self::ACRIS_ORDER_CONFIRMATION_PRODUCT_INFO_TEXT])), $context);
if ($mailTemplateTypeOrderConfirmationSearchResult->firstId()) {
$mailTemplateRepository = $this->container->get('mail_template.repository');
$eventActionRepository = $this->container->get('event_action.repository');
$deleteTemplateTypeData = [];
$deleteTemplateData = [];
$isUsedInEvent = false;
foreach ($mailTemplateTypeOrderConfirmationSearchResult->getIds() as $templateTypeId) {
/** @var IdSearchResult $mailTemplateSearchResult */
$mailTemplateSearchResult = $mailTemplateRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('mailTemplateTypeId', $templateTypeId))->addFilter(new EqualsFilter('updatedAt', null)), $context);
if ($eventActionRepository->search((new Criteria())->addFilter(new ContainsFilter('config', $templateTypeId)), $context)->getTotal() > 0) {
$isUsedInEvent = true;
}
if ($mailTemplateSearchResult->firstId() and !$isUsedInEvent) {
foreach ($mailTemplateSearchResult->getIds() as $id) {
if ($eventActionRepository->search((new Criteria())->addFilter(new ContainsFilter('config', $id)), $context)->getTotal() > 0) {
$isUsedInEvent = true;
}
$deleteTemplateData[] = ['id' => $id];
}
}
$deleteTemplateTypeData[] = ['id' => $templateTypeId];
}
if (!empty($deleteTemplateData) and !$isUsedInEvent) {
$mailTemplateRepository->delete($deleteTemplateData, $context);
}
if (!empty($deleteTemplateData) and !empty($deleteTemplateTypeData and !$isUsedInEvent)) {
$mailTemplateTypeRepository->delete($deleteTemplateTypeData, $context);
}
}
}
}