src/Package/Openform/Front/Controller/ProposalController.php line 128

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\FrontProposalRepository;
  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 ProposalController extends BaseController
  22. {
  23.     protected $routeLocalizer
  24.     private $em
  25.     private $proposalRepo
  26.     private $pageRepo
  27.     private $perPage
  28.     private $featured false
  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->proposalRepo $this->em->getRepository('Openform:Proposal')->getFrontRepository();
  37.         $this->pageRepo $this->em->getRepository('Openform:Page')->getFrontRepository();
  38.         
  39.         $this->perPage 12;
  40.         $this->featured $this->request->get('featured');
  41.     }    
  42.     public function indexAction(Request $request
  43.     {
  44.         $locale $request->getLocale();
  45.         $pageEntity $this->getPageByIdname('page_proposal'$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->proposalRepo->getVisibleByFilter(
  55.             $locale,
  56.             $request->query->get('page') ?? 1,
  57.             $this->perPage,
  58.             clone $filters
  59.         );
  60.         $entitiesCount $this->proposalRepo->getVisibleByFilterCount($locale, clone $filters);
  61.         
  62.         return $this->render('@openform_front_templates/Proposal/index.html.twig', [
  63.             'entity' => $pageEntity,
  64.             'parentPage' => $parentPageEntity,
  65.             'entities' => $entities,
  66.             'entitiesCount' => $entitiesCount,
  67.             'maxPages' => ceil($entitiesCount $this->perPage),
  68.             'filters' => $filters,
  69.         ]);
  70.     }
  71.     public function filterAjaxAction(Request $request): Response
  72.     {
  73.         $locale $request->getLocale();
  74.         $requestBody $request->getContent() ? json_decode($request->getContent()) : null;
  75.         
  76.         if ( !$requestBody) {
  77.             throw new NotFoundHttpException('No requestBody');
  78.         }
  79.         if (!$requestBody->filters) {
  80.             throw new NotFoundHttpException('No requestBody->filters');
  81.         }
  82.         
  83.         $defaultFilters $this->getDefaultFilters($requestBody $requestBody->filters : new \StdClass);
  84.         $filters $this->parseFilters($defaultFilters);
  85.         
  86.         $entities $this->proposalRepo->getVisibleByFilter(
  87.             $locale,
  88.             $requestBody->page ?? 1,
  89.             $this->perPage
  90.             clone $filters
  91.         );
  92.         
  93.         $entitiesCount $this->proposalRepo->getVisibleByFilterCount($locale, clone $filters);
  94.         $maxPages ceil($entitiesCount $this->perPage);
  95.         
  96.         // $filtersHtml = $this->renderView('@openform_front_templates/Proposal/components/_filters.html.twig', [
  97.         //     'filters' => $filters,
  98.         //     'maxPages' => $maxPages,
  99.         // ]);
  100.           
  101.         $entitiesHtml $this->renderView('@openform_front_templates/Proposal/components/_list.html.twig', [
  102.             'entities' => $entities,
  103.             'entitiesCount' => $entitiesCount,
  104.             'maxPages' => $maxPages,
  105.         ]);
  106.   
  107.         return new JsonResponse(array(
  108.             // 'filtersHtml' => $filtersHtml,
  109.             'entitiesHtml' => $entitiesHtml,
  110.             'maxPages' => $maxPages,
  111.         ));
  112.     }
  113.     public function itemAction(Request $requeststring $slug)
  114.     {
  115.         $locale $request->getLocale();
  116.         $parentPage $this->getPageByIdname('page_proposal'$locale);
  117.         $repo $this->em->getRepository('Openform:Proposal')->getFrontRepository();
  118.         $entity $repo->getVisibleBySlug($slug$locale);
  119.         if (!$entity) {
  120.             throw new NotFoundHttpException();
  121.         }
  122.         return $this->render('@openform_front_templates/Page/index.html.twig', [
  123.             'entity' => $entity,
  124.             'parentPage' => $parentPage,
  125. //            'langLinks' => $this->generateLangLinksProposal($entity),
  126.         ]);
  127.     }
  128.     
  129.     private function getDefaultFilters(\stdClass $filters null): \stdClass
  130.     {
  131.         if (!$filters) {
  132.             $filters = new \stdClass();
  133.         }
  134.         if ($this->featured) {
  135.             $filters->featured true;
  136.         }
  137.         
  138.         $filters->sort FrontProposalRepository::ORDER_ORD_DESC;
  139.         return $filters;
  140.     }
  141.     private function getUrlFilters(\stdClass $filters nullRequest $request): \stdClass
  142.     {
  143.         if ($request->query->get('dateFrom')) {
  144.             $filters->dateFrom $request->query->get('dateFrom');
  145.         }
  146.         
  147.         return $filters;
  148.     }
  149.     private function parseFilters(\stdClass $filters): \stdClass
  150.     {
  151.         foreach ($filters as $filter => $value) {
  152.             if (!$value) {
  153.                 unset($filters->$filter);
  154.             }
  155.         }
  156.         return $filters;
  157.     }
  158.     
  159.     protected function generateLangLinksProposal$entity)
  160.     {
  161.         $repo $this->em->getRepository('Openform:ProposalTranslation');
  162.         
  163.         $translations $repo->findBy(['translatableId' => $entity->getId()]);
  164.         $langLinks = [];
  165.         foreach ($translations as $transItem){
  166.             if (strlen($transItem->getSlug()) > 0) {
  167.                 $url $this->routeLocalizer->generate('proposal_item', ['slug' => $transItem->getSlug()], $transItem->getLang());
  168.                 
  169.                 $langLinks[$transItem->getLang()] = $url;
  170.             }
  171.         }
  172.         return $langLinks;        
  173.     }
  174. }