<?php
namespace App\Controller\Front;
use App\Entity\Home;
use App\Entity\Property;
use App\Entity\PropertyMap;
use App\Entity\Region;
use App\Service\LocationIP\LocationIpInterface as LocationIP;
use App\Service\Regionalization\Location;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Interfaces\Csfr;
use App\Interfaces\Em;
use App\Manager\HomeManager;
use App\Manager\InfoFormManager;
use App\Manager\PropertyManager;
use App\Manager\AboutManager;
use App\Manager\InfoMapManager;
use App\Manager\PayAutoManager;
use App\Manager\PayHereManager;
use App\Manager\PayOnlineManager;
use App\Manager\RegionManager;
use App\Traits\FormTokenTrait;
use App\Traits\AllowTrait;
class DefaultController extends AbstractController implements Csfr, Em
{
use FormTokenTrait;
use AllowTrait;
protected array $locals = [];
protected $sd_em;
private RequestStack $requestStack;
public function __construct(EntityManagerInterface $em, RequestStack $requestStack)
{
$this->sd_em = $em;
$this->requestStack = $requestStack;
$this->locals['menu_active'] = '';
}
public function getCsrfManager(): CsrfTokenManagerInterface
{
return $this->container->get('security.csrf.token_manager');
}
public function getEm(): EntityManagerInterface
{
return $this->sd_em;
}
#[Route(path: '/', name: 'home', methods: ['GET'])]
#[Template('front/default/home.html.twig')]
public function homeAction(Request $request, HomeManager $hmgr, PropertyManager $pmgr, InfoFormManager $ifmgr): RedirectResponse|array
{
//if (!is_null($city = $this->getCitySession())) {
// $parameters = array_merge(['city' => $city->getRegion()->getSlug()], $request->query->all());
// return $this->redirectToRoute('home_city', $parameters);
//}
$this->locals = $this->home($hmgr, $pmgr, $ifmgr);
return $this->locals;
}
#[Route(path: '/home-{city}', name: 'home_city', methods: ['GET'])]
#[Template('front/default/home.html.twig')]
public function homeCityAction(
//string $city, HomeManager $hmgr, PropertyManager $pmgr, InfoFormManager $ifmgr
): RedirectResponse
{
//$this->locals = $this->home($hmgr, $pmgr, $ifmgr);
//$this->locals['region_city'] = $this->getCitySession();
//return $this->locals;
return $this->redirectToRoute('home');
}
protected function home(HomeManager $hmgr, PropertyManager $pmgr, InfoFormManager $ifmgr): array
{
/** @var Home $home */
$home = $hmgr->find(1);
$home = $this->prepareBanner($home);
$banner = $this->processBanner($home->getBannerItems(), $pmgr->findAll());
$home->setBannerItems($banner);
$locals['home'] = $home;
$locals['property_types'] = $pmgr->listTabPropertyTypes();
$tmp = $pmgr->home();
$locals['list_items'] = [];
foreach ($tmp as $e) {
$locals['list_items'][$e['property_type_id']][] = $e;
}
$locals['_token'] = $this->generateToken($this->subscription_intention);
$locals['info_form'] = $ifmgr->find(1);
$locals['is_home_page'] = true;
return $locals;
}
#[Route(path: '/nosotros/', name: 'about', methods: ['GET'])]
#[Template('front/default/about.html.twig')]
public function aboutAction(Request $request, AboutManager $amgr): array
{
$this->locals['home'] = $amgr->find(1);
return $this->locals;
}
#[Route(path: '/mapa/', name: 'map', methods: ['GET'])]
#[Template('front/default/map.html.twig')]
public function mapAction(Request $request, PropertyManager $pmgr, RegionManager $rmgr, InfoMapManager $immgr): RedirectResponse|array
{
//if (!is_null($city = $this->getCitySession())) {
// $parameters = array_merge(['city' => $city->getRegion()->getSlug()], $request->query->all());
// return $this->redirectToRoute('map_city', $parameters);
//}
$map = $this->map($request, $pmgr, $rmgr, $immgr);
if ($map instanceof RedirectResponse) {
return $map;
}
$this->locals = $map;
return $this->locals;
}
#[Route(path: '/mapa/{city}', name: 'map_city', methods: ['GET'])]
#[Template('front/default/map_city.html.twig')]
public function mapCityAction(
//string $city, Request $request, PropertyManager $pmgr, RegionManager $rmgr, InfoMapManager $immgr
): RedirectResponse
{
//$this->locals = $this->map($request, $pmgr, $rmgr, $immgr);
//$this->locals['region_city'] = $this->getCitySession();
//return $this->locals;
return $this->redirectToRoute('map');
}
protected function map(Request $request, PropertyManager $pmgr, RegionManager $rmgr, InfoMapManager $immgr): RedirectResponse|array
{
if (!$this->allowMenu('map')) {
return $this->redirect($this->generateUrl('home'));
}
$type_slug = trim($request->query->get('sd_type'));
$propertyType = null;
$propertyTypes = $pmgr->listTypesFilter();
if ($type_slug != '') {
foreach ($propertyTypes as $item) {
if (!is_array($item)) {
continue;
}
$itemSlug = $item['id'] ?? null;
if (is_null($itemSlug)) {
continue;
}
if ($type_slug == $itemSlug) {
$propertyType = $item;
break;
}
}
}
$regions = $rmgr->listInMap($type_slug);
$regions = $this->filterRegions($regions);
$regions = $this->processRegions($regions);
$locals['home'] = $immgr->find(1);
$locals['locations'] = [];
$locals['regions'] = $regions;
$locals['property_type'] = $propertyType;
return $locals;
}
#[Route(path: '/paga-aqui/', name: 'pay_here', methods: ['GET'])]
#[Template('front/default/pay_here.html.twig')]
public function payHereAction(Request $request, PayHereManager $phmgr): array
{
$this->locals['home'] = $phmgr->find(1);
return $this->locals;
}
#[Route(path: '/paga-online/', name: 'pay_online', methods: ['GET'])]
#[Template('front/default/pay_online.html.twig')]
public function payOnlineAction(Request $request, PayOnlineManager $pomgr): array
{
$this->locals['home'] = $pomgr->find(1);
return $this->locals;
}
#[Route(path: '/pago-automatico/', name: 'pay_auto', methods: ['GET'])]
#[Template('front/default/pay_auto.html.twig')]
public function payAutoAction(Request $request, PayAutoManager $pamgr): array
{
$this->locals['home'] = $pamgr->find(1);
return $this->locals;
}
protected function filterRegions(?array $regions): ?array
{
if (!is_array($regions)) {
return [];
}
$filteredRegions = array_filter($regions, function (Region $region) {
return $region->getPropertiesActive()->count() > 0;
});
return array_values($filteredRegions);
}
protected function processRegions(array $regions): ?array
{
/** @var Region $region */
foreach ($regions as $region) {
$mapContent = $region->getMapContent();
if (is_null($mapContent) or trim($mapContent) == "") {
return $regions;
}
$mapContentCities = explode("\r\n", $mapContent);
$mapContentCities = array_map(function (string $city) {
return trim($city);
}, $mapContentCities);
$propertiesCities = array_map(function (PropertyMap $propertyMap) {
return trim($propertyMap->getProperty()->getLocation()->getTitle());
}, $region->getPropertiesActive()->toArray());
$citiesIntersect = [];
foreach ($mapContentCities as $city) {
$mapContentCity = mb_strtolower(preg_replace("/\s+/", " ", $city));
/** @var string $propertiesCity */
foreach ($propertiesCities as $propertiesCity) {
$propertyCity = mb_strtolower(preg_replace("/\s+/", " ", $propertiesCity));
if ($mapContentCity == $propertyCity) {
$citiesIntersect[] = $propertiesCity;
break;
}
}
}
$citiesIntersect = array_unique($citiesIntersect);
$cities = implode(PHP_EOL, $citiesIntersect);
$region->setMapContent($cities);
}
return $regions;
}
protected function prepareBanner(Home $home): Home
{
if (is_null($city = $this->getCitySession())) {
return $home;
}
$city = $city->getRegion();
if (is_null($city)) {
return $home;
}
if ($city->isBannerVideoShow() and $city->getBannerVideoUrl() and $city->getBannerVideoImg()) {
$home->replaceBannerFromRegion($city);
return $home;
}
if (count($city->getBannerItems()) > 0) {
$home->replaceBannerFromRegion($city);
return $home;
}
return $home;
}
protected function processBanner(array $banner, array $properties): array
{
$propertiesOptions = [];
/** @var Property $property */
foreach ($properties as $property) {
$propertiesOptions[$property->getSlug()] = $property;
}
return array_map(function ($item) use ($propertiesOptions) {
$item['propertyInfo'] = null;
$propertySlug = $item['property'] ?? null;
if (is_null($propertySlug)) {
return $item;
}
$property = $propertiesOptions[$propertySlug] ?? null;
if (is_null($property)) {
return $item;
}
$item['propertyInfo'] = [
'name' => $property->getApiSapZone()->getProjectDesc(),
'location' => $property->getLocation()->getTitle(),
'plaza' => $property->getApiSapZone()->getSquareDesc(),
'type' => $property->getPropertyType()->getTitle(),
];
return $item;
}, $banner);
}
protected function getCitySession(): ?Location
{
$session = $this->requestStack->getSession();
return $session->get(LocationIP::REGION);
}
}