<?php
namespace App\Controller\Page;
use App\Entity\Blog;
use App\Entity\Category;
use App\Entity\Employee;
use App\Filter\BlogFilter;
use App\Manager\BlogManager;
use App\Renderer\Page as PageRenderer;
use App\Service\BlogService;
use Flagception\Bundle\FlagceptionBundle\Annotations\Feature;
use FOS\HttpCacheBundle\Configuration\Tag;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
/**
* @Feature("feature_blog")
*/
#[Route(path: '/blog_posts')]
class BlogPostsController extends AbstractController
{
private BlogFilter $filter;
private PageRenderer $pageRenderer;
private BlogService $service;
/**
* BlogPostsController constructor.
*/
public function __construct(
BlogFilter $filter,
PageRenderer $pageRenderer,
BlogService $service
) {
$this->filter = $filter;
$this->pageRenderer = $pageRenderer;
$this->service = $service;
}
/**
* @Tag({"pages", "blog-overview", "blogs"})
*
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws \Throwable
*/
public function indexAction(BlogManager $blogManager, ParameterBagInterface $parameterBag, string $filters = null): Response
{
$selectedFilters = $blogManager->getSelectedFilters($filters);
$categories = $blogManager->getCategories();
$selectedCategoryTitle = $blogManager->getSelectedCategoryTitle($categories, $selectedFilters);
return $this->pageRenderer
->renderPage(
'{blogs}',
'@default/pages/blog_overview.html.twig',
[
'limitPerPage' => $parameterBag->get('site_blog_limit'),
'categories' => $categories,
'selectedFilters' => $selectedFilters,
'selectedCategoryTitle' => $selectedCategoryTitle,
]
);
}
/**
* @Tag("blogs")
*/
#[Route(path: '/get', name: 'fetch_categorized_blogs')]
public function fetchCategorizedBlogs(Request $request): Response
{
$categories = [];
if ($request->query->get('categories')) {
foreach ($request->query->get('categories') as $category) {
$category = $this->getDoctrine()->getRepository(Category::class)->find($category);
if (!$category) {
continue;
}
$categories[] = $category->getSlug();
}
}
$limit = $request->get('limit', 4);
$blogs = $this->filter->filterBlogPosts($this->getDoctrine()->getRepository(Blog::class)
->findBy([], ['createdAt' => 'DESC'], $limit), $categories);
$blogFilteredVacancyQueries = $this->service->getFilteredVacancyQueries($blogs);
return $this->render(
'@default/pages/categorized_blogs.html.twig',
compact('blogs', 'blogFilteredVacancyQueries')
);
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
public function postsByEmployee(Employee $employee, ParameterBagInterface $parameterBag): Response
{
return $this->pageRenderer
->renderPage(
'{blogs}',
'@default/pages/blog_by_employee_overview.html.twig',
[
'employee' => $employee,
'limitPerPage' => $parameterBag->get('site_blog_limit'),
'categories' => [],
]
);
}
}