<?php
namespace App\Controller\Front;
use App\Entity\Article;
use App\Entity\Comment;
use App\Entity\Category;
use App\Form\CommentType;
use App\Entity\SearchArticle;
use App\Form\SearchArticleType;
use App\EmailNotification\ToAdmin;
use App\Repository\AdminRepository;
use App\Repository\ArticleRepository;
use App\Repository\CategoryRepository;
use Doctrine\Persistence\ObjectManager;
use Knp\Component\Pager\PaginatorInterface;
use App\Repository\WebsiteLanguageRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route(path: '/{_locale}/blog')]
class BlogController extends AbstractController
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
#[Route(path: '/', name: 'blog')]
public function blog(ArticleRepository $articleRepository, CategoryRepository $categoryRepository, Request $request, PaginatorInterface $paginator, WebsiteLanguageRepository $websiteLanguageRepository): Response
{
$search = new SearchArticle();
$form = $this->createForm(SearchArticleType::class, $search);
$form->handleRequest($request);
$locale = ($request->getLocale() ? $request->getLocale() : 'en');
if ($form->isSubmitted() && $form->isValid()) {
$articles = $paginator->paginate(
$articleRepository->getSearchedArticles($locale, $search),
$request->query->getInt('page', 1),
16
);
$featured = null;
$latest = null;
$title = 'blog.search_results';
$render = 'front/blog/list-articles.html.twig';
}
else {
$featured = $articleRepository->getFourFeaturedArticles($locale);
$latest = $articleRepository->getLatestArticles($locale, 8);
$articles = null;
$title = 'blog.title';
$render = 'front/blog/index.html.twig';
}
$categories = $categoryRepository->getCategoriesByLocale($locale);
return $this->render($render, [
'menu' => 'blog',
'featured' => $featured,
'latest' => $latest,
'form' => $form->createView(),
'title' => $title,
'categories' => $categories,
'articles' => $articles
]);
}
#[Route(path: '/latest-news', name: 'latest_news')]
public function latestNews(ArticleRepository $articleRepository, Request $request, PaginatorInterface $paginator): Response
{
$search = new SearchArticle();
$form = $this->createForm(SearchArticleType::class, $search);
$form->handleRequest($request);
$locale = ($request->getLocale() ? $request->getLocale() : 'en');
if ($form->isSubmitted() && $form->isValid()) {
$articles = $paginator->paginate(
$articleRepository->getSearchedArticles($locale, $search),
$request->query->getInt('page', 1),
16
);
$title = 'blog.search_results';
} else {
$articles = $paginator->paginate(
$articleRepository->getLatestNews($locale),
$request->query->getInt('page', 1),
16
);
$title = 'blog.latest';
}
return $this->render('front/blog/list-articles.html.twig', [
'menu' => 'blog',
'title' => $title,
'articles' => $articles,
'form' => $form->createView()
]);
}
#[Route(path: '/{slug}', name: 'article_category')]
public function categoryArticles(Category $category, ArticleRepository $articleRepository, Request $request, PaginatorInterface $paginator): Response
{
$search = new SearchArticle();
$form = $this->createForm(SearchArticleType::class, $search);
$form->handleRequest($request);
$locale = ($request->getLocale() ? $request->getLocale() : 'en');
if ($form->isSubmitted() && $form->isValid()) {
$articles = $paginator->paginate(
$articleRepository->getSearchedCategoryArticles($locale, $search, $category),
$request->query->getInt('page', 1),
16
);
$title = 'blog.search_results';
}
else {
$articles = $paginator->paginate(
$articleRepository->findCategoryArticles($locale, $category),
$request->query->getInt('page', 1),
16
);
$title = $category->getTitle();
}
return $this->render('front/blog/list-articles.html.twig', [
'menu' => 'blog',
'title' => $title,
'articles' => $articles,
'form' => $form->createView()
]);
}
#[Route(path: '/articles/{slug}', name: 'show_article')]
public function showArticle($slug, ArticleRepository $articleRepository, Request $request, EntityManagerInterface $manager, AdminRepository $adminRepository, ToAdmin $toAdmin): Response
{
$article = $articleRepository->findOneBy(['slug' => $slug]);
if (!$article) {
return $this->redirectToRoute('blog');
}
$locale = ($request->getLocale() ? $request->getLocale() : 'fr');
if ($article->getWebsiteLanguage()->getSlug() != $locale) {
$articleInLocale = $articleRepository->getLocaleArticleByUnit($article->getUnit(), $locale);
if (!$articleInLocale) {
return $this->redirectToRoute('blog');
}
return $this->redirectToRoute('show_article', ['slug' => $articleInLocale->getSlug()]);
}
$moreArticles = $articleRepository->getNumberOfCategorizedArticles($article->getWebsiteLanguage()->getSlug() ,$article->getCategories(), 2, $article->getSlug());
$admins = $adminRepository->getActiveAdmins();
$article->setViews($article->getViews() + 1);
$manager->persist($article);
$manager->flush();
$comment = new Comment();
$comment->setArticle($article)
->setActivated(false)
->setLanguage($request->getLocale());
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$homepageUrl = $this->generateUrl('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL);
if (substr($homepageUrl, -1) == '/') {
$homepageUrl = substr($homepageUrl, 0, -1);
}
if ($request->server->get('HTTP_ORIGIN') != null && $request->server->get('HTTP_ORIGIN') .'/'. $request->getLocale() == $homepageUrl && $comment->getWebsite() == null) {
$comment->setCreatedAt(new \DateTime())
->setStatus('new');
$manager->persist($comment);
$manager->flush();
foreach ($admins as $admin) {
$toAdmin->newComment($admin, $comment);
}
$this->addFlash(
'success',
$this->translator->trans('flashes.blog_controller.add_comment')
);
return $this->redirectToRoute('show_article', ['slug' => $article->getSlug()]);
}
}
return $this->render('front/blog/article.html.twig', [
'menu' => 'blog',
'moreArticles' => $moreArticles,
'article' => $article,
'form' => $form->createView(),
'preview' => false
]);
}
}