diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php
index aa18526..972dc84 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\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
@@ -47,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);
@@ -56,6 +58,9 @@ 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);
}
// Let say the user there's a problem
@@ -88,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/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;
+ }
+}
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 99692d6..77697bd 100644
--- a/Form/ContactType.php
+++ b/Form/ContactType.php
@@ -6,8 +6,12 @@
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
*
@@ -20,14 +24,52 @@ class ContactType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
+ $this->addSubjet($builder, $options);
+
$builder
- ->add('name', TextType::class)
- ->add('email', EmailType::class)
- ->add('subject', TextType::class)
- ->add('body', TextareaType::class)
+ ->add('name', TextType::class, array(
+ 'label' => 'form.name',
+ 'translation_domain' => 'FrequenceWebContactBundle'))
+ ->add('email', EmailType::class, array(
+ 'label' => 'form.email',
+ 'translation_domain' => 'FrequenceWebContactBundle'))
+
+ ->add('body', TextareaType::class, array(
+ 'label' => 'form.body',
+ 'translation_domain' => 'FrequenceWebContactBundle'))
+ ->add('recaptcha', EWZRecaptchaType::class, array(
+ 'label' => false,
+ 'mapped' => false,
+ ))
;
}
+ 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}
*/
@@ -35,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/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 @@
-
+
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
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/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",
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"
>