new: usr: add Contact page with email sending behaviour #4
All checks were successful
Deploy to Production / deploy (push) Successful in 39s
All checks were successful
Deploy to Production / deploy (push) Successful in 39s
This commit is contained in:
@@ -10,10 +10,19 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\ContactMessage;
|
||||
use App\Form\ContactFormType;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use RuntimeException;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\AsController;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
/**
|
||||
@@ -31,11 +40,14 @@ class GameController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(env: 'APP_ENV')]
|
||||
private readonly string $env,
|
||||
private readonly string $env,
|
||||
#[Autowire(env: 'MERCURE_PUBLIC_URL')]
|
||||
private readonly string $mercurePublicUrl,
|
||||
private readonly string $mercurePublicUrl,
|
||||
#[Autowire(env: 'MERCURE_SUBSCRIBER_JWT')]
|
||||
private readonly string $mercureSubscriberJwt,
|
||||
private readonly string $mercureSubscriberJwt,
|
||||
#[Autowire(env: 'APP_CONTACT_MAIL_ADDRESS')]
|
||||
private readonly string $appContactMailAddress,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -69,9 +81,28 @@ class GameController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/contact', name: 'MineSeekerBundle_contact')]
|
||||
public function contact(): Response
|
||||
{
|
||||
return $this->render('Official/contact.html.twig');
|
||||
public function contact(
|
||||
Request $request,
|
||||
EntityManagerInterface $em,
|
||||
MailerInterface $mailer,
|
||||
): Response {
|
||||
$contactMessage = new ContactMessage();
|
||||
$form = $this->createForm(ContactFormType::class, $contactMessage);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$contactMessage->setIpAddress($request->getClientIp());
|
||||
$em->persist($contactMessage);
|
||||
$em->flush();
|
||||
$this->sendMail($mailer, $contactMessage);
|
||||
$this->addFlash('contact_success', 'Thank you for your message! We will get back to you soon.');
|
||||
|
||||
return $this->redirectToRoute('MineSeekerBundle_contact');
|
||||
}
|
||||
|
||||
return $this->render('Official/contact.html.twig', [
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/landing-page', name: 'MineSeekerBundle_landing')]
|
||||
@@ -79,4 +110,31 @@ class GameController extends AbstractController
|
||||
{
|
||||
return $this->render('Official/landing.html.twig');
|
||||
}
|
||||
|
||||
public function sendMail(MailerInterface $mailer, ContactMessage $contactMessage): void
|
||||
{
|
||||
try {
|
||||
$mailer->send(
|
||||
new TemplatedEmail()
|
||||
->from('noreply@mineseeker.hu')
|
||||
->to($this->appContactMailAddress)
|
||||
->replyTo($contactMessage->getEmail())
|
||||
->subject('New Contact Message from ' . $contactMessage->getName())
|
||||
->htmlTemplate('emails/contact_notification.html.twig')
|
||||
->context(['message' => $contactMessage])
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Failed to send contact notification email: ' . $e->getMessage(), [
|
||||
'exception' => $e,
|
||||
'message' => $contactMessage,
|
||||
]);
|
||||
throw new RuntimeException('Failed to send contact notification email: ' . $e->getMessage());
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
$this->logger->error('Failed to send contact notification email: ' . $e->getMessage(), [
|
||||
'exception' => $e,
|
||||
'message' => $contactMessage,
|
||||
]);
|
||||
throw new RuntimeException('Failed to send contact notification email: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
128
src/Entity/ContactMessage.php
Normal file
128
src/Entity/ContactMessage.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\ContactMessageRepository;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\GeneratedValue;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
|
||||
/**
|
||||
* Class ContactMessage
|
||||
*
|
||||
* @package App\Entity
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 15.
|
||||
*/
|
||||
#[Entity(repositoryClass: ContactMessageRepository::class)]
|
||||
#[Table(name: 'contact_messages')]
|
||||
class ContactMessage
|
||||
{
|
||||
#[Id, GeneratedValue, Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[Column]
|
||||
private string $name;
|
||||
|
||||
#[Column]
|
||||
private string $email;
|
||||
|
||||
#[Column(type: Types::TEXT)]
|
||||
private string $content;
|
||||
|
||||
#[Column]
|
||||
private bool $consent = false;
|
||||
|
||||
#[Column]
|
||||
private DateTimeImmutable $createdAt;
|
||||
|
||||
#[Column(length: 45, nullable: true)]
|
||||
private ?string $ipAddress = 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): self
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEmail(): string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function setEmail(string $email): self
|
||||
{
|
||||
$this->email = $email;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function setContent(string $content): self
|
||||
{
|
||||
$this->content = $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isConsent(): bool
|
||||
{
|
||||
return $this->consent;
|
||||
}
|
||||
|
||||
public function setConsent(bool $consent): self
|
||||
{
|
||||
$this->consent = $consent;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function getIpAddress(): ?string
|
||||
{
|
||||
return $this->ipAddress;
|
||||
}
|
||||
|
||||
public function setIpAddress(?string $ipAddress): self
|
||||
{
|
||||
$this->ipAddress = $ipAddress;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
89
src/Form/ContactFormType.php
Normal file
89
src/Form/ContactFormType.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\ContactMessage;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
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\Email;
|
||||
use Symfony\Component\Validator\Constraints\IsTrue;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
|
||||
/**
|
||||
* Class ContactFormType
|
||||
*
|
||||
* @package App\Form
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 15.
|
||||
*/
|
||||
class ContactFormType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('name', TextType::class, [
|
||||
'label' => 'Name',
|
||||
'constraints' => [
|
||||
new NotBlank(message: 'Please enter your name.'),
|
||||
new Length(
|
||||
min: 2,
|
||||
max: 255,
|
||||
minMessage: 'Name must be at least {{ limit }} characters.',
|
||||
maxMessage: 'Name cannot be longer than {{ limit }} characters.',
|
||||
),
|
||||
],
|
||||
])
|
||||
->add('email', EmailType::class, [
|
||||
'label' => 'Email',
|
||||
'constraints' => [
|
||||
new NotBlank(message: 'Please enter your email address.'),
|
||||
new Email(message: 'Please enter a valid email address.'),
|
||||
],
|
||||
])
|
||||
->add('content', TextareaType::class, [
|
||||
'label' => 'Message',
|
||||
'constraints' => [
|
||||
new NotBlank(message: 'Please enter your message.'),
|
||||
new Length(
|
||||
min: 10,
|
||||
max: 5000,
|
||||
minMessage: 'Message must be at least {{ limit }} characters.',
|
||||
maxMessage: 'Message cannot be longer than {{ limit }} characters.',
|
||||
),
|
||||
],
|
||||
])
|
||||
->add('consent', CheckboxType::class, [
|
||||
'label' => 'I have read the Privacy and Data Processing Policy and I consent to the processing of my data.',
|
||||
'mapped' => true,
|
||||
'constraints' => [
|
||||
new IsTrue(message: 'You must agree to the privacy policy to submit this form.'),
|
||||
],
|
||||
])
|
||||
->add('recaptcha', RecaptchaType::class);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => ContactMessage::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
47
src/Migrations/2026/04/Version20260415160446.php
Normal file
47
src/Migrations/2026/04/Version20260415160446.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Migrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Class Version20260415160446
|
||||
*
|
||||
* @package App\Migrations
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 15.
|
||||
*/
|
||||
final class Version20260415160446 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Add contact mail storage support';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('CREATE SEQUENCE contact_messages_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
|
||||
$this->addSql('CREATE TABLE contact_messages (id INT NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, content TEXT NOT NULL, consent BOOLEAN NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, ip_address VARCHAR(45) DEFAULT NULL, PRIMARY KEY(id))');
|
||||
$this->addSql('COMMENT ON COLUMN contact_messages.created_at IS \'(DC2Type:datetime_immutable)\'');
|
||||
$this->addSql('ALTER INDEX played_game_uuid_unique RENAME TO UNIQ_54BE8039D17F50A6');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('DROP SEQUENCE contact_messages_id_seq CASCADE');
|
||||
$this->addSql('DROP TABLE contact_messages');
|
||||
$this->addSql('ALTER INDEX uniq_54be8039d17f50a6 RENAME TO played_game_uuid_unique');
|
||||
}
|
||||
}
|
||||
35
src/Repository/ContactMessageRepository.php
Normal file
35
src/Repository/ContactMessageRepository.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of the SplendidBear Websites' projects.
|
||||
*
|
||||
* Copyright (c) 2026 @ www.splendidbear.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\ContactMessage;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* Class ContactMessageRepository
|
||||
*
|
||||
* @package App\Repository
|
||||
* @author Lang <https://www.splendidbear.org>
|
||||
* @category Class
|
||||
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
||||
* @link www.splendidbear.org
|
||||
* @since 2026. 04. 15.
|
||||
*
|
||||
* @extends ServiceEntityRepository<ContactMessage>
|
||||
*/
|
||||
class ContactMessageRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, ContactMessage::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user