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.
59 lines
2.1 KiB
PHP
59 lines
2.1 KiB
PHP
<?php
|
|
|
|
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;
|
|
|
|
#[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->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');
|
|
}
|
|
}
|