src/Package/Openform/Front/Controller/ExhibitionController.php line 153

Open in your IDE?
  1. <?php
  2. namespace App\Package\Openform\Front\Controller;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use App\Package\Openform\Front\Controller\BaseController;
  10. use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\Intl\Locales;
  13. /**
  14.  * PageController
  15.  *
  16.  * @author     Daniel Balowski <d.balowski@openform.pl> (_creator)
  17.  * @copyright  2019 Openform
  18.  * @since      07.2019
  19.  */
  20. class ExhibitionController extends BaseController
  21. {
  22.     protected $routeLocalizer
  23.     private $em
  24.     private $pageRepo
  25.     private $exhibitionRepo;
  26.     private $exhibitionDepartmentRepo;
  27.         
  28.     private $perPage
  29.     
  30.     public function __construct(RequestStack $requestRouteLocalizer $routeLocalizerEntityManagerInterface $em)
  31.     {
  32.         parent::__construct($request$routeLocalizer$em);
  33.         
  34.         $this->em $em;
  35.         $this->routeLocalizer $routeLocalizer;
  36.         $this->pageRepo $this->em->getRepository('Openform:Page')->getFrontRepository();
  37.         $this->exhibitionRepo $this->em->getRepository('Openform:Exhibition')->getFrontRepository();
  38.         $this->exhibitionDepartmentRepo $this->em->getRepository('Openform:ExhibitionDepartment')->getFrontRepository();
  39.         
  40.         $this->perPage 12;
  41.     }    
  42.     
  43.     public function indexAction(Request $request
  44.     {
  45.         $locale $request->getLocale();
  46.         $pageEntity $this->getPageByIdName('page_exhibitions'$locale);
  47.         if (!$pageEntity) {
  48.             throw new NotFoundHttpException();
  49.         }
  50.         $defaultFilters $this->getDefaultFilters(get_class($this->exhibitionRepo), new \stdClass());
  51.         $filters $this->getUrlFilters($defaultFilters$request);
  52.         $page $request->query->get('page') ?? 1;
  53.         
  54.         $entities $this->exhibitionRepo->getVisibleByFilter(
  55.             $locale,
  56.             $page,
  57.             $this->perPage,
  58.             clone $filters
  59.         );
  60.         $entitiesCount $this->exhibitionRepo->getVisibleByFilterCount($locale, clone $filters);
  61.         
  62.         return $this->render('@openform_front_templates/Exhibition/index.html.twig', [
  63.             'entity' => $pageEntity,
  64.             'parentPage' => $pageEntity->getParent(),
  65.             'entities' => $entities,
  66.             'entitiesCount' => $entitiesCount,
  67.             'page' => $page,
  68.             'perPage' => $this->perPage,
  69.             'maxPages' => ceil($entitiesCount $this->perPage),
  70.             'filters' => $filters,
  71.         ]);
  72.     }
  73.     public function filterAjaxAction(Request $request): Response
  74.     {
  75.         $locale $request->getLocale();
  76.         $requestBody $request->getContent() ? json_decode($request->getContent()) : null;
  77.         
  78. //        if ( !$requestBody) {
  79. //            throw new NotFoundHttpException('No requestBody');
  80. //        }
  81. //        if (!$requestBody->filters) {
  82. //            throw new NotFoundHttpException('No requestBody->filters');
  83. //        }
  84.         $defaultFilters $this->getDefaultFilters(get_class($this->exhibitionRepo), $requestBody $requestBody->filters : new \StdClass);
  85.         $filters $this->parseFilters($defaultFilters);
  86.         $page $requestBody->page ?? 1;
  87.         
  88.         $entities $this->exhibitionRepo->getVisibleByFilter(
  89.             $locale,
  90.             $page,
  91.             $this->perPage
  92.             clone $filters
  93.         );
  94.         $entitiesCount $this->exhibitionRepo->getVisibleByFilterCount($locale, clone $filters);
  95.         $maxPages ceil($entitiesCount $this->perPage);
  96.         $filtersHtml $this->renderView('@openform_front_templates/Exhibition/_filters.html.twig', [
  97.             'filters' => $filters,
  98.         ]);
  99.           
  100.         $entitiesHtml $this->renderView('@openform_front_templates/Exhibition/_list.html.twig', [
  101.             'entities' => $entities,
  102.             'entitiesCount' => $entitiesCount,
  103.             'page' => $page,
  104.             'maxPages' => $maxPages,
  105.             'perPage' => $this->perPage,
  106.         ]);
  107.         return new JsonResponse(array(
  108.             'filtersHtml' => $filtersHtml,
  109.             'entitiesHtml' => $entitiesHtml,
  110.             'entitiesCount' => $entitiesCount,
  111.             'page' => $page,
  112.             'maxPages' => $maxPages,
  113.             'perPage' => $this->perPage,
  114.             'filters' => $filters,
  115.         ));
  116.     }
  117.     public function itemAction(Request $requeststring $slug)
  118.     {
  119.         $locale $request->getLocale();
  120.         $parentPage $this->getPageByIdName('page_exhibitions'$locale);
  121.         $entity $this->exhibitionRepo->getVisibleBySlug($slug$locale);
  122.         if (!$entity) {
  123.             throw new NotFoundHttpException();
  124.         }
  125.         $rootParent \App\Package\Openform\Entity\RelictPack::getRootParent($entity);
  126.         $request->attributes->set('makSlug'$rootParent->getMakSlug($locale));
  127.         
  128.         return $this->render('@openform_front_templates/Exhibition/item.html.twig', [
  129.             'entity' => $entity,
  130.             'parentPage' => $parentPage,
  131.             'isDepartment' =>  false,
  132.             'rootParent' => $rootParent
  133.         ]);
  134.     }
  135.     public function departmentItemAction(Request $requeststring $slug)
  136.     {
  137.         $locale $request->getLocale();
  138.         $parentPage $this->getPageByIdName('page_exhibitions'$locale);
  139.         $entity $this->exhibitionDepartmentRepo->getVisibleBySlug($slug$locale);
  140.         if (!$entity) {
  141.             throw new NotFoundHttpException();
  142.         }
  143.         
  144.         $rootParent \App\Package\Openform\Entity\RelictPack::getRootParent($entity);
  145.         $request->attributes->set('makSlug'$rootParent->getMakSlug($locale));
  146.         
  147.         return $this->render('@openform_front_templates/Exhibition/item.html.twig', [
  148.             'entity' => $entity,
  149.             'parentPage' => $parentPage,
  150.             'isDepartment' =>  true,
  151.             'rootParent' => $rootParent
  152.         ]);
  153.     }
  154.     private function getDefaultFilters($entityClass\stdClass $filters null): \stdClass
  155.     {
  156.         if (!$filters) {
  157.             $filters = new \stdClass();
  158.         }
  159.         if (!property_exists($filters'sort')) {
  160.             $filters->sort $entityClass::ORDER_ORD_DESC;
  161.         }
  162.         return $filters;
  163.     }
  164.     private function getUrlFilters(\stdClass $filters nullRequest $request): \stdClass
  165.     {
  166.         if ($request->query->get('dateFrom')) {
  167.             $filters->dateFrom $request->query->get('dateFrom');
  168.         }
  169.         if ($request->query->get('sort')) {
  170.             $filters->sort $request->query->get('sort');
  171.         }
  172.         
  173.         return $filters;
  174.     }
  175.     private function parseFilters(\stdClass $filters): \stdClass
  176.     {
  177.         foreach ($filters as $filter => $value) {
  178.             if (!$value) {
  179.                 unset($filters->$filter);
  180.             }
  181.         }
  182.         return $filters;
  183.     }
  184. }