<commit_before><?php

namespace Frontend\Modules\FormBuilder\EventListener;

use Common\Mailer\Message;
use Swift_Mailer;
use Common\ModulesSettings;
use Frontend\Core\Language\Language;
use Frontend\Modules\FormBuilder\Event\FormBuilderSubmittedEvent;
use Swift_Mime_SimpleMessage;

/**
 * A Formbuilder submitted event subscriber that will send an email if needed
 */
final class FormBuilderSubmittedMailSubscriber
{
    /**
     * @var ModulesSettings
     */
    protected $modulesSettings;

    /**
     * @var Swift_Mailer
     */
    protected $mailer;

    /**
     * @param Swift_Mailer $mailer
     * @param ModulesSettings $modulesSettings
     */
    public function __construct(
        Swift_Mailer $mailer,
        ModulesSettings $modulesSettings
    ) {
        $this->mailer = $mailer;
        $this->modulesSettings = $modulesSettings;
    }

    /**
     * @param FormBuilderSubmittedEvent $event
     */
    public function onFormSubmitted(FormBuilderSubmittedEvent $event)
    {
        $form = $event->getForm();
        $fieldData = $this->getEmailFields($event->getData());

        // need to send mail
        if ($form['method'] === 'database_email' || $form['method'] === 'email') {
            $this->mailer->send($this->getMessage($form, $fieldData, $form['email_subject']));
        }

        // check if we need to send confirmation mails
        foreach ($form['fields'] as $field) {
            if (array_key_exists('send_confirmation_mail_to', $field['settings']) &&
                $field['settings']['send_confirmation_mail_to'] === true
            ) {
                $email = $fieldData[$field['id']]['value'];
                $this->mailer->send(
                    $this->getMessage($form, $fieldData, $field['settings']['confirmation_mail_subject'], $email, true)
                );
            }
        }
    }

    /**
     * @param array $form
     * @param array $fieldData
     * @param string $subject
     * @param string|null $to
     * @param bool $isConfirmationMail
     *
     * @return Swift_Mime_SimpleMessage
     */
    private function getMessage(
        array $form,
        array $fieldData,
        $subject = null,
        $to = null,
        $isConfirmationMail = false
    ) {
        if ($subject === null) {
            $subject = Language::getMessage('FormBuilderSubject');
        }

        $from = $this->modulesSettings->get('Core', 'mailer_from');

        $message = Message::newInstance(sprintf($subject, $form['name']))
            ->parseHtml(
                '/FormBuilder/Layout/Templates/Mails/' . $form['email_template'],
                array(
                    'subject' => $subject,
                    'sentOn' => time(),
                    'name' => $form['name'],
                    'fields' => $fieldData,
                    'is_confirmation_mail' => $isConfirmationMail,
                ),
                true
            )<commit_msg>Fix encoded htmlspecial chars in the email<commit_after><?php

namespace Frontend\Modules\FormBuilder\EventListener;

use Common\Mailer\Message;
use Swift_Mailer;
use Common\ModulesSettings;
use Frontend\Core\Language\Language;
use Frontend\Modules\FormBuilder\Event\FormBuilderSubmittedEvent;
use Swift_Mime_SimpleMessage;

/**
 * A Formbuilder submitted event subscriber that will send an email if needed
 */
final class FormBuilderSubmittedMailSubscriber
{
    /**
     * @var ModulesSettings
     */
    protected $modulesSettings;

    /**
     * @var Swift_Mailer
     */
    protected $mailer;

    /**
     * @param Swift_Mailer $mailer
     * @param ModulesSettings $modulesSettings
     */
    public function __construct(
        Swift_Mailer $mailer,
        ModulesSettings $modulesSettings
    ) {
        $this->mailer = $mailer;
        $this->modulesSettings = $modulesSettings;
    }

    /**
     * @param FormBuilderSubmittedEvent $event
     */
    public function onFormSubmitted(FormBuilderSubmittedEvent $event)
    {
        $form = $event->getForm();
        $fieldData = $this->getEmailFields($event->getData());

        // need to send mail
        if ($form['method'] === 'database_email' || $form['method'] === 'email') {
            $this->mailer->send($this->getMessage($form, $fieldData, $form['email_subject']));
        }

        // check if we need to send confirmation mails
        foreach ($form['fields'] as $field) {
            if (array_key_exists('send_confirmation_mail_to', $field['settings']) &&
                $field['settings']['send_confirmation_mail_to'] === true
            ) {
                $email = $fieldData[$field['id']]['value'];
                $this->mailer->send(
                    $this->getMessage($form, $fieldData, $field['settings']['confirmation_mail_subject'], $email, true)
                );
            }
        }
    }

    /**
     * @param array $form
     * @param array $fieldData
     * @param string $subject
     * @param string|null $to
     * @param bool $isConfirmationMail
     *
     * @return Swift_Mime_SimpleMessage
     */
    private function getMessage(
        array $form,
        array $fieldData,
        $subject = null,
        $to = null,
        $isConfirmationMail = false
    ) {
        if ($subject === null) {
            $subject = Language::getMessage('FormBuilderSubject');
        }

        $from = $this->modulesSettings->get('Core', 'mailer_from');

        $message = Message::newInstance(sprintf($subject, $form['name']))
            ->parseHtml(
                '/FormBuilder/Layout/Templates/Mails/' . $form['email_template'],
                array(
                    'subject' => $subject,
                    'sentOn' => time(),
                    'name' => $form['name'],
                    'fields' => array_map(
                        function (array $field) {
                            $field['value'] = html_entity_decode($field['value']);

                            return $field;
                        },
                        $fieldData
                    ),
                    'is_confirmation_mail' => $isConfirmationMail,
                ),
                true
            )
{'fraction_non_alphanumeric': 0.13047711781888996, 'fraction_numerical': 0.0, 'mean_word_length': 2.024043179587831, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1797445', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1642, 'n_words': 332}