<?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 Symfony\Component\Intl\Locales;
/**
* PageController
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright 2019 Openform
* @since 07.2019
*/
class ExhibitionController extends BaseController
{
protected $routeLocalizer;
private $em;
private $pageRepo;
private $exhibitionRepo;
private $exhibitionDepartmentRepo;
private $perPage;
public function __construct(RequestStack $request, RouteLocalizer $routeLocalizer, EntityManagerInterface $em)
{
parent::__construct($request, $routeLocalizer, $em);
$this->em = $em;
$this->routeLocalizer = $routeLocalizer;
$this->pageRepo = $this->em->getRepository('Openform:Page')->getFrontRepository();
$this->exhibitionRepo = $this->em->getRepository('Openform:Exhibition')->getFrontRepository();
$this->exhibitionDepartmentRepo = $this->em->getRepository('Openform:ExhibitionDepartment')->getFrontRepository();
$this->perPage = 12;
}
public function indexAction(Request $request)
{
$locale = $request->getLocale();
$pageEntity = $this->getPageByIdName('page_exhibitions', $locale);
if (!$pageEntity) {
throw new NotFoundHttpException();
}
$defaultFilters = $this->getDefaultFilters(get_class($this->exhibitionRepo), new \stdClass());
$filters = $this->getUrlFilters($defaultFilters, $request);
$page = $request->query->get('page') ?? 1;
$entities = $this->exhibitionRepo->getVisibleByFilter(
$locale,
$page,
$this->perPage,
clone $filters
);
$entitiesCount = $this->exhibitionRepo->getVisibleByFilterCount($locale, clone $filters);
return $this->render('@openform_front_templates/Exhibition/index.html.twig', [
'entity' => $pageEntity,
'parentPage' => $pageEntity->getParent(),
'entities' => $entities,
'entitiesCount' => $entitiesCount,
'page' => $page,
'perPage' => $this->perPage,
'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(get_class($this->exhibitionRepo), $requestBody ? $requestBody->filters : new \StdClass);
$filters = $this->parseFilters($defaultFilters);
$page = $requestBody->page ?? 1;
$entities = $this->exhibitionRepo->getVisibleByFilter(
$locale,
$page,
$this->perPage,
clone $filters
);
$entitiesCount = $this->exhibitionRepo->getVisibleByFilterCount($locale, clone $filters);
$maxPages = ceil($entitiesCount / $this->perPage);
$filtersHtml = $this->renderView('@openform_front_templates/Exhibition/_filters.html.twig', [
'filters' => $filters,
]);
$entitiesHtml = $this->renderView('@openform_front_templates/Exhibition/_list.html.twig', [
'entities' => $entities,
'entitiesCount' => $entitiesCount,
'page' => $page,
'maxPages' => $maxPages,
'perPage' => $this->perPage,
]);
return new JsonResponse(array(
'filtersHtml' => $filtersHtml,
'entitiesHtml' => $entitiesHtml,
'entitiesCount' => $entitiesCount,
'page' => $page,
'maxPages' => $maxPages,
'perPage' => $this->perPage,
'filters' => $filters,
));
}
public function itemAction(Request $request, string $slug)
{
$locale = $request->getLocale();
$parentPage = $this->getPageByIdName('page_exhibitions', $locale);
$entity = $this->exhibitionRepo->getVisibleBySlug($slug, $locale);
if (!$entity) {
throw new NotFoundHttpException();
}
$rootParent = \App\Package\Openform\Entity\RelictPack::getRootParent($entity);
$request->attributes->set('makSlug', $rootParent->getMakSlug($locale));
return $this->render('@openform_front_templates/Exhibition/item.html.twig', [
'entity' => $entity,
'parentPage' => $parentPage,
'isDepartment' => false,
'rootParent' => $rootParent
]);
}
public function departmentItemAction(Request $request, string $slug)
{
$locale = $request->getLocale();
$parentPage = $this->getPageByIdName('page_exhibitions', $locale);
$entity = $this->exhibitionDepartmentRepo->getVisibleBySlug($slug, $locale);
if (!$entity) {
throw new NotFoundHttpException();
}
$rootParent = \App\Package\Openform\Entity\RelictPack::getRootParent($entity);
$request->attributes->set('makSlug', $rootParent->getMakSlug($locale));
return $this->render('@openform_front_templates/Exhibition/item.html.twig', [
'entity' => $entity,
'parentPage' => $parentPage,
'isDepartment' => true,
'rootParent' => $rootParent
]);
}
private function getDefaultFilters($entityClass, \stdClass $filters = null): \stdClass
{
if (!$filters) {
$filters = new \stdClass();
}
if (!property_exists($filters, 'sort')) {
$filters->sort = $entityClass::ORDER_ORD_DESC;
}
return $filters;
}
private function getUrlFilters(\stdClass $filters = null, Request $request): \stdClass
{
if ($request->query->get('dateFrom')) {
$filters->dateFrom = $request->query->get('dateFrom');
}
if ($request->query->get('sort')) {
$filters->sort = $request->query->get('sort');
}
return $filters;
}
private function parseFilters(\stdClass $filters): \stdClass
{
foreach ($filters as $filter => $value) {
if (!$value) {
unset($filters->$filter);
}
}
return $filters;
}
}