From 5ba137c24c69b2e7e2dc1a44127ce7bca47a2003 Mon Sep 17 00:00:00 2001 From: Chrysweel Date: Tue, 30 Sep 2014 11:59:30 +0200 Subject: [PATCH 1/6] include label in ContactType & add translation spanish --- Form/ContactType.php | 16 ++++++++++++---- .../FrequenceWebContactBundle.en.yml | 5 +++++ .../FrequenceWebContactBundle.es.yml | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 Resources/translations/FrequenceWebContactBundle.es.yml diff --git a/Form/ContactType.php b/Form/ContactType.php index 2819b25..bbe0f5d 100644 --- a/Form/ContactType.php +++ b/Form/ContactType.php @@ -18,10 +18,18 @@ class ContactType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('name', 'text') - ->add('email', 'email') - ->add('subject', 'text') - ->add('body', 'textarea') + ->add('name', 'text', array( + 'label' => 'form.name', + 'translation_domain' => 'FrequenceWebContactBundle')) + ->add('email', 'email', array( + 'label' => 'form.email', + 'translation_domain' => 'FrequenceWebContactBundle')) + ->add('subject', 'text', array( + 'label' => 'form.subject', + 'translation_domain' => 'FrequenceWebContactBundle')) + ->add('body', 'textarea', array( + 'label' => 'form.body', + 'translation_domain' => 'FrequenceWebContactBundle')) ; } diff --git a/Resources/translations/FrequenceWebContactBundle.en.yml b/Resources/translations/FrequenceWebContactBundle.en.yml index ffa6dd6..0fa8151 100644 --- a/Resources/translations/FrequenceWebContactBundle.en.yml +++ b/Resources/translations/FrequenceWebContactBundle.en.yml @@ -7,3 +7,8 @@ contact: new: 'New message from "%name%"' sent: you: Sent you a message +form: + name: Name + email: Email + subject: Subject + body: Body \ No newline at end of file diff --git a/Resources/translations/FrequenceWebContactBundle.es.yml b/Resources/translations/FrequenceWebContactBundle.es.yml new file mode 100644 index 0000000..938bead --- /dev/null +++ b/Resources/translations/FrequenceWebContactBundle.es.yml @@ -0,0 +1,14 @@ +contact: + submit: + button: Enviar + success: Tu mensaje ha sido enviado correctamente + failure: Tu mensaje no se ha podido enviar debido a algún fallo + message: + new: 'Nuevo mensaje de "%name%"' + sent: + you: Te ha enviado un mensaje +form: + name: Nombre + email: Corre electrónico + subject: Asunto + body: Mensaje \ No newline at end of file From 329320c8205fd7fc82521cc0d50bb4c53990a7e8 Mon Sep 17 00:00:00 2001 From: Chrysweel Date: Mon, 10 Oct 2016 11:40:47 +0200 Subject: [PATCH 2/6] include send log error with normData of Contact --- Controller/DefaultController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index 0cc2c6f..05e329b 100644 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -4,7 +4,7 @@ use FrequenceWeb\Bundle\ContactBundle\EventDispatcher\ContactEvents; use FrequenceWeb\Bundle\ContactBundle\EventDispatcher\Event\MessageSubmitEvent; -use Symfony\Component\DependencyInjection\ContainerAware; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Form\FormInterface; @@ -14,7 +14,7 @@ * * @author Yohan Giarelli */ -class DefaultController extends ContainerAware +class DefaultController extends Controller { /** * Action that displays the contact form @@ -40,7 +40,7 @@ public function indexAction(Request $request) public function submitAction(Request $request) { $form = $this->getForm(); - $form->bind($request); + $form->handleRequest($request); if ($form->isValid()) { // Send the event for message handling (send mail, add to DB, don't care) @@ -54,7 +54,10 @@ public function submitAction(Request $request) // Redirect somewhere return new RedirectResponse($this->container->get('session')->get('_fw_contact_referer')); } - + + $normData = $this->get('serializer')->serialize($form->getNormData(), 'json'); + $this->get('logger')->error("[FREQUENCE] Error to send email " . $normData); + // Let say the user there's a problem $message = $this->container->get('translator')->trans('contact.submit.failure', array(), 'FrequenceWebContactBundle'); $this->container->get('session')->getFlashBag()->add('error', $message); From a878359f8ec74aa8c3eb935912dcc6f0a5170175 Mon Sep 17 00:00:00 2001 From: Chrysweel Date: Tue, 15 Nov 2016 11:03:01 +0100 Subject: [PATCH 3/6] send event onErrorSubmitForm --- Controller/DefaultController.php | 7 ++-- EventDispatcher/ContactEvents.php | 4 +++ EventDispatcher/Event/ErrorMessageSubmit.php | 35 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 EventDispatcher/Event/ErrorMessageSubmit.php diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index 05e329b..55e7688 100644 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -3,6 +3,7 @@ namespace FrequenceWeb\Bundle\ContactBundle\Controller; use FrequenceWeb\Bundle\ContactBundle\EventDispatcher\ContactEvents; +use FrequenceWeb\Bundle\ContactBundle\EventDispatcher\Event\ErrorMessageSubmit; use FrequenceWeb\Bundle\ContactBundle\EventDispatcher\Event\MessageSubmitEvent; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -53,11 +54,11 @@ public function submitAction(Request $request) // Redirect somewhere return new RedirectResponse($this->container->get('session')->get('_fw_contact_referer')); + }else{ + $event = new ErrorMessageSubmit($form->getData()); + $this->container->get('event_dispatcher')->dispatch(ContactEvents::onErrorMessageSubmit, $event); } - $normData = $this->get('serializer')->serialize($form->getNormData(), 'json'); - $this->get('logger')->error("[FREQUENCE] Error to send email " . $normData); - // Let say the user there's a problem $message = $this->container->get('translator')->trans('contact.submit.failure', array(), 'FrequenceWebContactBundle'); $this->container->get('session')->getFlashBag()->add('error', $message); diff --git a/EventDispatcher/ContactEvents.php b/EventDispatcher/ContactEvents.php index 1ab2bbf..41372e7 100644 --- a/EventDispatcher/ContactEvents.php +++ b/EventDispatcher/ContactEvents.php @@ -13,4 +13,8 @@ class ContactEvents * This event is thrown each time an user send a message (Only and Only if validation pass) */ const onMessageSubmit = 'contact.submit'; + /** + * This event is thrown each time an user send a message and form is not valid + */ + const onErrorMessageSubmit = 'contact.error_submit'; } diff --git a/EventDispatcher/Event/ErrorMessageSubmit.php b/EventDispatcher/Event/ErrorMessageSubmit.php new file mode 100644 index 0000000..4004284 --- /dev/null +++ b/EventDispatcher/Event/ErrorMessageSubmit.php @@ -0,0 +1,35 @@ +contact = $contact; + } + + /** + * @return Contact + */ + public function getContact() + { + return $this->contact; + } +} From e2bd139cf890f8aa2fd491245bfec0d3be414186 Mon Sep 17 00:00:00 2001 From: Borja Date: Wed, 10 May 2017 09:31:37 +0200 Subject: [PATCH 4/6] Dont force to checkMx to form --- Resources/config/validation.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/config/validation.xml b/Resources/config/validation.xml index 5ad4e35..6d8b507 100644 --- a/Resources/config/validation.xml +++ b/Resources/config/validation.xml @@ -10,7 +10,7 @@ - + From 0e4b5946e1ae9ae151a671d19de2b2846f92c172 Mon Sep 17 00:00:00 2001 From: Carrillo Date: Mon, 12 Mar 2018 13:55:10 +0100 Subject: [PATCH 5/6] invisible recaptcha in contact form --- Form/ContactType.php | 6 ++++++ Tests/Functional/AppKernel.php | 1 + Tests/Functional/config.yml | 9 +++++++++ composer.json | 11 ++++++----- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Form/ContactType.php b/Form/ContactType.php index 49b23b2..3826ca9 100644 --- a/Form/ContactType.php +++ b/Form/ContactType.php @@ -8,6 +8,8 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; +use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType; + /** * The contact form * @@ -33,6 +35,10 @@ public function buildForm(FormBuilderInterface $builder, array $options) ->add('body', TextareaType::class, array( 'label' => 'form.body', 'translation_domain' => 'FrequenceWebContactBundle')) + ->add('recaptcha', EWZRecaptchaType::class, array( + 'label' => false, + 'mapped' => false, + )) ; } diff --git a/Tests/Functional/AppKernel.php b/Tests/Functional/AppKernel.php index cd0be81..e65fcf3 100644 --- a/Tests/Functional/AppKernel.php +++ b/Tests/Functional/AppKernel.php @@ -22,6 +22,7 @@ public function registerBundles() new \Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(), new \Symfony\Bundle\TwigBundle\TwigBundle(), new \FrequenceWeb\Bundle\ContactBundle\FrequenceWebContactBundle(), + new \EWZ\Bundle\RecaptchaBundle\EWZRecaptchaBundle(), ); } diff --git a/Tests/Functional/config.yml b/Tests/Functional/config.yml index 49b0bb9..a197af1 100644 --- a/Tests/Functional/config.yml +++ b/Tests/Functional/config.yml @@ -11,6 +11,7 @@ framework: resource: %kernel.root_dir%/routing.yml templating: engines: [twig] + translator: [] profiler: { only_exceptions: false } swiftmailer: @@ -18,3 +19,11 @@ swiftmailer: frequence_web_contact: to: foo@bar.baz + +ewz_recaptcha: + public_key: 'recaptcha_public_key' + private_key: 'recaptcha_private_key' + enabled: true + + + diff --git a/composer.json b/composer.json index b0871f5..24e404f 100644 --- a/composer.json +++ b/composer.json @@ -11,11 +11,12 @@ } ], "require": { - "php": "^5.3.3 || ^7.0", - "symfony/framework-bundle": "~2.8 || ^3.0", - "symfony/form": "~2.8 || ^3.0", - "symfony/twig-bundle": "~2.8 || ^3.0", - "symfony/validator": "~2.8 || ^3.0" + "php": "^5.3.3 || ^7.0", + "symfony/framework-bundle": "~2.8 || ^3.0", + "symfony/form": "~2.8 || ^3.0", + "symfony/twig-bundle": "~2.8 || ^3.0", + "symfony/validator": "~2.8 || ^3.0", + "excelwebzone/recaptcha-bundle" : "^1.5" }, "require-dev": { "phpunit/phpunit": "^3.0", From 2596e3547ad306d3f5b5b129754c1972c9efa1d1 Mon Sep 17 00:00:00 2001 From: Borja Date: Mon, 10 Sep 2018 16:49:06 +0200 Subject: [PATCH 6/6] Fix multiple messages per departaments --- Controller/DefaultController.php | 12 +++++- DependencyInjection/Configuration.php | 15 +++++-- .../FrequenceWebContactExtension.php | 4 +- .../Listener/EmailContactListener.php | 14 ++++++- Form/ContactType.php | 40 +++++++++++++++++-- Model/Contact.php | 18 ++++----- README.md | 13 ++++++ Resources/views/Mails/mail.html.twig | 2 - phpunit.xml.dist | 2 +- 9 files changed, 97 insertions(+), 23 deletions(-) diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index dc1c05d..972dc84 100644 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -48,6 +48,7 @@ public function submitAction(Request $request) if ($form->isValid()) { // Send the event for message handling (send mail, add to DB, don't care) + $event = new MessageSubmitEvent($form->getData()); $this->container->get('event_dispatcher')->dispatch(ContactEvents::onMessageSubmit, $event); @@ -92,9 +93,18 @@ protected function renderFormResponse(FormInterface $form) */ protected function getForm() { + $subjects = $this->container->getParameter('frequence_web_contact.fixed_to_and_subject'); + + if(count($subjects) > 0) { + $options = array("fixed_to_and_subject" => $this->container->getParameter('frequence_web_contact.fixed_to_and_subject')); + } else { + $options = array("fixed_to_and_subject" => array()); + } + return $this->container->get('form.factory')->create( $this->container->getParameter('frequence_web_contact.type'), - $this->container->get('frequence_web_contact.model') + $this->container->get('frequence_web_contact.model'), + $options ); } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 2aa286d..8e6b959 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -23,13 +23,20 @@ public function getConfigTreeBuilder() $rootNode = $treeBuilder->root('frequence_web_contact'); $rootNode - ->addDefaultsIfNotSet() ->children() ->scalarNode('send_mails')->defaultTrue()->end() - ->scalarNode('to')->defaultValue(null)->end() + ->scalarNode('to')->defaultNull()->end() ->scalarNode('from')->defaultValue('no-reply@example.com')->end() - ->scalarNode('subject')->defaultValue('contact.message.new') - ->end() + ->scalarNode('subject')->defaultNull()->end() + ->arrayNode('fixed_to_and_subject') + ->arrayPrototype() + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('title')->end() + ->scalarNode('email')->end() + ->end() + ->end() + ->end() ; return $treeBuilder; diff --git a/DependencyInjection/FrequenceWebContactExtension.php b/DependencyInjection/FrequenceWebContactExtension.php index d2d679f..87b816b 100644 --- a/DependencyInjection/FrequenceWebContactExtension.php +++ b/DependencyInjection/FrequenceWebContactExtension.php @@ -33,10 +33,12 @@ public function load(array $configs, ContainerBuilder $container) $definition = $container->getDefinition('frequence_web_contact.email_listener'); $definition->addArgument($config); + $container->setParameter( 'frequence_web_contact.fixed_to_and_subject', $config[ 'fixed_to_and_subject' ]); + // If mail listener is activated if (true === $config['send_mails']) { // "To" field is mandatory - if (null === $config['to']) { + if (null === $config['to'] && count($config[ 'fixed_to_and_subject' ]) == 0) { throw new \InvalidArgumentException('You have to define a "frequence_web_contact.to" address to use the email contact'); } // Add the mail event listener to the dispatcher diff --git a/EventDispatcher/Listener/EmailContactListener.php b/EventDispatcher/Listener/EmailContactListener.php index a7f38bd..c677106 100644 --- a/EventDispatcher/Listener/EmailContactListener.php +++ b/EventDispatcher/Listener/EmailContactListener.php @@ -55,16 +55,26 @@ public function __construct(\Swift_Mailer $mailer, EngineInterface $templating, public function onMessageSubmit(MessageSubmitEvent $event) { $contact = $event->getContact(); + if(count($this->config["fixed_to_and_subject"]) > 0) { + $selected_destination = $this->config["fixed_to_and_subject"][$contact->getSubject()]; + $custom_subject = $selected_destination["title"].": ".$contact->getEmail(); + $custom_to = $selected_destination["email"]; + } else { + $custom_subject = $contact->getSubject(); + $custom_to = $this->config["to"]; + } + + $message = new \Swift_Message($this->translator->trans( - $this->config['subject'], + $custom_subject, $contact->toTranslateArray(), 'FrequenceWebContactBundle' )); $message->addFrom($this->config['from']); $message->addReplyTo($contact->getEmail(), $contact->getName()); - $message->addTo($this->config['to']); + $message->addTo($custom_to); $message->addPart( $this->templating->render( 'FrequenceWebContactBundle:Mails:mail.html.twig', diff --git a/Form/ContactType.php b/Form/ContactType.php index 3826ca9..77697bd 100644 --- a/Form/ContactType.php +++ b/Form/ContactType.php @@ -6,9 +6,11 @@ use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType; +use Symfony\Component\OptionsResolver\OptionsResolver; /** * The contact form @@ -22,6 +24,8 @@ class ContactType extends AbstractType */ public function buildForm(FormBuilderInterface $builder, array $options) { + $this->addSubjet($builder, $options); + $builder ->add('name', TextType::class, array( 'label' => 'form.name', @@ -29,9 +33,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) ->add('email', EmailType::class, array( 'label' => 'form.email', 'translation_domain' => 'FrequenceWebContactBundle')) - ->add('subject', TextType::class, array( - 'label' => 'form.subject', - 'translation_domain' => 'FrequenceWebContactBundle')) + ->add('body', TextareaType::class, array( 'label' => 'form.body', 'translation_domain' => 'FrequenceWebContactBundle')) @@ -42,6 +44,32 @@ public function buildForm(FormBuilderInterface $builder, array $options) ; } + private function addSubjet(FormBuilderInterface $builder, array $options) { + if(count($options["fixed_to_and_subject"]) > 0) { + + $subject = $options["fixed_to_and_subject"]; + $field_data = array(); + $i = 0; + foreach ($subject as $s) { + $field_data[$s["title"]] = $i; + $i++; + } + + $builder + ->add('subject', ChoiceType::class, array( + 'label' => 'form.subject', + 'translation_domain' => 'FrequenceWebContactBundle', + 'choices' => $field_data + )); + } else { + $builder + ->add('subject', TextType::class, array( + 'label' => 'form.subject', + 'translation_domain' => 'FrequenceWebContactBundle') + ); + } + } + /** * @{inheritDoc} */ @@ -49,4 +77,10 @@ public function getName() { return 'contact'; } + + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setRequired('fixed_to_and_subject'); + } } diff --git a/Model/Contact.php b/Model/Contact.php index ae1d065..80020de 100644 --- a/Model/Contact.php +++ b/Model/Contact.php @@ -27,7 +27,7 @@ class Contact /** * The message subject * - * @var string + * @var array */ protected $subject; @@ -87,21 +87,21 @@ public function getName() } /** - * @param string $subject + * @return array */ - public function setSubject($subject) - { - $this->subject = $subject; + public function getSubject() { + return $this->subject; } /** - * @return string + * @param array $subject */ - public function getSubject() - { - return $this->subject; + public function setSubject( $subject ) { + $this->subject = $subject; } + + /** * Returns data that can be injected in the translating message subject * diff --git a/README.md b/README.md index 3cdece0..b7c5363 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,19 @@ frequence_web_contact: ``` +Also yo can create a fixed select to split emails via multiple departaments + +```yaml + +frequence_web_contact: + send_mails: true # True to use the bundle EmailListener that send emails when contact form is submited + from: null # The contact mail sender + fixed_to_and_subject: + - { title: "Departarment 1", email: "departament1@example.com" } + - { title: "Departarment 2", email: "departament2@example.com" } + - { title: "Departarment 3", email: "departament3@example.com" } +``` + Routing ------- diff --git a/Resources/views/Mails/mail.html.twig b/Resources/views/Mails/mail.html.twig index 62ba45d..99b9bc4 100644 --- a/Resources/views/Mails/mail.html.twig +++ b/Resources/views/Mails/mail.html.twig @@ -3,8 +3,6 @@ {% trans from 'FrequenceWebContactBundle' %}contact.message.sent.you{% endtrans %} : -

{{ contact.subject }}

-

{{ contact.body }}

diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 599227f..e958be8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,7 +9,7 @@ processIsolation="false" stopOnFailure="false" syntaxCheck="false" - bootstrap="./vendor/autoload.php" + bootstrap="vendor/autoload.php" >