diff --git a/src/Controller/MessageController.php b/src/Controller/MessageController.php index 55eb5ba..a722587 100644 --- a/src/Controller/MessageController.php +++ b/src/Controller/MessageController.php @@ -3,8 +3,10 @@ namespace App\Controller; use App\Entity\Message; +use App\Entity\UserApp; use App\Form\MessageType; use App\Repository\MessageRepository; +use App\Repository\UserRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -17,31 +19,58 @@ final class MessageController extends AbstractController #[Route(name: 'app_message_index', methods: ['GET'])] public function index(MessageRepository $messageRepository): Response { + $user = $this->getUser(); + $messages = $messageRepository->findByUser($user); // Recherche de tous les messages reçus ou envoyés par l'utilisateur + return $this->render('message/index.html.twig', [ - 'messages' => $messageRepository->findAll(), + 'messages' => $messages, ]); } - #[Route('/new', name: 'app_message_new', methods: ['GET', 'POST'])] - public function new(Request $request, EntityManagerInterface $entityManager): Response + #[Route('/conversation/{id}', name: 'app_message_conversation', methods: ['GET'])] + public function conversation(Message $message, MessageRepository $messageRepository): Response + { + $user = $this->getUser(); + $otherUser = $message->getSender() === $user ? $message->getReceiver() : $message->getSender(); + + // Récupérer tous les messages échangés entre l'utilisateur actuel et l'autre participant + $conversation = $messageRepository->findByConversation($user, $otherUser); + + return $this->render('message/conversation.html.twig', [ + 'conversation' => $conversation, + 'receiverId' => $otherUser->getId(), + 'receiver' => $otherUser, + ]); + } + + + #[Route('/new/{receiverId}', name: 'app_message_new', methods: ['GET', 'POST'])] + public function new(Request $request, EntityManagerInterface $entityManager, ?int $receiverId = null, UserRepository $userRepository): Response { $message = new Message(); + + // Pré-remplir le destinataire si disponible + if ($receiverId) { + $receiver = $userRepository->find($receiverId); + if ($receiver) { + $message->setReceiver($receiver); + } + } + $form = $this->createForm(MessageType::class, $message); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $message->setSendingDate(new \DateTime()); $message->setSender($this->getUser()); $entityManager->persist($message); $entityManager->flush(); - return $this->redirectToRoute('app_message_index', [], Response::HTTP_SEE_OTHER); + return $this->redirectToRoute('app_message_index'); } return $this->render('message/new.html.twig', [ - 'message' => $message, 'form' => $form, ]); } @@ -83,4 +112,6 @@ final class MessageController extends AbstractController return $this->redirectToRoute('app_message_index', [], Response::HTTP_SEE_OTHER); } + + } diff --git a/src/Repository/MessageRepository.php b/src/Repository/MessageRepository.php index e7f1849..33f1713 100644 --- a/src/Repository/MessageRepository.php +++ b/src/Repository/MessageRepository.php @@ -3,6 +3,7 @@ namespace App\Repository; use App\Entity\Message; +use App\Entity\UserApp; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; @@ -16,6 +17,30 @@ class MessageRepository extends ServiceEntityRepository parent::__construct($registry, Message::class); } + public function findByUser(UserApp $user): array + { + return $this->createQueryBuilder('m') + ->where('m.sender = :user OR m.receiver = :user') + ->setParameter('user', $user) + ->orderBy('m.sendingDate', 'ASC') + ->getQuery() + ->getResult(); + } + + public function findByConversation(UserApp $user1, UserApp $user2): array + { + return $this->createQueryBuilder('m') + ->where('(m.sender = :user1 AND m.receiver = :user2) OR (m.sender = :user2 AND m.receiver = :user1)') + ->setParameter('user1', $user1) + ->setParameter('user2', $user2) + ->orderBy('m.sendingDate', 'ASC') + ->getQuery() + ->getResult(); + } + + + + // /** // * @return Message[] Returns an array of Message objects // */ diff --git a/templates/base.html.twig b/templates/base.html.twig index e6b7994..15387a1 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -73,6 +73,7 @@
Téléphone: +33 1 23 45 67 89
+