Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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
);
}
}
15 changes: 11 additions & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion DependencyInjection/FrequenceWebContactExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions EventDispatcher/ContactEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
35 changes: 35 additions & 0 deletions EventDispatcher/Event/ErrorMessageSubmit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace FrequenceWeb\Bundle\ContactBundle\EventDispatcher\Event;

use FrequenceWeb\Bundle\ContactBundle\Model\Contact;
use Symfony\Component\EventDispatcher\Event;

/**
* This event is thrown each time an user send a message and get an error
*
* @author Chrysweel
*/
class ErrorMessageSubmit extends Event
{
/**
* @var Contact
*/
protected $contact;

/**
* @param Contact $contact
*/
public function __construct(Contact $contact)
{
$this->contact = $contact;
}

/**
* @return Contact
*/
public function getContact()
{
return $this->contact;
}
}
14 changes: 12 additions & 2 deletions EventDispatcher/Listener/EmailContactListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
56 changes: 52 additions & 4 deletions Form/ContactType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -20,19 +24,63 @@ 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}
*/
public function getName()
{
return 'contact';
}


public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('fixed_to_and_subject');
}
}
18 changes: 9 additions & 9 deletions Model/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Contact
/**
* The message subject
*
* @var string
* @var array
*/
protected $subject;

Expand Down Expand Up @@ -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
*
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------

Expand Down
2 changes: 1 addition & 1 deletion Resources/config/validation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<property name="email">
<constraint name="NotBlank" />
<constraint name="Email">
<option name="checkMX">true</option>
<option name="checkMX">false</option>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ no go

</constraint>
</property>
<property name="subject">
Expand Down
5 changes: 5 additions & 0 deletions Resources/translations/FrequenceWebContactBundle.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing new line at EOF

14 changes: 14 additions & 0 deletions Resources/translations/FrequenceWebContactBundle.es.yml
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing new line at EOF

2 changes: 0 additions & 2 deletions Resources/views/Mails/mail.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
{% trans from 'FrequenceWebContactBundle' %}contact.message.sent.you{% endtrans %} :
</h1>

<h2>{{ contact.subject }}</h2>

<p>
{{ contact.body }}
</p>
1 change: 1 addition & 0 deletions Tests/Functional/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}

Expand Down
9 changes: 9 additions & 0 deletions Tests/Functional/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ framework:
resource: %kernel.root_dir%/routing.yml
templating:
engines: [twig]
translator: []
profiler: { only_exceptions: false }

swiftmailer:
disable_delivery: true

frequence_web_contact:
to: foo@bar.baz

ewz_recaptcha:
public_key: 'recaptcha_public_key'
private_key: 'recaptcha_private_key'
enabled: true



11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./vendor/autoload.php"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="FrequenceWebContactBundle Test Suite">
Expand Down