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()))
{
$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),
]);

View File

@ -80,4 +80,4 @@ final class FAQController extends AbstractController
return $this->redirectToRoute('app_faq_index', [], Response::HTTP_SEE_OTHER);
}
}
}

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

View File

@ -45,7 +45,4 @@ class ProfileController extends AbstractController
'form' => $form,
]);
}
}
}

View File

@ -58,4 +58,4 @@ final class UserAppController extends AbstractController
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
}
}
}

View File

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

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,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();
}
}

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' %}
{% 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>
{% endblock %}
<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') }}
{% endblock %}
<!-- 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 %}