add contact page
This commit is contained in:
parent
38bec1c0fa
commit
5e8b49a131
38
src/Controller/BotController.php
Normal file
38
src/Controller/BotController.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use App\Core\Controller\Site\PageController;
|
||||
|
||||
class BotController extends PageController
|
||||
{
|
||||
public function formWithoutJavascript(Request $request)
|
||||
{
|
||||
$action = $request->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,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
81
src/Form/ContactType.php
Normal file
81
src/Form/ContactType.php
Normal file
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Validator\Constraints\Email;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
|
||||
class ContactType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->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([
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -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 %}
|
||||
<div class="col-12 alert alert-{{ level }}">
|
||||
{{- message|nl2br -}}
|
||||
|
|
38
templates/bot/form_without_javascript.html.twig
Normal file
38
templates/bot/form_without_javascript.html.twig
Normal file
|
@ -0,0 +1,38 @@
|
|||
{% extends "base.html.twig" %}
|
||||
|
||||
{% block page_title %}
|
||||
{{- 'Validation nécessaire' -}}
|
||||
{% endblock %}
|
||||
|
||||
{% block page_subtitle %}
|
||||
{% endblock %}
|
||||
|
||||
{% block metas %}
|
||||
{% endblock %}
|
||||
|
||||
{% block meta_title %}
|
||||
{{- 'Validation nécessaire' -}}
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<div class="col-12">
|
||||
<div class="body">
|
||||
<p>
|
||||
{% set p %}
|
||||
Merci de cliquer sur le bouton ci-dessous pour valider
|
||||
le formulaire :
|
||||
{% endset %}
|
||||
|
||||
{{- p -}}
|
||||
</p>
|
||||
|
||||
<form action="{{ action }}" method="POST">
|
||||
{% for name, value in datas %}
|
||||
<input type="hidden" name="{{ name }}" value="{{ value }}">
|
||||
{% endfor %}
|
||||
|
||||
<input class="button" type="submit" value="Valider le formulaire">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -1,20 +0,0 @@
|
|||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Hello ContactController!{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<style>
|
||||
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
|
||||
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
|
||||
</style>
|
||||
|
||||
<div class="example-wrapper">
|
||||
<h1>Hello {{ controller_name }}! ✅</h1>
|
||||
|
||||
This friendly message is coming from:
|
||||
<ul>
|
||||
<li>Your controller at <code><a href="{{ '/home/simon/public_html/www/repo/deblan.io-murph/src/Controller/ContactController.php'|file_link(0) }}">src/Controller/ContactController.php</a></code></li>
|
||||
<li>Your template at <code><a href="{{ '/home/simon/public_html/www/repo/deblan.io-murph/templates/contact/index.html.twig'|file_link(0) }}">templates/contact/index.html.twig</a></code></li>
|
||||
</ul>
|
||||
</div>
|
||||
{% endblock %}
|
5
templates/mail/contact.html.twig
Normal file
5
templates/mail/contact.html.twig
Normal file
|
@ -0,0 +1,5 @@
|
|||
Auteur : {{ message.name }}
|
||||
Email : {{ message.email }}
|
||||
Sujet : {{ message.subject|raw }}
|
||||
|
||||
{{ message.message|raw }}
|
|
@ -15,9 +15,47 @@
|
|||
<div class="body">
|
||||
<div class="body-content">
|
||||
{{- _page.content.value|markdown('post') -}}
|
||||
|
||||
CONTACT
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if showForm %}
|
||||
<div class="body col-12">
|
||||
<form class="form" method="POST" data-form-bot action="{{ safe_path('blog_tech_form_without_javascript', {page: app.request.uri}) }}">
|
||||
<div class="row">
|
||||
<div class="field col-6">
|
||||
{{ form_label(form.name) }}
|
||||
{{ form_widget(form.name) }}
|
||||
{{ form_errors(form.name) }}
|
||||
</div>
|
||||
<div class="field col-6">
|
||||
{{ form_label(form.email) }}
|
||||
{{ form_widget(form.email) }}
|
||||
{{ form_errors(form.email) }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="field col-12">
|
||||
{{ form_label(form.subject) }}
|
||||
{{ form_widget(form.subject) }}
|
||||
{{ form_errors(form.subject) }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
{{ form_label(form.message) }}
|
||||
{{ form_errors(form.message) }}
|
||||
{{ form_widget(form.message, {attr: {cols: 30, rows: 10}}) }}
|
||||
</div>
|
||||
<div class="field">
|
||||
<input type="checkbox" id="rgpd" required>
|
||||
<label for="rgpd">En validant ce formulaire, vous acceptez que j'utilise votre e-mail pour vous fournir une réponse.</label>
|
||||
</div>
|
||||
<div class="field">
|
||||
<input type="submit" class="button" value="Envoyer" />
|
||||
</div>
|
||||
|
||||
{{ form_rest(form) }}
|
||||
</form>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in a new issue