<?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;
use \App\Package\Openform\Entity\PrivateCollectionDepartment;
use \App\Package\Openform\Entity\ArchiveDepartment;
use \App\Package\Openform\Entity\MuseumDepartment;
/**
* PageController
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright 2019 Openform
* @since 07.2019
*/
class MakController extends BaseController
{
protected $routeLocalizer;
private $em;
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->pageRepo = $this->em->getRepository('Openform:Page')->getFrontRepository();
$this->perPage = 12;
}
public function indexAction(Request $request)
{
$locale = $request->getLocale();
$pageEntity = $this->pageRepo->getVisibleBySlug($request->attributes->get('makSlug'), $locale);
if (!$pageEntity) {
throw new NotFoundHttpException();
}
$makRepo = $this->prepareMakRepo($pageEntity->getIdname());
$defaultFilters = $this->getDefaultFilters(get_class($makRepo), new \stdClass());
$filters = $this->getUrlFilters($defaultFilters, $request);
$page = $request->query->get('page') ?? 1;
$entities = $makRepo->getVisibleByFilter(
$locale,
$page,
$this->perPage,
clone $filters
);
$entitiesCount = $makRepo->getVisibleByFilterCount($locale, clone $filters);
return $this->render('@openform_front_templates/Mak/index.html.twig', [
'entity' => $pageEntity,
'makSlug' => $request->attributes->get('makSlug'),
'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');
// }
$pageEntity = $this->pageRepo->getVisibleBySlug($request->attributes->get('makSlug'), $locale);
$makRepo = $this->prepareMakRepo($pageEntity->getIdName());
$defaultFilters = $this->getDefaultFilters(get_class($makRepo), $requestBody ? $requestBody->filters : new \StdClass);
$filters = $this->parseFilters($defaultFilters);
$page = $requestBody->page ?? 1;
$entities = $makRepo->getVisibleByFilter(
$locale,
$page,
$this->perPage,
clone $filters
);
$entitiesCount = $makRepo->getVisibleByFilterCount($locale, clone $filters);
$maxPages = ceil($entitiesCount / $this->perPage);
$filtersHtml = $this->renderView('@openform_front_templates/Mak/_filters.html.twig', [
'filters' => $filters,
]);
$entitiesHtml = $this->renderView('@openform_front_templates/Mak/_list.html.twig', [
'entities' => $entities,
'makSlug' => $request->attributes->get('makSlug'),
'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 $makSlug, string $slug)
{
$locale = $request->getLocale();
$parentPage = $this->pageRepo->getVisibleBySlug($makSlug, $locale);
$makRepo = $this->prepareMakRepo($parentPage->getIdName());
$entity = $makRepo->getVisibleBySlug($slug, $locale);
if (!$entity) {
throw new NotFoundHttpException();
}
return $this->render('@openform_front_templates/Mak/item.html.twig', [
'entity' => $entity,
'parentPage' => $parentPage,
'isDepartment' => false,
'rootParent' => \App\Package\Openform\Entity\AntiqueSuit::getRootParent($entity)
]);
}
public function departmentItemAction(Request $request, string $makSlug, string $slug)
{
$locale = $request->getLocale();
$parentPage = $this->pageRepo->getVisibleBySlug($makSlug, $locale);
$makRepo = $this->prepareMakDepartmentRepo($parentPage->getIdName());
$entity = $makRepo->getVisibleBySlug($slug, $locale);
if (!$entity) {
throw new NotFoundHttpException();
}
return $this->render('@openform_front_templates/Mak/item.html.twig', [
'entity' => $entity,
'parentPage' => $parentPage,
'isDepartment' => true,
'rootParent' => \App\Package\Openform\Entity\AntiqueSuit::getRootParent($entity)
]);
}
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;
}
protected function prepareMakTranslationRepo($pageIdName)
{
$repoTranslationMap = [
'page_museums' => 'Openform:MuseumTranslation',
'page_archives' => 'Openform:ArchiveTranslation',
'page_private_collections' => 'Openform:PrivateCollectionTranslation',
];
return $this->em->getRepository($repoTranslationMap[$pageIdName]);
}
protected function prepareMakRepo($pageIdName)
{
$repoMap = [
'page_museums' => 'Openform:Museum',
'page_archives' => 'Openform:Archive',
'page_private_collections' => 'Openform:PrivateCollection',
];
return $this->em->getRepository($repoMap[$pageIdName])->getFrontRepository();
}
protected function prepareMakDepartmentRepo($pageIdName)
{
$repoMap = [
'page_museums' => 'Openform:MuseumDepartment',
'page_archives' => 'Openform:ArchiveDepartment',
'page_private_collections' => 'Openform:PrivateCollectionDepartment',
];
return $this->em->getRepository($repoMap[$pageIdName])->getFrontRepository();
}
}