Skip to content

Commit

Permalink
Improved mail processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Allimant committed Jul 20, 2023
1 parent 47ae4e7 commit c6131b4
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.8</version>
<version>1.0.9</version>
<authors>
<author>
<name>Thomas Da Silva Mendonca</name>
Expand Down
6 changes: 2 additions & 4 deletions Controller/Front/CustomContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
use CustomContact\Model\CustomContactQuery;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Requirement\Requirement;
use Thelia\Controller\Front\BaseFrontController;

#[Route('/custom_contact/{code}', name: 'front_custom_contact_', requirements: ['code' => Requirement::ASCII_SLUG])]
#[Route('/custom_contact/{code}', name: 'front_custom_contact_', requirements: ['code' => '[A-Za-z0-9_-]+'])]
class CustomContactController extends BaseFrontController
{
#[Route('', name:'view', methods: 'GET')]
Expand Down Expand Up @@ -79,4 +77,4 @@ public function success(string $code): Response
]
);
}
}
}
51 changes: 31 additions & 20 deletions EventListener/CustomContactEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,37 @@ public static function getSubscribedEvents()

public function sendCustomerContact(CustomContactEvent $event)
{
$storeEmail = ConfigQuery::getStoreEmail();
$receiverEmail = $event->getCustomContact()->getEmail() ?? $storeEmail;

$email = $this->mailer->createEmailMessage(
CustomContact::MAIL_CUSTOM_CONTACT,
[$storeEmail => ConfigQuery::getStoreName()],
[$receiverEmail => ConfigQuery::getStoreName()],
[
'store_name' => ConfigQuery::getStoreName(),
'fields' => $event->getFields()
]
);

foreach ($event->getFileFields() as $fileField) {
/** @var UploadedFile $file */
foreach ($fileField as $file) {
$email->attach($file->getContent(), $file->getClientOriginalName());
}
$customContact = $event->getCustomContact();

$destinationEmails = array_map('trim', explode(',', $customContact->getEmail()));

if (empty($destinationEmails)) {
$destinationEmails = [ConfigQuery::getStoreEmail()];
}

$this->mailer->send($email);
foreach ($destinationEmails as $emailAddress) {
$email = $this->mailer->createEmailMessage(
CustomContact::MAIL_CUSTOM_CONTACT,
[ConfigQuery::getStoreEmail() => $event->getCustomContact()->getTitle()],
[$emailAddress => ConfigQuery::getStoreName()],
[
'form_title' => $customContact->getTitle(),
'store_name' => ConfigQuery::getStoreName(),
'fields' => $event->getFields()
]
);

foreach ($event->getFileFields() as $fileField) {
/** @var UploadedFile $file */
foreach ($fileField as $file) {
$email->attach($file->getContent(), $file->getClientOriginalName());
}
}

// Override subject
$email->subject($customContact->getTitle());

$this->mailer->send($email);
}
}
}
}
51 changes: 47 additions & 4 deletions Form/CustomContactForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
namespace CustomContact\Form;

use CustomContact\CustomContact;
use CustomContact\Model\CustomContactQuery;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Form\BaseForm;

Expand All @@ -14,6 +20,13 @@ class CustomContactForm extends BaseForm
protected function buildForm()
{
$this->formBuilder
->add(
'id',
HiddenType::class,
options: [
'label' => Translator::getInstance()->trans('The form ID', [], CustomContact::DOMAIN_NAME)
]
)
->add(
'title',
TextType::class,
Expand All @@ -27,7 +40,14 @@ protected function buildForm()
TextType::class,
options: [
'required' => true,
'label' => Translator::getInstance()->trans('Code', [], CustomContact::DOMAIN_NAME)
'constraints' => [
new Regex('/^[A-Za-z0-9_-]+$/', $this->translator->trans('Les caractères autorisés sont les lettres, les chiffres le signe - et le signe _', [], CustomContact::DOMAIN_NAME)),
new Callback([$this, 'checkUnicity'])
],
'label' => Translator::getInstance()->trans('Code', [], CustomContact::DOMAIN_NAME),
'label_attr' => [
'help' => $this->translator->trans('Les caractères autorisés sont les lettres, les chiffres le signe - et le signe _', [], CustomContact::DOMAIN_NAME),
],
]
)
->add(
Expand All @@ -43,7 +63,10 @@ protected function buildForm()
TextType::class,
options: [
'required' => true,
'label' => Translator::getInstance()->trans('Receiver email', [], CustomContact::DOMAIN_NAME)
'label' => Translator::getInstance()->trans('Receiver emails', [], CustomContact::DOMAIN_NAME),
'label_attr' => [
'help' => $this->translator->trans('Vous pouvez indiquer une ou plusieurs adresses email séparé&es par des virgules', [], CustomContact::DOMAIN_NAME),
],
]
)
->add(
Expand All @@ -56,12 +79,32 @@ protected function buildForm()
)
->add(
'success_message',
TextType::class,
TextareaType::class,
options: [
'required' => false,
'label' => Translator::getInstance()->trans('Success message', [], CustomContact::DOMAIN_NAME)
]
)
;
}
}

public function checkUnicity($value, ExecutionContextInterface $context)
{
$formId = (int) $context->getRoot()->getData()['id'];

$query = CustomContactQuery::create()
->filterByCode($value)
->filterById($formId, Criteria::NOT_EQUAL)
;

if ($query->count() > 0) {
$context->addViolation(
Translator::getInstance()->trans(
"A form with code %code already exists, please une another code.",
[ "%code" => $value ],
CustomContact::DOMAIN_NAME
)
);
}
}
}
5 changes: 4 additions & 1 deletion I18n/email/default/fr_FR.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

return array(
'A new form has been submited from %store : %form_title' => 'Un nouveau formulaire à été envoyé depuis le site %store : %form_title ',
'Have a nice day !' => 'Bonne journée !',
'Hello,' => 'Bonjour,',
'Here is your contact : ' => 'Voici votre contact : ',
'Here the data entered for each field :' => 'Voici les données indiquées pour chaque champ :',
'Welcome to %store_name' => 'Bienvenue sur %store_name',
'Your Contact from %store' => 'Votre Contact de %store ',
);
50 changes: 11 additions & 39 deletions templates/backOffice/default/custom_contact/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,52 +61,24 @@
<form method="POST"
action="{url path="/admin/custom_contact/%id" id=$id}" {form_enctype} >
<div class="row">
{if $form_error}
<div class="col-sm-8 col-sm-offset-2">
<div class="alert alert-danger">{$form_error_message}</div>
</div>
{/if}
<div class="col-sm-8 col-sm-offset-2">
{render_form_field field="error_url" value={url path='/admin/custom_contact/%id' id=$id}}
{render_form_field field="success_url" value={url path='/admin/module/CustomContact'}}

{form_hidden_fields }

{form_field field="title"}
<div class="form-group {if $error}has-error{/if}">
<label for="{$label_attr.for}" class="control-label">{$label} : </label>
<input type="text" id="{$label_attr.for}" name="{$name}"
class="form-control" value="{$customContact->getTitle()|default:null}">
</div>
{/form_field}

{form_field field="code"}
<div class="form-group {if $error}has-error{/if}">
<label for="{$label_attr.for}" class="control-label">{$label} : </label>
<input type="text" id="{$label_attr.for}" name="{$name}"
class="form-control" value="{$customContact->getCode()|default:null}">
</div>
{/form_field}

{form_field field="receiver_email"}
<div class="form-group {if $error}has-error{/if}">
<label for="{$label_attr.for}" class="control-label">{$label} : </label>
<input type="text" id="{$label_attr.for}" name="{$name}"
class="form-control" value="{$customContact->getEmail()|default:null}">
</div>
{/form_field}

{form_field field="return_url"}
<div class="form-group {if $error}has-error{/if}">
<label for="{$label_attr.for}" class="control-label">{$label} : </label>
<input type="text" id="{$label_attr.for}" name="{$name}"
class="form-control" value="{$customContact->getReturnUrl()|default:null}">
</div>
{/form_field}
{render_form_field field="id" value=$customContact->getId()|default:null}

{form_field field="success_message"}
<div class="form-group {if $error}has-error{/if}">
<label for="{$label_attr.for}" class="control-label">{$label} : </label>
<br>
<textarea id="{$label_attr.for}" name="{$name}" rows="4"
cols="50" class="form-control">{$customContact->getSuccessMessage()}</textarea>
</div>
{/form_field}
{render_form_field field="title" value=$customContact->getTitle()|default:null}
{render_form_field field="code" value=$customContact->getCode()|default:null}
{render_form_field field="receiver_email" value=$customContact->getEmail()|default:null}
{render_form_field field="return_url" value=$customContact->getReturnUrl()|default:null}
{render_form_field field="success_message" value=$customContact->getSuccessMessage()|default:null}

{form_field field="field_configuration"}
<input id="json_field" type="hidden" name="{$name}">
Expand Down
1 change: 1 addition & 0 deletions templates/backOffice/default/module_configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
</a>
<a href="{url path="/admin/custom_contact/delete/$ID"}"
class="btn btn-danger"
onclick="return confirm('{intl l='Delete this form ? This cannot be undone' d='customcontact.bo.default'}');"
title="{intl l='Delete this custom contact form' d='customcontact.bo.default'}">
<i class="glyphicon glyphicon-trash"></i>
</a>
Expand Down
19 changes: 14 additions & 5 deletions templates/email/default/custom_contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@
{block name="pre-header"}{/block}

{* Subject *}
{block name="email-subject"}{intl l="Your Contact from %store" store={config key="store_name"}}{/block}
{block name="email-subject"}{$form_title}{/block}

{* Title *}
{block name="email-title"}{/block}

{* Content *}
{block name="email-content"}
<p>{intl l="Welcome to %store_name" store_name={config key="store_name"}}</p>
<p>{intl l="Hello," d='customcontact.email.default'}</p>

<p>{intl l="Here is your contact : "}</p>
<p>{intl l="A new form has been submited from %store : %form_title" store=$store_name form_title=$form_title d='customcontact.email.default'}</p>

<p>{intl l="Here the data entered for each field :" d='customcontact.email.default'}</p>

<table border="1" cellpadding="10" style="border:1px solid #ccc">
{foreach from=$fields key=label item=value}
<p>{$label} : {$value}</p>
<tr>
<td style="padding: 10px">{intl l=$label d='customcontact.email.default'}</td>
<td style="padding: 10px">{$value}</td>
</tr>
{/foreach}
{/block}
</table>

<p>{intl l="Have a nice day !" d='customcontact.email.default'}</p>
{/block}
11 changes: 5 additions & 6 deletions templates/email/default/custom_contact.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{default_translation_domain domain="customcontact.email.default"}
{intl l="Hello," d='customcontact.email.default'}
{intl l="A new form has been submited from %store : %form_title" store=$store_name form_title=$form_title' d='customcontact.email.default'}</p>

{intl l="Welcome to %store_name" store_name={config key="store_name"}}

{intl l="Here is your contact : "}
{intl l="Here the data entered for each field :" d='customcontact.email.default'}

{foreach from=$fields key=label item=value}
{$label} : {$value}
{/foreach}
- {intl l=$label d='customcontact.email.default'} : {$value}
{/foreach}
2 changes: 1 addition & 1 deletion templates/frontOffice/default/custom_contact/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@
</div>
{/loop}

{/block}
{/block}

0 comments on commit c6131b4

Please sign in to comment.