src/Controller/ArticleController.php line 221

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Administrateur;
  4. use App\Entity\Article;
  5. use App\Entity\Payment\Subscription;
  6. use App\Entity\Plan;
  7. use App\Entity\Audio;
  8. use App\Entity\Centre;
  9. use App\Entity\ArticleCategorie;
  10. use App\Entity\ArticleTag;
  11. use App\Entity\Categorie;
  12. use App\Entity\Token;
  13. use App\Entity\Tag;
  14. use App\Service\PublicFunction;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Psr\Log\LoggerInterface;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use DateTime;
  22. use App\Service\FileUploader;
  23. class ArticleController extends AbstractController
  24. {
  25.     /**
  26.      * @Route("/article/categorie/{id}", name="deleteArticleCategorie", methods={"DELETE"})
  27.      */
  28.     public function deleteArticleCategorie(Categorie $categorie): Response
  29.     {
  30.         $entityManager $this->getDoctrine()->getManager();
  31.         $articleCategorie $this->getDoctrine()
  32.         ->getRepository(ArticleCategorie::class)
  33.         ->findBy(array('id_categorie' => $categorie));
  34.         
  35.         $cat $this->getDoctrine() 
  36.         ->getRepository(Categorie::class)
  37.         ->findOneBy(array('id' => 1));
  38.         foreach($articleCategorie as $ac){
  39.             $ac->setIdCategorie($cat);
  40.             $entityManager->persist($cat);
  41.             $entityManager->flush();
  42.         }
  43.         
  44.         $entityManager->remove($categorie);
  45.         $entityManager->flush();
  46.         return $this->json([
  47.             'message' => "Categorie has been successfully deleted",
  48.             'status' => 200,
  49.         ]);
  50.     }
  51.       /**
  52.      * @Route("/article/{id}", name="deleteArticle", methods={"DELETE"})
  53.      */
  54.     public function deleteArticle(Request $requestArticle $article): Response
  55.     {
  56.         if (!$request->query->get('token')) {
  57.             return new Response(json_encode([
  58.                 "message" => "Pas de token n'a été spécifié",
  59.                 "status" => 401,
  60.             ]), 401);
  61.         }
  62.         $entityManager $this->getDoctrine()->getManager();
  63.         /** @var Token */
  64.         $token $this->getDoctrine()
  65.             ->getRepository(Token::class)
  66.             ->findOneBy(['token' =>  $request->query->get('token')]);
  67.         if (!$token || !($token->getIdAdmin())) {
  68.             return new Response(json_encode([
  69.                 "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.",
  70.                 "status" => 404,
  71.             ]));
  72.         }
  73.         // get token age
  74.         $dateDiff $token->getCreationDate()->diff(new DateTime());
  75.         // if the token if older than 7 days
  76.         if ($dateDiff->7) {
  77.             $entityManager->remove($token);
  78.             $entityManager->flush();
  79.             return $this->json([
  80.                 'message' => 'Ce token est obselète. Veuillez vous reconnecter.',
  81.                 'path' => 'src/Controller/ClientController.php',
  82.                 "status" => 401,
  83.             ]);
  84.         }
  85.         $entityManager $this->getDoctrine()->getManager();
  86.         $articleCategorie $this->getDoctrine()
  87.         ->getRepository(ArticleCategorie::class)
  88.         ->findBy(array('id_article' => $article));
  89.         
  90.         foreach($articleCategorie as $ac){
  91.             $entityManager->remove($ac);
  92.             $entityManager->flush();
  93.         }
  94.         
  95.         $entityManager->remove($article);
  96.         $entityManager->flush();
  97.         return $this->json([
  98.             'message' => "Article has been successfully deleted",
  99.             'status' => 200,
  100.         ]);
  101.     }
  102.      /**
  103.       * @Route("/article/categorie", name="postCategorieArticle", methods={"POST"})
  104.     */
  105.      public function postArticleCategorie(Request $request): Response
  106.      {
  107.         if (!$request->query->get('token')) {
  108.             return new Response(json_encode([
  109.                 "message" => "Pas de token n'a été spécifié",
  110.                 "status" => 401,
  111.             ]), 401);
  112.         }
  113.         $entityManager $this->getDoctrine()->getManager();
  114.         /** @var Token */
  115.         $token $this->getDoctrine()
  116.             ->getRepository(Token::class)
  117.             ->findOneBy(['token' =>  $request->query->get('token')]);
  118.         if (!$token || !($token->getIdAdmin())) {
  119.             return new Response(json_encode([
  120.                 "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.",
  121.                 "status" => 404,
  122.             ]));
  123.         }
  124.         // get token age
  125.         $dateDiff $token->getCreationDate()->diff(new DateTime());
  126.         // if the token if older than 7 days
  127.         if ($dateDiff->7) {
  128.             $entityManager->remove($token);
  129.             $entityManager->flush();
  130.             return $this->json([
  131.                 'message' => 'Ce token est obselète. Veuillez vous reconnecter.',
  132.                 'path' => 'src/Controller/ClientController.php',
  133.                 "status" => 401,
  134.             ]);
  135.         }
  136.         $categorie = new Categorie();
  137.         $categorie->setTitle($_POST["categorie"]);
  138.         $entityManager $this->getDoctrine()->getManager();
  139.         $entityManager->persist($categorie);
  140.         $entityManager->flush();
  141.         return $this->json([
  142.             "message" => "Catégorie ajouté",
  143.             "status" => 200,
  144.         ]);
  145.         
  146.     }
  147.     /**
  148.      * @Route("/article/image/{slug}", name="getArticleImage", methods={"GET"})
  149.      */
  150.     public function getArticleImage($slugPublicFunction $publicFunction): Response
  151.     {
  152.         $article $this->getDoctrine()
  153.         ->getRepository(Article::class)
  154.         ->findOneBy(["slug" => $slug]);
  155.         if (!$article->getImgUrl())
  156.             return new Response(json_encode([
  157.                 "message" => "this article does not have an image attached to it",
  158.                 "status" => 404,
  159.             ]), 404);
  160.         return $publicFunction->replyWithFile('images/article/'$article->getImgUrl());
  161.     }
  162.     /**
  163.      * @Route("/articles", name="getArticles", methods={"GET","HEAD"})
  164.      */
  165.     public function getArticles(Request $request): Response
  166.     {
  167.         
  168.         /*  $audioss = $this->getDoctrine()->getRepository(Centre::class)->findAll();
  169.         $plan = $this->getDoctrine()->getRepository(Plan::class)->find(1);
  170.         $entityManager = $this->getDoctrine()->getManager();
  171.        // getIdGerant
  172.         foreach ($audioss as $audio) {
  173.             if ($audio->getIdGerant()->getId() == 104) {
  174.                // $audioCentre = $audio->getAudioCentre()->getCentre(); // Assuming this returns an AudioCentre object
  175.         
  176.                 $subscription = new Subscription();
  177.                 $subscription->setPlanInterval("month");
  178.                 $subscription->setPlanIntervalCount(1);
  179.                 $subscription->setQuantity(1);
  180.                 
  181.                 $subscription->setPaidAmount(0);
  182.                 $audioCentre = $audio->getAudioCentre(); // Get the associated AudioCentre
  183.         
  184.               
  185.                     $subscription->setCentre($audio);
  186.                 
  187.                 $subscription->setPlan($plan);
  188.                 $subscription->setAudio($audio->getIdGerant());
  189.                 $entityManager->persist($subscription);
  190.             }
  191.         }
  192.         
  193.         $entityManager->flush();*/
  194.         
  195.         $isProValues = [02];
  196.         $etat 0;
  197.         $limit $request->query->get('limit'null); 
  198.         
  199.         $articles $this->getDoctrine()
  200.         ->getRepository(Article::class)
  201.         ->findByIsProAndEtat($isProValues$etat$limit);
  202.     
  203.     $result = new ArrayCollection();
  204.     
  205.     foreach ($articles as $article) {
  206.         $categorieResult = new ArrayCollection();
  207.         $articleCategoriesList $this->getDoctrine()
  208.             ->getRepository(ArticleCategorie::class)
  209.             ->findBy(["id_article" => $article->getId()]);
  210.         
  211.         foreach ($articleCategoriesList as $articleCategorie) {
  212.             $categorieResult->add([
  213.                 "id" => $articleCategorie->getIdCategorie()->getId(),
  214.                 "title" => $articleCategorie->getIdCategorie()->getTitle(),
  215.             ]);
  216.         }
  217.     
  218.         $admin $this->getDoctrine()
  219.             ->getRepository(Administrateur::class)
  220.             ->findOneBy(["id" => $article->getIdAdmin()]);
  221.     
  222.         $extrait $article->getExtrait();
  223.         if (empty($extrait)) {
  224.             $extrait strip_tags($article->getTexte());
  225.         }
  226.     
  227.         // Ensure $categorieResult has elements before accessing $categorieResult[0]
  228.         $category $categorieResult->first() ?: null// Get the first element or null
  229.         $categoryId $category['id'] ?? null;
  230.         $categoryTitle $category['title'] ?? null;
  231.     
  232.         $result->add([
  233.             "id" => $article->getId(),
  234.             "title" => $article->getTitle(),
  235.             "subtitle" => $article->getSubtitle(),
  236.             "date" => $article->getPublicationDate(),
  237.             "source" => $article->getSource(),
  238.             "slug" => $article->getSlug(),
  239.             "imgUrl" => $article->getImgUrl(),
  240.             "externalId" => $article->getExternalId(),
  241.             "externalLink" => $article->getExternalLink(),
  242.             'description' => $extrait,
  243.             "category" => $category,
  244.             "categoryId" => $categoryId,
  245.             "categoryTitle" => $categoryTitle,
  246.             "admin" => $admin $admin->getName() . ' ' $admin->getLastName() : null,
  247.         ]);
  248.     }
  249.     
  250.     return $this->json([
  251.         "articles" => $result->toArray(),
  252.     ]);
  253.     
  254.     }
  255.     /**
  256.      * @Route("/articles/categories", name="getArticlesCategories", methods={"GET","HEAD"})
  257.      */
  258.     public function getArticlesCategories(): Response
  259.     {
  260.         /** @var Categorie[]*/
  261.         $categories $this->getDoctrine()
  262.             ->getRepository(Categorie::class)
  263.             ->findAll();
  264.         $result = new ArrayCollection();
  265.         foreach ($categories as $categorie) {
  266.             $result->add([
  267.                 "id" => $categorie->getId(),
  268.                 "libelle" => $categorie->getTitle(),
  269.             ]);
  270.         }
  271.         return $this->json([
  272.             "content" => $result->toArray(),
  273.             "status" => 200,
  274.         ]);
  275.     }
  276.        /**
  277.      * @Route("/articles/categories/withArticle", name="getArticlesCategoriesWithArticle", methods={"GET","HEAD"})
  278.      */
  279.     public function getArticlesCategoriesWithArticle(): Response
  280.     {
  281.         /** @var Categorie[]*/
  282.         $categories $this->getDoctrine()
  283.             ->getRepository(Categorie::class)
  284.             ->findCategorieWithArticle();
  285.         return $this->json([
  286.             "content" => $categories,
  287.             "status" => 200,
  288.         ]);
  289.     }
  290.       /**
  291.      * @Route("/articles/pro/categories/withArticle", name="getArticlesCategoriesWithArticlePro", methods={"GET","HEAD"})
  292.      */
  293.         public function getArticlesCategoriesWithArticlePro(): Response
  294.         {
  295.             /** @var Categorie[]*/
  296.             $categories $this->getDoctrine()
  297.                 ->getRepository(Categorie::class)
  298.                 ->findCategorieWithArticlePro();
  299.             return $this->json([
  300.                 "content" => $categories,
  301.                 "status" => 200,
  302.             ]);
  303.         }
  304.     /**
  305.      * @Route("/articles/pro", name="getArticlesPro", methods={"GET","HEAD"})
  306.      */
  307.     public function getArticlesPro(Request $request): Response
  308.     {
  309.         $isProValues = [12];
  310.         $etat 0;
  311.         $limit $request->query->get('limit'null); 
  312.         
  313.         $articles $this->getDoctrine()
  314.             ->getRepository(Article::class)
  315.             ->findByIsProAndEtat($isProValues$etat$limit);
  316.         $result = new ArrayCollection();
  317.         foreach ($articles as $article) {
  318.             $categorieResult = new ArrayCollection();
  319.             $articleCategoriesList $this->getDoctrine()
  320.                 ->getRepository(ArticleCategorie::class)
  321.                 ->findBy(["id_article" => $article->getId()]);
  322.             foreach ($articleCategoriesList as $articleCategorie) {
  323.                 $categorieResult->add([
  324.                     "id" => $articleCategorie->getIdCategorie()->getId(),
  325.                     "title" => $articleCategorie->getIdCategorie()->getTitle(),
  326.                 ]);
  327.             }
  328.             $extrait $article->getExtrait();
  329.             if ($extrait == ""){
  330.                 $extrait strip_tags($article->getTexte());
  331.             }
  332.             $result->add([
  333.                 "id" => $article->getId(),
  334.                 "title" => $article->getTitle(),
  335.                 "subtitle" => $article->getSubtitle(),
  336.                 "date" =>  $article->getPublicationDate(),
  337.                 "slug" => $article->getSlug(),
  338.                 "source" => $article->getSource(),
  339.                 "imgUrl" => $article->getImgUrl(),
  340.                 'description' =>$extrait,
  341.                 "externalId" => $article->getExternalId(),
  342.                 "externalLink" => $article->getExternalLink(),
  343.                 "category" => $categorieResult[0],
  344.                 "categoryId" =>$categorieResult[0]['id'],
  345.                 "categoryTitle"=>$categorieResult[0]['title'],
  346.             ]);
  347.         }
  348.         return $this->json([
  349.             "articles" => $result->toArray()
  350.         ]);
  351.     }
  352.     /**
  353.      * @Route("/article/{slug}", name="getArticleBySlug", methods={"GET","HEAD"})
  354.      */
  355.     public function getArticleBySlug($slug): Response
  356.     {
  357.         $categorieResult = new ArrayCollection();
  358.         $article $this->getDoctrine()
  359.         ->getRepository(Article::class)
  360.         ->findOneBy(["slug" => $slug]);
  361.         
  362.         $articleCategoriesList $this->getDoctrine()
  363.             ->getRepository(ArticleCategorie::class)
  364.             ->findBy(["id_article" => $article->getId()]);
  365.         foreach ($articleCategoriesList as $articleCategorie) {
  366.             $categorieResult->add([
  367.                 "id" => $articleCategorie->getIdCategorie()->getId(),
  368.                 "title" => $articleCategorie->getIdCategorie()->getTitle(),
  369.             ]);
  370.         }
  371.         $tagResult = new ArrayCollection();
  372.         $articleTagsList $this->getDoctrine()
  373.             ->getRepository(ArticleTag::class)
  374.             ->findBy(["id_article" => $article->getId()]);
  375.         foreach ($articleTagsList as $articleTag) {
  376.             $tagResult->add([
  377.                 "id" => $articleTag->getIdTag()->getId(),
  378.                 "title" => $articleTag->getIdTag()->getTitle(),
  379.             ]);
  380.         }
  381.         return $this->json([
  382.             "id" => $article->getId(),
  383.             "admin_id" => $article->getIdAdmin()->getId(),
  384.             "title" => $article->getTitle(),
  385.             "subtitle" => $article->getSubtitle(),
  386.             "slug" => $article->getSlug(),
  387.             "creation_date" => $article->getCreationDate(),
  388.             "publication_date" => $article->getPublicationDate(),
  389.             "source" => $article->getSource(),
  390.             "imgUrl" => $article->getImgUrl(),
  391.             "texte" => $article->getTexte(),
  392.             "is_pro" => $article->getIsPro(),
  393.             "tags" => $tagResult->toArray(),
  394.             "etat" => $article->getEtat(),
  395.             "extrait" => $article->getExtrait(),
  396.             "categories" => $categorieResult->toArray()
  397.         ]);
  398.     }
  399.      /**
  400.      * @Route("/admin-article/{id}", name="getArticleByID", methods={"GET","HEAD"})
  401.      */
  402.     public function getArticleById(Article $article): Response
  403.     {
  404.         $categorieResult = new ArrayCollection();
  405.         
  406.         $articleCategoriesList $this->getDoctrine()
  407.             ->getRepository(ArticleCategorie::class)
  408.             ->findBy(["id_article" => $article->getId()]);
  409.         foreach ($articleCategoriesList as $articleCategorie) {
  410.             $categorieResult->add([
  411.                 "id" => $articleCategorie->getIdCategorie()->getId(),
  412.                 "title" => $articleCategorie->getIdCategorie()->getTitle(),
  413.             ]);
  414.         }
  415.         $tagResult = new ArrayCollection();
  416.         $articleTagsList $this->getDoctrine()
  417.             ->getRepository(ArticleTag::class)
  418.             ->findBy(["id_article" => $article->getId()]);
  419.         foreach ($articleTagsList as $articleTag) {
  420.             $tagResult->add([
  421.                 "id" => $articleTag->getIdTag()->getId(),
  422.                 "title" => $articleTag->getIdTag()->getTitle(),
  423.             ]);
  424.         }
  425.         return $this->json([
  426.             "id" => $article->getId(),
  427.             "admin_id" => $article->getIdAdmin()->getId(),
  428.             "title" => $article->getTitle(),
  429.             "subtitle" => $article->getSubtitle(),
  430.             "slug" => $article->getSlug(),
  431.             "creation_date" => $article->getCreationDate(),
  432.             "publication_date" => $article->getPublicationDate(),
  433.             "source" => $article->getSource(),
  434.             "imgUrl" => $article->getImgUrl(),
  435.             "texte" => $article->getTexte(),
  436.             "is_pro" => $article->getIsPro(),
  437.             "tags" => $tagResult->toArray(),
  438.             "etat" => $article->getEtat(),
  439.             "extrait" => $article->getExtrait(),
  440.             "categories" => $categorieResult->toArray()
  441.         ]);
  442.     }
  443.     /**
  444.      * @Route("/article", name="postArticle", methods={"POST"})
  445.      */
  446.     public function postArticle(Request $requestFileUploader $fileUploader): Response
  447.     {
  448.         if (!$request->query->get('token')) {
  449.             return new Response(json_encode([
  450.                 "message" => "Pas de token n'a été spécifié",
  451.                 "status" => 401,
  452.             ]), 401);
  453.         }
  454.         $entityManager $this->getDoctrine()->getManager();
  455.         /** @var Token */
  456.         $token $this->getDoctrine()
  457.             ->getRepository(Token::class)
  458.             ->findOneBy(['token' =>  $request->query->get('token')]);
  459.         if (!$token || !($token->getIdAdmin())) {
  460.             return new Response(json_encode([
  461.                 "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.",
  462.                 "status" => 404,
  463.             ]));
  464.         }
  465.         // get token age
  466.         $dateDiff $token->getCreationDate()->diff(new DateTime());
  467.         // if the token if older than 7 days
  468.         if ($dateDiff->7) {
  469.             $entityManager->remove($token);
  470.             $entityManager->flush();
  471.             return $this->json([
  472.                 'message' => 'Ce token est obselète. Veuillez vous reconnecter.',
  473.                 'path' => 'src/Controller/ClientController.php',
  474.                 "status" => 401,
  475.             ]);
  476.         }
  477.         $article = new Article();
  478.         if (isset($_FILES["imageAvant"]) && $_FILES['imageAvant']['size'] != 0) {
  479.             $newfilename $fileUploader->uploadFormData($_FILES["imageAvant"], "images/article/");
  480.             $article->setImgUrl($newfilename);
  481.         }
  482.         $admin $this->getDoctrine()
  483.         ->getRepository(Administrateur::class)
  484.         ->findBy(['id' => intval($request->query->get('admin'))]);
  485.         
  486.         $categorie $this->getDoctrine()
  487.         ->getRepository(Categorie::class)
  488.         ->findOneBy(['id' => intval($_POST["categorie"])]);
  489.        
  490.         
  491.         $article->setTitle($_POST["titre"]);
  492.         $article->setSubTitle($_POST["sousTitre"]);
  493.         $article->setExtrait($_POST["extrait"]);
  494.         $article->setTexte($_POST["texte"]);
  495.         $article->setSource($_POST["source"]);
  496.         $article->setIsPro($_POST["type"]);
  497.         $article->setSlug($_POST["slug"]);
  498.         $article->setEtat($_POST["etat"]);
  499.         $article->setIdAdmin($admin[0]);
  500.         $article->setCreationDate(new \DateTime());
  501.         $article->setPublicationDate(new \DateTime());
  502.         $entityManager $this->getDoctrine()->getManager();
  503.         $entityManager->persist($article);
  504.         $categorieArticle = new ArticleCategorie();
  505.         $categorieArticle->setIdArticle($article);
  506.         $categorieArticle->setIdCategorie($categorie);
  507.         $entityManager->persist($categorieArticle);
  508.         $entityManager->flush();
  509.         return $this->json([
  510.             "status" => 200,
  511.         ]);
  512.     }
  513.     
  514.     /**
  515.     * @Route("article/{id}/block", name="suspendreArticleByID", methods={"PUT"})
  516.     */
  517.     public function suspendreArticleByID(Article $articleRequest $request){
  518.         if (!$request->query->get('token')) {
  519.             return new Response(json_encode([
  520.                 "message" => "Pas de token n'a été spécifié",
  521.                 "status" => 401,
  522.             ]), 401);
  523.         }
  524.         $entityManager $this->getDoctrine()->getManager();
  525.         /** @var Token */
  526.         $token $this->getDoctrine()
  527.             ->getRepository(Token::class)
  528.             ->findOneBy(['token' =>  $request->query->get('token')]);
  529.         if (!$token || !($token->getIdAdmin())) {
  530.             return new Response(json_encode([
  531.                 "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.",
  532.                 "status" => 404,
  533.             ]));
  534.         }
  535.         // get token age
  536.         $dateDiff $token->getCreationDate()->diff(new DateTime());
  537.         // if the token if older than 7 days
  538.         if ($dateDiff->7) {
  539.             $entityManager->remove($token);
  540.             $entityManager->flush();
  541.             return $this->json([
  542.                 'message' => 'Ce token est obselète. Veuillez vous reconnecter.',
  543.                 'path' => 'src/Controller/ClientController.php',
  544.                 "status" => 401,
  545.             ]);
  546.         }
  547.         $entityManager $this->getDoctrine()->getManager();
  548.         $article->setEtat(3);
  549.         $article->setPublicationDate(new DateTime());
  550.         $entityManager->flush();
  551.         return $this->json([
  552.             "status" => 200,
  553.         ]);
  554.     }
  555.       /**
  556.     * @Route("article/{id}/publish", name="publieArticleByID", methods={"PUT"})
  557.     */
  558.     public function publieArticleByID(Article $articleRequest $request){
  559.         if (!$request->query->get('token')) {
  560.             return new Response(json_encode([
  561.                 "message" => "Pas de token n'a été spécifié",
  562.                 "status" => 401,
  563.             ]), 401);
  564.         }
  565.         $entityManager $this->getDoctrine()->getManager();
  566.         /** @var Token */
  567.         $token $this->getDoctrine()
  568.             ->getRepository(Token::class)
  569.             ->findOneBy(['token' =>  $request->query->get('token')]);
  570.         if (!$token || !($token->getIdAdmin())) {
  571.             return new Response(json_encode([
  572.                 "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.",
  573.                 "status" => 404,
  574.             ]));
  575.         }
  576.         // get token age
  577.         $dateDiff $token->getCreationDate()->diff(new DateTime());
  578.         // if the token if older than 7 days
  579.         if ($dateDiff->7) {
  580.             $entityManager->remove($token);
  581.             $entityManager->flush();
  582.             return $this->json([
  583.                 'message' => 'Ce token est obselète. Veuillez vous reconnecter.',
  584.                 'path' => 'src/Controller/ClientController.php',
  585.                 "status" => 401,
  586.             ]);
  587.         }
  588.         $entityManager $this->getDoctrine()->getManager();
  589.         $article->setEtat(0);
  590.         $article->setPublicationDate(new DateTime());
  591.         $entityManager->flush();
  592.         return $this->json([
  593.             "status" => 200,
  594.         ]);
  595.     }
  596.     /**
  597.      * @Route("/article/edit/{id}", name="editArticlesByID", methods={"POST"})
  598.      */
  599.     public function editArticlesByID(Article $articleRequest $requestFileUploader $fileUploader) : Response
  600.     {
  601.         if (!$request->query->get('token')) {
  602.             return new Response(json_encode([
  603.                 "message" => "Pas de token n'a été spécifié",
  604.                 "status" => 401,
  605.             ]), 401);
  606.         }
  607.         $entityManager $this->getDoctrine()->getManager();
  608.         /** @var Token */
  609.         $token $this->getDoctrine()
  610.             ->getRepository(Token::class)
  611.             ->findOneBy(['token' =>  $request->query->get('token')]);
  612.         if (!$token || !($token->getIdAdmin())) {
  613.             return new Response(json_encode([
  614.                 "message" => "Pas de token trouvé dans la base de données. Veuillez vous reconnecter.",
  615.                 "status" => 404,
  616.             ]));
  617.         }
  618.         // get token age
  619.         $dateDiff $token->getCreationDate()->diff(new DateTime());
  620.         // if the token if older than 7 days
  621.         if ($dateDiff->7) {
  622.             $entityManager->remove($token);
  623.             $entityManager->flush();
  624.             return $this->json([
  625.                 'message' => 'Ce token est obselète. Veuillez vous reconnecter.',
  626.                 'path' => 'src/Controller/ClientController.php',
  627.                 "status" => 401,
  628.             ]);
  629.         }
  630.         if (isset($_FILES["imageAvant"]) && $_FILES['imageAvant']['size'] != 0) {
  631.                 $newfilename $fileUploader->uploadFormData($_FILES["imageAvant"], "images/article/");
  632.                $article->setImgUrl($newfilename);
  633.         }
  634.        
  635.         $article->setTitle($_POST["titre"]);
  636.         $article->setSubTitle($_POST["sousTitre"]);
  637.         $article->setSlug($_POST["slug"]);
  638.         $article->setExtrait($_POST["extrait"]);
  639.         $article->setTexte($_POST["texte"]);
  640.         $article->setSource($_POST["source"]);
  641.         $article->setIsPro($_POST["type"]);
  642.         $article->setEtat($_POST["etat"]);
  643.         $article->setPublicationDate(new \DateTime());
  644.         $entityManager $this->getDoctrine()->getManager();
  645.         $entityManager->persist($article);
  646.         $entityManager->flush();
  647.         $categorieArticle $this->getDoctrine()
  648.         ->getRepository(ArticleCategorie::class)
  649.         ->findOneBy(['id_article' => $article]);
  650.         if ($_POST['categorie'] != $categorieArticle->getIdCategorie()->getId()){
  651.             $categorie $this->getDoctrine()
  652.             ->getRepository(Categorie::class)
  653.             ->findOneBy(['id' => intval($_POST["categorie"])]);
  654.             $categorieArticle->setIdCategorie($categorie);
  655.             $entityManager->persist($categorieArticle);
  656.             $entityManager->flush();
  657.         }
  658.       
  659.         return $this->json([
  660.             "status" => 200,
  661.         ]);
  662.     }
  663.     
  664.     /**
  665.      * @Route("/articles/publish", name="getAllArticlesPublish", methods={"GET","HEAD"})
  666.      */
  667.     public function getAllArticlesPublish(Request $request): Response
  668.     {
  669.             $articles $this->getDoctrine()
  670.                 ->getRepository(Article::class)
  671.                 ->findBy(['etat' => 0]);
  672.         $result = new ArrayCollection();
  673.         foreach ($articles as $article) {
  674.             $categorieResult = new ArrayCollection();
  675.             $articleCategoriesList $this->getDoctrine()
  676.                 ->getRepository(ArticleCategorie::class)
  677.                 ->findBy(["id_article" => $article->getId()]);
  678.             foreach ($articleCategoriesList as $articleCategorie) {
  679.                 $categorieResult->add([
  680.                     "id" => $articleCategorie->getIdCategorie()->getId(),
  681.                     "title" => $articleCategorie->getIdCategorie()->getTitle(),
  682.                 ]);
  683.             }
  684.             $tagResult = new ArrayCollection();
  685.             $articleTagList $this->getDoctrine()
  686.             ->getRepository(ArticleTag::class)
  687.             ->findBy(["id_article" => $article->getId()]);
  688.         foreach ($articleTagList as $articleTag) {
  689.             $tagResult->add([
  690.                 "id" => $articleTag->getIdTag()->getId(),
  691.                 "title" => $articleTag->getIdTag()->getTitle(),
  692.             ]);
  693.         }
  694.             $admin =  $this->getDoctrine()
  695.             ->getRepository(Administrateur::class)
  696.             ->findOneBy(["id" => $article->getIdAdmin()]);
  697.             $etat_libelle "";
  698.             switch ( $article->getEtat()){
  699.                 case 0:
  700.                     $etat_libelle "Publié";
  701.                     break;
  702.                 case 1:
  703.                     $etat_libelle "Brouillon";
  704.                     break;
  705.                 case 2:
  706.                     $etat_libelle "Suspendu";
  707.                     break;
  708.                 case 3:
  709.                     $etat_libelle "Corbeille";
  710.                     break;
  711.             }
  712.             $extrait $article->getExtrait();
  713.             if ($extrait == ""){
  714.                 $extrait strip_tags($article->getTexte());
  715.             }
  716.             $result->add([
  717.                 "id" => $article->getId(),
  718.                 "title" => $article->getTitle(),
  719.                 "subtitle" => $article->getSubtitle(),
  720.                 "date" => $article->getPublicationDate(),
  721.                 "source" => $article->getSource(),
  722.                 "imgUrl" => $article->getImgUrl(),
  723.                 'description' => $extrait,
  724.                 "category" => $categorieResult[0],
  725.                 "categoryId" =>$categorieResult[0]['id'],
  726.                 "categoryTitle"=>$categorieResult[0]['title'],
  727.                 "tag" => $tagResult,
  728.                 "admin" => $admin->getName().' '.$admin->getLastName(),
  729.                 "isPro" => $article->getIsPro(),
  730.                 "etat_libelle" => $etat_libelle,
  731.                 "etat" => $article->getEtat()
  732.             ]);
  733.         }
  734.         return $this->json([
  735.             "articles" => $result->toArray()
  736.         ]);
  737.     }
  738. /**
  739.      * @Route("/articles/test", name="getArticlesTest", methods={"GET","HEAD"})
  740.      */
  741.     public function getArticlesTest(Request $request): Response
  742.     {
  743.         if (!$request->query->get('nom')){
  744.             return $this->json([
  745.                 "list" => array(
  746.                     array( "nom" => 'Pierre'),
  747.                     array( "nom" => 'Paul')
  748.                 )
  749.             ]);
  750.         } else {
  751.             if ($request->query->get('nom') == "Paul") {
  752.                 return $this->json([
  753.                     "list" => array(
  754.                         array( "nom" => 'Paul')
  755.                     )
  756.                 ]);
  757.             } elseif ($request->query->get('nom') == "Pierre") {
  758.                 return $this->json([
  759.                     "list" => array(
  760.                         array( "nom" => 'Pierre')
  761.                     )
  762.                 ]);
  763.             } else {
  764.                 return $this->json([
  765.                     "list" => array(
  766.                         array( "nom" => 'Jacques'),
  767.                         array( "nom" => 'Paul')
  768.                     )
  769.                 ]);
  770.             }
  771.         }
  772.        
  773.     }
  774.     
  775. }