src/Controller/Front/DefaultController.php line 323

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Home;
  4. use App\Entity\Property;
  5. use App\Entity\PropertyMap;
  6. use App\Entity\Region;
  7. use App\Service\LocationIP\LocationIpInterface as LocationIP;
  8. use App\Service\Regionalization\Location;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use App\Interfaces\Csfr;
  18. use App\Interfaces\Em;
  19. use App\Manager\HomeManager;
  20. use App\Manager\InfoFormManager;
  21. use App\Manager\PropertyManager;
  22. use App\Manager\AboutManager;
  23. use App\Manager\InfoMapManager;
  24. use App\Manager\PayAutoManager;
  25. use App\Manager\PayHereManager;
  26. use App\Manager\PayOnlineManager;
  27. use App\Manager\RegionManager;
  28. use App\Traits\FormTokenTrait;
  29. use App\Traits\AllowTrait;
  30. class DefaultController extends AbstractController implements CsfrEm
  31. {
  32.     use FormTokenTrait;
  33.     use AllowTrait;
  34.     protected array $locals = [];
  35.     protected $sd_em;
  36.     private RequestStack $requestStack;
  37.     public function __construct(EntityManagerInterface $emRequestStack $requestStack)
  38.     {
  39.         $this->sd_em $em;
  40.         $this->requestStack $requestStack;
  41.         $this->locals['menu_active'] = '';
  42.     }
  43.     public function getCsrfManager(): CsrfTokenManagerInterface
  44.     {
  45.         return $this->container->get('security.csrf.token_manager');
  46.     }
  47.     public function getEm(): EntityManagerInterface
  48.     {
  49.         return $this->sd_em;
  50.     }
  51.     #[Route(path'/'name'home'methods: ['GET'])]
  52.     #[Template('front/default/home.html.twig')]
  53.     public function homeAction(Request $requestHomeManager $hmgrPropertyManager $pmgrInfoFormManager $ifmgr): RedirectResponse|array
  54.     {
  55.         //if (!is_null($city = $this->getCitySession())) {
  56.         //    $parameters = array_merge(['city' => $city->getRegion()->getSlug()], $request->query->all());
  57.         //    return $this->redirectToRoute('home_city', $parameters);
  58.         //}
  59.         $this->locals $this->home($hmgr$pmgr$ifmgr);
  60.         return $this->locals;
  61.     }
  62.     #[Route(path'/home-{city}'name'home_city'methods: ['GET'])]
  63.     #[Template('front/default/home.html.twig')]
  64.     public function homeCityAction(
  65.         //string $city, HomeManager $hmgr, PropertyManager $pmgr, InfoFormManager $ifmgr
  66.     ): RedirectResponse
  67.     {
  68.         //$this->locals = $this->home($hmgr, $pmgr, $ifmgr);
  69.         //$this->locals['region_city'] = $this->getCitySession();
  70.         //return $this->locals;
  71.         return $this->redirectToRoute('home');
  72.     }
  73.     protected function home(HomeManager $hmgrPropertyManager $pmgrInfoFormManager $ifmgr): array
  74.     {
  75.         /** @var Home $home */
  76.         $home $hmgr->find(1);
  77.         $home $this->prepareBanner($home);
  78.         $banner $this->processBanner($home->getBannerItems(), $pmgr->findAll());
  79.         $home->setBannerItems($banner);
  80.         $locals['home'] = $home;
  81.         $locals['property_types'] = $pmgr->listTabPropertyTypes();
  82.         $tmp $pmgr->home();
  83.         $locals['list_items'] = [];
  84.         foreach ($tmp as $e) {
  85.             $locals['list_items'][$e['property_type_id']][] = $e;
  86.         }
  87.         $locals['_token'] = $this->generateToken($this->subscription_intention);
  88.         $locals['info_form'] = $ifmgr->find(1);
  89.         $locals['is_home_page'] = true;
  90.         return $locals;
  91.     }
  92.     #[Route(path'/nosotros/'name'about'methods: ['GET'])]
  93.     #[Template('front/default/about.html.twig')]
  94.     public function aboutAction(Request $requestAboutManager $amgr): array
  95.     {
  96.         $this->locals['home'] = $amgr->find(1);
  97.         return $this->locals;
  98.     }
  99.     #[Route(path'/mapa/'name'map'methods: ['GET'])]
  100.     #[Template('front/default/map.html.twig')]
  101.     public function mapAction(Request $requestPropertyManager $pmgrRegionManager $rmgrInfoMapManager $immgr): RedirectResponse|array
  102.     {
  103.         //if (!is_null($city = $this->getCitySession())) {
  104.         //    $parameters = array_merge(['city' => $city->getRegion()->getSlug()], $request->query->all());
  105.         //    return $this->redirectToRoute('map_city', $parameters);
  106.         //}
  107.         $map $this->map($request$pmgr$rmgr$immgr);
  108.         if ($map instanceof RedirectResponse) {
  109.             return $map;
  110.         }
  111.         $this->locals $map;
  112.         return $this->locals;
  113.     }
  114.     #[Route(path'/mapa/{city}'name'map_city'methods: ['GET'])]
  115.     #[Template('front/default/map_city.html.twig')]
  116.     public function mapCityAction(
  117.         //string $city, Request $request, PropertyManager $pmgr, RegionManager $rmgr, InfoMapManager $immgr
  118.     ): RedirectResponse
  119.     {
  120.         //$this->locals = $this->map($request, $pmgr, $rmgr, $immgr);
  121.         //$this->locals['region_city'] = $this->getCitySession();
  122.         //return $this->locals;
  123.         return $this->redirectToRoute('map');
  124.     }
  125.     protected function map(Request $requestPropertyManager $pmgrRegionManager $rmgrInfoMapManager $immgr): RedirectResponse|array
  126.     {
  127.         if (!$this->allowMenu('map')) {
  128.             return $this->redirect($this->generateUrl('home'));
  129.         }
  130.         $type_slug trim($request->query->get('sd_type'));
  131.         $propertyType null;
  132.         $propertyTypes $pmgr->listTypesFilter();
  133.         if ($type_slug != '') {
  134.             foreach ($propertyTypes as $item) {
  135.                 if (!is_array($item)) {
  136.                     continue;
  137.                 }
  138.                 $itemSlug $item['id'] ?? null;
  139.                 if (is_null($itemSlug)) {
  140.                     continue;
  141.                 }
  142.                 if ($type_slug == $itemSlug) {
  143.                     $propertyType $item;
  144.                     break;
  145.                 }
  146.             }
  147.         }
  148.         $regions $rmgr->listInMap($type_slug);
  149.         $regions $this->filterRegions($regions);
  150.         $regions $this->processRegions($regions);
  151.         $locals['home'] = $immgr->find(1);
  152.         $locals['locations'] = [];
  153.         $locals['regions'] = $regions;
  154.         $locals['property_type'] = $propertyType;
  155.         return $locals;
  156.     }
  157.     #[Route(path'/paga-aqui/'name'pay_here'methods: ['GET'])]
  158.     #[Template('front/default/pay_here.html.twig')]
  159.     public function payHereAction(Request $requestPayHereManager $phmgr): array
  160.     {
  161.         $this->locals['home'] = $phmgr->find(1);
  162.         return $this->locals;
  163.     }
  164.     #[Route(path'/paga-online/'name'pay_online'methods: ['GET'])]
  165.     #[Template('front/default/pay_online.html.twig')]
  166.     public function payOnlineAction(Request $requestPayOnlineManager $pomgr): array
  167.     {
  168.         $this->locals['home'] = $pomgr->find(1);
  169.         return $this->locals;
  170.     }
  171.     #[Route(path'/pago-automatico/'name'pay_auto'methods: ['GET'])]
  172.     #[Template('front/default/pay_auto.html.twig')]
  173.     public function payAutoAction(Request $requestPayAutoManager $pamgr): array
  174.     {
  175.         $this->locals['home'] = $pamgr->find(1);
  176.         return $this->locals;
  177.     }
  178.     protected function filterRegions(?array $regions): ?array
  179.     {
  180.         if (!is_array($regions)) {
  181.             return [];
  182.         }
  183.         $filteredRegions array_filter($regions, function (Region $region) {
  184.             return $region->getPropertiesActive()->count() > 0;
  185.         });
  186.         return array_values($filteredRegions);
  187.     }
  188.     protected function processRegions(array $regions): ?array
  189.     {
  190.         /** @var Region $region */
  191.         foreach ($regions as $region) {
  192.             $mapContent $region->getMapContent();
  193.             if (is_null($mapContent) or trim($mapContent) == "") {
  194.                 return $regions;
  195.             }
  196.             $mapContentCities explode("\r\n"$mapContent);
  197.             $mapContentCities array_map(function (string $city) {
  198.                 return trim($city);
  199.             }, $mapContentCities);
  200.             $propertiesCities array_map(function (PropertyMap $propertyMap) {
  201.                 return trim($propertyMap->getProperty()->getLocation()->getTitle());
  202.             }, $region->getPropertiesActive()->toArray());
  203.             $citiesIntersect = [];
  204.             foreach ($mapContentCities as $city) {
  205.                 $mapContentCity mb_strtolower(preg_replace("/\s+/"" "$city));
  206.                 /** @var string $propertiesCity */
  207.                 foreach ($propertiesCities as $propertiesCity) {
  208.                     $propertyCity mb_strtolower(preg_replace("/\s+/"" "$propertiesCity));
  209.                     if ($mapContentCity == $propertyCity) {
  210.                         $citiesIntersect[] = $propertiesCity;
  211.                         break;
  212.                     }
  213.                 }
  214.             }
  215.             $citiesIntersect array_unique($citiesIntersect);
  216.             $cities implode(PHP_EOL$citiesIntersect);
  217.             $region->setMapContent($cities);
  218.         }
  219.         return $regions;
  220.     }
  221.     protected function prepareBanner(Home $home): Home
  222.     {
  223.         if (is_null($city $this->getCitySession())) {
  224.             return $home;
  225.         }
  226.         $city $city->getRegion();
  227.         if (is_null($city)) {
  228.             return $home;
  229.         }
  230.         if ($city->isBannerVideoShow() and $city->getBannerVideoUrl() and $city->getBannerVideoImg()) {
  231.             $home->replaceBannerFromRegion($city);
  232.             return $home;
  233.         }
  234.         if (count($city->getBannerItems()) > 0) {
  235.             $home->replaceBannerFromRegion($city);
  236.             return $home;
  237.         }
  238.         return $home;
  239.     }
  240.     protected function processBanner(array $banner, array $properties): array
  241.     {
  242.         $propertiesOptions = [];
  243.         /** @var Property $property */
  244.         foreach ($properties as $property) {
  245.             $propertiesOptions[$property->getSlug()] = $property;
  246.         }
  247.         return array_map(function ($item) use ($propertiesOptions) {
  248.             $item['propertyInfo'] = null;
  249.             $propertySlug $item['property'] ?? null;
  250.             if (is_null($propertySlug)) {
  251.                 return $item;
  252.             }
  253.             $property $propertiesOptions[$propertySlug] ?? null;
  254.             if (is_null($property)) {
  255.                 return $item;
  256.             }
  257.             $item['propertyInfo'] = [
  258.                 'name' => $property->getApiSapZone()->getProjectDesc(),
  259.                 'location' => $property->getLocation()->getTitle(),
  260.                 'plaza' => $property->getApiSapZone()->getSquareDesc(),
  261.                 'type' => $property->getPropertyType()->getTitle(),
  262.             ];
  263.             return $item;
  264.         }, $banner);
  265.     }
  266.     protected function getCitySession(): ?Location
  267.     {
  268.         $session $this->requestStack->getSession();
  269.         return $session->get(LocationIP::REGION);
  270.     }
  271. }