<?php
declare(strict_types=1);
namespace Mad\MyWerb;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Mad\MyWerb\DependencyInjection\CompilerPass\BusinessEventCompilerPass;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Event\EventAction\EventActionEntity;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class MyWerb extends Plugin
{
public const TEMPLATE_TYPE_NAME = 'MyWerb';
public const TEMPLATE_TYPE_TECHNICAL_NAME = 'order.my_werb_notification';
public const MAIL_TEMPLATE_NAME = 'MyWerbMailTemplate';
public const EVENT_ACTION_NAME = self::TEMPLATE_TYPE_TECHNICAL_NAME;
public const EVENT_ACTION_TITLE = 'MyWerb Bestell-Info an Shopbetreiber';
/**
* @deprecated
*/
public const TEMPLATE_TYPE_TECHNICAL_NAME_OLD = 'my_werb';
public function build(ContainerBuilder $container): void
{
parent::build($container);
}
public function update(UpdateContext $updateContext): void
{
if (\version_compare($updateContext->getCurrentPluginVersion(), '1.1.0', '<')) {
$this->renameMailTemplateType($updateContext);
$this->addEventAction($updateContext);
}
}
public function install(InstallContext $installContext): void
{
$context = $installContext->getContext();
$mailTemplateTypeRepository = $this->getMailTemplateTypeRepository();
$mailTemplateRepository = $this->getMailTemplateRepository();
$mailTemplateTypeId = $mailTemplateTypeRepository->searchIds(
(new Criteria())
->addFilter(
new EqualsFilter('technicalName', self::TEMPLATE_TYPE_TECHNICAL_NAME)
),
$context)->firstId();
if ($mailTemplateTypeId === null) {
$mailTemplateTypeId = Uuid::randomHex();
}
$mailTemplateType = [
[
'id' => $mailTemplateTypeId,
'name' => self::TEMPLATE_TYPE_NAME,
'technicalName' => self::TEMPLATE_TYPE_TECHNICAL_NAME,
'availableEntities' => [
'product' => 'product',
'salesChannel' => 'sales_channel',
'order'=>'order'
]
]
];
$mailTemplateId = $mailTemplateRepository->searchIds(
(new Criteria())
->addFilter(new EqualsFilter('mailTemplateTypeId', $mailTemplateTypeId)),
$context)->firstId();
if ($mailTemplateId === null) {
$mailTemplateId = Uuid::randomHex();
}
$mailTemplate = [
[
'id' => $mailTemplateId,
'mailTemplateTypeId' => $mailTemplateTypeId,
'subject' => [
'en-GB' => 'New MyWerb Order',
'de-DE' => 'Neue MyWerb Bestellung'
],
'contentPlain' => "text",
'contentHtml' => 'html',
]
];
try {
$mailTemplateTypeRepository->upsert($mailTemplateType, $installContext->getContext());
$mailTemplateRepository->upsert($mailTemplate, $installContext->getContext());
$this->addEventAction($installContext);
} catch (UniqueConstraintViolationException $exception) {
}
}
/**
* @since 1.1.0
*/
private function renameMailTemplateType(UpdateContext $updateContext): void
{
$context = $updateContext->getContext();
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
$mailTemplateType = $mailTemplateTypeRepository->search(
(new Criteria())
->addFilter(
new EqualsFilter('technicalName', self::TEMPLATE_TYPE_TECHNICAL_NAME_OLD)
),
$context
)->first();
if ($mailTemplateType instanceof MailTemplateTypeEntity) {
$mailTemplateTypeRepository->update([
[
'id' => $mailTemplateType->getId(),
'technicalName' => self::TEMPLATE_TYPE_TECHNICAL_NAME,
]
], $context);
}
}
private function getMailTemplateTypeRepository(): EntityRepositoryInterface
{
return $this->container->get('mail_template_type.repository');
}
private function getMailTemplateRepository(): EntityRepositoryInterface
{
return $this->container->get('mail_template.repository');
}
}
//docker-compose exec app_server bin/console cache:clear