<?php
namespace App\Controller;
use App\Service\ArticleEntityService;
use App\Service\TagEntityService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class TagController extends AbstractController
{
/**
* @Route("/tags/{id}/articles/{page}", name="tags.articles", methods={"GET"}, defaults={"page": 1})
*/
public function getArticles(Request $request,TagEntityService $entityService, ArticleEntityService $articleEntityService, $id, $page)
{
$tag = $entityService->get($id);
if (!$tag || $tag->getDeletedAt() !== null) {
// return $this->json([
// 'error' => 'tag.not_found',
// 'message' => 'Tag not found',
// ]);
return $this->redirectToRoute('home');
}
$page = (int) $page;
$page = max(1, $page);
$limit = 100;
$articles = $articleEntityService->getArticlesByTag($tag, $limit, $page);
$count_articles = (int) $articleEntityService->countArticles($tag);
$pages_number = ceil($count_articles / $limit);
$isLastPage = $page === $pages_number;
if ($request->isXmlHttpRequest()) {
$articles = $articleEntityService->getArticlesByTag($tag, $page * 9, 1);
return $this->render('themes/theme-5/_partials/article/tag-articles.html.twig', [
'tag' => $tag,
'count_articles' => $count_articles,
'articles' => $articles,
'page' => $page,
'is_last_page' => $isLastPage,
]);
}
return $this->render('/tags/tags.html.twig', [
'tag' => $tag,
'count_articles' => $count_articles,
'articles' => array_slice($articles, 0, 9, true),
'page' => $page,
'is_last_page' => $isLastPage,
]);
}
}