Add contact form with reCAPTCHA, saved to DB, viewable in admin
Adds a public /contact page (name/email/message, protected by the same reCAPTCHA v3 setup used on registration) that persists submissions to a new contact_message table, plus a Contact section in the admin panel to read them. Linked from the site footer.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Game\Controller;
|
||||
|
||||
use App\Website\Repository\ContactMessageRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[Route('/admin/contact')]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
final class GameAdminContactController extends AbstractController
|
||||
{
|
||||
#[Route('', name: 'game_admin_contact', methods: ['GET'])]
|
||||
public function index(ContactMessageRepository $contactMessageRepository): Response
|
||||
{
|
||||
return $this->render('game/admin/contact/index.html.twig', [
|
||||
'messages' => $contactMessageRepository->findBy([], ['createdAt' => 'DESC']),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Website\Controller;
|
||||
|
||||
use App\Website\Form\ContactFormType;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
final class ContactController extends AbstractController
|
||||
{
|
||||
#[Route(path: '/contact', name: 'website_contact', methods: ['GET', 'POST'])]
|
||||
public function contact(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$form = $this->createForm(ContactFormType::class);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->persist($form->getData());
|
||||
$entityManager->flush();
|
||||
|
||||
$this->addFlash('success', 'Thanks for reaching out — your message has been sent.');
|
||||
|
||||
return $this->redirectToRoute('website_contact');
|
||||
}
|
||||
|
||||
return $this->render('website/contact.html.twig', [
|
||||
'contactForm' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Website\Entity;
|
||||
|
||||
use App\Website\Repository\ContactMessageRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: ContactMessageRepository::class)]
|
||||
#[ORM\Table(name: 'contact_message')]
|
||||
class ContactMessage
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $name = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $email = null;
|
||||
|
||||
#[ORM\Column(type: Types::TEXT)]
|
||||
private ?string $message = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
|
||||
private ?\DateTimeImmutable $createdAt = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->createdAt = new \DateTimeImmutable();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEmail(): ?string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function setEmail(string $email): static
|
||||
{
|
||||
$this->email = $email;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMessage(): ?string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
public function setMessage(string $message): static
|
||||
{
|
||||
$this->message = $message;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setCreatedAt(\DateTimeImmutable $createdAt): static
|
||||
{
|
||||
$this->createdAt = $createdAt;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Website\Form;
|
||||
|
||||
use App\Website\Entity\ContactMessage;
|
||||
use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
|
||||
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
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\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
|
||||
class ContactFormType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('name', TextType::class, [
|
||||
'constraints' => [
|
||||
new NotBlank(message: 'Please enter your name'),
|
||||
new Length(max: 255, maxMessage: 'Your name cannot be longer than {{ limit }} characters'),
|
||||
],
|
||||
])
|
||||
->add('email', EmailType::class, [
|
||||
'constraints' => [
|
||||
new NotBlank(message: 'Please enter your email address'),
|
||||
],
|
||||
])
|
||||
->add('message', TextareaType::class, [
|
||||
'attr' => ['rows' => 6],
|
||||
'constraints' => [
|
||||
new NotBlank(message: 'Please enter a message'),
|
||||
new Length(max: 5000, maxMessage: 'Your message cannot be longer than {{ limit }} characters'),
|
||||
],
|
||||
])
|
||||
->add('captcha', Recaptcha3Type::class, [
|
||||
'constraints' => new Recaptcha3(),
|
||||
'action_name' => 'contact',
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => ContactMessage::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Website\Repository;
|
||||
|
||||
use App\Website\Entity\ContactMessage;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<ContactMessage>
|
||||
*/
|
||||
class ContactMessageRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, ContactMessage::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user