<?php declare(strict_types=1);
namespace CioImports;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Framework\Context;
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\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
class CioImports extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$mailTemplateTypeId = Uuid::randomHex();
$this->createEmailTypeWithTemplates([
'id' => $mailTemplateTypeId,
'name' => 'ImportStockMissingProductNotificationType',
'technicalName' => 'import_stock_missing_product_notification_type',
'availableEntities' => [
'product' => 'product'
]
],
[
[
'id' => Uuid::randomHex(),
'mailTemplateTypeId' => $mailTemplateTypeId,
'subject' => [
'en-GB' => 'Product is missing in stock import file',
'de-DE' => 'Produkt fehlt beim Import des Lagerbestands'
],
'contentPlain' => "Hello,\nthis is the content in plain text for my custom mail template\n\nKind Regards,\nYours",
'contentHtml' => "<p>Hello,<br>this is the content in plain text for my custom mail template<br><br>Kind Regards,<br>Yours</p>",
]
], $installContext->getContext());
$eventActionId = Uuid::randomHex();
$this->createEventAction([
'id' => $eventActionId,
'title' => 'Lagerbestand Import Benachrichtigung über fehlendes Produkt',
'eventName' => 'import_notification_stock_missing',
'actionName' => 'action.mail.send',
'config' => [
'mail_template_type_id' => $mailTemplateTypeId
],
'active' => true,
'createdAt' => new \DateTime()
], $installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
//Keep UserData? Then do nothing here
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeEventAction('import_notification_stock_missing', $uninstallContext->getContext());
$this->removeEmailTypeWithTemplates('import_stock_missing_product_notification_type', $uninstallContext->getContext());
}
protected function createEmailTypeWithTemplates(array $type, array $templates, Context $context)
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
try {
$mailTemplateTypeRepository->create([$type], $context);
$mailTemplateRepository->create($templates, $context);
} catch (UniqueConstraintViolationException $exception) {
// No, we've already installed the fields, it's fine.
}
}
protected function createEventAction(array $eventAction, Context $context)
{
/** @var EntityRepositoryInterface $eventActionRepository */
$eventActionRepository = $this->container->get('event_action.repository');
// prevent duplicates of the event
if ($eventActionRepository->search((new Criteria())->addFilter(new EqualsFilter('eventName', $eventAction['eventName'])), $context)->first() instanceof EventActionEntity) {
return;
}
try {
$eventActionRepository->create([$eventAction], $context);
} catch (UniqueConstraintViolationException $exception) {
// No, we've already installed the fields, it's fine.
}
}
protected function removeEventAction(string $eventActionName, Context $context)
{
/** @var EntityRepositoryInterface $eventActionRepository */
$eventActionRepository = $this->container->get('event_action.repository');
$eventAction = $eventActionRepository->search(
(new Criteria())->addFilter(new EqualsFilter('eventName', $eventActionName)), $context
)->first();
$eventActionRepository->delete([
['id' => $eventAction->getId()]
], $context);
}
protected function removeEmailTypeWithTemplates(string $typeName, Context $context)
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
/** @var MailTemplateTypeEntity $myCustomMailTemplateType */
$myCustomMailTemplateType = $mailTemplateTypeRepository->search(
(new Criteria())->addFilter(new EqualsFilter('technicalName', $typeName)), $context
)->first();
$ids = [];
if ($myCustomMailTemplateType instanceof MailTemplateTypeEntity) {
$mailTemplateIds = $mailTemplateRepository->searchIds(
(new Criteria())->addFilter(new EqualsFilter('mailTemplateTypeId', $myCustomMailTemplateType->getId())), $context
)->getIds();
//Get the Ids from the fetched Entities
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $mailTemplateIds);
}
$mailTemplateTypeRepository->delete([
['id' => $myCustomMailTemplateType->getId()]
], $context);
$mailTemplateRepository->delete($ids, $context);
}
}