src/Controller/UserTicketController.php line 50

Open in your IDE?
  1. <?php
  2. # src/Controller/UserTicketController.php
  3. namespace App\Controller;
  4. use App\Entity\Ticket;
  5. use App\Form\TicketType;
  6. use App\Entity\TicketReply;
  7. use App\Form\TicketReplyType;
  8. use App\Repository\TicketRepository;
  9. use App\Repository\TicketReplyRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. #[Route('/userinterface/ticket')]
  16. class UserTicketController extends AbstractController
  17. {
  18.     private $ticketReplyRepository;
  19.     public function __construct(TicketReplyRepository $ticketReplyRepository)
  20.     {
  21.         $this->ticketReplyRepository $ticketReplyRepository;
  22.     }
  23.     // Neue Methode zum Abrufen der Anzahl ungelesener Antworten
  24.     public function countUnreadReplies(): int
  25.     {
  26.         $user $this->getUser();
  27.         return $this->ticketReplyRepository->countNewRepliesForUser($user->getId());
  28.     }
  29.     #[Route('/'name'user_ticket_index'methods: ['GET'])]
  30.     public function index(TicketRepository $ticketRepository): Response
  31.     {
  32.         // Hol den aktuell angemeldeten Benutzer
  33.         $user $this->getUser();
  34.         
  35.         // Tickets für den angemeldeten Benutzer abrufen
  36.         $tickets $ticketRepository->findBy(['user' => $user]);
  37.         return $this->render('user/ticket_dashboard.html.twig', [
  38.             'tickets' => $tickets,
  39.         ]);
  40.     }
  41.     #[Route('/new'name'user_ticket_new'methods: ['GET''POST'])]
  42.     public function new(Request $requestEntityManagerInterface $entityManager): Response
  43.     {
  44.         $ticket = new Ticket();
  45.         $form $this->createForm(TicketType::class, $ticket);
  46.         $form->handleRequest($request);
  47.         if ($form->isSubmitted() && $form->isValid()) {
  48.             $ticket->setUser($this->getUser());
  49.             $entityManager->persist($ticket);
  50.             $entityManager->flush();
  51.             return $this->redirectToRoute('user_ticket_index', [], Response::HTTP_SEE_OTHER);
  52.         }
  53.         return $this->renderForm('user/ticket_new.html.twig', [
  54.             'ticket' => $ticket,
  55.             'form' => $form,
  56.         ]);
  57.     }
  58.     
  59.     #[Route('/{id}'name'user_ticket_show'methods: ['GET''POST'])]
  60.     public function show(Ticket $ticketRequest $requestEntityManagerInterface $entityManager): Response
  61.     {
  62.         // Setze alle Antworten dieses Tickets als gelesen für den Benutzer
  63.         foreach ($ticket->getReplies() as $reply) {
  64.             if (!$reply->getUserIsRead()) {
  65.                 $reply->setUserIsRead(true);
  66.             }
  67.         }
  68.     
  69.         $entityManager->flush(); // Speicher die Änderungen, um die Antworten als gelesen zu markieren
  70.     
  71.         // Antwortformular nur anzeigen, wenn Ticket offen oder in Bearbeitung
  72.         $replyForm null;
  73.         if ($ticket->getStatus() !== 'geschlossen') {
  74.             $reply = new TicketReply();
  75.             $reply->setTicket($ticket);
  76.             $reply->setAuthor($this->getUser());
  77.     
  78.             // Antwortformular erstellen
  79.             $replyForm $this->createForm(TicketReplyType::class, $reply);
  80.             $replyForm->handleRequest($request);
  81.     
  82.             // Antwort speichern, wenn das Formular abgeschickt und gültig ist
  83.             if ($replyForm->isSubmitted() && $replyForm->isValid()) {
  84.                 $entityManager->persist($reply);
  85.                 $entityManager->flush();
  86.     
  87.                 $this->addFlash('success''Deine Antwort wurde erfolgreich gespeichert.');
  88.     
  89.                 // Verwende absolute Pfade in der Umleitung
  90.                 return $this->redirectToRoute('user_ticket_show', ['id' => $ticket->getId()]);
  91.             }
  92.         }
  93.     
  94.         return $this->render('user/ticket_show.html.twig', [
  95.             'ticket' => $ticket,
  96.             'replyForm' => $replyForm $replyForm->createView() : null,
  97.         ]);
  98.     }
  99.     
  100.     #[Route('/{id}/edit'name'user_ticket_edit'methods: ['GET''POST'])]
  101.     public function edit(Request $requestTicket $ticketEntityManagerInterface $entityManager): Response
  102.     {
  103.         // Überprüfen, ob das Ticket Antworten hat
  104.         if (count($ticket->getReplies()) > 0) {
  105.             $this->addFlash('danger''Dieses Ticket kann nicht bearbeitet werden, da bereits eine Antwort vorhanden ist.');
  106.             return $this->redirectToRoute('user_ticket_show', ['id' => $ticket->getId()]);
  107.         }
  108.         // Nur erlauben, dass der Besitzer des Tickets es bearbeiten kann
  109.         if ($ticket->getUser() !== $this->getUser()) {
  110.             throw $this->createAccessDeniedException('Du kannst nur deine eigenen Tickets bearbeiten.');
  111.         }
  112.         // Ticket bearbeiten
  113.         $form $this->createForm(TicketType::class, $ticket);
  114.         $form->handleRequest($request);
  115.         if ($form->isSubmitted() && $form->isValid()) {
  116.             $entityManager->flush();
  117.             $this->addFlash('success''Ticket erfolgreich bearbeitet.');
  118.             return $this->redirectToRoute('user_ticket_index', [], Response::HTTP_SEE_OTHER);
  119.         }
  120.         return $this->renderForm('user/ticket_edit.html.twig', [
  121.             'ticket' => $ticket,
  122.             'form' => $form,
  123.         ]);
  124.     }
  125. }