<?php
namespace App\Package\Openform\Front\Controller;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Package\Openform\Front\Controller\BaseController;
use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Package\Openform\RepositoryFront\FrontAboutUsRepository;
use App\Package\Openform\RepositoryFront\FrontNewsRepository;
use Symfony\Component\Intl\Locales;
/**
* PageController
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright 2019 Openform
* @since 07.2019
*/
class AboutUsController extends BaseController
{
protected $routeLocalizer;
private $em;
private $aboutUsRepo;
private $newsRepo;
private $pageRepo;
private $perPage;
public function __construct(RequestStack $request, RouteLocalizer $routeLocalizer, EntityManagerInterface $em)
{
parent::__construct($request, $routeLocalizer, $em);
$this->em = $em;
$this->routeLocalizer = $routeLocalizer;
$this->newsRepo = $this->em->getRepository('Openform:News')->getFrontRepository();
$this->pageRepo = $this->em->getRepository('Openform:Page')->getFrontRepository();
$this->perPage = 12;
}
public function indexAction(Request $request)
{
$locale = $request->getLocale();
$pageEntity = $this->getPageByIdname('page_about_us', $request->getLocale());
$parentPageEntity = $this->getPageByIdname('home', $locale);
if (!$pageEntity) {
throw new NotFoundHttpException();
}
$defaultFilters = $this->getDefaultFilters(new \stdClass());
$filters = $this->getUrlFilters($defaultFilters, $request);
$entities = $this->newsRepo->getVisibleByFilter(
$locale,
//$request->query->get('page') ?? 1,
1,
//$this->perPage,
6,
clone $filters
);
$entitiesCount = $this->newsRepo->getVisibleByFilterCount($locale, clone $filters);
return $this->render('@openform_front_templates/AboutUs/index.html.twig', [
'entity' => $pageEntity,
'parentPage' => $parentPageEntity,
'entities' => $entities,
'entitiesCount' => $entitiesCount,
'maxPages' => ceil($entitiesCount / $this->perPage),
'filters' => $filters,
]);
}
public function filterAjaxAction(Request $request): Response
{
$locale = $request->getLocale();
$requestBody = $request->getContent() ? json_decode($request->getContent()) : null;
if ( !$requestBody) {
throw new NotFoundHttpException('No requestBody');
}
if (!$requestBody->filters) {
throw new NotFoundHttpException('No requestBody->filters');
}
$defaultFilters = $this->getDefaultFilters($requestBody ? $requestBody->filters : new \StdClass);
$filters = $this->parseFilters($defaultFilters);
$entities = $this->aboutUsRepo->getVisibleByFilter(
$locale,
$requestBody->page ?? 1,
$this->perPage,
clone $filters
);
$entitiesCount = $this->aboutUsRepo->getVisibleByFilterCount($locale, clone $filters);
$maxPages = ceil($entitiesCount / $this->perPage);
// $filtersHtml = $this->renderView('@openform_front_templates/AboutUs/components/_filters.html.twig', [
// 'filters' => $filters,
// 'maxPages' => $maxPages,
// ]);
$entitiesHtml = $this->renderView('@openform_front_templates/AboutUs/components/_list.html.twig', [
'entities' => $entities,
'entitiesCount' => $entitiesCount,
'maxPages' => $maxPages,
]);
return new JsonResponse(array(
// 'filtersHtml' => $filtersHtml,
'entitiesHtml' => $entitiesHtml,
'maxPages' => $maxPages,
));
}
public function itemAction(Request $request, string $slug)
{
$locale = $request->getLocale();
$parentPage = $this->getPageByIdname('page_about_us', $locale);
$repo = $this->em->getRepository('Openform:AboutUs')->getFrontRepository();
$entity = $repo->getVisibleBySlug($slug, $locale);
if (!$entity) {
throw new NotFoundHttpException();
}
return $this->render('@openform_front_templates/AboutUs/item.html.twig', [
'entity' => $entity,
'parentPage' => $parentPage,
// 'langLinks' => $this->generateLangLinksAboutUs($entity),
]);
}
private function getDefaultFilters(\stdClass $filters = null): \stdClass
{
if (!$filters) {
$filters = new \stdClass();
}
//$filters->sort = FrontAboutUsRepository::ORDER_DATE_DESC;
$filters->sort = FrontNewsRepository::ORDER_DATE_DESC;
return $filters;
}
private function getUrlFilters(\stdClass $filters = null, Request $request): \stdClass
{
if ($request->query->get('dateFrom')) {
$filters->dateFrom = $request->query->get('dateFrom');
}
return $filters;
}
private function parseFilters(\stdClass $filters): \stdClass
{
foreach ($filters as $filter => $value) {
if (!$value) {
unset($filters->$filter);
}
}
return $filters;
}
protected function generateLangLinksAboutUs( $entity)
{
$repo = $this->em->getRepository('Openform:AboutUsTranslation');
$translations = $repo->findBy(['translatableId' => $entity->getId()]);
$langLinks = [];
foreach ($translations as $transItem){
if (strlen($transItem->getSlug()) > 0) {
$url = $this->routeLocalizer->generate('about_us_item', ['slug' => $transItem->getSlug()], $transItem->getLang());
$langLinks[$transItem->getLang()] = $url;
}
}
return $langLinks;
}
}