<?php
namespace App\Controller;
use App\Entity\BlogArticle;
use App\Repository\BlogArticleRepository;
use App\Repository\BlogCategoryRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Common\Collections\Criteria;
/**
* @Route("/blog")
*/
class BlogController extends AbstractController {
/**
* Blog Article List.
* @Route(
* "/list/{page}/{sort}/{dir}",
* name="client_blog_article_list",
* methods={"GET","POST"},
* requirements={"page"="\d+","sort"="[0-9a-zA-Z\.]+","dir"="asc|desc"},
* defaults={"page"=1,"sort"="createdAt","dir"="desc"}
* )
*
* @return string
*/
public function list(
Request $request, $page, $sort, $dir,
\App\Service\SeoHelper $seoHelper,
BlogArticleRepository $blogArticleRepository
): Response {
$limit = $request->query->getInt('limit', 20);
$seoHelper->setInternalSeo('Blog Articles', 'client_blog_article_list');
$criteria = Criteria::create();
$criteria->andWhere(Criteria::expr()->eq('isEnabled', true));
$criteria->orderBy([$sort => $dir]);
//$totalCriteria->andWhere(Criteria::expr()->eq('isDeceased', false));
$entries = $blogArticleRepository->matching($criteria);
$entries = $entries->slice(($page-1)*$limit, $limit);
$total = $blogArticleRepository->countAll();
$pages = ceil($total/$limit);
return $this->render('client/blog/list.html.twig', [
'title' => 'Blog Articles',
'listParams' => [],
'entries' => $entries,
'ordering' => [
'sort' => $sort,
'dir' => $dir
],
'meta' => [
'title' => 'Blog Articles',
'type' => 'website',
],
'page' => [
'icon' => 'list',
'title' => 'Blog Articles',
'subtitle' => 'View the list with the Blog Articles',
'header' => 'Training Manager - Blog Articles List'
],
'pagination' => [
'listURL' => 'client_blog_article_list',
'totalSearch' => $total,
'total' => $total,
'limit' => $limit,
'totalPages' => $pages,
'page' => $page,
],
]);
}
/**
* Blog Category Article List.
* @Route(
* "/category/{slug}/{page}/{sort}/{dir}",
* name="client_blog_category_article_list",
* methods={"GET","POST"},
* requirements={"page"="\d+","sort"="[0-9a-zA-Z\.]+","dir"="asc|desc"},
* defaults={"page"=1,"sort"="createdAt","dir"="desc"}
* )
*
* @return string
*/
public function category(
Request $request, $page, $sort, $dir, $slug,
\App\Service\SeoHelper $seoHelper,
BlogCategoryRepository $blogCategoryRepository
): Response {
$limit = $request->query->getInt('limit', 20);
$category = $blogCategoryRepository->findOneBy(["slug" => $slug, "isEnabled" => true], ["createdAt" => "Desc"]);
if (!$category) throw $this->createNotFoundException('The blog category does not exist');
$seoHelper->setBlogCategorySeo($category);
$entries = $category->getArticles();
/*$criteria = Criteria::create();
$criteria->andWhere(Criteria::expr()->eq('isEnabled', true));
$criteria->andWhere(Criteria::expr()->eq('slug', $slug));
$criteria->orderBy([$sort => $dir]);*/
//$entries = $blogArticleRepository->matching($criteria);
$entries = $entries->slice(($page-1)*$limit, $limit);
$total = $category->getArticles()->count();
$pages = ceil($total/$limit);
return $this->render('client/blog/list.html.twig', [
'title' => $category->getTitle(),
'listParams' => [],
'entries' => $entries,
'ordering' => [
'sort' => $sort,
'dir' => $dir
],
'page' => [
'icon' => 'list',
'title' => 'Blog Articles',
'subtitle' => 'View the list with the Blog Articles',
'header' => 'Training Manager - Blog Articles List',
],
'meta' => [
'title' => $category->getTitle(),
'type' => 'website',
'description' => $category->getMetaDescription(),
'keywords' => $category->getMetaKeywords()
],
'pagination' => [
'listURL' => 'client_blog_article_list',
'totalSearch' => $total,
'total' => $total,
'limit' => $limit,
'totalPages' => $pages,
'page' => $page,
],
]);
}
/**
* @Route("/view/{slug}", name="client_blog_article_view", methods={"GET"})
*/
public function show(
BlogArticleRepository $blogArticleRepository,
\App\Service\SeoHelper $seoHelper,
$slug
): Response {
$article = $blogArticleRepository->findOneBy(['slug' => $slug, 'isEnabled' => true]);
if (!$article) throw $this->createNotFoundException('The blog article does not exist');
$seoHelper->setArticleSeo($article);
return $this->render('client/blog/show.html.twig', [
'title' => $article->getTitle(),
'page' => [
'icon' => 'star',
'title' => $article->getTitle()
],
'entry' => $article,
]);
}
}