Browse Source

quand mentionné --> affichage sur flux du mentionné

Clément K 4 years ago
parent
commit
7eb5215784

+ 7 - 0
src/Controller/HomeController.php

@@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpFoundation\Session\Session;
 use Symfony\Component\Routing\Annotation\Route;
+use Symfony\Component\Validator\Constraints\Date;
 
 class HomeController extends AbstractController
 {
@@ -84,8 +85,14 @@ class HomeController extends AbstractController
         $em = $this->getDoctrine()->getManager();
         $repository_profile = $em->getRepository(User::class);
         $profile = $repository_profile->findOneBy(array('username' => $session->get('user')));
+        $mentioned_messages = $profile->getMentionedMessages();
         $subscriptions = $profile->getSubscriptions();
         $sub_msg = array();
+        foreach ($mentioned_messages as $msg_m) {
+            if (!in_array($msg_m->getSender(), $subscriptions->getValues())) {
+                array_push($sub_msg, $msg_m);
+            }
+        }
         foreach ($profile->getMessages() as $msg_u) {
             array_push($sub_msg, $msg_u);
         }

+ 1 - 1
src/Entity/Message.php

@@ -31,7 +31,7 @@ class Message
     private $sender;
 
     /**
-     * @ORM\ManyToMany(targetEntity=User::class)
+     * @ORM\ManyToMany(targetEntity=User::class, inversedBy="mentionedMessages")
      */
     private $mentions;
 

+ 33 - 0
src/Entity/User.php

@@ -63,12 +63,18 @@ class User implements UserInterface
      */
     private $description;
 
+    /**
+     * @ORM\ManyToMany(targetEntity=Message::class, mappedBy="mentions")
+     */
+    private $mentionedMessages;
+
     public function __construct()
     {
         $this->blockedUsers = new ArrayCollection();
         $this->messages = new ArrayCollection();
         $this->followers = new ArrayCollection();
         $this->subscriptions = new ArrayCollection();
+        $this->mentionedMessages = new ArrayCollection();
     }
 
     public function getId(): ?int
@@ -240,4 +246,31 @@ class User implements UserInterface
     {
         // TODO: Implement eraseCredentials() method.
     }
+
+    /**
+     * @return Collection|Message[]
+     */
+    public function getMentionedMessages(): Collection
+    {
+        return $this->mentionedMessages;
+    }
+
+    public function addMentionedMessage(Message $mentionedMessage): self
+    {
+        if (!$this->mentionedMessages->contains($mentionedMessage)) {
+            $this->mentionedMessages[] = $mentionedMessage;
+            $mentionedMessage->addMentiotest($this);
+        }
+
+        return $this;
+    }
+
+    public function removeMentionedMessage(Message $mentionedMessage): self
+    {
+        if ($this->mentionedMessages->removeElement($mentionedMessage)) {
+            $mentionedMessage->removeMentiotest($this);
+        }
+
+        return $this;
+    }
 }

+ 2 - 2
templates/profile/index.html.twig

@@ -44,8 +44,8 @@ Bonjour {{sessionUser}}, <a href="{{ path('disconnect') }}">Se déconnecter</a>
     <ul>
         {% for msg in messages %}
         <li>
-            <div>{{ msg.sender.getUsername() }}</div>
-            <div>{{ msg.text }}</div>
+            <a href={{ path('profile', {'username': msg.sender.getUsername() }) }}>{{ msg.sender.getUsername() }}</a>
+            <div>{{ msg.text | striptags('<a>') | raw }}</div>
         </li>
         {% endfor %}
     </ul>