<?php
declare(strict_types=1);
namespace MadFrontendContactForm;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use MadCreatePartnerAccount\Core\Event\SendPartnerAccountMailEvent;
use MadFrontendContactForm\Core\Event\SendFrontendContactFormToCustomerEvent;
use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
use Shopware\Core\Content\MailTemplate\MailTemplateActions;
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 MadFrontendContactForm extends Plugin
{
public const TEMPLATE_TYPE_NAME = 'Contact Form Customer';
public const TEMPLATE_TYPE_TECHNICAL_NAME = 'contact_form_customer';
public function install(InstallContext $installContext): void
{
/** @var EntityRepositoryInterface $mailTemplateTypeRepository */
$mailTemplateTypeRepository = $this->container->get('mail_template_type.repository');
/** @var EntityRepositoryInterface $mailTemplateRepository */
$mailTemplateRepository = $this->container->get('mail_template.repository');
$mailTemplateTypeId = Uuid::randomHex();
$mailTemplateType = [
[
'id' => $mailTemplateTypeId,
'name' => self::TEMPLATE_TYPE_NAME,
'technicalName' => self::TEMPLATE_TYPE_TECHNICAL_NAME,
'availableEntities' => [
'salesChannel' => 'sales_channel',
'customer' => 'customer',
'customerGroup' => 'customer_group'
]
]
];
//You can add translations with the matching ISO-Codes. You always have to provide a value vor the default system language
//Later you can change and add translations also in the administration
$mailTemplateId = Uuid::randomHex();
$mailTemplate = [
[
'id' => $mailTemplateId,
'mailTemplateTypeId' => $mailTemplateTypeId,
'subject' => [
'en-GB' => 'contact form to customer',
'de-DE' => 'Kontaktformular zum Kunden',
],
'senderName' => [
'en-GB' => '{{ salesChannel.name }}',
'de-DE' => '{{ salesChannel.name }}',
],
'contentPlain' => "PLACEHOLDER, please use twig-tempate in Theme",
'contentHtml' => 'PLACEHOLDER, please use twig-tempate in Theme',
]
];
try {
$mailTemplateTypeRepository->create($mailTemplateType, $installContext->getContext());
$mailTemplateRepository->create($mailTemplate, $installContext->getContext());
} catch (UniqueConstraintViolationException $exception) {
// No, we've already installed the fields, it's fine.
}
$eventActionRepository = $this->container->get('event_action.repository');
$event = [
[
'title' => 'Kontaktformular Kunden Mail',
'eventName' => SendFrontendContactFormToCustomerEvent::EVENT_NAME,
'actionName' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
'config' => [
'mail_template_id' => $mailTemplateId,
'mail_template_type_id' => $mailTemplateTypeId,
],
'active' => true,
]
];
try {
$eventActionRepository->create($event, $installContext->getContext());
} catch (UniqueConstraintViolationException $exception) {
// No, we've already installed the fields, it's fine.
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
//Keep UserData? Then do nothing here
if ($uninstallContext->keepUserData()) {
return;
}
//get the Templates and Associations added by this Plugin from the DB
/** @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', self::TEMPLATE_TYPE_TECHNICAL_NAME)),
$uninstallContext
->getContext()
)->first();
$mailTemplateIds = $mailTemplateRepository->searchIds(
(new Criteria())
->addFilter(new EqualsFilter('mailTemplateTypeId', $myCustomMailTemplateType->getId())),
$uninstallContext
->getContext()
)->getIds();
//Get the Ids from the fetched Entities
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $mailTemplateIds);
//Delete the Templates which were added by this Plugin
$mailTemplateRepository->delete($ids, $uninstallContext->getContext());
//Delete the TemplateType which were added by this Plugin
$mailTemplateTypeRepository->delete([
['id' => $myCustomMailTemplateType->getId()]
], $uninstallContext->getContext());
/** @var EntityRepositoryInterface $eventActionRepository */
$eventActionRepository = $this->container->get('event_action.repository');
/* @var $createPartnerAccountEventAction \Shopware\Core\Framework\Event\EventAction\EventActionEntity */
$createPartnerAccountEventAction = $eventActionRepository->search(
(new Criteria())
->addFilter(new EqualsFilter('eventName', SendPartnerAccountMailEvent::EVENT_NAME)),
$uninstallContext
->getContext()
)->first();
//Delete the TemplateType which were added by this Plugin
if ($createPartnerAccountEventAction instanceof EventActionEntity) {
$eventActionRepository->delete([
['id' => $createPartnerAccountEventAction->getId()]
], $uninstallContext->getContext());
}
}
}