From c1a79f7621dc02410b7fa380ec6155c0588d34e1 Mon Sep 17 00:00:00 2001 From: bourgoino Date: Thu, 12 Dec 2024 16:48:06 +0100 Subject: [PATCH] messagerio de jannero TATATATADATA --- src/Controller/MessageController.php | 43 +++++++++++++--- src/Repository/MessageRepository.php | 25 ++++++++++ templates/base.html.twig | 1 + templates/message/conversation.html.twig | 26 ++++++++++ templates/message/index.html.twig | 62 ++++++++++++------------ templates/message/new.html.twig | 24 +++++++-- templates/message/show.html.twig | 42 ++++++++-------- 7 files changed, 161 insertions(+), 62 deletions(-) create mode 100644 templates/message/conversation.html.twig 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

+ diff --git a/templates/message/conversation.html.twig b/templates/message/conversation.html.twig new file mode 100644 index 0000000..98c8cf4 --- /dev/null +++ b/templates/message/conversation.html.twig @@ -0,0 +1,26 @@ +{% extends 'base.html.twig' %} + +{% block title %}Conversation{% endblock %} + +{% block body %} +
+

Conversation avec {{ receiver.nickname }}

+ +
+ {% for msg in conversation %} +
+

{{ msg.sender.nickname }} :

+

{{ msg.content }}

+

{{ msg.sendingDate|date('Y-m-d H:i:s') }}

+
+ {% endfor %} +
+ + + +
+{% endblock %} diff --git a/templates/message/index.html.twig b/templates/message/index.html.twig index 0aff18b..4c7da31 100644 --- a/templates/message/index.html.twig +++ b/templates/message/index.html.twig @@ -1,37 +1,39 @@ {% extends 'base.html.twig' %} -{% block title %}Message index{% endblock %} +{% block title %}Boîte de Réception{% endblock %} {% block body %} -

Message index

+
+

Boîte de Réception

- - - - - - - - - - - {% for message in messages %} - - - - - - - {% else %} - - - - {% endfor %} - -
IdContentSendingDateactions
{{ message.id }}{{ message.content }}{{ message.sendingDate ? message.sendingDate|date('Y-m-d H:i:s') : '' }} - show - edit -
no records found
+ + + + +
+ {% for message in messages %} + {% if message.receiver == app.user or message.sender == app.user %} +
+
+
+

De : {{ message.sender.nickname }}

+

{{ message.sendingDate|date('d/m/Y H:i') }}

+
+ + Voir la Conversation + +
+

{{ message.content }}

+
+ {% endif %} + {% else %} +

Vous n'avez reçu aucun message.

+ {% endfor %} +
+
{% endblock %} diff --git a/templates/message/new.html.twig b/templates/message/new.html.twig index 5ee1626..6cb60da 100644 --- a/templates/message/new.html.twig +++ b/templates/message/new.html.twig @@ -1,11 +1,27 @@ {% extends 'base.html.twig' %} -{% block title %}New Message{% endblock %} +{% block title %}Nouveau Message{% endblock %} {% block body %} -

Create new Message

+
+

Écrire un Nouveau Message

- {{ include('message/_form.html.twig') }} +
+ {{ form_start(form) }} +
+ + {{ form_widget(form.receiver, {'attr': {'class': 'w-full p-3 border rounded'}}) }} +
- back to list +
+ + {{ form_widget(form.content, {'attr': {'class': 'w-full p-3 border rounded', 'rows': 6}}) }} +
+ + + {{ form_end(form) }} +
+
{% endblock %} diff --git a/templates/message/show.html.twig b/templates/message/show.html.twig index 575f7ab..df1a969 100644 --- a/templates/message/show.html.twig +++ b/templates/message/show.html.twig @@ -1,30 +1,28 @@ {% extends 'base.html.twig' %} -{% block title %}Message{% endblock %} +{% block title %}Voir le Message{% endblock %} {% block body %} -

Message

+
+

Message de {{ message.sender.nickname }}

- - - - - - - - - - - - - - - -
Id{{ message.id }}
Content{{ message.content }}
SendingDate{{ message.sendingDate ? message.sendingDate|date('Y-m-d H:i:s') : '' }}
+
+
+

De : {{ message.sender.nickname }}

+

Reçu le : {{ message.sendingDate|date('d/m/Y H:i') }}

+
- back to list +
+

{{ message.content }}

+
- edit - - {{ include('message/_delete_form.html.twig') }} + + +
+
{% endblock %}