src/Package/Openform/Front/Controller/MissionController.php line 125

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 App\Package\Openform\RepositoryFront\FrontMissionRepository;
  13. use Symfony\Component\Intl\Locales;
  14. /**
  15.  * PageController
  16.  *
  17.  * @author     Daniel Balowski <d.balowski@openform.pl> (_creator)
  18.  * @copyright  2019 Openform
  19.  * @since      07.2019
  20.  */
  21. class MissionController extends BaseController
  22. {
  23.     protected $routeLocalizer
  24.     private $em
  25.     private $missionRepo
  26.     private $pageRepo
  27.     private $perPage
  28.     
  29.     public function __construct(RequestStack $requestRouteLocalizer $routeLocalizerEntityManagerInterface $em)
  30.     {
  31.         parent::__construct($request$routeLocalizer$em);
  32.         
  33.         $this->em $em;
  34.         $this->routeLocalizer $routeLocalizer;
  35.         $this->missionRepo $this->em->getRepository('Openform:Mission')->getFrontRepository();
  36.         $this->pageRepo $this->em->getRepository('Openform:Page')->getFrontRepository();
  37.         
  38.         $this->perPage 12;
  39.     }    
  40.     public function indexAction(Request $request
  41.     {
  42.         $locale $request->getLocale();
  43.         $pageEntity $this->getPageByIdname('page_mission'$request->getLocale());
  44.         $parentPageEntity $this->getPageByIdname('home'$locale);
  45.         if (!$pageEntity) {
  46.             throw new NotFoundHttpException();
  47.         }
  48.         
  49.         $defaultFilters $this->getDefaultFilters(new \stdClass());
  50.         $filters $this->getUrlFilters($defaultFilters$request);
  51.         
  52.         $entities $this->missionRepo->getVisibleByFilter(
  53.             $locale,
  54.             $request->query->get('page') ?? 1,
  55.             $this->perPage,
  56.             clone $filters
  57.         );
  58.         $entitiesCount $this->missionRepo->getVisibleByFilterCount($locale, clone $filters);
  59.         
  60.         return $this->render('@openform_front_templates/Mission/index.html.twig', [
  61.             'entity' => $pageEntity,
  62.             'parentPage' => $parentPageEntity,
  63.             'entities' => $entities,
  64.             'entitiesCount' => $entitiesCount,
  65.             'maxPages' => ceil($entitiesCount $this->perPage),
  66.             'filters' => $filters,
  67.         ]);
  68.     }
  69.     public function filterAjaxAction(Request $request): Response
  70.     {
  71.         $locale $request->getLocale();
  72.         $requestBody $request->getContent() ? json_decode($request->getContent()) : null;
  73.         
  74.         if ( !$requestBody) {
  75.             throw new NotFoundHttpException('No requestBody');
  76.         }
  77.         if (!$requestBody->filters) {
  78.             throw new NotFoundHttpException('No requestBody->filters');
  79.         }
  80.         
  81.         $defaultFilters $this->getDefaultFilters($requestBody $requestBody->filters : new \StdClass);
  82.         $filters $this->parseFilters($defaultFilters);
  83.         
  84.         $entities $this->missionRepo->getVisibleByFilter(
  85.             $locale,
  86.             $requestBody->page ?? 1,
  87.             $this->perPage
  88.             clone $filters
  89.         );
  90.         
  91.         $entitiesCount $this->missionRepo->getVisibleByFilterCount($locale, clone $filters);
  92.         $maxPages ceil($entitiesCount $this->perPage);
  93.         
  94.         // $filtersHtml = $this->renderView('@openform_front_templates/Mission/components/_filters.html.twig', [
  95.         //     'filters' => $filters,
  96.         //     'maxPages' => $maxPages,
  97.         // ]);
  98.           
  99.         $entitiesHtml $this->renderView('@openform_front_templates/Mission/components/_list.html.twig', [
  100.             'entities' => $entities,
  101.             'entitiesCount' => $entitiesCount,
  102.             'maxPages' => $maxPages,
  103.         ]);
  104.   
  105.         return new JsonResponse(array(
  106.             // 'filtersHtml' => $filtersHtml,
  107.             'entitiesHtml' => $entitiesHtml,
  108.             'maxPages' => $maxPages,
  109.         ));
  110.     }
  111.     public function itemAction(Request $requeststring $slug)
  112.     {
  113.         $locale $request->getLocale();
  114.         $parentPage $this->getPageByIdname('page_mission'$locale);
  115.         $repo $this->em->getRepository('Openform:Mission')->getFrontRepository();
  116.         $entity $repo->getVisibleBySlug($slug$locale);
  117.         if (!$entity) {
  118.             throw new NotFoundHttpException();
  119.         }
  120.         return $this->render('@openform_front_templates/Page/index.html.twig', [
  121.             'entity' => $entity,
  122.             'parentPage' => $parentPage,
  123. //            'langLinks' => $this->generateLangLinksMission($entity),
  124.         ]);
  125.     }
  126.     
  127.     private function getDefaultFilters(\stdClass $filters null): \stdClass
  128.     {
  129.         if (!$filters) {
  130.             $filters = new \stdClass();
  131.         }
  132.         $filters->sort FrontMissionRepository::ORDER_ORD_DESC;
  133.         return $filters;
  134.     }
  135.     private function getUrlFilters(\stdClass $filters nullRequest $request): \stdClass
  136.     {
  137.         if ($request->query->get('dateFrom')) {
  138.             $filters->dateFrom $request->query->get('dateFrom');
  139.         }
  140.         
  141.         return $filters;
  142.     }
  143.     private function parseFilters(\stdClass $filters): \stdClass
  144.     {
  145.         foreach ($filters as $filter => $value) {
  146.             if (!$value) {
  147.                 unset($filters->$filter);
  148.             }
  149.         }
  150.         return $filters;
  151.     }
  152.     
  153.     protected function generateLangLinksMission$entity)
  154.     {
  155.         $repo $this->em->getRepository('Openform:MissionTranslation');
  156.         
  157.         $translations $repo->findBy(['translatableId' => $entity->getId()]);
  158.         $langLinks = [];
  159.         foreach ($translations as $transItem){
  160.             if (strlen($transItem->getSlug()) > 0) {
  161.                 $url $this->routeLocalizer->generate('mission_item', ['slug' => $transItem->getSlug()], $transItem->getLang());
  162.                 
  163.                 $langLinks[$transItem->getLang()] = $url;
  164.             }
  165.         }
  166.         return $langLinks;        
  167.     }
  168. }