<?php
declare(strict_types=1);
namespace MadFrontendContactForm\Subscriber;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
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\Validation\DataBag\DataBag;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use MadFrontendContactForm\Core\Event\SendFrontendContactFormToCustomerEvent;
use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
class MailSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $mailTemplateRepository;
private SystemConfigService $systemConfigService;
private EventDispatcherInterface $eventDispatcher;
public function __construct(
EntityRepositoryInterface $mailTemplateRepository,
SystemConfigService $systemConfigService,
EventDispatcherInterface $eventDispatcher
)
{
$this->mailTemplateRepository = $mailTemplateRepository;
$this->systemConfigService = $systemConfigService;
$this->eventDispatcher = $eventDispatcher;
}
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'onBeforeMailValidateEvent',
MailBeforeSentEvent::class => 'onMailBeforeSentEvent'
];
}
public function onBeforeMailValidateEvent(MailBeforeValidateEvent $event) {
if(!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
return;
}
if(
!array_key_exists('templateId', $event->getData())
|| !$this->checkTamplate($event->getData()['templateId'], $event->getContext())
) {
$this->customerEmail = null;
return;
}
/* Change sender mail adress */
if($this->systemConfigService->get('MadFrontendContactForm.config.contactFormSender')){
/* ['email' => 'firstName lastName'] */
$recipients = array($this->systemConfigService->get('MadFrontendContactForm.config.contactFormSender') => $this->systemConfigService->get('MadFrontendContactForm.config.contactFormSender'));
$data = $event->getData();
$data['recipients'] = $recipients;
$event->setData($data);
}
/* EOF: Change sender mail adress */
/* Change mail sender address */
if($this->systemConfigService->get('MadFrontendContactForm.config.contactFormMailSender')){
$senders = array($this->systemConfigService->get('MadFrontendContactForm.config.contactFormMailSender') => $this->systemConfigService->get('MadFrontendContactForm.config.contactFormMailSender'));
foreach ($senders as $sender){
$event->addData('senderEmail', $sender);
break;
}
$receivers = [
$event->getTemplateData()['contactFormData']['email'] => $event->getTemplateData()['contactFormData']['email'],
];
$dataCustomerMail = new DataBag();
$dataCustomerMail->set('salutationId', $event->getTemplateData()['contactFormData']['salutationId']);
$dataCustomerMail->set('firstName', $event->getTemplateData()['contactFormData']['firstName']);
$dataCustomerMail->set('lastName', $event->getTemplateData()['contactFormData']['lastName']);
$dataCustomerMail->set('email', $event->getTemplateData()['contactFormData']['email']);
$dataCustomerMail->set('phone', $event->getTemplateData()['contactFormData']['phone']);
$dataCustomerMail->set('subject', $event->getTemplateData()['contactFormData']['subject']);
$dataCustomerMail->set('comment', $event->getTemplateData()['contactFormData']['comment']);
$dataCustomerMail->set('shopware_surname_confirm', $event->getTemplateData()['contactFormData']['shopware_surname_confirm']);
$dataCustomerMail->set('privacy', $event->getTemplateData()['contactFormData']['privacy']);
$dataCustomerMail->set('navigationId', $event->getTemplateData()['contactFormData']['navigationId']);
$dataCustomerMail->set('slotId', $event->getTemplateData()['contactFormData']['slotId']);
$eventMailCustomer = new SendFrontendContactFormToCustomerEvent(
$event->getContext(),
$event->getContext()->getSource()->getSalesChannelId(),
new MailRecipientStruct($receivers),
$dataCustomerMail
);
$this->eventDispatcher->dispatch($eventMailCustomer);
}
/* EOF: Change mail sender address */
$this->customerEmail = $event->getTemplateData()['contactFormData']['email'];
}
public function onMailBeforeSentEvent(MailBeforeSentEvent $event)
{
if(!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
return;
}
if(
!array_key_exists('templateId', $event->getData())
|| !$this->checkTamplate($event->getData()['templateId'], $event->getContext())
|| $this->customerEmail === null
) {
return;
}
$event->getMessage()->addReplyTo($this->customerEmail);
}
private function checkTamplate(string $templateId, Context $context): bool
{
$criteria = new Criteria();
$criteria->addAssociation('mailTemplateType');
$criteria->addFilter(
new EqualsFilter('id', $templateId),
new EqualsFilter('mailTemplateType.technicalName', 'contact_form')
);
$result = $this->mailTemplateRepository->search($criteria, $context);
if($result->getTotal() < 1) {
return false;
}
return true;
}
}