src/Controller/ArticleController.php line 53
<?php
namespace App\Controller;
use App\Entity\Comment;
use App\Repository\ArticleRepository;
use App\Repository\TagRepository;
use App\Repository\CommentRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Asset\Package;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\UrlHelper;
use App\Service\DateManipulationService;
use App\Service\TextManipulationService;
use App\Service\ConstantsService;
use App\Service\ConfigurationService;
use App\Service\LinkedDatasJsonService;
use App\Service\GetImageSizeService;
use Knp\Component\Pager\PaginatorInterface;
use App\Form\PostCommentType;
use Doctrine\Persistence\ManagerRegistry as PersistenceManagerRegistry;
class ArticleController extends AbstractController
{
private $articleRepository;
public function __construct(
ArticleRepository $articleRepository,
TagRepository $tagRepository,
PaginatorInterface $paginator,
TextManipulationService $textManipulationService,
CommentRepository $commentRepository,
ConfigurationService $configurationService,
LinkedDatasJsonService $linkedDatasJsonService,
GetImageSizeService $getImageSizeService,
UrlHelper $urlHelper,
PersistenceManagerRegistry $doctrine,
) {
$this->articleRepository = $articleRepository;
$this->tagRepository = $tagRepository;
$this->paginator = $paginator;
$this->textManipulationService = $textManipulationService;
$this->commentRepository = $commentRepository;
$this->configurationService = $configurationService;
$this->linkedDatasJsonService = $linkedDatasJsonService;
$this->getImageSizeService = $getImageSizeService;
$this->urlHelper = $urlHelper;
$this->doctrine = $doctrine;
}
#[Route('/article/{slug}', name: 'article')]
public function article (
string $slug,
DateManipulationService $dateManipulationService,
Request $request
)
{
$article = $this->articleRepository->findOneBy(array("slug" => $slug));
$comments = $this->commentRepository->findBy(array("article" => $article), array("publicationDate" => "asc"));
if ($article->getMedia() !== null) {
$imageSize = $this->getImageSizeService->getSize("build/images/".$article->getMedia()->getMediaName());
$absoluteMediaUrl = $this->urlHelper->getAbsoluteUrl('buid/images/'.$article->getMedia()->getMediaName());
} else {
$imageSize = ['width' => 0, 'height' => 0];
$absoluteMediaUrl = null;
}
$comment = new Comment();
$comment->setArticle($article);
$commentForm = $this->createForm(PostCommentType::class, $comment);
$commentForm->get('article')->setData($article);
$jsonld = $this->linkedDatasJsonService-> getBlogPostingJsonLd(
$article->getTitle(),
$article->getMetaDescription(),
$article->getCreationDate()->format("Y-m-d"),
$article->getUpdateDate()->format("Y-m-d"),
$absoluteMediaUrl,
$imageSize['width'],
$imageSize['height'],
$article->getUser()->getRealUsername(),
$this->configurationService->getConfiguration()->getWebsiteName(),
$this->configurationService->getConfiguration()->getLogo()
);
$commentForm->handleRequest($request);
if ($commentForm->isSubmitted() && $commentForm->isValid()) {
$user = $this->getUser();
$comment->setArticle($article);
$comment->setUser($user);
$entityManager = $this->doctrine->getManager();
$entityManager->persist($comment);
$entityManager->flush();
$this->addFlash(
'success',
'Merci pour votre commentaire'
);
return $this->redirectToRoute($request->get('_route'), ['slug' => $slug]);
}
return $this->render('article/index.html.twig', [
'controller_name' => 'ArticleController',
'article' => $article,
'dateManipulationService' => $dateManipulationService,
'comments' => $comments,
'commentForm' => $commentForm->createView(),
'jsonld' => $jsonld,
'activeComments' => $this->configurationService->getConfiguration()->getActiveComments()
]);
}
#[Route('article/tag/{tag}/{page}', name: 'article_by_tag', requirements: ['page'=> '\d+'])]
public function articleByTag(string $tag, int $page = 1)
{
$tagData = $this->tagRepository->findBy(["name" => $tag]);
$articlesList = $tagData[0]->getArticles();
$articles = $this->paginator->paginate(
$articlesList,
$page,
ConstantsService::ARTICLES_PER_PAGE
);
$response = $this->render('article/tags.html.twig', [
'articles' => $articles,
'textManipulationService' => $this->textManipulationService,
'tag' => $tag
]);
$response->setSharedMaxAge(3600);
return $response;
}
}