<?php
declare(strict_types=1);
namespace CioEnergieImport;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Shopware\Core\Checkout\Order\OrderStates;
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\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionEntity;
class CioEnergieImport extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$context = $installContext;
// add mail templates
$mailTemplateTypeId = Uuid::randomHex();
$this->createEmailTypeWithTemplates(
[
'id' => $mailTemplateTypeId,
'name' => 'Passwort vergessen E-Mail für einen neuen Kunden aus Stammdaten-Import ',
'technicalName' => 'energie_import_master_data_forgot_password',
'availableEntities' => [
'customer' => 'customer',
]
],
[
[
'id' => Uuid::randomHex(),
'mailTemplateTypeId' => $mailTemplateTypeId,
'subject' => [
'en-GB' => 'Ihr Account wurde erstellt. Ändern Sie bitte Ihr Passwort.',
'de-DE' => 'Ihr Account wurde erstellt. Ändern Sie bitte Ihr Passwort.'
],
'contentPlain' => "Ihr Account wurde erstellt. Damit Sie sich einloggen können, ändern Sie bitte Ihr Passwort unter {{ forgot_password_url }}.",
'contentHtml' => "<p>Ihr Account wurde erstellt. Damit Sie sich einloggen können, ändern Sie bitte Ihr Passwort unter <a href=\"{{ forgot_password_url }}\">{{ forgot_password_url }}</a></p>",
]
],
$installContext->getContext()
);
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
//Keep UserData? Then do nothing here
if ($uninstallContext->keepUserData()) {
return;
}
$this->removeEmailTypeWithTemplates('energie_import_master_data_forgot_password', $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 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);
}
}