Explorar o código

Ajout page accueil avec vision des tweets et possibilité de tweeter

Clément K %!s(int64=4) %!d(string=hai) anos
pai
achega
1a801a62d3

+ 61 - 0
src/Controller/HomeController.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Controller;
+
+use App\Entity\Message;
+use App\Entity\User;
+use Doctrine\DBAL\Types\TextType;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\Routing\Annotation\Route;
+
+class HomeController extends AbstractController
+{
+    #[Route('/home', name: 'home')]
+    public function index(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('home'));
+        $form = $formBuilder->getForm();
+
+        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();
+                $message->setText($form->get('text')->getData());
+                $message->setSender($profile);
+                $em->persist($message);
+                $em->flush();
+            }
+        }
+
+        $em = $this->getDoctrine()->getManager();
+        $repository_profile = $em->getRepository(User::class);
+        $profile = $repository_profile->findOneBy(array('username' => $session->get('user')));
+        $subscriptions = $profile->getSubscriptions();
+        $msg = array();
+        array_push($msg, $profile->getMessages());
+        foreach ($subscriptions as $user) {
+            array_push($msg, $user->getMessages());
+        }
+
+        return $this->render('home/index.html.twig', [
+            'controller_name' => 'HomeController',
+            "messages" => $msg,
+            'form' => $form->createView()
+        ]);
+    }
+}

+ 77 - 0
src/Entity/Hashtag.php

@@ -0,0 +1,77 @@
+<?php
+
+namespace App\Entity;
+
+use App\Repository\HashtagRepository;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity(repositoryClass=HashtagRepository::class)
+ */
+class Hashtag
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(type="text")
+     */
+    private $name;
+
+    /**
+     * @ORM\ManyToMany(targetEntity=Message::class)
+     */
+    private $message;
+
+    public function __construct()
+    {
+        $this->message = new ArrayCollection();
+    }
+
+    public function getId(): ?int
+    {
+        return $this->id;
+    }
+
+    public function getName(): ?string
+    {
+        return $this->name;
+    }
+
+    public function setName(string $name): self
+    {
+        $this->name = $name;
+
+        return $this;
+    }
+
+    /**
+     * @return Collection|Message[]
+     */
+    public function getMessage(): Collection
+    {
+        return $this->message;
+    }
+
+    public function addMessage(Message $message): self
+    {
+        if (!$this->message->contains($message)) {
+            $this->message[] = $message;
+        }
+
+        return $this;
+    }
+
+    public function removeMessage(Message $message): self
+    {
+        $this->message->removeElement($message);
+
+        return $this;
+    }
+}

+ 36 - 0
src/Entity/Message.php

@@ -3,6 +3,8 @@
 namespace App\Entity;
 
 use App\Repository\MessageRepository;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
 use Doctrine\ORM\Mapping as ORM;
 
 /**
@@ -28,6 +30,16 @@ class Message
      */
     private $sender;
 
+    /**
+     * @ORM\ManyToMany(targetEntity=User::class)
+     */
+    private $mentions;
+
+    public function __construct()
+    {
+        $this->mentions = new ArrayCollection();
+    }
+
     public function getId(): ?int
     {
         return $this->id;
@@ -56,4 +68,28 @@ class Message
 
         return $this;
     }
+
+    /**
+     * @return Collection|User[]
+     */
+    public function getMentions(): Collection
+    {
+        return $this->mentions;
+    }
+
+    public function addMention(User $mention): self
+    {
+        if (!$this->mentions->contains($mention)) {
+            $this->mentions[] = $mention;
+        }
+
+        return $this;
+    }
+
+    public function removeMention(User $mention): self
+    {
+        $this->mentions->removeElement($mention);
+
+        return $this;
+    }
 }

+ 50 - 0
src/Repository/HashtagRepository.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace App\Repository;
+
+use App\Entity\Hashtag;
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\Persistence\ManagerRegistry;
+
+/**
+ * @method Hashtag|null find($id, $lockMode = null, $lockVersion = null)
+ * @method Hashtag|null findOneBy(array $criteria, array $orderBy = null)
+ * @method Hashtag[]    findAll()
+ * @method Hashtag[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
+ */
+class HashtagRepository extends ServiceEntityRepository
+{
+    public function __construct(ManagerRegistry $registry)
+    {
+        parent::__construct($registry, Hashtag::class);
+    }
+
+    // /**
+    //  * @return Hashtag[] Returns an array of Hashtag objects
+    //  */
+    /*
+    public function findByExampleField($value)
+    {
+        return $this->createQueryBuilder('h')
+            ->andWhere('h.exampleField = :val')
+            ->setParameter('val', $value)
+            ->orderBy('h.id', 'ASC')
+            ->setMaxResults(10)
+            ->getQuery()
+            ->getResult()
+        ;
+    }
+    */
+
+    /*
+    public function findOneBySomeField($value): ?Hashtag
+    {
+        return $this->createQueryBuilder('h')
+            ->andWhere('h.exampleField = :val')
+            ->setParameter('val', $value)
+            ->getQuery()
+            ->getOneOrNullResult()
+        ;
+    }
+    */
+}

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

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