<?php
# src/Controller/UserTicketController.php
namespace App\Controller;
use App\Entity\Ticket;
use App\Form\TicketType;
use App\Entity\TicketReply;
use App\Form\TicketReplyType;
use App\Repository\TicketRepository;
use App\Repository\TicketReplyRepository;
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;
#[Route('/userinterface/ticket')]
class UserTicketController extends AbstractController
{
private $ticketReplyRepository;
public function __construct(TicketReplyRepository $ticketReplyRepository)
{
$this->ticketReplyRepository = $ticketReplyRepository;
}
// Neue Methode zum Abrufen der Anzahl ungelesener Antworten
public function countUnreadReplies(): int
{
$user = $this->getUser();
return $this->ticketReplyRepository->countNewRepliesForUser($user->getId());
}
#[Route('/', name: 'user_ticket_index', methods: ['GET'])]
public function index(TicketRepository $ticketRepository): Response
{
// Hol den aktuell angemeldeten Benutzer
$user = $this->getUser();
// Tickets für den angemeldeten Benutzer abrufen
$tickets = $ticketRepository->findBy(['user' => $user]);
return $this->render('user/ticket_dashboard.html.twig', [
'tickets' => $tickets,
]);
}
#[Route('/new', name: 'user_ticket_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$ticket = new Ticket();
$form = $this->createForm(TicketType::class, $ticket);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$ticket->setUser($this->getUser());
$entityManager->persist($ticket);
$entityManager->flush();
return $this->redirectToRoute('user_ticket_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('user/ticket_new.html.twig', [
'ticket' => $ticket,
'form' => $form,
]);
}
#[Route('/{id}', name: 'user_ticket_show', methods: ['GET', 'POST'])]
public function show(Ticket $ticket, Request $request, EntityManagerInterface $entityManager): Response
{
// Setze alle Antworten dieses Tickets als gelesen für den Benutzer
foreach ($ticket->getReplies() as $reply) {
if (!$reply->getUserIsRead()) {
$reply->setUserIsRead(true);
}
}
$entityManager->flush(); // Speicher die Änderungen, um die Antworten als gelesen zu markieren
// Antwortformular nur anzeigen, wenn Ticket offen oder in Bearbeitung
$replyForm = null;
if ($ticket->getStatus() !== 'geschlossen') {
$reply = new TicketReply();
$reply->setTicket($ticket);
$reply->setAuthor($this->getUser());
// Antwortformular erstellen
$replyForm = $this->createForm(TicketReplyType::class, $reply);
$replyForm->handleRequest($request);
// Antwort speichern, wenn das Formular abgeschickt und gültig ist
if ($replyForm->isSubmitted() && $replyForm->isValid()) {
$entityManager->persist($reply);
$entityManager->flush();
$this->addFlash('success', 'Deine Antwort wurde erfolgreich gespeichert.');
// Verwende absolute Pfade in der Umleitung
return $this->redirectToRoute('user_ticket_show', ['id' => $ticket->getId()]);
}
}
return $this->render('user/ticket_show.html.twig', [
'ticket' => $ticket,
'replyForm' => $replyForm ? $replyForm->createView() : null,
]);
}
#[Route('/{id}/edit', name: 'user_ticket_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Ticket $ticket, EntityManagerInterface $entityManager): Response
{
// Überprüfen, ob das Ticket Antworten hat
if (count($ticket->getReplies()) > 0) {
$this->addFlash('danger', 'Dieses Ticket kann nicht bearbeitet werden, da bereits eine Antwort vorhanden ist.');
return $this->redirectToRoute('user_ticket_show', ['id' => $ticket->getId()]);
}
// Nur erlauben, dass der Besitzer des Tickets es bearbeiten kann
if ($ticket->getUser() !== $this->getUser()) {
throw $this->createAccessDeniedException('Du kannst nur deine eigenen Tickets bearbeiten.');
}
// Ticket bearbeiten
$form = $this->createForm(TicketType::class, $ticket);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
$this->addFlash('success', 'Ticket erfolgreich bearbeitet.');
return $this->redirectToRoute('user_ticket_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('user/ticket_edit.html.twig', [
'ticket' => $ticket,
'form' => $form,
]);
}
}