<?php



class ComsController{

    private $entityManager;

    public function __construct($entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function add_com($contenu, $billet_id){

        $user = $this->entityManager->find(User::class, $_SESSION["id_user"]);
        $id_billet = $this->entityManager->find(Billets::class, $_POST["id_billet"]);

        if (isset($_SESSION["id_user"])){
            $com = new Coms();
            $com->setContenu($contenu);
            $com->setAuteur($user);
            $com->setBillet($id_billet);
            $com->setDatePost(new \DateTime());

            $this->entityManager->persist($com);
            $this->entityManager->flush();   
        }
    }

    public function supr_com($id)
    {
        $com = $this->entityManager->find(Coms::class, $id);
        if ($com) {
            $this->entityManager->remove($com);
            $this->entityManager->flush();
        }
    }

    public function modify_com($id, $contenu)
    {
        $com = $this->entityManager->find(Coms::class, $id);

        $com->setContenu($contenu);
        $this->entityManager->flush();   
     }
}
?>