<?php
namespace App\Controller\Front;
use App\Entity\Property;
use App\Manager\InfoPropertyManager;
use App\Manager\PropertyManager;
use App\Manager\InfoFormManager;
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\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use App\Interfaces\Csfr;
use App\Manager\LandingScheduleManager;
use App\Traits\FormTokenTrait;
class PropertyController extends AbstractController implements Csfr
{
use FormTokenTrait;
protected array $locals = [];
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
$this->locals['menu_active'] = '';
}
public function getCsrfManager(): CsrfTokenManagerInterface
{
return $this->container->get('security.csrf.token_manager');
}
#[Route(path: '/catalogo/', name: 'projects', methods: ['GET'])]
#[Template('front/property/properties.html.twig')]
public function propertiesAction(Request $request, InfoPropertyManager $ipmgr, PropertyManager $pmgr): RedirectResponse|array
{
//if (!is_null($city = $this->getCitySession())) {
// $parameters = array_merge(['city' => $city->getRegion()->getSlug()], $request->query->all());
// return $this->redirectToRoute('projects_city', $parameters);
//}
$this->locals = $this->properties($request, $pmgr, $ipmgr);
return $this->locals;
}
#[Route(path: '/catalogo-{city}/', name: 'projects_city', methods: ['GET'])]
#[Template('front/property/properties.html.twig')]
public function propertiesCityAction(
//string $city, Request $request, InfoPropertyManager $ipmgr, PropertyManager $pmgr
): RedirectResponse
{
//$this->locals = $this->properties($request, $pmgr, $ipmgr);
//$this->locals['region_city'] = $this->getCitySession();
//return $this->locals;
return $this->redirectToRoute('projects');
}
public function properties(Request $request, PropertyManager $pmgr, InfoPropertyManager $ipmgr): array
{
$type_slug = trim($request->query->get('sd_type', ''));
$location_slug = trim($request->query->get('sd_location', ''));
$some = trim($request->query->get('some', 'no'));
$types = [];
if ($some == 'yes') {
//solo cuando es de location piura y se requiere obtener todas las casas
$filter_types = $pmgr->listTypesFilter();
foreach ($filter_types as $f) {
if (str_contains($f['id'], 'casa')) {
$types[] = $f['id'];
}
}
}
$locals['home'] = $ipmgr->find(1);
$locals['list_items'] = $pmgr->search($type_slug, $location_slug, $types);
if ($location_slug) {
$locals['filter_types'] = $pmgr->listTypesFilter($location_slug);
}
return $locals;
}
#[Route(path: '/asia/opciones/', name: 'asia_options', methods: ['GET'])]
#[Template('front/property/asia_options.html.twig')]
public function asiaOptionsAction(Request $request, InfoPropertyManager $ipmgr): array
{
$option = $ipmgr->find(1);
$options = [
[
'optionOnlyImage' => $option->getOptionAsiaCondOnlyImage(),
'optionImage' => $option->getOptionAsiaCondImage(),
'optionTitle' => $option->getOptionAsiaCondTitle(),
'optionUrl' => $option->getOptionAsiaCondUrl(),
'optionText' => $option->getOptionAsiaCondText(),
'optionSequence' => $option->getOptionAsiaCondSequence(),
'optionIsCasa' => true,
'optionWithOpacity' => $option->getOptionAsiaCondWithOpacity()
],
[
'optionOnlyImage' => $option->getOptionAsiaLotesOnlyImage(),
'optionImage' => $option->getOptionAsiaLotesImage(),
'optionTitle' => $option->getOptionAsiaLotesTitle(),
'optionUrl' => $option->getOptionAsiaLotesUrl(),
'optionText' => $option->getOptionAsiaLotesText(),
'optionSequence' => $option->getOptionAsiaLotesSequence(),
'optionIsCasa' => false,
'optionWithOpacity' => $option->getOptionAsiaLotesWithOpacity()
]
];
$options = $this->orderOptions($options);
$page_title = $option->getOptionAsiaPageTitle();
unset($option);
$this->locals['options'] = $options;
$this->locals['page_title'] = $page_title;
return $this->locals;
}
#[Route(path: '/chilca/opciones/', name: 'chilca_options', methods: ['GET'])]
#[Template('front/property/chilca_options.html.twig')]
public function chilcaOptionsAction(Request $request, InfoPropertyManager $ipmgr): array
{
$option = $ipmgr->find(1);
$options = [
[
'optionOnlyImage' => $option->getOptionChilcaLotesOnlyImage(),
'optionImage' => $option->getOptionChilcaLotesImage(),
'optionTitle' => $option->getOptionChilcaLotesTitle(),
'optionUrl' => $option->getOptionChilcaLotesUrl(),
'optionText' => $option->getOptionChilcaLotesText(),
'optionSequence' => $option->getOptionChilcaLotesSequence(),
'optionIsCasa' => false,
'optionWithOpacity' => $option->getOptionChilcaLotesWithOpacity()
],
[
'optionOnlyImage' => $option->getOptionChilcaCasasOnlyImage(),
'optionImage' => $option->getOptionChilcaCasasImage(),
'optionTitle' => $option->getOptionChilcaCasasTitle(),
'optionUrl' => $option->getOptionChilcaCasasUrl(),
'optionText' => $option->getOptionChilcaCasasText(),
'optionSequence' => $option->getOptionChilcaCasasSequence(),
'optionIsCasa' => true,
'optionWithOpacity' => $option->getOptionChilcaCasasWithOpacity()
]
];
$options = $this->orderOptions($options);
$page_title = $option->getOptionChilcaPageTitle();
unset($option);
$this->locals['options'] = $options;
$this->locals['page_title'] = $page_title;
return $this->locals;
}
#[Route(path: '/chiclayo/opciones/', name: 'chiclayo_options', methods: ['GET'])]
#[Template('front/property/chiclayo_options.html.twig')]
public function chiclayoOptionsAction(Request $request, InfoPropertyManager $ipmgr): array
{
$option = $ipmgr->find(1);
$options = [
[
'optionOnlyImage' => $option->getOptionChiclayoLotesOnlyImage(),
'optionImage' => $option->getOptionChiclayoLotesImage(),
'optionTitle' => $option->getOptionChiclayoLotesTitle(),
'optionUrl' => $option->getOptionChiclayoLotesUrl(),
'optionText' => $option->getOptionChiclayoLotesText(),
'optionSequence' => $option->getOptionChiclayoLotesSequence(),
'optionIsCasa' => false,
'optionWithOpacity' => $option->getOptionChiclayoLotesWithOpacity()
],
[
'optionOnlyImage' => $option->getOptionChiclayoCasasOnlyImage(),
'optionImage' => $option->getOptionChiclayoCasasImage(),
'optionTitle' => $option->getOptionChiclayoCasasTitle(),
'optionUrl' => $option->getOptionChiclayoCasasUrl(),
'optionText' => $option->getOptionChiclayoCasasText(),
'optionSequence' => $option->getOptionChiclayoCasasSequence(),
'optionIsCasa' => true,
'optionWithOpacity' => $option->getOptionChiclayoCasasWithOpacity()
]
];
$options = $this->orderOptions($options);
$page_title = $option->getOptionChiclayoPageTitle();
unset($option);
$this->locals['options'] = $options;
$this->locals['page_title'] = $page_title;
return $this->locals;
}
#[Route(path: '/ica/opciones/', name: 'ica_options', methods: ['GET'])]
#[Template('front/property/ica_options.html.twig')]
public function icaOptionsAction(Request $request, InfoPropertyManager $ipmgr): array
{
$option = $ipmgr->find(1);
$options = [
[
'optionOnlyImage' => $option->getOptionIcaLotesOnlyImage(),
'optionImage' => $option->getOptionIcaLotesImage(),
'optionTitle' => $option->getOptionIcaLotesTitle(),
'optionUrl' => $option->getOptionIcaLotesUrl(),
'optionText' => $option->getOptionIcaLotesText(),
'optionSequence' => $option->getOptionIcaLotesSequence(),
'optionIsCasa' => false,
'optionWithOpacity' => $option->getOptionIcaLotesWithOpacity()
],
[
'optionOnlyImage' => $option->getOptionIcaCasasOnlyImage(),
'optionImage' => $option->getOptionIcaCasasImage(),
'optionTitle' => $option->getOptionIcaCasasTitle(),
'optionUrl' => $option->getOptionIcaCasasUrl(),
'optionText' => $option->getOptionIcaCasasText(),
'optionSequence' => $option->getOptionIcaCasasSequence(),
'optionIsCasa' => true,
'optionWithOpacity' => $option->getOptionIcaCasasWithOpacity()
],
[
'optionOnlyImage' => $option->getOptionIcaTechoOnlyImage(),
'optionImage' => $option->getOptionIcaTechoImage(),
'optionTitle' => $option->getOptionIcaTechoTitle(),
'optionUrl' => $option->getOptionIcaTechoUrl(),
'optionText' => $option->getOptionIcaTechoText(),
'optionSequence' => $option->getOptionIcaTechoSequence(),
'optionIsCasa' => true,
'optionWithOpacity' => $option->getOptionIcaTechoWithOpacity()
]
];
$options = $this->orderOptions($options);
$page_title = $option->getOptionIcaPageTitle();
unset($option);
$this->locals['options'] = $options;
$this->locals['page_title'] = $page_title;
return $this->locals;
}
#[Route(path: '/lambayeque/opciones/', name: 'lambayeque_options', methods: ['GET'])]
#[Template('front/property/lambayeque_options.html.twig')]
public function lambayequeOptionsAction(Request $request, InfoPropertyManager $ipmgr): array
{
$option = $ipmgr->find(1);
$options = [
[
'optionOnlyImage' => $option->getOptionLambayequeLotesOnlyImage(),
'optionImage' => $option->getOptionLambayequeLotesImage(),
'optionTitle' => $option->getOptionLambayequeLotesTitle(),
'optionUrl' => $option->getOptionLambayequeLotesUrl(),
'optionText' => $option->getOptionLambayequeLotesText(),
'optionSequence' => $option->getOptionLambayequeLotesSequence(),
'optionIsCasa' => false,
'optionWithOpacity' => $option->getOptionLambayequeLotesWithOpacity()
],
[
'optionOnlyImage' => $option->getOptionLambayequeCasasOnlyImage(),
'optionImage' => $option->getOptionLambayequeCasasImage(),
'optionTitle' => $option->getOptionLambayequeCasasTitle(),
'optionUrl' => $option->getOptionLambayequeCasasUrl(),
'optionText' => $option->getOptionLambayequeCasasText(),
'optionSequence' => $option->getOptionLambayequeCasasSequence(),
'optionIsCasa' => true,
'optionWithOpacity' => $option->getOptionLambayequeCasasWithOpacity()
]
];
$options = $this->orderOptions($options);
$page_title = $option->getOptionLambayequePageTitle();
unset($option);
$this->locals['options'] = $options;
$this->locals['page_title'] = $page_title;
return $this->locals;
}
#[Route(path: '/piura/opciones/', name: 'piura_options', methods: ['GET'])]
#[Template('front/property/piura_options.html.twig')]
public function piuraOptionsAction(Request $request, InfoPropertyManager $ipmgr, PropertyManager $pmgr): array
{
$option = $ipmgr->find(1);
$options = [
[
'optionOnlyImage' => $option->getOptionCasasOnlyImage(),
'optionImage' => $option->getOptionCasasImage(),
'optionTitle' => $option->getOptionCasasTitle(),
'optionText' => $option->getOptionCasasText(),
'optionUrl' => $option->getOptionCasasUrl(),
'optionSequence' => $option->getOptionCasasSequence(),
'optionIsCasa' => true,
'optionWithOpacity' => $option->getOptionCasasWithOpacity()
],
[
'optionOnlyImage' => $option->getOptionLotesOnlyImage(),
'optionImage' => $option->getOptionLotesImage(),
'optionTitle' => $option->getOptionLotesTitle(),
'optionText' => $option->getOptionLotesText(),
'optionUrl' => $option->getOptionLotesUrl(),
'optionSequence' => $option->getOptionLotesSequence(),
'optionIsCasa' => false,
'optionWithOpacity' => $option->getOptionLotesWithOpacity()
],
[
'optionOnlyImage' => $option->getOptionTechoOnlyImage(),
'optionImage' => $option->getOptionTechoImage(),
'optionTitle' => $option->getOptionTechoTitle(),
'optionText' => $option->getOptionTechoText(),
'optionUrl' => $option->getOptionTechoUrl(),
'optionSequence' => $option->getOptionTechoSequence(),
'optionIsCasa' => true,
'optionWithOpacity' => $option->getOptionTechoWithOpacity()
]
];
$options = $this->orderOptions($options);
$page_title = $option->getOptionPageTitle();
unset($option);
$filter_types = $pmgr->listTypesFilter();
$type_lote = null;
foreach ($filter_types as $f) {
if (str_contains($f['id'], 'lote')) {
$type_lote = $f['id'];
break;
}
}
$this->locals['options'] = $options;
$this->locals['piura_page_title'] = $page_title;
$this->locals['location_piura_slug'] = 'piura';
$this->locals['type_lotes_slug'] = $type_lote;
return $this->locals;
}
#[Route(path: '/piura/lotes/', name: 'piura_lotes', methods: ['GET'])]
#[Template('front/property/piura_lotes.html.twig')]
public function piuraLotesAction(Request $request, InfoPropertyManager $ipmgr): array
{
$this->locals['home'] = $ipmgr->find(1);
return $this->locals;
}
#[Route(path: '/piura/casas-techo-propio/', name: 'piura_techo', methods: ['GET'])]
#[Template('front/property/piura_techo.html.twig')]
public function piuraTechoAction(Request $request, InfoPropertyManager $ipmgr): array
{
$this->locals['home'] = $ipmgr->find(1);
return $this->locals;
}
#[Route(path: '/ica/lotes/', name: 'ica_lotes', methods: ['GET'])]
#[Template('front/property/ica_lotes.html.twig')]
public function icaLotesAction(Request $request, InfoPropertyManager $ipmgr): array
{
$this->locals['home'] = $ipmgr->find(1);
return $this->locals;
}
#[Route(path: '/chiclayo/lotes/', name: 'chiclayo_lotes', methods: ['GET'])]
#[Template('front/property/chiclayo_lotes.html.twig')]
public function chiclayoLotesAction(Request $request, InfoPropertyManager $ipmgr): array
{
$this->locals['home'] = $ipmgr->find(1);
return $this->locals;
}
#[Route(path: '/images-to-mailing/{code}/', name: 'images-to-mailing', methods: ['GET'])]
public function imagesToMailing(string $code, Request $request, PropertyManager $pmgr): JsonResponse
{
$proyecto = $pmgr->findOneBy(['sap_project' => $code]);
$img_header = '';
$img_promocion = '';
$data = [
'img_header' => $img_header,
'img_promocion' => $img_promocion,
];
if (is_null($proyecto)) {
return new JsonResponse($data);
}
if ($proyecto->getBannerEmbedVideoShow()) {
$img_header = $proyecto->getBannerEmbedImageDesktop();
}
if ($img_header == '') {
$slides = $proyecto->getBanners();
foreach ($slides as $index => $slide) {
if ($slide['show'] and $slide['img_desktop']) {
$img_header = $slide['img_desktop'];
break;
}
}
}
$img_header = is_null($img_header) ? '' : $img_header;
$img_promocion = is_null($proyecto->getThankImage()) ? '' : $proyecto->getThankImage();
$base_url = $this->getParameter('file_base_url');
$base_path = $this->getParameter('file_base_path');
$package = new Package(new EmptyVersionStrategy());
if ($img_header !== '') {
$img_header = $base_url . DIRECTORY_SEPARATOR . $base_path . $package->getUrl($img_header);
}
if ($img_promocion !== '') {
$img_promocion = $base_url . DIRECTORY_SEPARATOR . $base_path . $package->getUrl($img_promocion);
}
return new JsonResponse([
'img_header' => $img_header,
'img_promocion' => $img_promocion,
]);
}
#[Route(path: '/{slug}/', name: 'property', methods: ['GET'])]
public function propertyAction(
$slug,
PropertyManager $pmgr,
InfoFormManager $ifmgr,
InfoPropertyManager $ipmgr
): Response {
$this->locals['IS_PROPERTY_DETAIL'] = true;
$this->locals['detail'] = $pmgr->loadBySlug($slug);
if (!$this->locals['detail']) {
throw $this->createNotFoundException('No encontrado');
}
// $replace = $this->getReplaceLanding($this->locals['detail']);
// if ($replace) {
// return $this->redirectToRoute('property', ['slug' => $replace->getSlug()], 302);
// }
$this->locals['relateds'] = $pmgr->listActiveRelateds($this->locals['detail']->getId());
$template = 'front/property/property_' . $this->locals['detail']->getTemplate() . '.html.twig';
$this->locals['_token'] = $this->generateToken($this->quote_intention);
$this->locals['info_form'] = $ifmgr->find(1);
$this->locals['home'] = $ipmgr->find(1);
$tmp = $this->locals['detail']->getTemplate();
$this->locals['property_header_menu'] = array_filter(
$this->locals['home']->getMenuItems(),
fn($e) => $e['template'] == $tmp
);
if (count((array) $this->locals['property_header_menu']) > 0) {
$this->locals['property_header_menu'] = end($this->locals['property_header_menu']);
}
return $this->render($template, $this->locals);
}
private function getReplaceLanding($source, LandingScheduleManager $lsmgr): ?Property
{
$schedules = $lsmgr->getReplaces($source->getId());
if (!$schedules) {
return null;
}
$current = date('w');
$replace = null;
foreach ($schedules as $sc) {
if (in_array($current, $sc->getScheduleDays())) {
$replace = $sc->getPtarget();
break;
}
}
return $replace;
}
private function orderOptions(array $opts): array
{
usort($opts, fn($a, $b) => $a['optionSequence'] <=> $b['optionSequence']);
return $opts;
}
protected function getCitySession(): ?Location
{
$session = $this->requestStack->getSession();
return $session->get(LocationIP::REGION);
}
}