From f66fb6028d1c51d510ade4075f8dc2d7bbc49894 Mon Sep 17 00:00:00 2001 From: allavenavr Date: Fri, 20 Dec 2024 09:06:54 +0100 Subject: [PATCH] fix:messages --- src/Controller/AnnouncementController.php | 5 +- src/Controller/FAQController.php | 2 +- src/Controller/MessageController.php | 41 ++++++++++++--- src/Controller/ProfileController.php | 5 +- src/Controller/UserAppController.php | 2 +- src/Entity/UserApp.php | 12 ----- src/Repository/MessageRepository.php | 43 ++++++++-------- templates/message/conversation.html.twig | 29 +++++++++++ templates/message/index.html.twig | 62 ++++++++++++----------- templates/message/new.html.twig | 26 ++++++++-- templates/message/show.html.twig | 44 ++++++++-------- 11 files changed, 163 insertions(+), 108 deletions(-) create mode 100644 templates/message/conversation.html.twig diff --git a/src/Controller/AnnouncementController.php b/src/Controller/AnnouncementController.php index dcfe11b..f9f0243 100644 --- a/src/Controller/AnnouncementController.php +++ b/src/Controller/AnnouncementController.php @@ -43,11 +43,10 @@ final class AnnouncementController extends AbstractController if (in_array('ROLE_INTERN', $user->getRoles())) { - $announcements = $announcementRepository->findBy(['status' => 'verified']); + $announcements = $announcementRepository->findBy(['status' => 'Verified']); } - // Rendre la vue avec les annonces - return $this->render('announcement/list.html.twig', [ + return $this->render('announcement/index.html.twig', [ 'announcements' => $announcements, 'showNonValidated' => $request->query->get('show_non_validated', false), ]); diff --git a/src/Controller/FAQController.php b/src/Controller/FAQController.php index 2a3aa50..41e5d74 100644 --- a/src/Controller/FAQController.php +++ b/src/Controller/FAQController.php @@ -80,4 +80,4 @@ final class FAQController extends AbstractController return $this->redirectToRoute('app_faq_index', [], Response::HTTP_SEE_OTHER); } -} +} \ No newline at end of file diff --git a/src/Controller/MessageController.php b/src/Controller/MessageController.php index 55eb5ba..af9633a 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,56 @@ 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); + 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(); + + $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(); + + 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 +110,6 @@ final class MessageController extends AbstractController return $this->redirectToRoute('app_message_index', [], Response::HTTP_SEE_OTHER); } + + } diff --git a/src/Controller/ProfileController.php b/src/Controller/ProfileController.php index abacea9..5781c19 100644 --- a/src/Controller/ProfileController.php +++ b/src/Controller/ProfileController.php @@ -45,7 +45,4 @@ class ProfileController extends AbstractController 'form' => $form, ]); } -} - - - +} \ No newline at end of file diff --git a/src/Controller/UserAppController.php b/src/Controller/UserAppController.php index 87635ae..ad55bd2 100644 --- a/src/Controller/UserAppController.php +++ b/src/Controller/UserAppController.php @@ -58,4 +58,4 @@ final class UserAppController extends AbstractController return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER); } -} +} \ No newline at end of file diff --git a/src/Entity/UserApp.php b/src/Entity/UserApp.php index 517c83e..0ccf691 100644 --- a/src/Entity/UserApp.php +++ b/src/Entity/UserApp.php @@ -186,18 +186,6 @@ class UserApp implements UserInterface, PasswordAuthenticatedUserInterface return $this; } - - public function isVerified(): bool - { - return $this->isVerified; - } - - public function setVerified(bool $isVerified): static - { - $this->isVerified = $isVerified; - - return $this; - } } diff --git a/src/Repository/MessageRepository.php b/src/Repository/MessageRepository.php index e7f1849..fd3e6b3 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,28 +17,24 @@ class MessageRepository extends ServiceEntityRepository parent::__construct($registry, Message::class); } - // /** - // * @return Message[] Returns an array of Message objects - // */ - // public function findByExampleField($value): array - // { - // return $this->createQueryBuilder('m') - // ->andWhere('m.exampleField = :val') - // ->setParameter('val', $value) - // ->orderBy('m.id', 'ASC') - // ->setMaxResults(10) - // ->getQuery() - // ->getResult() - // ; - // } + 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 findOneBySomeField($value): ?Message - // { - // return $this->createQueryBuilder('m') - // ->andWhere('m.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } + 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(); + } } diff --git a/templates/message/conversation.html.twig b/templates/message/conversation.html.twig new file mode 100644 index 0000000..f2fecaf --- /dev/null +++ b/templates/message/conversation.html.twig @@ -0,0 +1,29 @@ +{% 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 %} +
+ + + + + Retour à la liste des Messages + +
+{% 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..2a15b2d 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 -{% endblock %} +
+ + {{ form_widget(form.content, {'attr': {'class': 'w-full p-3 border rounded', 'rows': 6}}) }} +
+ + + {{ form_end(form) }} +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/message/show.html.twig b/templates/message/show.html.twig index 575f7ab..490849f 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 %} + + +
+
+{% endblock %} \ No newline at end of file