Add read-tracking and soft-delete to contact messages

Admin can now open a message via a per-row "View" page (which stamps
read_at the first time it's opened), see a sortable Read column on the
list instead of the raw message text, and soft-delete messages via a
Delete button on both the list and detail view. Deleted messages are
excluded from the default list so it doesn't pile up over time.
This commit is contained in:
Frank
2026-08-29 11:19:54 +02:00
parent 6fb3ed597b
commit e470a9848d
7 changed files with 206 additions and 4 deletions
+26
View File
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20260829130000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add read_at to contact_message so admin can track which messages have been read';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE contact_message ADD read_at DATETIME DEFAULT NULL');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE contact_message DROP read_at');
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20260829140000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add deleted_at to contact_message for soft-deleting messages from the admin list';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE contact_message ADD deleted_at DATETIME DEFAULT NULL');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE contact_message DROP deleted_at');
}
}
@@ -4,8 +4,11 @@ declare(strict_types=1);
namespace App\Game\Controller;
use App\Website\Entity\ContactMessage;
use App\Website\Repository\ContactMessageRepository;
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;
use Symfony\Component\Security\Http\Attribute\IsGranted;
@@ -18,7 +21,38 @@ final class GameAdminContactController extends AbstractController
public function index(ContactMessageRepository $contactMessageRepository): Response
{
return $this->render('game/admin/contact/index.html.twig', [
'messages' => $contactMessageRepository->findBy([], ['createdAt' => 'DESC']),
'messages' => $contactMessageRepository->findActive(),
]);
}
#[Route('/{id}', name: 'game_admin_contact_show', methods: ['GET'])]
public function show(ContactMessage $contactMessage, EntityManagerInterface $entityManager): Response
{
if (!$contactMessage->isRead()) {
$contactMessage->setReadAt(new \DateTimeImmutable());
$entityManager->flush();
}
return $this->render('game/admin/contact/show.html.twig', [
'message' => $contactMessage,
]);
}
#[Route('/{id}/delete', name: 'game_admin_contact_delete', methods: ['POST'])]
public function delete(ContactMessage $contactMessage, Request $request, EntityManagerInterface $entityManager): Response
{
if (!$this->isCsrfTokenValid('delete_contact_' . $contactMessage->getId(), $request->request->get('_token'))) {
$this->addFlash('danger', 'Invalid CSRF token.');
return $this->redirectToRoute('game_admin_contact');
}
if (!$contactMessage->isDeleted()) {
$contactMessage->setDeletedAt(new \DateTimeImmutable());
$entityManager->flush();
}
$this->addFlash('success', sprintf('Message from "%s" deleted.', $contactMessage->getName()));
return $this->redirectToRoute('game_admin_contact');
}
}
+40
View File
@@ -27,6 +27,12 @@ class ContactMessage
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
private ?\DateTimeImmutable $readAt = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
private ?\DateTimeImmutable $deletedAt = null;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
@@ -84,4 +90,38 @@ class ContactMessage
return $this;
}
public function getReadAt(): ?\DateTimeImmutable
{
return $this->readAt;
}
public function setReadAt(?\DateTimeImmutable $readAt): static
{
$this->readAt = $readAt;
return $this;
}
public function isRead(): bool
{
return $this->readAt !== null;
}
public function getDeletedAt(): ?\DateTimeImmutable
{
return $this->deletedAt;
}
public function setDeletedAt(?\DateTimeImmutable $deletedAt): static
{
$this->deletedAt = $deletedAt;
return $this;
}
public function isDeleted(): bool
{
return $this->deletedAt !== null;
}
}
@@ -15,4 +15,12 @@ class ContactMessageRepository extends ServiceEntityRepository
{
parent::__construct($registry, ContactMessage::class);
}
/**
* @return ContactMessage[]
*/
public function findActive(): array
{
return $this->findBy(['deletedAt' => null], ['createdAt' => 'DESC']);
}
}
+18 -3
View File
@@ -15,7 +15,8 @@
<th style="padding: 0.75rem 1rem; text-align: left; color: #475569; font-weight: 600;">Received</th>
<th style="padding: 0.75rem 1rem; text-align: left; color: #475569; font-weight: 600;">Name</th>
<th style="padding: 0.75rem 1rem; text-align: left; color: #475569; font-weight: 600;">Email</th>
<th style="padding: 0.75rem 1rem; text-align: left; color: #475569; font-weight: 600;">Message</th>
<th data-type="boolean" style="padding: 0.75rem 1rem; text-align: left; color: #475569; font-weight: 600;">Read</th>
<th data-sortable="false" style="padding: 0.75rem 1rem; text-align: left; color: #475569; font-weight: 600;">Actions</th>
</tr>
</thead>
<tbody>
@@ -26,11 +27,25 @@
<td style="padding: 0.75rem 1rem; color: #475569;">
<a href="mailto:{{ entry.email }}" style="color: #3b82f6; text-decoration: none;">{{ entry.email }}</a>
</td>
<td style="padding: 0.75rem 1rem; color: #475569; max-width: 480px; white-space: pre-wrap;">{{ entry.message }}</td>
<td style="padding: 0.75rem 1rem; color: #16a34a; font-weight: 600;">{{ entry.read ? '✓' : '' }}</td>
<td style="padding: 0.75rem 1rem;">
<a href="{{ path('game_admin_contact_show', {id: entry.id}) }}" style="color: #3b82f6; text-decoration: none; font-size: 0.85rem; margin-right: 0.75rem;">View</a>
<form method="post" action="{{ path('game_admin_contact_delete', {id: entry.id}) }}" style="display: inline;" onsubmit="return confirm('Delete message from {{ entry.name }}?')">
<input type="hidden" name="_token" value="{{ csrf_token('delete_contact_' ~ entry.id) }}">
<button type="submit" style="
background: none;
border: none;
color: #ef4444;
cursor: pointer;
font-size: 0.85rem;
padding: 0;
">Delete</button>
</form>
</td>
</tr>
{% else %}
<tr>
<td colspan="4" style="padding: 2rem; text-align: center; color: #94a3b8;">No messages yet.</td>
<td colspan="5" style="padding: 2rem; text-align: center; color: #94a3b8;">No messages yet.</td>
</tr>
{% endfor %}
</tbody>
@@ -0,0 +1,53 @@
{% extends 'game/admin/base.html.twig' %}
{% block title %}Message from {{ message.name }} — Admin{% endblock %}
{% block admin_body %}
<div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.5rem;">
<h1 style="margin: 0; font-size: 1.5rem; color: #0f172a;">Message from {{ message.name }}</h1>
<a href="{{ path('game_admin_contact') }}" style="color: #3b82f6; text-decoration: none; font-size: 0.85rem;">&larr; Back to Contact Messages</a>
</div>
<div style="background: #fff; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,.07); padding: 1.5rem; max-width: 720px;">
<dl style="display: grid; grid-template-columns: 140px 1fr; row-gap: 0.75rem; margin: 0 0 1.5rem;">
<dt style="color: #64748b; font-weight: 600;">Name</dt>
<dd style="margin: 0; color: #0f172a;">{{ message.name }}</dd>
<dt style="color: #64748b; font-weight: 600;">Email</dt>
<dd style="margin: 0;">
<a href="mailto:{{ message.email }}" style="color: #3b82f6; text-decoration: none;">{{ message.email }}</a>
</dd>
<dt style="color: #64748b; font-weight: 600;">Received</dt>
<dd style="margin: 0; color: #0f172a;">{{ message.createdAt|date('Y-m-d H:i:s') }}</dd>
<dt style="color: #64748b; font-weight: 600;">Read at</dt>
<dd style="margin: 0; color: #0f172a;">
{% if message.readAt %}
{{ message.readAt|date('Y-m-d H:i:s') }}
{% else %}
<span style="color: #94a3b8;">Just now</span>
{% endif %}
</dd>
</dl>
<hr style="border-color: #f1f5f9; margin-bottom: 1.5rem;">
<div style="white-space: pre-wrap; color: #0f172a; line-height: 1.6; margin-bottom: 1.5rem;">{{ message.message }}</div>
<hr style="border-color: #f1f5f9; margin-bottom: 1.5rem;">
<form method="post" action="{{ path('game_admin_contact_delete', {id: message.id}) }}" onsubmit="return confirm('Delete message from {{ message.name }}?')">
<input type="hidden" name="_token" value="{{ csrf_token('delete_contact_' ~ message.id) }}">
<button type="submit" style="
background: none;
border: 1px solid #fca5a5;
color: #ef4444;
cursor: pointer;
font-size: 0.85rem;
padding: 0.4rem 0.9rem;
border-radius: 6px;
">Delete Message</button>
</form>
</div>
{% endblock %}