diff --git a/src/Controller/BotController.php b/src/Controller/BotController.php new file mode 100644 index 0000000..86802e7 --- /dev/null +++ b/src/Controller/BotController.php @@ -0,0 +1,38 @@ +query->get('page'); + $posted = $request->request->all(); + $datas = []; + + if (empty($action) || empty($posted) || !$request->isMethod('POST')) { + throw $this->createNotFoundException(); + } + + foreach ($posted as $name => $value) { + if (is_array($value)) { + foreach ($value as $k => $v) { + $datas[$name.'['.$k.']'] = $v; + } + } else { + $datas[$name] = $value; + } + } + + return $this->defaultRender('bot/form_without_javascript.html.twig', [ + 'action' => $action, + 'datas' => $datas, + ]); + } +} diff --git a/src/Controller/ContactController.php b/src/Controller/ContactController.php index 9eedf0c..8df16e2 100644 --- a/src/Controller/ContactController.php +++ b/src/Controller/ContactController.php @@ -6,12 +6,42 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use App\Core\Controller\Site\PageController; +use App\Form\ContactType; +use App\Core\Notification\MailNotifier; +use Symfony\Component\HttpFoundation\Request; class ContactController extends PageController { - public function contact(): Response + public function contact(MailNotifier $notifier, Request $request): Response { + $form = $this->createForm(ContactType::class); + $showForm = true; + + if ($request->isMethod('POST')) { + $form->handleRequest($request); + + if ($form->isValid()) { + $showForm = false; + $data = $form->getData(); + + $notifier + ->addRecipient('simon@deblan.fr') + ->setSubject('[Deblan] Nouveau message') + ->setReplyTo($data['email']) + ->notify('mail/contact.html.twig', [ + 'message' => $data, + ], 'text/plain'); + + $this->addFlash( + 'success', + 'Votre message a bien été envoyé.' + ); + } + } + return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [ + 'form' => $form->createView(), + 'showForm' => $showForm, ]); } } diff --git a/src/Form/ContactType.php b/src/Form/ContactType.php new file mode 100644 index 0000000..2b50a08 --- /dev/null +++ b/src/Form/ContactType.php @@ -0,0 +1,81 @@ +add( + 'name', + TextType::class, + [ + 'label' => 'Nom', + 'required' => true, + 'attr' => [ + ], + 'constraints' => [ + new NotBlank(), + ], + ] + ); + + $builder->add( + 'subject', + TextType::class, + [ + 'label' => 'Sujet', + 'required' => true, + 'attr' => [ + ], + 'constraints' => [ + new NotBlank(), + ], + ] + ); + + $builder->add( + 'email', + EmailType::class, + [ + 'label' => 'E-mail', + 'required' => true, + 'attr' => [ + ], + 'constraints' => [ + new NotBlank(), + new Email(), + ], + ] + ); + + $builder->add( + 'message', + TextareaType::class, + [ + 'label' => 'Message', + 'required' => true, + 'attr' => [ + ], + 'constraints' => [ + new NotBlank(), + ], + ] + ); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + ]); + } +} diff --git a/templates/base.html.twig b/templates/base.html.twig index ffcefbc..be90fd1 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -85,7 +85,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX {% set flashes = app.flashes %} {% if flashes|length %} - {% for level, messages in appflashes %} + {% for level, messages in flashes %} {% for message in messages %}
+ {% set p %} + Merci de cliquer sur le bouton ci-dessous pour valider + le formulaire : + {% endset %} + + {{- p -}} +
+ + +src/Controller/ContactController.php
templates/contact/index.html.twig