src/Controller/ArticleController.php line 53

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Comment;
  4. use App\Repository\ArticleRepository;
  5. use App\Repository\TagRepository;
  6. use App\Repository\CommentRepository;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Asset\Package;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\UrlHelper;
  12. use App\Service\DateManipulationService;
  13. use App\Service\TextManipulationService;
  14. use App\Service\ConstantsService;
  15. use App\Service\ConfigurationService;
  16. use App\Service\LinkedDatasJsonService;
  17. use App\Service\GetImageSizeService;
  18. use Knp\Component\Pager\PaginatorInterface;
  19. use App\Form\PostCommentType;
  20. use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;
  21. class ArticleController extends AbstractController
  22. {
  23.     private $articleRepository;
  24.     
  25.     public function __construct(
  26.         ArticleRepository $articleRepository,
  27.         TagRepository $tagRepository,
  28.         PaginatorInterface $paginator,
  29.         TextManipulationService $textManipulationService,
  30.         CommentRepository $commentRepository,
  31.         ConfigurationService $configurationService,
  32.         LinkedDatasJsonService $linkedDatasJsonService,
  33.         GetImageSizeService $getImageSizeService,
  34.         UrlHelper $urlHelper,
  35.         PersistenceManagerRegistry $doctrine,
  36.     ) {
  37.         $this->articleRepository $articleRepository;
  38.         $this->tagRepository $tagRepository;
  39.         $this->paginator $paginator;
  40.         $this->textManipulationService $textManipulationService;
  41.         $this->commentRepository $commentRepository;
  42.         $this->configurationService $configurationService;
  43.         $this->linkedDatasJsonService $linkedDatasJsonService;
  44.         $this->getImageSizeService $getImageSizeService;
  45.         $this->urlHelper $urlHelper;
  46.         $this->doctrine $doctrine;
  47.     }
  48.     #[Route('/article/{slug}'name'article')]
  49.     public function article (
  50.         string $slug,
  51.         DateManipulationService $dateManipulationService,
  52.         Request $request
  53.     )
  54.     {
  55.         $article $this->articleRepository->findOneBy(array("slug" => $slug));
  56.         $comments $this->commentRepository->findBy(array("article" => $article), array("publicationDate" => "asc"));
  57.         if ($article->getMedia() !== null) {
  58.             $imageSize $this->getImageSizeService->getSize("build/images/".$article->getMedia()->getMediaName());
  59.             $absoluteMediaUrl $this->urlHelper->getAbsoluteUrl('buid/images/'.$article->getMedia()->getMediaName());
  60.         } else {
  61.             $imageSize = ['width' => 0'height' => 0];
  62.             $absoluteMediaUrl null;
  63.         }
  64.         $comment = new Comment();
  65.         $comment->setArticle($article);
  66.         $commentForm $this->createForm(PostCommentType::class, $comment);
  67.         $commentForm->get('article')->setData($article);
  68.         
  69.         $jsonld $this->linkedDatasJsonService-> getBlogPostingJsonLd(
  70.             $article->getTitle(),
  71.             $article->getMetaDescription(),
  72.             $article->getCreationDate()->format("Y-m-d"),
  73.             $article->getUpdateDate()->format("Y-m-d"),
  74.             $absoluteMediaUrl,
  75.             $imageSize['width'],
  76.             $imageSize['height'],
  77.             $article->getUser()->getRealUsername(),
  78.             $this->configurationService->getConfiguration()->getWebsiteName(),
  79.             $this->configurationService->getConfiguration()->getLogo()
  80.         );
  81.         $commentForm->handleRequest($request);
  82.         if ($commentForm->isSubmitted() && $commentForm->isValid()) {
  83.             $user $this->getUser();
  84.             $comment->setArticle($article);
  85.             $comment->setUser($user);
  86.             $entityManager $this->doctrine->getManager();
  87.             $entityManager->persist($comment);
  88.             $entityManager->flush();
  89.             $this->addFlash(
  90.                 'success',
  91.                 'Merci pour votre commentaire'
  92.             );
  93.             return $this->redirectToRoute($request->get('_route'), ['slug' => $slug]);
  94.         }
  95.         return $this->render('article/index.html.twig', [
  96.             'controller_name' => 'ArticleController',
  97.             'article' => $article,
  98.             'dateManipulationService' => $dateManipulationService,
  99.             'comments' => $comments,
  100.             'commentForm' => $commentForm->createView(),
  101.             'jsonld' => $jsonld,
  102.             'activeComments' => $this->configurationService->getConfiguration()->getActiveComments()
  103.         ]);
  104.     }
  105.     #[Route('article/tag/{tag}/{page}'name'article_by_tag'requirements: ['page'=> '\d+'])]
  106.     public function articleByTag(string $tagint $page 1)
  107.     {
  108.         $tagData $this->tagRepository->findBy(["name" => $tag]);
  109.         $articlesList $tagData[0]->getArticles();
  110.         
  111.         $articles $this->paginator->paginate(
  112.             $articlesList,
  113.             $page,
  114.             ConstantsService::ARTICLES_PER_PAGE
  115.         );
  116.         
  117.         $response =  $this->render('article/tags.html.twig', [
  118.             'articles' => $articles,
  119.             'textManipulationService' => $this->textManipulationService,
  120.             'tag' => $tag
  121.         ]);
  122.         $response->setSharedMaxAge(3600);
  123.         return $response;
  124.     }
  125. }