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