src/Package/Openform/Front/Controller/MakController.php line 163

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. use \App\Package\Openform\Entity\PrivateCollectionDepartment;
  14. use \App\Package\Openform\Entity\ArchiveDepartment;
  15. use \App\Package\Openform\Entity\MuseumDepartment;
  16. /**
  17.  * PageController
  18.  *
  19.  * @author     Daniel Balowski <d.balowski@openform.pl> (_creator)
  20.  * @copyright  2019 Openform
  21.  * @since      07.2019
  22.  */
  23. class MakController extends BaseController
  24. {
  25.     protected $routeLocalizer
  26.     private $em
  27.     private $pageRepo
  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.         
  38.         $this->perPage 12;
  39.     }    
  40.     
  41.     public function indexAction(Request $request
  42.     {
  43.         $locale $request->getLocale();
  44.         $pageEntity $this->pageRepo->getVisibleBySlug($request->attributes->get('makSlug'), $locale);
  45.         if (!$pageEntity) {
  46.             throw new NotFoundHttpException();
  47.         }
  48.         $makRepo $this->prepareMakRepo($pageEntity->getIdname());
  49.         $defaultFilters $this->getDefaultFilters(get_class($makRepo), new \stdClass());
  50.         $filters $this->getUrlFilters($defaultFilters$request);
  51.         $page $request->query->get('page') ?? 1;
  52.         
  53.         $entities $makRepo->getVisibleByFilter(
  54.             $locale,
  55.             $page,
  56.             $this->perPage,
  57.             clone $filters
  58.         );
  59.         $entitiesCount $makRepo->getVisibleByFilterCount($locale, clone $filters);
  60.         
  61.         return $this->render('@openform_front_templates/Mak/index.html.twig', [
  62.             'entity' => $pageEntity,
  63.             'makSlug' => $request->attributes->get('makSlug'),
  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.         $pageEntity $this->pageRepo->getVisibleBySlug($request->attributes->get('makSlug'), $locale);
  85.         $makRepo $this->prepareMakRepo($pageEntity->getIdName());
  86.         $defaultFilters $this->getDefaultFilters(get_class($makRepo), $requestBody $requestBody->filters : new \StdClass);
  87.         $filters $this->parseFilters($defaultFilters);
  88.         $page $requestBody->page ?? 1;
  89.         
  90.         $entities $makRepo->getVisibleByFilter(
  91.             $locale,
  92.             $page,
  93.             $this->perPage
  94.             clone $filters
  95.         );
  96.         $entitiesCount $makRepo->getVisibleByFilterCount($locale, clone $filters);
  97.         $maxPages ceil($entitiesCount $this->perPage);
  98.         $filtersHtml $this->renderView('@openform_front_templates/Mak/_filters.html.twig', [
  99.             'filters' => $filters,
  100.         ]);
  101.           
  102.         $entitiesHtml $this->renderView('@openform_front_templates/Mak/_list.html.twig', [
  103.             'entities' => $entities,
  104.             'makSlug' => $request->attributes->get('makSlug'),
  105.             'entitiesCount' => $entitiesCount,
  106.             'page' => $page,
  107.             'maxPages' => $maxPages,
  108.             'perPage' => $this->perPage,
  109.         ]);
  110.         return new JsonResponse(array(
  111.             'filtersHtml' => $filtersHtml,
  112.             'entitiesHtml' => $entitiesHtml,
  113.             'entitiesCount' => $entitiesCount,
  114.             'page' => $page,
  115.             'maxPages' => $maxPages,
  116.             'perPage' => $this->perPage,
  117.             'filters' => $filters,
  118.         ));
  119.     }
  120.     public function itemAction(Request $requeststring $makSlugstring $slug)
  121.     {
  122.         $locale $request->getLocale();
  123.         $parentPage $this->pageRepo->getVisibleBySlug($makSlug$locale);
  124.         $makRepo $this->prepareMakRepo($parentPage->getIdName());
  125.         $entity $makRepo->getVisibleBySlug($slug$locale);
  126.         if (!$entity) {
  127.             throw new NotFoundHttpException();
  128.         }
  129.         return $this->render('@openform_front_templates/Mak/item.html.twig', [
  130.             'entity' => $entity,
  131.             'parentPage' => $parentPage,
  132.             'isDepartment' =>  false,
  133.             'rootParent' => \App\Package\Openform\Entity\AntiqueSuit::getRootParent($entity)
  134.         ]);
  135.     }
  136.     public function departmentItemAction(Request $requeststring $makSlugstring $slug)
  137.     {
  138.         $locale $request->getLocale();
  139.         $parentPage $this->pageRepo->getVisibleBySlug($makSlug$locale);
  140.         $makRepo $this->prepareMakDepartmentRepo($parentPage->getIdName());
  141.         $entity $makRepo->getVisibleBySlug($slug$locale);
  142.         if (!$entity) {
  143.             throw new NotFoundHttpException();
  144.         }
  145.         return $this->render('@openform_front_templates/Mak/item.html.twig', [
  146.             'entity' => $entity,
  147.             'parentPage' => $parentPage,
  148.             'isDepartment' =>  true,
  149.             'rootParent' => \App\Package\Openform\Entity\AntiqueSuit::getRootParent($entity)
  150.         ]);
  151.     }
  152.     private function getDefaultFilters($entityClass\stdClass $filters null): \stdClass
  153.     {
  154.         if (!$filters) {
  155.             $filters = new \stdClass();
  156.         }
  157.         if (!property_exists($filters'sort')) {
  158.             $filters->sort $entityClass::ORDER_ORD_DESC;
  159.         }
  160.         return $filters;
  161.     }
  162.     private function getUrlFilters(\stdClass $filters nullRequest $request): \stdClass
  163.     {
  164.         if ($request->query->get('dateFrom')) {
  165.             $filters->dateFrom $request->query->get('dateFrom');
  166.         }
  167.         if ($request->query->get('sort')) {
  168.             $filters->sort $request->query->get('sort');
  169.         }
  170.         
  171.         return $filters;
  172.     }
  173.     private function parseFilters(\stdClass $filters): \stdClass
  174.     {
  175.         foreach ($filters as $filter => $value) {
  176.             if (!$value) {
  177.                 unset($filters->$filter);
  178.             }
  179.         }
  180.         return $filters;
  181.     }
  182.     protected function prepareMakTranslationRepo($pageIdName
  183.     {
  184.         $repoTranslationMap = [
  185.             'page_museums' => 'Openform:MuseumTranslation',
  186.             'page_archives' => 'Openform:ArchiveTranslation',
  187.             'page_private_collections' => 'Openform:PrivateCollectionTranslation',
  188.         ];
  189.         
  190.         return $this->em->getRepository($repoTranslationMap[$pageIdName]);
  191.     }
  192.     protected function prepareMakRepo($pageIdName
  193.     {
  194.         $repoMap = [
  195.             'page_museums' => 'Openform:Museum',
  196.             'page_archives' => 'Openform:Archive',
  197.             'page_private_collections' => 'Openform:PrivateCollection',
  198.         ];
  199.         
  200.         return $this->em->getRepository($repoMap[$pageIdName])->getFrontRepository();
  201.     }
  202.     protected function prepareMakDepartmentRepo($pageIdName
  203.     {
  204.         $repoMap = [
  205.             'page_museums' => 'Openform:MuseumDepartment',
  206.             'page_archives' => 'Openform:ArchiveDepartment',
  207.             'page_private_collections' => 'Openform:PrivateCollectionDepartment',
  208.         ];
  209.         
  210.         return $this->em->getRepository($repoMap[$pageIdName])->getFrontRepository();
  211.     }
  212. }