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 %} +
Id | -Content | -SendingDate | -actions | -
---|---|---|---|
{{ message.id }} | -{{ message.content }} | -{{ message.sendingDate ? message.sendingDate|date('Y-m-d H:i:s') : '' }} | -- show - edit - | -
no records found | -
{{ message.sendingDate|date('d/m/Y H:i') }}
+{{ message.content }}
+Vous n'avez reçu aucun message.
+ {% endfor %} +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') }}
+{{ message.content }}
+