SPAETER NATHAN 4 жил өмнө
parent
commit
aefd2eafda

+ 102 - 0
src/Controller/MessageController.php

@@ -0,0 +1,102 @@
+<?php
+
+namespace App\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Annotation\Route;
+use App\Entity\Hashtag;
+use App\Entity\Message;
+use App\Entity\Retweets;
+use App\Entity\User;
+use App\Form\MessageType;
+use Doctrine\DBAL\Types\TextType;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\Validator\Constraints\Date;
+
+class MessageController extends AbstractController
+{
+    private function cmp_array($a, $b): int
+    {
+        if ($a->getDate() == $b->getDate()) {
+            return 0;
+        }
+        return ($a->getDate() > $b->getDate()) ? -1 : 1;
+    }
+
+    #[Route('/message/{msgId}', name: 'message')]
+    public function index(Request $request,$msgId): Response
+    {
+        $session = $this->get('session');
+        $em = $this->getDoctrine()->getManager();
+        $repository_message = $em->getRepository(Message::class);
+
+        if (null === $session->get('user')) {
+            return  $this->redirectToRoute('login');
+        }
+
+        $options = array("action" => $this->generateUrl('message', array('msgId' => $msgId)));
+        $form = $this->createForm(MessageType::class, null, $options);
+
+        if ($request->getMethod() == 'POST') {
+            $form->handleRequest($request);
+            if ($form->isValid()) {
+                $session = $this->get('session');
+                $em = $this->getDoctrine()->getManager();
+                $repository_profile = $em->getRepository(User::class);
+                $profile = $repository_profile->findOneBy(array('username' => $session->get('user')));
+                $message = new Message();
+                $text = $form->get('text')->getData();
+                $mentions = array();
+                $hashtags = array();
+                preg_match_all("~@([a-zA-Z0-9_]*)~", $text, $mentions);
+                preg_match_all("~#([a-zA-Z0-9_]*)~", $text, $hashtags);
+                for ($i = 0; $i < sizeof($mentions[0]); $i++) {
+                    $user = $repository_profile->findOneBy(array('username' => $mentions[1][$i]));
+                    if ($user) {
+                        $message->addMention($user);
+                        $mentions[1][$i] = "<a href='/profile/" . $mentions[1][$i] . "'>" . $mentions[0][$i] . "</a>";
+                        $mentions[0][$i] = "~" . $mentions[0][$i] . "~";
+                    } else {
+                        unset($mentions[1][$i]);
+                        unset($mentions[0][$i]);
+                    }
+                }
+                $repository_hashtag = $em->getRepository(Hashtag::class);
+                for ($i = 0; $i < sizeof($hashtags[0]); $i++) {
+                    $hashtag = $repository_hashtag->findOneBy(array('name' => $hashtags[1][$i]));
+                    if (!$hashtag) {
+                        $hashtag = new Hashtag();
+                        $hashtag->setName($hashtags[1][$i]);
+                    }
+                    $hashtag->addMessage($message);
+                    $hashtags[1][$i] = "<a href='/hashtag/" . $hashtags[1][$i] . "'>" . $hashtags[0][$i] . "</a>";
+                    $hashtags[0][$i] = "~" . $hashtags[0][$i] . "~";
+                    $em->persist($hashtag);
+                }
+                $text = preg_replace($mentions[0], $mentions[1], $text);
+                $message->setText(preg_replace($hashtags[0], $hashtags[1], $text));
+                $message->setSender($profile);
+                $message->setMessage($repository_message->find($msgId));
+                $message->setDate(new \DateTime("now"));
+                $em->persist($message);
+                $em->flush();
+            }
+        }
+
+        $message = $repository_message->find($msgId);
+
+        $parent = $message->getMessage();
+
+        $replies = $message->getReplies();
+
+        return $this->render('message/index.html.twig', [
+            'username' => $session->get('user'),
+            'msg' => $message,
+            'replies' => $replies,
+            'parent' => $parent,
+            'form' => $form->createView(),
+        ]);
+    }
+}

+ 53 - 0
src/Entity/Message.php

@@ -45,10 +45,21 @@ class Message
      */
     private $retweets;
 
+    /**
+     * @ORM\ManyToOne(targetEntity=Message::class, inversedBy="replies")
+     */
+    private $message;
+
+    /**
+     * @ORM\OneToMany(targetEntity=Message::class, mappedBy="message")
+     */
+    private $replies;
+
     public function __construct()
     {
         $this->mentions = new ArrayCollection();
         $this->retweets = new ArrayCollection();
+        $this->replies = new ArrayCollection();
     }
 
     public function getId(): ?int
@@ -149,4 +160,46 @@ class Message
 
         return $this;
     }
+
+    public function getMessage(): ?self
+    {
+        return $this->message;
+    }
+
+    public function setMessage(?self $message): self
+    {
+        $this->message = $message;
+
+        return $this;
+    }
+
+    /**
+     * @return Collection|self[]
+     */
+    public function getReplies(): Collection
+    {
+        return $this->replies;
+    }
+
+    public function addReply(self $reply): self
+    {
+        if (!$this->replies->contains($reply)) {
+            $this->replies[] = $reply;
+            $reply->setMessage($this);
+        }
+
+        return $this;
+    }
+
+    public function removeReply(self $reply): self
+    {
+        if ($this->replies->removeElement($reply)) {
+            // set the owning side to null (unless already changed)
+            if ($reply->getMessage() === $this) {
+                $reply->setMessage(null);
+            }
+        }
+
+        return $this;
+    }
 }

+ 1 - 0
templates/home/index.html.twig

@@ -20,6 +20,7 @@
 					</h5>
 					<h6 class="card-subtitle mb-2 text-muted">{{ msg.getDate()|date('H:i - d/m/Y') }}</h6>
 					<p class="card-text">{{ msg.text | striptags('<a>') | raw }}</p>
+					<a href="{{ path('message', {'msgId': msg.getId() }) }}" class="card-link">Répondre</a>
 					<a href="{{ path('rt_message', {'msgId': msg.getId() }) }}" class="card-link">RT</a>
 				</div>
 			</div>

+ 48 - 0
templates/message/index.html.twig

@@ -0,0 +1,48 @@
+{% extends 'base.html.twig' %}
+
+{% block title %}
+	Message
+{% endblock %}
+
+{% block body %}
+	<div class="d-flex flex-column align-items-center">
+		{% if parent %}
+			<div class="card" style="width: 75rem;">
+				<h5 class="card-title">
+					<a href="{{ path('message', {'msgId': parent.getId() }) }}">Afficher le message parent</a>
+				</h5>
+			</div>
+		{% endif %}
+		<div class="card" style="width: 75rem;">
+			<div class="card-body">
+				<h5 class="card-title">
+					<a href={{ path('profile', {'username': msg.sender.getUsername() } ) }}>{{ msg.sender.getUsername() }}</a>
+				</h5>
+				<h6 class="card-subtitle mb-2 text-muted">{{ msg.getDate()|date('H:i - d/m/Y') }}</h6>
+				<p class="card-text">{{ msg.text | striptags('<a>') | raw }}</p>
+				<a href="{{ path('message', {'msgId': msg.getId() }) }}" class="card-link">Répondre</a>
+				<a href="{{ path('rt_message', {'msgId': msg.getId() }) }}" class="card-link">RT</a>
+			</div>
+		</div>
+		<div class="card" style="width: 50rem;">
+			{{ form_start(form) }}
+			{{ form_errors(form) }}
+			{{ form_widget(form) }}
+			<button type="submit" class="btn btn-primary">Envoyer</button>
+			{{ form_end(form) }}
+		</div>
+		{% for msg in replies %}
+			<div class="card" style="width: 50rem;">
+				<div class="card-body">
+					<h5 class="card-title">
+						<a href={{ path('profile', {'username': msg.sender.getUsername() } ) }}>{{ msg.sender.getUsername() }}</a>
+					</h5>
+					<h6 class="card-subtitle mb-2 text-muted">{{ msg.getDate()|date('H:i - d/m/Y') }}</h6>
+					<p class="card-text">{{ msg.text | striptags('<a>') | raw }}</p>
+					<a href="{{ path('message', {'msgId': msg.getId() }) }}" class="card-link">Répondre</a>
+					<a href="{{ path('rt_message', {'msgId': msg.getId() }) }}" class="card-link">RT</a>
+				</div>
+			</div>
+		{% endfor %}
+	</div>
+{% endblock %}