<?php
namespace App\Package\Openform\Front\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
use App\Package\Openform\Entity\Page;
use App\Package\Openform\RepositoryFront\FrontSearchRepository;
class SearchController extends BaseController {
private $em;
private $searchRepo;
private $pageRepo;
private $perPage = 10;
public function __construct(RequestStack $request, RouteLocalizer $routeLocalizer, EntityManagerInterface $em)
{
parent::__construct($request, $routeLocalizer, $em);
$this->em = $em;
$this->searchRepo = $this->em->getRepository('Openform:Search')->getFrontRepository();
$this->pageRepo = $this->em->getRepository('Openform:Page')->getFrontRepository();
}
/**
*
* @param Request $request
* @param Page $page
* @return Response
*/
public function indexAction(Request $request): Response
{
$locale = $request->getLocale();
$defaultFilters = $this->getDefaultFilters(new \stdClass());
$filters = $this->getUrlFilters($defaultFilters, $request);
$page = $request->query->get('page') ?? 1;
if ($request->query->get('search') == 1) {
$page = 1;
}
$entities = $this->searchRepo->getVisibleByFilter(
$locale,
$page,
$this->perPage,
clone $filters
);
$entitiesCount = $this->searchRepo->getVisibleByFilterCount($locale, clone $filters);
return $this->render('@openform_front_templates/Search/index.html.twig', [
'entity' => $this->getPageByIdname('page_search', $locale),
'request' => $request,
'entities' => $entities,
'entitiesCount' => $entitiesCount,
'pages' => ceil($entitiesCount / $this->perPage),
'page' => $page,
'filters' => $filters,
]);
}
/**
*
* @param Request $request
* @return Response
* @throws NotFoundHttpException
*/
public function filterAjaxAction(Request $request): Response
{
$locale = $request->getLocale();
$requestBody = $request->getContent() ? json_decode($request->getContent()) : null;
if ( !$requestBody || !$requestBody->filters || !$requestBody->page ) {
throw new NotFoundHttpException('Request page empty');
}
$defaultFilters = $this->getDefaultFilters($requestBody->filters);
$filters = $this->parseFilters($defaultFilters);
$entities = $this->searchRepo->getVisibleByFilter(
$locale,
$requestBody->page,
$this->perPage,
clone $filters
);
$entitiesCount = $this->searchRepo->getVisibleByFilterCount($locale, clone $filters);
$pages = ceil($entitiesCount / $this->perPage);
$entitiesHtml = $this->renderView('@openform_front_templates/Search/_list.html.twig', [
'entities' => $entities,
'page' => $requestBody->page,
'pages' => $pages,
'entitiesCount' => $entitiesCount,
]);
return new Response(json_encode(array(
'entitiesHtml' => $entitiesHtml,
'page' => $requestBody->page,
'pages' => $pages,
'entitiesCount' => $entitiesCount,
)));
}
/**
*
* @param \stdClass $filters
* @param Request $request
* @return \stdClass
*/
private function getUrlFilters(\stdClass $filters = null, Request $request): \stdClass
{
if ($request->query->get('phrase')) {
$filters->phrase = $request->query->get('phrase');
}
// if ($request->query->get('entityName')) {
// $filters->entityName = $request->query->get('entityName');
// }
if ($request->query->get('exhibitions') && count(array_filter($request->query->get('exhibitions'))) > 0) {
$filters->exhibitions = $request->query->get('exhibitions');
}
if ($request->query->get('museums') && count(array_filter($request->query->get('museums'))) > 0) {
$filters->museums = $request->query->get('museums');
}
if ($request->query->get('privateCollections') && count(array_filter($request->query->get('privateCollections'))) > 0) {
$filters->privateCollections = $request->query->get('privateCollections');
}
if ($request->query->get('archives') && count(array_filter($request->query->get('archives'))) > 0) {
$filters->archives = $request->query->get('archives');
}
// if ($request->query->get('rangePath')) {
// $filters->rangePath = $request->query->get('rangePath');
// }
if ($request->query->get('rangeEverywhere')) {
$filters->rangeEverywhere = $request->query->get('rangeEverywhere');
}
if ($request->query->get('rangeAllExhibition')) {
$filters->rangeAllExhibition = $request->query->get('rangeAllExhibition');
}
if ($request->query->get('rangeAllMuseum')) {
$filters->rangeAllMuseum = $request->query->get('rangeAllMuseum');
}
if ($request->query->get('rangeAllArchive')) {
$filters->rangeAllArchive = $request->query->get('rangeAllArchive');
}
if ($request->query->get('rangeAllPrivateCollection')) {
$filters->rangeAllPrivateCollection = $request->query->get('rangeAllPrivateCollection');
}
if ($request->query->get('artist')) {
$filters->artist = $request->query->get('artist');
}
if ($request->query->get('antiqueCreationInstitution')) {
$filters->antiqueCreationInstitution = $request->query->get('antiqueCreationInstitution');
}
if ($request->query->get('antiqueCreationPlace')) {
$filters->antiqueCreationPlace = $request->query->get('antiqueCreationPlace');
}
if ($request->query->get('antiqueStorageInstitution')) {
$filters->antiqueStorageInstitution = $request->query->get('antiqueStorageInstitution');
}
if ($request->query->get('antiqueStoragePlace')) {
$filters->antiqueStoragePlace = $request->query->get('antiqueStoragePlace');
}
if ($request->query->get('antiqueType')) {
$filters->antiqueType = $request->query->get('antiqueType');
}
if ($request->query->get('antiqueOwner')) {
$filters->antiqueOwner = $request->query->get('antiqueOwner');
}
if ($request->query->get('antiqueArtifact')) {
$filters->antiqueArtifact = $request->query->get('antiqueArtifact');
}
if ($request->query->get('antiqueCopyrights')) {
$filters->antiqueCopyrights = $request->query->get('antiqueCopyrights');
}
if ($request->query->get('antiqueCharacterKind')) {
$filters->antiqueCharacterKind = $request->query->get('antiqueCharacterKind');
}
if ($request->query->get('antiqueCharacterKindAdditional')) {
$filters->antiqueCharacterKindAdditional = $request->query->get('antiqueCharacterKindAdditional');
}
if ($request->query->get('antiquePhisicalAttributeMain')) {
$filters->antiquePhisicalAttributeMain = $request->query->get('antiquePhisicalAttributeMain');
}
if ($request->query->get('antiquePhisicalAttributeAdditional1')) {
$filters->antiquePhisicalAttributeAdditional1 = $request->query->get('antiquePhisicalAttributeAdditional1');
}
if ($request->query->get('antiquePhisicalAttributeAdditional2')) {
$filters->antiquePhisicalAttributeAdditional2 = $request->query->get('antiquePhisicalAttributeAdditional2');
}
if ($request->query->get('antiquePhisicalAttributeAdditional3')) {
$filters->antiquePhisicalAttributeAdditional3 = $request->query->get('antiquePhisicalAttributeAdditional3');
}
if ($request->query->get('antiqueMaterial')) {
$filters->antiqueMaterial = $request->query->get('antiqueMaterial');
}
if ($request->query->get('year')) {
$filters->year = $request->query->get('year');
}
if ($request->query->get('age')) {
$filters->age = $request->query->get('age');
}
return $filters;
}
/**
*
* @param \stdClass $filters
* @return \stdClass
*/
private function getDefaultFilters(\stdClass $filters = null): \stdClass
{
if (!$filters) {
$filters = new \stdClass();
}
$filters->sort = FrontSearchRepository::ORDER_ORD_DESC;
return $filters;
}
/**
*
* @param \stdClass $filters
* @return \stdClass
*/
private function parseFilters(\stdClass $filters): \stdClass
{
foreach ($filters as $filter => $value) {
if (!$value) {
unset($filters->$filter);
}
}
return $filters;
}
}