src/Package/Openform/Front/Controller/AboutUsController.php line 78

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