src/Controller/Front/BlogController.php line 158

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Article;
  4. use App\Entity\Comment;
  5. use App\Entity\Category;
  6. use App\Form\CommentType;
  7. use App\Entity\SearchArticle;
  8. use App\Form\SearchArticleType;
  9. use App\EmailNotification\ToAdmin;
  10. use App\Repository\AdminRepository;
  11. use App\Repository\ArticleRepository;
  12. use App\Repository\CategoryRepository;
  13. use Doctrine\Persistence\ObjectManager;
  14. use Knp\Component\Pager\PaginatorInterface;
  15. use App\Repository\WebsiteLanguageRepository;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. #[Route(path'/{_locale}/blog')]
  24. class BlogController extends AbstractController
  25. {
  26.     private $translator;
  27.     public function __construct(TranslatorInterface $translator)
  28.     {
  29.         $this->translator $translator;
  30.     }
  31.     #[Route(path'/'name'blog')]
  32.     public function blog(ArticleRepository $articleRepositoryCategoryRepository $categoryRepositoryRequest $requestPaginatorInterface $paginatorWebsiteLanguageRepository $websiteLanguageRepository): Response
  33.     {
  34.         $search = new SearchArticle();
  35.         
  36.         $form $this->createForm(SearchArticleType::class, $search);
  37.         $form->handleRequest($request);
  38.         $locale = ($request->getLocale() ? $request->getLocale() : 'en');
  39.         if ($form->isSubmitted() && $form->isValid()) {
  40.             $articles $paginator->paginate(
  41.                 $articleRepository->getSearchedArticles($locale$search),
  42.                 $request->query->getInt('page'1),
  43.                 16
  44.             );
  45.             $featured null;
  46.             $latest null;
  47.             $title 'blog.search_results';
  48.             $render 'front/blog/list-articles.html.twig';
  49.         } 
  50.         
  51.         else {
  52.             $featured $articleRepository->getFourFeaturedArticles($locale);
  53.             $latest $articleRepository->getLatestArticles($locale8);
  54.             $articles null;
  55.             $title 'blog.title';
  56.             $render 'front/blog/index.html.twig';
  57.         }
  58.         
  59.         $categories $categoryRepository->getCategoriesByLocale($locale);
  60.         
  61.         return $this->render($render, [
  62.             'menu' => 'blog',
  63.             'featured' => $featured,
  64.             'latest' => $latest,
  65.             'form' => $form->createView(),
  66.             'title' => $title,
  67.             'categories' => $categories,
  68.             'articles' => $articles
  69.         ]);
  70.     }
  71.     
  72.     #[Route(path'/latest-news'name'latest_news')]
  73.     public function latestNews(ArticleRepository $articleRepositoryRequest $requestPaginatorInterface $paginator): Response
  74.     {
  75.         $search = new SearchArticle();
  76.         
  77.         $form $this->createForm(SearchArticleType::class, $search);
  78.         $form->handleRequest($request);
  79.         $locale = ($request->getLocale() ? $request->getLocale() : 'en');
  80.         if ($form->isSubmitted() && $form->isValid()) {
  81.             $articles $paginator->paginate(
  82.                 $articleRepository->getSearchedArticles($locale$search),
  83.                 $request->query->getInt('page'1),
  84.                 16
  85.             );
  86.             $title 'blog.search_results';
  87.         } else {
  88.             $articles $paginator->paginate(
  89.                 $articleRepository->getLatestNews($locale),
  90.                 $request->query->getInt('page'1),
  91.                 16
  92.             );
  93.             $title 'blog.latest';
  94.         }
  95.         
  96.         return $this->render('front/blog/list-articles.html.twig', [
  97.             'menu' => 'blog',
  98.             'title' => $title,
  99.             'articles' => $articles,
  100.             'form' => $form->createView()
  101.         ]);
  102.     }
  103.     
  104.     #[Route(path'/{slug}'name'article_category')]
  105.     public function categoryArticles(Category $categoryArticleRepository $articleRepositoryRequest $requestPaginatorInterface $paginator): Response
  106.     {
  107.         $search = new SearchArticle();
  108.         
  109.         $form $this->createForm(SearchArticleType::class, $search);
  110.         $form->handleRequest($request);
  111.         $locale = ($request->getLocale() ? $request->getLocale() : 'en');
  112.         if ($form->isSubmitted() && $form->isValid()) {
  113.             $articles $paginator->paginate(
  114.                 $articleRepository->getSearchedCategoryArticles($locale$search$category),
  115.                 $request->query->getInt('page'1),
  116.                 16
  117.             );
  118.             $title 'blog.search_results';
  119.         } 
  120.         
  121.         else {
  122.             $articles $paginator->paginate(
  123.                 $articleRepository->findCategoryArticles($locale$category),
  124.                 $request->query->getInt('page'1),
  125.                 16
  126.             );
  127.             $title $category->getTitle();
  128.         }
  129.         return $this->render('front/blog/list-articles.html.twig', [
  130.             'menu' => 'blog',
  131.             'title' => $title,
  132.             'articles' => $articles,
  133.             'form' => $form->createView()
  134.         ]);
  135.     }
  136.     #[Route(path'/articles/{slug}'name'show_article')]
  137.     public function showArticle($slugArticleRepository $articleRepositoryRequest $requestEntityManagerInterface $managerAdminRepository $adminRepositoryToAdmin $toAdmin): Response
  138.     {
  139.         $article $articleRepository->findOneBy(['slug' => $slug]);
  140.         if (!$article) {
  141.             return $this->redirectToRoute('blog');
  142.         }
  143.         $locale = ($request->getLocale() ? $request->getLocale() : 'fr');
  144.         if ($article->getWebsiteLanguage()->getSlug() != $locale) {
  145.             $articleInLocale $articleRepository->getLocaleArticleByUnit($article->getUnit(), $locale);
  146.             if (!$articleInLocale) {
  147.                 return $this->redirectToRoute('blog');
  148.             }
  149.             return $this->redirectToRoute('show_article', ['slug' => $articleInLocale->getSlug()]);
  150.         }
  151.         $moreArticles $articleRepository->getNumberOfCategorizedArticles($article->getWebsiteLanguage()->getSlug() ,$article->getCategories(), 2$article->getSlug());
  152.         
  153.         $admins $adminRepository->getActiveAdmins();
  154.         $article->setViews($article->getViews() + 1);
  155.         $manager->persist($article);
  156.         $manager->flush();
  157.         $comment = new Comment();
  158.         $comment->setArticle($article)
  159.                 ->setActivated(false)
  160.                 ->setLanguage($request->getLocale());
  161.         $form $this->createForm(CommentType::class, $comment);
  162.         $form->handleRequest($request);
  163.         if ($form->isSubmitted() && $form->isValid()) {
  164.             $homepageUrl $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL);
  165.             if (substr($homepageUrl, -1) == '/') {
  166.                 $homepageUrl substr($homepageUrl0, -1);
  167.             }
  168.             if ($request->server->get('HTTP_ORIGIN') != null  && $request->server->get('HTTP_ORIGIN') .'/'$request->getLocale() == $homepageUrl && $comment->getWebsite() == null) {
  169.                 $comment->setCreatedAt(new \DateTime())
  170.                         ->setStatus('new');
  171.                 
  172.                 $manager->persist($comment);
  173.                 $manager->flush();
  174.     
  175.                 foreach ($admins as $admin) {
  176.                     $toAdmin->newComment($admin$comment);
  177.                 }
  178.     
  179.                 $this->addFlash(
  180.                     'success',
  181.                     $this->translator->trans('flashes.blog_controller.add_comment')
  182.                 );
  183.     
  184.                 return $this->redirectToRoute('show_article', ['slug' => $article->getSlug()]);
  185.             }
  186.         }
  187.         
  188.         return $this->render('front/blog/article.html.twig', [
  189.             'menu' => 'blog',
  190.             'moreArticles' => $moreArticles,
  191.             'article' => $article,
  192.             'form' => $form->createView(),
  193.             'preview' => false
  194.         ]);
  195.     }
  196. }