src/Package/Openform/Front/Controller/SearchController.php line 63

Open in your IDE?
  1. <?php
  2. namespace App\Package\Openform\Front\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
  8. use App\Package\Openform\Entity\Page;
  9. use App\Package\Openform\RepositoryFront\FrontSearchRepository;
  10. class SearchController extends BaseController {
  11.     
  12.     private $em
  13.     private $searchRepo
  14.     private $pageRepo
  15.     private $perPage 10
  16.     
  17.     public function __construct(RequestStack $requestRouteLocalizer $routeLocalizerEntityManagerInterface $em)
  18.     {
  19.         parent::__construct($request$routeLocalizer$em);
  20.         
  21.         $this->em $em;
  22.         $this->searchRepo $this->em->getRepository('Openform:Search')->getFrontRepository();
  23.         $this->pageRepo $this->em->getRepository('Openform:Page')->getFrontRepository();
  24.     }
  25.     
  26.     /**
  27.      * 
  28.      * @param Request $request
  29.      * @param Page $page
  30.      * @return Response
  31.      */
  32.     public function indexAction(Request $request): Response 
  33.     {
  34.         $locale $request->getLocale();
  35.         
  36.         $defaultFilters $this->getDefaultFilters(new \stdClass());
  37.         $filters $this->getUrlFilters($defaultFilters$request);
  38.         $page $request->query->get('page') ?? 1;
  39.         if ($request->query->get('search') == 1) {
  40.             $page 1;
  41.         }
  42.         
  43.         $entities $this->searchRepo->getVisibleByFilter(
  44.             $locale
  45.             $page,
  46.             $this->perPage,
  47.             clone $filters
  48.         );
  49.         $entitiesCount $this->searchRepo->getVisibleByFilterCount($locale, clone $filters);
  50.         return $this->render('@openform_front_templates/Search/index.html.twig', [
  51.             'entity'    => $this->getPageByIdname('page_search'$locale),
  52.             'request' => $request,
  53.             'entities' => $entities,
  54.             'entitiesCount' => $entitiesCount,
  55.             'pages' => ceil($entitiesCount $this->perPage),
  56.             'page' => $page,
  57.             'filters' => $filters,
  58.         ]);
  59.     }
  60.     
  61.     /**
  62.      * 
  63.      * @param Request $request
  64.      * @return Response
  65.      * @throws NotFoundHttpException
  66.      */
  67.     public function filterAjaxAction(Request $request): Response
  68.     {
  69.         $locale $request->getLocale();
  70.         
  71.         $requestBody $request->getContent() ? json_decode($request->getContent()) : null;
  72.         
  73.         if ( !$requestBody || !$requestBody->filters || !$requestBody->page ) {
  74.             throw new NotFoundHttpException('Request page empty');
  75.         }
  76.         $defaultFilters $this->getDefaultFilters($requestBody->filters);
  77.         $filters $this->parseFilters($defaultFilters);
  78.         $entities $this->searchRepo->getVisibleByFilter(
  79.             $locale
  80.             $requestBody->page
  81.             $this->perPage,
  82.             
  83.             clone $filters
  84.         );
  85.         
  86.         $entitiesCount $this->searchRepo->getVisibleByFilterCount($locale, clone $filters);
  87.         $pages ceil($entitiesCount $this->perPage);
  88.         
  89.         $entitiesHtml $this->renderView('@openform_front_templates/Search/_list.html.twig', [
  90.             'entities' => $entities,
  91.             'page' => $requestBody->page,
  92.             'pages' => $pages,
  93.             'entitiesCount' => $entitiesCount,
  94.         ]);
  95.                 
  96.         return new Response(json_encode(array(
  97.             'entitiesHtml' => $entitiesHtml,
  98.             'page' => $requestBody->page,
  99.             'pages' => $pages,
  100.             'entitiesCount' => $entitiesCount,
  101.         )));
  102.     }
  103.     /**
  104.      * 
  105.      * @param \stdClass $filters
  106.      * @param Request $request
  107.      * @return \stdClass
  108.      */
  109.     private function getUrlFilters(\stdClass $filters nullRequest $request): \stdClass
  110.     {        
  111.         if ($request->query->get('phrase')) {
  112.             $filters->phrase $request->query->get('phrase');
  113.         }
  114. //        if ($request->query->get('entityName')) {
  115. //            $filters->entityName = $request->query->get('entityName');
  116. //        }
  117.         if ($request->query->get('exhibitions') && count(array_filter($request->query->get('exhibitions'))) > 0) {
  118.             $filters->exhibitions $request->query->get('exhibitions');            
  119.         }
  120.         if ($request->query->get('museums') && count(array_filter($request->query->get('museums'))) > 0) {
  121.             $filters->museums $request->query->get('museums');
  122.         }
  123.         if ($request->query->get('privateCollections') && count(array_filter($request->query->get('privateCollections'))) > 0) {
  124.             $filters->privateCollections $request->query->get('privateCollections');
  125.         }
  126.         if ($request->query->get('archives') && count(array_filter($request->query->get('archives'))) > 0) {
  127.             $filters->archives $request->query->get('archives');
  128.         }
  129.  
  130. //        if ($request->query->get('rangePath')) {
  131. //            $filters->rangePath = $request->query->get('rangePath');
  132. //        }        
  133.         if ($request->query->get('rangeEverywhere')) {
  134.             $filters->rangeEverywhere $request->query->get('rangeEverywhere');
  135.         }
  136.         if ($request->query->get('rangeAllExhibition')) {
  137.             $filters->rangeAllExhibition $request->query->get('rangeAllExhibition');
  138.         }
  139.         if ($request->query->get('rangeAllMuseum')) {
  140.             $filters->rangeAllMuseum $request->query->get('rangeAllMuseum');
  141.         }
  142.         if ($request->query->get('rangeAllArchive')) {
  143.             $filters->rangeAllArchive $request->query->get('rangeAllArchive');
  144.         }
  145.         if ($request->query->get('rangeAllPrivateCollection')) {
  146.             $filters->rangeAllPrivateCollection $request->query->get('rangeAllPrivateCollection');
  147.         }
  148.         
  149.         if ($request->query->get('artist')) {
  150.             $filters->artist $request->query->get('artist');
  151.         }
  152.         if ($request->query->get('antiqueCreationInstitution')) {
  153.             $filters->antiqueCreationInstitution $request->query->get('antiqueCreationInstitution');
  154.         }
  155.         if ($request->query->get('antiqueCreationPlace')) {
  156.             $filters->antiqueCreationPlace $request->query->get('antiqueCreationPlace');
  157.         }
  158.         if ($request->query->get('antiqueStorageInstitution')) {
  159.             $filters->antiqueStorageInstitution $request->query->get('antiqueStorageInstitution');
  160.         }
  161.         if ($request->query->get('antiqueStoragePlace')) {
  162.             $filters->antiqueStoragePlace $request->query->get('antiqueStoragePlace');
  163.         }
  164.         if ($request->query->get('antiqueType')) {
  165.             $filters->antiqueType $request->query->get('antiqueType');
  166.         }
  167.         if ($request->query->get('antiqueOwner')) {
  168.             $filters->antiqueOwner $request->query->get('antiqueOwner');
  169.         }
  170.         if ($request->query->get('antiqueArtifact')) {
  171.             $filters->antiqueArtifact $request->query->get('antiqueArtifact');
  172.         }
  173.         if ($request->query->get('antiqueCopyrights')) {
  174.             $filters->antiqueCopyrights $request->query->get('antiqueCopyrights');
  175.         }
  176.         if ($request->query->get('antiqueCharacterKind')) {
  177.             $filters->antiqueCharacterKind $request->query->get('antiqueCharacterKind');
  178.         }
  179.         if ($request->query->get('antiqueCharacterKindAdditional')) {
  180.             $filters->antiqueCharacterKindAdditional $request->query->get('antiqueCharacterKindAdditional');
  181.         }
  182.         
  183.         if ($request->query->get('antiquePhisicalAttributeMain')) {
  184.             $filters->antiquePhisicalAttributeMain $request->query->get('antiquePhisicalAttributeMain');
  185.         }
  186.         if ($request->query->get('antiquePhisicalAttributeAdditional1')) {
  187.             $filters->antiquePhisicalAttributeAdditional1 $request->query->get('antiquePhisicalAttributeAdditional1');
  188.         }
  189.         if ($request->query->get('antiquePhisicalAttributeAdditional2')) {
  190.             $filters->antiquePhisicalAttributeAdditional2 $request->query->get('antiquePhisicalAttributeAdditional2');
  191.         }
  192.         if ($request->query->get('antiquePhisicalAttributeAdditional3')) {
  193.             $filters->antiquePhisicalAttributeAdditional3 $request->query->get('antiquePhisicalAttributeAdditional3');
  194.         }
  195.         if ($request->query->get('antiqueMaterial')) {
  196.             $filters->antiqueMaterial $request->query->get('antiqueMaterial');
  197.         }
  198.         if ($request->query->get('year')) {
  199.             $filters->year $request->query->get('year');
  200.         }
  201.         if ($request->query->get('age')) {
  202.             $filters->age $request->query->get('age');
  203.         }
  204.         return $filters;
  205.     }
  206.     
  207.     /**
  208.      * 
  209.      * @param \stdClass $filters
  210.      * @return \stdClass
  211.      */
  212.     private function getDefaultFilters(\stdClass $filters null): \stdClass
  213.     {
  214.         if (!$filters) {
  215.             $filters = new \stdClass();
  216.         }
  217.         $filters->sort FrontSearchRepository::ORDER_ORD_DESC;
  218.         return $filters;
  219.     }
  220.     
  221.     /**
  222.      * 
  223.      * @param \stdClass $filters
  224.      * @return \stdClass
  225.      */
  226.     private function parseFilters(\stdClass $filters): \stdClass
  227.     {
  228.         foreach ($filters as $filter => $value) {
  229.             if (!$value) {
  230.                 unset($filters->$filter);
  231.             }
  232.         }
  233.         return $filters;
  234.     }
  235. }