<?php 
 
namespace App\Controller; 
 
use App\Entity\Administrateur; 
use App\Entity\Article; 
use App\Entity\Payment\Subscription; 
use App\Entity\Plan; 
use App\Entity\Audio; 
use App\Entity\Centre; 
use App\Entity\ArticleCategorie; 
use App\Entity\ArticleTag; 
use App\Entity\Categorie; 
use App\Entity\Token; 
use App\Entity\Tag; 
use App\Service\PublicFunction; 
use Doctrine\Common\Collections\ArrayCollection; 
use Psr\Log\LoggerInterface; 
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\Annotation\Route; 
use DateTime; 
use App\Service\FileUploader; 
 
 
class ArticleController extends AbstractController 
{ 
    /** 
     * @Route("/article/categorie/{id}", name="deleteArticleCategorie", methods={"DELETE"}) 
     */ 
    public function deleteArticleCategorie(Categorie $categorie): Response 
    { 
        $entityManager = $this->getDoctrine()->getManager(); 
 
        $articleCategorie = $this->getDoctrine() 
        ->getRepository(ArticleCategorie::class) 
        ->findBy(array('id_categorie' => $categorie)); 
         
        $cat = $this->getDoctrine()  
        ->getRepository(Categorie::class) 
        ->findOneBy(array('id' => 1)); 
        foreach($articleCategorie as $ac){ 
            $ac->setIdCategorie($cat); 
            $entityManager->persist($cat); 
            $entityManager->flush(); 
        } 
         
        $entityManager->remove($categorie); 
        $entityManager->flush(); 
        return $this->json([ 
            'message' => "Categorie has been successfully deleted", 
            'status' => 200, 
        ]); 
    } 
 
      /** 
     * @Route("/article/{id}", name="deleteArticle", methods={"DELETE"}) 
     */ 
    public function deleteArticle(Request $request, Article $article): Response 
    { 
 
        if (!$request->query->get('token')) { 
            return new Response(json_encode([ 
                "message" => "Pas de token n'a été spécifié", 
                "status" => 401, 
            ]), 401); 
        } 
 
        $entityManager = $this->getDoctrine()->getManager(); 
        /** @var Token */ 
        $token = $this->getDoctrine() 
            ->getRepository(Token::class) 
            ->findOneBy(['token' =>  $request->query->get('token')]); 
        if (!$token || !($token->getIdAdmin())) { 
            return new Response(json_encode([ 
                "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.", 
                "status" => 404, 
            ])); 
        } 
 
        // get token age 
        $dateDiff = $token->getCreationDate()->diff(new DateTime()); 
        // if the token if older than 7 days 
        if ($dateDiff->d > 7) { 
            $entityManager->remove($token); 
            $entityManager->flush(); 
            return $this->json([ 
                'message' => 'Ce token est obselète. Veuillez vous reconnecter.', 
                'path' => 'src/Controller/ClientController.php', 
                "status" => 401, 
            ]); 
        } 
 
        $entityManager = $this->getDoctrine()->getManager(); 
 
        $articleCategorie = $this->getDoctrine() 
        ->getRepository(ArticleCategorie::class) 
        ->findBy(array('id_article' => $article)); 
         
        foreach($articleCategorie as $ac){ 
            $entityManager->remove($ac); 
            $entityManager->flush(); 
        } 
         
        $entityManager->remove($article); 
        $entityManager->flush(); 
        return $this->json([ 
            'message' => "Article has been successfully deleted", 
            'status' => 200, 
        ]); 
    } 
 
 
     /** 
      * @Route("/article/categorie", name="postCategorieArticle", methods={"POST"}) 
    */ 
     public function postArticleCategorie(Request $request): Response 
     { 
        if (!$request->query->get('token')) { 
            return new Response(json_encode([ 
                "message" => "Pas de token n'a été spécifié", 
                "status" => 401, 
            ]), 401); 
        } 
 
        $entityManager = $this->getDoctrine()->getManager(); 
        /** @var Token */ 
        $token = $this->getDoctrine() 
            ->getRepository(Token::class) 
            ->findOneBy(['token' =>  $request->query->get('token')]); 
        if (!$token || !($token->getIdAdmin())) { 
            return new Response(json_encode([ 
                "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.", 
                "status" => 404, 
            ])); 
        } 
 
        // get token age 
        $dateDiff = $token->getCreationDate()->diff(new DateTime()); 
        // if the token if older than 7 days 
        if ($dateDiff->d > 7) { 
            $entityManager->remove($token); 
            $entityManager->flush(); 
            return $this->json([ 
                'message' => 'Ce token est obselète. Veuillez vous reconnecter.', 
                'path' => 'src/Controller/ClientController.php', 
                "status" => 401, 
            ]); 
        } 
 
        $categorie = new Categorie(); 
        $categorie->setTitle($_POST["categorie"]); 
        $entityManager = $this->getDoctrine()->getManager(); 
        $entityManager->persist($categorie); 
        $entityManager->flush(); 
        return $this->json([ 
            "message" => "Catégorie ajouté", 
            "status" => 200, 
        ]); 
         
    } 
 
    /** 
     * @Route("/article/image/{slug}", name="getArticleImage", methods={"GET"}) 
     */ 
    public function getArticleImage($slug, PublicFunction $publicFunction): Response 
    { 
 
        $article = $this->getDoctrine() 
        ->getRepository(Article::class) 
        ->findOneBy(["slug" => $slug]); 
 
        if (!$article->getImgUrl()) 
            return new Response(json_encode([ 
                "message" => "this article does not have an image attached to it", 
                "status" => 404, 
            ]), 404); 
        return $publicFunction->replyWithFile('images/article/', $article->getImgUrl()); 
    } 
 
 
    /** 
     * @Route("/articles", name="getArticles", methods={"GET","HEAD"}) 
     */ 
    public function getArticles(Request $request): Response 
    { 
         
        /*  $audioss = $this->getDoctrine()->getRepository(Centre::class)->findAll(); 
        $plan = $this->getDoctrine()->getRepository(Plan::class)->find(1); 
        $entityManager = $this->getDoctrine()->getManager(); 
       // getIdGerant 
        foreach ($audioss as $audio) { 
            if ($audio->getIdGerant()->getId() == 104) { 
               // $audioCentre = $audio->getAudioCentre()->getCentre(); // Assuming this returns an AudioCentre object 
         
                $subscription = new Subscription(); 
                $subscription->setPlanInterval("month"); 
                $subscription->setPlanIntervalCount(1); 
                $subscription->setQuantity(1); 
                 
                $subscription->setPaidAmount(0); 
                $audioCentre = $audio->getAudioCentre(); // Get the associated AudioCentre 
         
               
                    $subscription->setCentre($audio); 
                 
                $subscription->setPlan($plan); 
                $subscription->setAudio($audio->getIdGerant()); 
                $entityManager->persist($subscription); 
            } 
        } 
         
        $entityManager->flush();*/ 
         
        $isProValues = [0, 2]; 
        $etat = 0; 
        $limit = $request->query->get('limit', null);  
         
        $articles = $this->getDoctrine() 
        ->getRepository(Article::class) 
        ->findByIsProAndEtat($isProValues, $etat, $limit); 
     
    $result = new ArrayCollection(); 
     
    foreach ($articles as $article) { 
        $categorieResult = new ArrayCollection(); 
        $articleCategoriesList = $this->getDoctrine() 
            ->getRepository(ArticleCategorie::class) 
            ->findBy(["id_article" => $article->getId()]); 
         
        foreach ($articleCategoriesList as $articleCategorie) { 
            $categorieResult->add([ 
                "id" => $articleCategorie->getIdCategorie()->getId(), 
                "title" => $articleCategorie->getIdCategorie()->getTitle(), 
            ]); 
        } 
     
        $admin = $this->getDoctrine() 
            ->getRepository(Administrateur::class) 
            ->findOneBy(["id" => $article->getIdAdmin()]); 
     
        $extrait = $article->getExtrait(); 
        if (empty($extrait)) { 
            $extrait = strip_tags($article->getTexte()); 
        } 
     
        // Ensure $categorieResult has elements before accessing $categorieResult[0] 
        $category = $categorieResult->first() ?: null; // Get the first element or null 
        $categoryId = $category['id'] ?? null; 
        $categoryTitle = $category['title'] ?? null; 
     
        $result->add([ 
            "id" => $article->getId(), 
            "title" => $article->getTitle(), 
            "subtitle" => $article->getSubtitle(), 
            "date" => $article->getPublicationDate(), 
            "source" => $article->getSource(), 
            "slug" => $article->getSlug(), 
            "imgUrl" => $article->getImgUrl(), 
            "externalId" => $article->getExternalId(), 
            "externalLink" => $article->getExternalLink(), 
            'description' => $extrait, 
            "category" => $category, 
            "categoryId" => $categoryId, 
            "categoryTitle" => $categoryTitle, 
            "admin" => $admin ? $admin->getName() . ' ' . $admin->getLastName() : null, 
            "isPro" => $article->getIsPro(), 
        ]); 
    } 
     
    return $this->json([ 
        "articles" => $result->toArray(), 
    ]); 
     
    } 
 
    /** 
     * @Route("/articles/categories", name="getArticlesCategories", methods={"GET","HEAD"}) 
     */ 
    public function getArticlesCategories(): Response 
    { 
        /** @var Categorie[]*/ 
        $categories = $this->getDoctrine() 
            ->getRepository(Categorie::class) 
            ->findAll(); 
        $result = new ArrayCollection(); 
        foreach ($categories as $categorie) { 
            $result->add([ 
                "id" => $categorie->getId(), 
                "libelle" => $categorie->getTitle(), 
            ]); 
        } 
        return $this->json([ 
            "content" => $result->toArray(), 
            "status" => 200, 
        ]); 
    } 
 
 
 
       /** 
     * @Route("/articles/categories/withArticle", name="getArticlesCategoriesWithArticle", methods={"GET","HEAD"}) 
     */ 
    public function getArticlesCategoriesWithArticle(): Response 
    { 
        /** @var Categorie[]*/ 
        $categories = $this->getDoctrine() 
            ->getRepository(Categorie::class) 
            ->findCategorieWithArticle(); 
        return $this->json([ 
            "content" => $categories, 
            "status" => 200, 
        ]); 
    } 
 
      /** 
     * @Route("/articles/pro/categories/withArticle", name="getArticlesCategoriesWithArticlePro", methods={"GET","HEAD"}) 
     */ 
        public function getArticlesCategoriesWithArticlePro(): Response 
        { 
            /** @var Categorie[]*/ 
            $categories = $this->getDoctrine() 
                ->getRepository(Categorie::class) 
                ->findCategorieWithArticlePro(); 
            return $this->json([ 
                "content" => $categories, 
                "status" => 200, 
            ]); 
        } 
 
 
    /** 
     * @Route("/articles/pro", name="getArticlesPro", methods={"GET","HEAD"}) 
     */ 
    public function getArticlesPro(Request $request): Response 
    { 
 
        $isProValues = [1]; 
        $etat = 0; 
        $limit = $request->query->get('limit', null); 
 
        $articles = $this->getDoctrine() 
            ->getRepository(Article::class) 
            ->findByIsProAndEtat($isProValues, $etat, $limit); 
 
        $result = new ArrayCollection(); 
        foreach ($articles as $article) { 
            $categorieResult = new ArrayCollection(); 
            $articleCategoriesList = $this->getDoctrine() 
                ->getRepository(ArticleCategorie::class) 
                ->findBy(["id_article" => $article->getId()]); 
            foreach ($articleCategoriesList as $articleCategorie) { 
                $categorieResult->add([ 
                    "id" => $articleCategorie->getIdCategorie()->getId(), 
                    "title" => $articleCategorie->getIdCategorie()->getTitle(), 
                ]); 
            } 
 
            $extrait = $article->getExtrait(); 
            if ($extrait == ""){ 
                $extrait = strip_tags($article->getTexte()); 
            } 
 
            $result->add([ 
                "id" => $article->getId(), 
                "title" => $article->getTitle(), 
                "subtitle" => $article->getSubtitle(), 
                "date" =>  $article->getPublicationDate(), 
                "slug" => $article->getSlug(), 
                "source" => $article->getSource(), 
                "imgUrl" => $article->getImgUrl(), 
                'description' =>$extrait, 
                "externalId" => $article->getExternalId(), 
                "externalLink" => $article->getExternalLink(), 
                "category" => $categorieResult[0], 
                "categoryId" =>$categorieResult[0]['id'], 
                "categoryTitle"=>$categorieResult[0]['title'], 
            ]); 
        } 
        return $this->json([ 
            "articles" => $result->toArray() 
        ]); 
    } 
 
    /** 
     * @Route("/article/{slug}", name="getArticleBySlug", methods={"GET","HEAD"}) 
     */ 
    public function getArticleBySlug($slug): Response 
    { 
        $categorieResult = new ArrayCollection(); 
        $article = $this->getDoctrine() 
        ->getRepository(Article::class) 
        ->findOneBy(["slug" => $slug]); 
         
        $articleCategoriesList = $this->getDoctrine() 
            ->getRepository(ArticleCategorie::class) 
            ->findBy(["id_article" => $article->getId()]); 
        foreach ($articleCategoriesList as $articleCategorie) { 
            $categorieResult->add([ 
                "id" => $articleCategorie->getIdCategorie()->getId(), 
                "title" => $articleCategorie->getIdCategorie()->getTitle(), 
            ]); 
        } 
        $tagResult = new ArrayCollection(); 
        $articleTagsList = $this->getDoctrine() 
            ->getRepository(ArticleTag::class) 
            ->findBy(["id_article" => $article->getId()]); 
        foreach ($articleTagsList as $articleTag) { 
            $tagResult->add([ 
                "id" => $articleTag->getIdTag()->getId(), 
                "title" => $articleTag->getIdTag()->getTitle(), 
            ]); 
        } 
        return $this->json([ 
            "id" => $article->getId(), 
            "admin_id" => $article->getIdAdmin()->getId(), 
            "title" => $article->getTitle(), 
            "subtitle" => $article->getSubtitle(), 
            "slug" => $article->getSlug(), 
            "creation_date" => $article->getCreationDate(), 
            "publication_date" => $article->getPublicationDate(), 
            "source" => $article->getSource(), 
            "imgUrl" => $article->getImgUrl(), 
            "texte" => $article->getTexte(), 
            "is_pro" => $article->getIsPro(), 
            "tags" => $tagResult->toArray(), 
            "etat" => $article->getEtat(), 
            "extrait" => $article->getExtrait(), 
            "categories" => $categorieResult->toArray() 
        ]); 
    } 
 
     /** 
     * @Route("/admin-article/{id}", name="getArticleByID", methods={"GET","HEAD"}) 
     */ 
    public function getArticleById(Article $article): Response 
    { 
        $categorieResult = new ArrayCollection(); 
         
        $articleCategoriesList = $this->getDoctrine() 
            ->getRepository(ArticleCategorie::class) 
            ->findBy(["id_article" => $article->getId()]); 
        foreach ($articleCategoriesList as $articleCategorie) { 
            $categorieResult->add([ 
                "id" => $articleCategorie->getIdCategorie()->getId(), 
                "title" => $articleCategorie->getIdCategorie()->getTitle(), 
            ]); 
        } 
        $tagResult = new ArrayCollection(); 
        $articleTagsList = $this->getDoctrine() 
            ->getRepository(ArticleTag::class) 
            ->findBy(["id_article" => $article->getId()]); 
        foreach ($articleTagsList as $articleTag) { 
            $tagResult->add([ 
                "id" => $articleTag->getIdTag()->getId(), 
                "title" => $articleTag->getIdTag()->getTitle(), 
            ]); 
        } 
        return $this->json([ 
            "id" => $article->getId(), 
            "admin_id" => $article->getIdAdmin()->getId(), 
            "title" => $article->getTitle(), 
            "subtitle" => $article->getSubtitle(), 
            "slug" => $article->getSlug(), 
            "creation_date" => $article->getCreationDate(), 
            "publication_date" => $article->getPublicationDate(), 
            "source" => $article->getSource(), 
            "imgUrl" => $article->getImgUrl(), 
            "texte" => $article->getTexte(), 
            "is_pro" => $article->getIsPro(), 
            "tags" => $tagResult->toArray(), 
            "etat" => $article->getEtat(), 
            "extrait" => $article->getExtrait(), 
            "categories" => $categorieResult->toArray() 
        ]); 
    } 
 
 
    /** 
     * @Route("/article", name="postArticle", methods={"POST"}) 
     */ 
    public function postArticle(Request $request, FileUploader $fileUploader): Response 
    { 
        if (!$request->query->get('token')) { 
            return new Response(json_encode([ 
                "message" => "Pas de token n'a été spécifié", 
                "status" => 401, 
            ]), 401); 
        } 
 
        $entityManager = $this->getDoctrine()->getManager(); 
        /** @var Token */ 
        $token = $this->getDoctrine() 
            ->getRepository(Token::class) 
            ->findOneBy(['token' =>  $request->query->get('token')]); 
        if (!$token || !($token->getIdAdmin())) { 
            return new Response(json_encode([ 
                "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.", 
                "status" => 404, 
            ])); 
        } 
 
        // get token age 
        $dateDiff = $token->getCreationDate()->diff(new DateTime()); 
        // if the token if older than 7 days 
        if ($dateDiff->d > 7) { 
            $entityManager->remove($token); 
            $entityManager->flush(); 
            return $this->json([ 
                'message' => 'Ce token est obselète. Veuillez vous reconnecter.', 
                'path' => 'src/Controller/ClientController.php', 
                "status" => 401, 
            ]); 
        } 
 
        $article = new Article(); 
        if (isset($_FILES["imageAvant"]) && $_FILES['imageAvant']['size'] != 0) { 
            $newfilename = $fileUploader->uploadFormData($_FILES["imageAvant"], "images/article/"); 
            $article->setImgUrl($newfilename); 
 
        } 
 
        $admin = $this->getDoctrine() 
        ->getRepository(Administrateur::class) 
        ->findBy(['id' => intval($request->query->get('admin'))]); 
 
         
        $categorie = $this->getDoctrine() 
        ->getRepository(Categorie::class) 
        ->findOneBy(['id' => intval($_POST["categorie"])]); 
        
         
        $article->setTitle($_POST["titre"]); 
        $article->setSubTitle($_POST["sousTitre"]); 
        $article->setExtrait($_POST["extrait"]); 
        $article->setTexte($_POST["texte"]); 
        $article->setSource($_POST["source"]); 
        $article->setIsPro($_POST["type"]); 
        $article->setSlug($_POST["slug"]); 
        $article->setEtat($_POST["etat"]); 
        $article->setIdAdmin($admin[0]); 
        $article->setCreationDate(new \DateTime()); 
        $article->setPublicationDate(new \DateTime()); 
        $entityManager = $this->getDoctrine()->getManager(); 
        $entityManager->persist($article); 
 
        $categorieArticle = new ArticleCategorie(); 
        $categorieArticle->setIdArticle($article); 
        $categorieArticle->setIdCategorie($categorie); 
        $entityManager->persist($categorieArticle); 
        $entityManager->flush(); 
 
        return $this->json([ 
            "status" => 200, 
        ]); 
 
    } 
 
     
    /** 
    * @Route("article/{id}/block", name="suspendreArticleByID", methods={"PUT"}) 
    */ 
 
    public function suspendreArticleByID(Article $article, Request $request){ 
 
        if (!$request->query->get('token')) { 
            return new Response(json_encode([ 
                "message" => "Pas de token n'a été spécifié", 
                "status" => 401, 
            ]), 401); 
        } 
 
        $entityManager = $this->getDoctrine()->getManager(); 
        /** @var Token */ 
        $token = $this->getDoctrine() 
            ->getRepository(Token::class) 
            ->findOneBy(['token' =>  $request->query->get('token')]); 
        if (!$token || !($token->getIdAdmin())) { 
            return new Response(json_encode([ 
                "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.", 
                "status" => 404, 
            ])); 
        } 
 
        // get token age 
        $dateDiff = $token->getCreationDate()->diff(new DateTime()); 
        // if the token if older than 7 days 
        if ($dateDiff->d > 7) { 
            $entityManager->remove($token); 
            $entityManager->flush(); 
            return $this->json([ 
                'message' => 'Ce token est obselète. Veuillez vous reconnecter.', 
                'path' => 'src/Controller/ClientController.php', 
                "status" => 401, 
            ]); 
        } 
 
        $entityManager = $this->getDoctrine()->getManager(); 
        $article->setEtat(3); 
        $article->setPublicationDate(new DateTime()); 
        $entityManager->flush(); 
 
        return $this->json([ 
            "status" => 200, 
        ]); 
 
    } 
 
      /** 
    * @Route("article/{id}/publish", name="publieArticleByID", methods={"PUT"}) 
    */ 
 
    public function publieArticleByID(Article $article, Request $request){ 
 
        if (!$request->query->get('token')) { 
            return new Response(json_encode([ 
                "message" => "Pas de token n'a été spécifié", 
                "status" => 401, 
            ]), 401); 
        } 
 
        $entityManager = $this->getDoctrine()->getManager(); 
        /** @var Token */ 
        $token = $this->getDoctrine() 
            ->getRepository(Token::class) 
            ->findOneBy(['token' =>  $request->query->get('token')]); 
        if (!$token || !($token->getIdAdmin())) { 
            return new Response(json_encode([ 
                "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.", 
                "status" => 404, 
            ])); 
        } 
 
        // get token age 
        $dateDiff = $token->getCreationDate()->diff(new DateTime()); 
        // if the token if older than 7 days 
        if ($dateDiff->d > 7) { 
            $entityManager->remove($token); 
            $entityManager->flush(); 
            return $this->json([ 
                'message' => 'Ce token est obselète. Veuillez vous reconnecter.', 
                'path' => 'src/Controller/ClientController.php', 
                "status" => 401, 
            ]); 
        } 
 
        $entityManager = $this->getDoctrine()->getManager(); 
        $article->setEtat(0); 
        $article->setPublicationDate(new DateTime()); 
        $entityManager->flush(); 
 
        return $this->json([ 
            "status" => 200, 
        ]); 
 
    } 
 
    /** 
     * @Route("/article/edit/{id}", name="editArticlesByID", methods={"POST"}) 
     */ 
    public function editArticlesByID(Article $article, Request $request, FileUploader $fileUploader) : Response 
    { 
        if (!$request->query->get('token')) { 
            return new Response(json_encode([ 
                "message" => "Pas de token n'a été spécifié", 
                "status" => 401, 
            ]), 401); 
        } 
 
        $entityManager = $this->getDoctrine()->getManager(); 
        /** @var Token */ 
        $token = $this->getDoctrine() 
            ->getRepository(Token::class) 
            ->findOneBy(['token' =>  $request->query->get('token')]); 
        if (!$token || !($token->getIdAdmin())) { 
            return new Response(json_encode([ 
                "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.", 
                "status" => 404, 
            ])); 
        } 
 
        // get token age 
        $dateDiff = $token->getCreationDate()->diff(new DateTime()); 
        // if the token if older than 7 days 
        if ($dateDiff->d > 7) { 
            $entityManager->remove($token); 
            $entityManager->flush(); 
            return $this->json([ 
                'message' => 'Ce token est obselète. Veuillez vous reconnecter.', 
                'path' => 'src/Controller/ClientController.php', 
                "status" => 401, 
            ]); 
        } 
 
        if (isset($_FILES["imageAvant"]) && $_FILES['imageAvant']['size'] != 0) { 
                $newfilename = $fileUploader->uploadFormData($_FILES["imageAvant"], "images/article/"); 
               $article->setImgUrl($newfilename); 
 
        } 
        
        $article->setTitle($_POST["titre"]); 
        $article->setSubTitle($_POST["sousTitre"]); 
        $article->setSlug($_POST["slug"]); 
        $article->setExtrait($_POST["extrait"]); 
        $article->setTexte($_POST["texte"]); 
        $article->setSource($_POST["source"]); 
        $article->setIsPro($_POST["type"]); 
        $article->setEtat($_POST["etat"]); 
        $article->setPublicationDate(new \DateTime()); 
        $entityManager = $this->getDoctrine()->getManager(); 
        $entityManager->persist($article); 
        $entityManager->flush(); 
 
        $categorieArticle = $this->getDoctrine() 
        ->getRepository(ArticleCategorie::class) 
        ->findOneBy(['id_article' => $article]); 
 
        if ($_POST['categorie'] != $categorieArticle->getIdCategorie()->getId()){ 
            $categorie = $this->getDoctrine() 
            ->getRepository(Categorie::class) 
            ->findOneBy(['id' => intval($_POST["categorie"])]); 
            $categorieArticle->setIdCategorie($categorie); 
            $entityManager->persist($categorieArticle); 
            $entityManager->flush(); 
        } 
 
       
        return $this->json([ 
            "status" => 200, 
        ]); 
 
    } 
 
 
     
    /** 
     * @Route("/articles/publish", name="getAllArticlesPublish", methods={"GET","HEAD"}) 
     */ 
    public function getAllArticlesPublish(Request $request): Response 
    { 
            $articles = $this->getDoctrine() 
                ->getRepository(Article::class) 
                ->findBy(['etat' => 0]); 
        $result = new ArrayCollection(); 
        foreach ($articles as $article) { 
            $categorieResult = new ArrayCollection(); 
            $articleCategoriesList = $this->getDoctrine() 
                ->getRepository(ArticleCategorie::class) 
                ->findBy(["id_article" => $article->getId()]); 
            foreach ($articleCategoriesList as $articleCategorie) { 
                $categorieResult->add([ 
                    "id" => $articleCategorie->getIdCategorie()->getId(), 
                    "title" => $articleCategorie->getIdCategorie()->getTitle(), 
                ]); 
            } 
            $tagResult = new ArrayCollection(); 
            $articleTagList = $this->getDoctrine() 
            ->getRepository(ArticleTag::class) 
            ->findBy(["id_article" => $article->getId()]); 
        foreach ($articleTagList as $articleTag) { 
            $tagResult->add([ 
                "id" => $articleTag->getIdTag()->getId(), 
                "title" => $articleTag->getIdTag()->getTitle(), 
            ]); 
        } 
 
            $admin =  $this->getDoctrine() 
            ->getRepository(Administrateur::class) 
            ->findOneBy(["id" => $article->getIdAdmin()]); 
            $etat_libelle = ""; 
            switch ( $article->getEtat()){ 
                case 0: 
                    $etat_libelle = "Publié"; 
                    break; 
                case 1: 
                    $etat_libelle = "Brouillon"; 
                    break; 
                case 2: 
                    $etat_libelle = "Suspendu"; 
                    break; 
                case 3: 
                    $etat_libelle = "Corbeille"; 
                    break; 
            } 
 
            $extrait = $article->getExtrait(); 
            if ($extrait == ""){ 
                $extrait = strip_tags($article->getTexte()); 
            } 
            $result->add([ 
                "id" => $article->getId(), 
                "title" => $article->getTitle(), 
                "subtitle" => $article->getSubtitle(), 
                "date" => $article->getPublicationDate(), 
                "source" => $article->getSource(), 
                "imgUrl" => $article->getImgUrl(), 
                'description' => $extrait, 
                "category" => $categorieResult[0], 
                "categoryId" =>$categorieResult[0]['id'], 
                "categoryTitle"=>$categorieResult[0]['title'], 
                "tag" => $tagResult, 
                "admin" => $admin->getName().' '.$admin->getLastName(), 
                "isPro" => $article->getIsPro(), 
                "etat_libelle" => $etat_libelle, 
                "etat" => $article->getEtat() 
            ]); 
        } 
        return $this->json([ 
            "articles" => $result->toArray() 
        ]); 
    } 
 
 
/** 
     * @Route("/articles/test", name="getArticlesTest", methods={"GET","HEAD"}) 
     */ 
    public function getArticlesTest(Request $request): Response 
    { 
        if (!$request->query->get('nom')){ 
            return $this->json([ 
                "list" => array( 
                    array( "nom" => 'Pierre'), 
                    array( "nom" => 'Paul') 
                ) 
            ]); 
        } else { 
            if ($request->query->get('nom') == "Paul") { 
                return $this->json([ 
                    "list" => array( 
                        array( "nom" => 'Paul') 
                    ) 
                ]); 
            } elseif ($request->query->get('nom') == "Pierre") { 
                return $this->json([ 
                    "list" => array( 
                        array( "nom" => 'Pierre') 
                    ) 
                ]); 
            } else { 
                return $this->json([ 
                    "list" => array( 
                        array( "nom" => 'Jacques'), 
                        array( "nom" => 'Paul') 
                    ) 
                ]); 
            } 
        } 
        
    } 
 
     
}