Browse Source

ajout messages privés

Clément K 4 năm trước cách đây
mục cha
commit
a104ce3bae

+ 53 - 0
src/Controller/PrivateDiscussionController.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace App\Controller;
+
+use App\Entity\PrivateDiscussion;
+use App\Entity\PrivateMessage;
+use App\Entity\User;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Annotation\Route;
+
+class PrivateDiscussionController extends AbstractController
+{
+    #[Route('/private/discussion/{id}', name: 'private_discussion')]
+    public function index($id, Request $request): Response
+    {
+        $session = $this->get('session');
+
+        if (null === $session->get('user')) {
+            return  $this->redirectToRoute('home');
+        }
+
+        $formBuilder = $this->createFormBuilder();
+        $formBuilder->add('text', \Symfony\Component\Form\Extension\Core\Type\TextType::class)
+            ->setAction($this->generateUrl('private_discussion', ['id' => $id]));
+        $form = $formBuilder->getForm();
+
+        $em = $this->getDoctrine()->getManager();
+        $repo_pd = $em->getRepository(PrivateDiscussion::class);
+        $pd = $repo_pd->find($id);
+        $messages = $pd->getPrivateMessages();
+
+        if ($request->getMethod() == 'POST') {
+            $form->handleRequest($request);
+            if($form->isValid()) {
+                $user = $em->getRepository(User::class)->findOneBy(array('username' => $session->get('user')));
+                $pm = new PrivateMessage();
+                $pm->setText($form->get('text')->getData());
+                $pm->setSender($user);
+                $pm->setPrivateDiscussion($pd);
+                $em->persist($pm);
+                $em->flush();
+            }
+        }
+
+        return $this->render('private_discussion/index.html.twig', [
+            'controller_name' => 'PrivateDiscussionController',
+            'messages' => $messages,
+            'form' => $form->createView()
+        ]);
+    }
+}

+ 16 - 4
src/Controller/PrivateDiscussionsController.php

@@ -26,16 +26,27 @@ class PrivateDiscussionsController extends AbstractController
             ->setAction($this->generateUrl('private_discussions'));
         $form = $formBuilder->getForm();
 
+        $em = $this->getDoctrine()->getManager();
+        $repo_user = $em->getRepository(User::class);
+        $user = $repo_user->findOneBy(array('username' => $session->get("user")));
+        $private_discussions = array();
+        foreach ($user->getPrivateDiscussions() as $pd) {
+            $participants = $pd->getParticipants();
+            if ($participants[0] == $user) {
+                $participant = $participants[1];
+            } else {
+                $participant = $participants[0];
+            }
+            array_push($private_discussions, array($pd->getId() => $participant->getUsername()));
+        }
+
         if ($request->getMethod() == 'POST') {
             $form->handleRequest($request);
             if ($form->isValid()) {
-                $em = $this->getDoctrine()->getManager();
-                $repo_user = $em->getRepository(User::class);
                 $recept = $repo_user->findOneBy(array('username' => $form->get('username')->getData()));
                 if (!$recept) {
                     return new Response("Profil non existant");
                 }
-                $user = $repo_user->findOneBy(array('username' => $session->get("user")));
                 $disc_found = false;
                 foreach ($user->getPrivateDiscussions() as $disc) {
                     if(in_array($recept, $disc->getParticipants()->getValues())) {
@@ -61,7 +72,8 @@ class PrivateDiscussionsController extends AbstractController
 
         return $this->render('private_discussions/index.html.twig', [
             'controller_name' => 'PrivateDiscussionsController',
-            'form' => $form->createView()
+            'form' => $form->createView(),
+            'private_discussions' => $private_discussions
         ]);
     }
 }

+ 18 - 0
templates/private_discussion/index.html.twig

@@ -0,0 +1,18 @@
+{% extends 'base.html.twig' %}
+
+{% block title %}Hello PrivateDiscussionController!{% endblock %}
+
+{% block body %}
+    <ul>
+    {% for msg in messages %}
+        <li>
+            <div>{{ msg.sender.getUsername() }}</div>
+            <div>{{ msg.text }}</div>
+        </li>
+    {% endfor %}
+    </ul>
+    {{ form_start(form) }}
+    {{ form_errors(form) }}
+    {{ form_widget(form) }} <input type="submit">
+    {{ form_end(form) }}
+{% endblock %}

+ 11 - 0
templates/private_discussions/index.html.twig

@@ -7,4 +7,15 @@
     {{ form_errors(form) }}
     {{ form_widget(form) }} <input type="submit">
     {{ form_end(form) }}
+    <ul>
+    {% for pd in private_discussions %}
+        {% for key, value in pd %}
+            <li>
+                <div>
+                <a href={{ path('private_discussion', {'id': key }) }}>Discussion privée avec </a><a href={{ path('profile', {'username': value }) }}>@{{ value }}</a>
+                </div>
+            </li>
+        {% endfor %}
+    {% endfor %}
+    </ul>
 {% endblock %}