custom/plugins/WbnContactConfirmation/src/Subscriber/CustomFormSend.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace WbnContactConfirmation\Subscriber;
  3. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldCollection;
  6. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldEntity;
  7. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\Type\Email;
  8. use Swag\CmsExtensions\Form\Event\CustomFormEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Psr\Log\LoggerInterface;
  11. use Monolog\Logger;
  12. class CustomFormSend implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     private $systemConfigService;
  18.     /**
  19.      * @var AbstractMailService
  20.      */
  21.     private $mailService;
  22.     private LoggerInterface $logger;
  23.     public function __construct(LoggerInterface $loggerSystemConfigService $systemConfigServiceAbstractMailService $mailService)
  24.     {
  25.         $this->logger $logger;
  26.         $this->systemConfigService $systemConfigService;
  27.         $this->mailService $mailService;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         // check if plugin Swag\CmsExtensions is installed
  32.         if (class_exists('Swag\CmsExtensions\Form\Event\CustomFormEvent'true)) {
  33.             return [CustomFormEvent::getName() => 'sendMail'];
  34.         }
  35.         return [];
  36.     }
  37.     public function sendMail(CustomFormEvent $event): void
  38.     {
  39.         // check if function is in plugin enabled
  40.         if (!$this->systemConfigService->get('WbnContactConfirmation.config.customFormsEnabled')) return;
  41.         $receivers $event->getForm()->getReceivers();
  42.         $groups $event->getForm()->getGroups();
  43.         $fields $groups === null ? new FormGroupFieldCollection() : $groups->getFields();
  44.         /* Check for email field to send mail to submitting user */
  45.         $senderMail $this->getSenderMail($fields$event->getFormData());
  46.         if ($senderMail$receivers = [$senderMail];
  47.         $mailTemplate $event->getForm()->getMailTemplate();
  48.         $data $mailTemplate !== null $mailTemplate->jsonSerialize() : [];
  49.         $data['salesChannelId'] = $event->getSalesChannelId();
  50.         if ($sender $this->getSenderMail($fields$event->getFormData())) {
  51.             $data['replyTo'] = $sender;
  52.         }
  53.         $templateData = [
  54.             'form' => $event->getForm(),
  55.             'fields' => $fields,
  56.             'formData' => $event->getFormData(),
  57.             'salesChannel' => $event->getSalesChannelContext()->getSalesChannel(),
  58.         ];
  59.         foreach ($receivers as $mail) {
  60.             if (!\is_string($mail)) {
  61.                 continue;
  62.             }
  63.             $data['recipients'] = [$mail => $mail];
  64.             // add confirmation mail identifier
  65.             $templateData['isConfirmationMail'] = true;
  66.             $this->mailService->send($data$event->getContext(), $templateData);
  67. //            $this->logger->log(Logger::DEBUG,json_encode($data));
  68. //            $this->logger->log(Logger::DEBUG,json_encode($templateData));
  69.         }
  70.     }
  71.     private function getSenderMail(FormGroupFieldCollection $fieldCollection, array $formData): ?string
  72.     {
  73.         $mailField $fieldCollection->filter(static function (FormGroupFieldEntity $field) {
  74.             return $field->getType() === Email::NAME;
  75.         })->first();
  76.         if ($mailField === null) {
  77.             return null;
  78.         }
  79.         return $formData[$mailField->getTechnicalName()] ?? null;
  80.     }
  81. }