<?php declare(strict_types=1);
namespace WbnContactConfirmation\Subscriber;
use Shopware\Core\Content\Mail\Service\AbstractMailService;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldCollection;
use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldEntity;
use Swag\CmsExtensions\Form\Aggregate\FormGroupField\Type\Email;
use Swag\CmsExtensions\Form\Event\CustomFormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Psr\Log\LoggerInterface;
use Monolog\Logger;
class CustomFormSend implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var AbstractMailService
*/
private $mailService;
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger, SystemConfigService $systemConfigService, AbstractMailService $mailService)
{
$this->logger = $logger;
$this->systemConfigService = $systemConfigService;
$this->mailService = $mailService;
}
public static function getSubscribedEvents(): array
{
// check if plugin Swag\CmsExtensions is installed
if (class_exists('Swag\CmsExtensions\Form\Event\CustomFormEvent', true)) {
return [CustomFormEvent::getName() => 'sendMail'];
}
return [];
}
public function sendMail(CustomFormEvent $event): void
{
// check if function is in plugin enabled
if (!$this->systemConfigService->get('WbnContactConfirmation.config.customFormsEnabled')) return;
$receivers = $event->getForm()->getReceivers();
$groups = $event->getForm()->getGroups();
$fields = $groups === null ? new FormGroupFieldCollection() : $groups->getFields();
/* Check for email field to send mail to submitting user */
$senderMail = $this->getSenderMail($fields, $event->getFormData());
if ($senderMail) $receivers = [$senderMail];
$mailTemplate = $event->getForm()->getMailTemplate();
$data = $mailTemplate !== null ? $mailTemplate->jsonSerialize() : [];
$data['salesChannelId'] = $event->getSalesChannelId();
if ($sender = $this->getSenderMail($fields, $event->getFormData())) {
$data['replyTo'] = $sender;
}
$templateData = [
'form' => $event->getForm(),
'fields' => $fields,
'formData' => $event->getFormData(),
'salesChannel' => $event->getSalesChannelContext()->getSalesChannel(),
];
foreach ($receivers as $mail) {
if (!\is_string($mail)) {
continue;
}
$data['recipients'] = [$mail => $mail];
// add confirmation mail identifier
$templateData['isConfirmationMail'] = true;
$this->mailService->send($data, $event->getContext(), $templateData);
// $this->logger->log(Logger::DEBUG,json_encode($data));
// $this->logger->log(Logger::DEBUG,json_encode($templateData));
}
}
private function getSenderMail(FormGroupFieldCollection $fieldCollection, array $formData): ?string
{
$mailField = $fieldCollection->filter(static function (FormGroupFieldEntity $field) {
return $field->getType() === Email::NAME;
})->first();
if ($mailField === null) {
return null;
}
return $formData[$mailField->getTechnicalName()] ?? null;
}
}