fix:messages

This commit is contained in:
allavenavr 2024-12-20 09:06:54 +01:00
parent 3be6867e5e
commit f66fb6028d
11 changed files with 163 additions and 108 deletions

View File

@ -43,11 +43,10 @@ final class AnnouncementController extends AbstractController
if (in_array('ROLE_INTERN', $user->getRoles())) 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/index.html.twig', [
return $this->render('announcement/list.html.twig', [
'announcements' => $announcements, 'announcements' => $announcements,
'showNonValidated' => $request->query->get('show_non_validated', false), 'showNonValidated' => $request->query->get('show_non_validated', false),
]); ]);

View File

@ -3,8 +3,10 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Message; use App\Entity\Message;
use App\Entity\UserApp;
use App\Form\MessageType; use App\Form\MessageType;
use App\Repository\MessageRepository; use App\Repository\MessageRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -17,31 +19,56 @@ final class MessageController extends AbstractController
#[Route(name: 'app_message_index', methods: ['GET'])] #[Route(name: 'app_message_index', methods: ['GET'])]
public function index(MessageRepository $messageRepository): Response public function index(MessageRepository $messageRepository): Response
{ {
$user = $this->getUser();
$messages = $messageRepository->findByUser($user);
return $this->render('message/index.html.twig', [ return $this->render('message/index.html.twig', [
'messages' => $messageRepository->findAll(), 'messages' => $messages,
]); ]);
} }
#[Route('/new', name: 'app_message_new', methods: ['GET', 'POST'])] #[Route('/conversation/{id}', name: 'app_message_conversation', methods: ['GET'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response 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(); $message = new Message();
if ($receiverId) {
$receiver = $userRepository->find($receiverId);
if ($receiver) {
$message->setReceiver($receiver);
}
}
$form = $this->createForm(MessageType::class, $message); $form = $this->createForm(MessageType::class, $message);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$message->setSendingDate(new \DateTime()); $message->setSendingDate(new \DateTime());
$message->setSender($this->getUser()); $message->setSender($this->getUser());
$entityManager->persist($message); $entityManager->persist($message);
$entityManager->flush(); $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', [ return $this->render('message/new.html.twig', [
'message' => $message,
'form' => $form, 'form' => $form,
]); ]);
} }
@ -83,4 +110,6 @@ final class MessageController extends AbstractController
return $this->redirectToRoute('app_message_index', [], Response::HTTP_SEE_OTHER); return $this->redirectToRoute('app_message_index', [], Response::HTTP_SEE_OTHER);
} }
} }

View File

@ -46,6 +46,3 @@ class ProfileController extends AbstractController
]); ]);
} }
} }

View File

@ -186,18 +186,6 @@ class UserApp implements UserInterface, PasswordAuthenticatedUserInterface
return $this; return $this;
} }
public function isVerified(): bool
{
return $this->isVerified;
}
public function setVerified(bool $isVerified): static
{
$this->isVerified = $isVerified;
return $this;
}
} }

View File

@ -3,6 +3,7 @@
namespace App\Repository; namespace App\Repository;
use App\Entity\Message; use App\Entity\Message;
use App\Entity\UserApp;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
@ -16,28 +17,24 @@ class MessageRepository extends ServiceEntityRepository
parent::__construct($registry, Message::class); parent::__construct($registry, Message::class);
} }
// /** public function findByUser(UserApp $user): array
// * @return Message[] Returns an array of Message objects {
// */ return $this->createQueryBuilder('m')
// public function findByExampleField($value): array ->where('m.sender = :user OR m.receiver = :user')
// { ->setParameter('user', $user)
// return $this->createQueryBuilder('m') ->orderBy('m.sendingDate', 'ASC')
// ->andWhere('m.exampleField = :val') ->getQuery()
// ->setParameter('val', $value) ->getResult();
// ->orderBy('m.id', 'ASC') }
// ->setMaxResults(10)
// ->getQuery() public function findByConversation(UserApp $user1, UserApp $user2): array
// ->getResult() {
// ; return $this->createQueryBuilder('m')
// } ->where('(m.sender = :user1 AND m.receiver = :user2) OR (m.sender = :user2 AND m.receiver = :user1)')
->setParameter('user1', $user1)
// public function findOneBySomeField($value): ?Message ->setParameter('user2', $user2)
// { ->orderBy('m.sendingDate', 'ASC')
// return $this->createQueryBuilder('m') ->getQuery()
// ->andWhere('m.exampleField = :val') ->getResult();
// ->setParameter('val', $value) }
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
} }

View File

@ -0,0 +1,29 @@
{% extends 'base.html.twig' %}
{% block title %}Conversation{% endblock %}
{% block body %}
<div class="container mx-auto p-6">
<h1 class="text-3xl font-bold mb-6 text-center">Conversation avec {{ receiver.nickname }}</h1>
<div class="conversation bg-gray-100 p-6 rounded-lg shadow-lg">
{% for msg in conversation %}
<div class="message {% if msg.sender == app.user %}bg-teal-500 text-white self-end{% else %}bg-gray-200 text-gray-700 self-start{% endif %} p-4 my-2 rounded-lg max-w-xs">
<p class="font-semibold">{{ msg.sender.nickname }} :</p>
<p class="mt-2">{{ msg.content }}</p>
<p class="text-xs text-gray-500 mt-2">{{ msg.sendingDate|date('Y-m-d H:i:s') }}</p>
</div>
{% endfor %}
</div>
<!-- Bouton pour répondre -->
<div class="mt-4 text-center">
<a href="{{ path('app_message_new', {'receiverId': receiverId}) }}" class="bg-teal-500 text-white px-6 py-3 rounded-md hover:bg-teal-600">
Répondre
</a>
</div>
<a href="{{ path('app_message_index') }}" class="text-teal-500 hover:text-teal-700 text-lg">
<i class="fas fa-arrow-left"></i> Retour à la liste des Messages
</a>
</div>
{% endblock %}

View File

@ -1,37 +1,39 @@
{% extends 'base.html.twig' %} {% extends 'base.html.twig' %}
{% block title %}Message index{% endblock %} {% block title %}Boîte de Réception{% endblock %}
{% block body %} {% block body %}
<h1>Message index</h1> <div class="container mx-auto p-6">
<h1 class="text-3xl font-bold mb-6 text-center">Boîte de Réception</h1>
<table class="table"> <!-- Bouton pour écrire un nouveau message -->
<thead> <div class="mb-6 text-center">
<tr> <a href="{{ path('app_message_new', {'receiverId': app.user.id}) }}" class="bg-teal-500 text-white px-6 py-3 rounded-md hover:bg-teal-600">
<th>Id</th> Écrire un Nouveau Message
<th>Content</th> </a>
<th>SendingDate</th>
<th>actions</th> </div>
</tr>
</thead> <!-- Liste des messages reçus -->
<tbody> <div class="max-w-4xl mx-auto">
{% for message in messages %} {% for message in messages %}
<tr> {% if message.receiver == app.user or message.sender == app.user %}
<td>{{ message.id }}</td> <div class="bg-white p-6 rounded-lg shadow mb-4">
<td>{{ message.content }}</td> <div class="flex justify-between items-center">
<td>{{ message.sendingDate ? message.sendingDate|date('Y-m-d H:i:s') : '' }}</td> <div>
<td> <h2 class="text-xl font-semibold">De : {{ message.sender.nickname }}</h2>
<a href="{{ path('app_message_show', {'id': message.id}) }}">show</a> <p class="text-gray-600">{{ message.sendingDate|date('d/m/Y H:i') }}</p>
<a href="{{ path('app_message_edit', {'id': message.id}) }}">edit</a> </div>
</td> <a href="{{ path('app_message_conversation', { id: message.id }) }}" class="bg-teal-500 text-white px-4 py-2 rounded hover:bg-teal-600">
</tr> Voir la Conversation
</a>
</div>
<p class="mt-4 text-gray-700">{{ message.content }}</p>
</div>
{% endif %}
{% else %} {% else %}
<tr> <p class="text-center text-gray-500">Vous n'avez reçu aucun message.</p>
<td colspan="4">no records found</td>
</tr>
{% endfor %} {% endfor %}
</tbody> </div>
</table> </div>
<a href="{{ path('app_message_new') }}">Create new</a>
{% endblock %} {% endblock %}

View File

@ -1,11 +1,27 @@
{% extends 'base.html.twig' %} {% extends 'base.html.twig' %}
{% block title %}New Message{% endblock %} {% block title %}Nouveau Message{% endblock %}
{% block body %} {% block body %}
<h1>Create new Message</h1> <div class="container mx-auto p-6">
<h1 class="text-3xl font-bold mb-6 text-center">Écrire un Nouveau Message</h1>
{{ include('message/_form.html.twig') }} <div class="max-w-lg mx-auto bg-white p-6 rounded-lg shadow">
{{ form_start(form) }}
<div class="mb-4">
<label class="block text-gray-700 font-medium mb-2">Destinataire</label>
{{ form_widget(form.receiver, {'attr': {'class': 'w-full p-3 border rounded'}}) }}
</div>
<a href="{{ path('app_message_index') }}">back to list</a> <div class="mb-4">
<label class="block text-gray-700 font-medium mb-2">Message</label>
{{ form_widget(form.content, {'attr': {'class': 'w-full p-3 border rounded', 'rows': 6}}) }}
</div>
<button type="submit" class="w-full bg-teal-500 text-white px-4 py-2 rounded hover:bg-teal-600">
Envoyer
</button>
{{ form_end(form) }}
</div>
</div>
{% endblock %} {% endblock %}

View File

@ -1,30 +1,28 @@
{% extends 'base.html.twig' %} {% extends 'base.html.twig' %}
{% block title %}Message{% endblock %} {% block title %}Voir le Message{% endblock %}
{% block body %} {% block body %}
<h1>Message</h1> <div class="container mx-auto p-6">
<h1 class="text-3xl font-bold mb-6 text-center">Message de {{ message.sender.nickname }}</h1>
<table class="table"> <div class="max-w-lg mx-auto bg-white p-6 rounded-lg shadow">
<tbody> <div class="mb-4">
<tr> <p class="text-gray-700"><strong>De :</strong> {{ message.sender.nickname }}</p>
<th>Id</th> <p class="text-gray-700"><strong>Reçu le :</strong> {{ message.sendingDate|date('d/m/Y H:i') }}</p>
<td>{{ message.id }}</td> </div>
</tr>
<tr>
<th>Content</th>
<td>{{ message.content }}</td>
</tr>
<tr>
<th>SendingDate</th>
<td>{{ message.sendingDate ? message.sendingDate|date('Y-m-d H:i:s') : '' }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('app_message_index') }}">back to list</a> <div class="bg-gray-100 p-4 rounded mb-6">
<p class="text-gray-800 whitespace-pre-line">{{ message.content }}</p>
</div>
<a href="{{ path('app_message_edit', {'id': message.id}) }}">edit</a> <!-- Bouton pour répondre -->
<div class="text-center">
{{ include('message/_delete_form.html.twig') }} <a href="{{ path('app_message_new', {'receiver': message.sender.id}) }}"
class="bg-teal-500 text-white px-6 py-2 rounded hover:bg-teal-600">
Répondre
</a>
</div>
</div>
</div>
{% endblock %} {% endblock %}