messagerio de jannero TATATATADATA

This commit is contained in:
bourgoino 2024-12-12 16:48:06 +01:00
parent d6d0a9bbad
commit c1a79f7621
7 changed files with 161 additions and 62 deletions

View File

@ -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);
}
}

View File

@ -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
// */

View File

@ -73,6 +73,7 @@
<p>Téléphone: +33 1 23 45 67 89</p>
</div>
</div>
</footer>
</body>
</html>

View File

@ -0,0 +1,26 @@
{% 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>
</div>
{% endblock %}

View File

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

View File

@ -1,11 +1,27 @@
{% extends 'base.html.twig' %}
{% block title %}New Message{% endblock %}
{% block title %}Nouveau Message{% endblock %}
{% 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 %}

View File

@ -1,30 +1,28 @@
{% extends 'base.html.twig' %}
{% block title %}Message{% endblock %}
{% block title %}Voir le Message{% endblock %}
{% 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">
<tbody>
<tr>
<th>Id</th>
<td>{{ message.id }}</td>
</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>
<div class="max-w-lg mx-auto bg-white p-6 rounded-lg shadow">
<div class="mb-4">
<p class="text-gray-700"><strong>De :</strong> {{ message.sender.nickname }}</p>
<p class="text-gray-700"><strong>Reçu le :</strong> {{ message.sendingDate|date('d/m/Y H:i') }}</p>
</div>
<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>
{{ include('message/_delete_form.html.twig') }}
<!-- Bouton pour répondre -->
<div class="text-center">
<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 %}