<?php
namespace App\Controller;
use App\Entity\Home;
use App\Form\HomeType;
use App\Entity\HomeGaleria;
use App\Repository\CategoriaSitioRepository;
use App\Repository\CiudadesRepository;
use App\Repository\SitiosGaleriaRepository;
use App\Repository\SitiosComodidadesRepository;
use App\Repository\SitiosRedesSocialesRepository;
use App\Repository\SitiosRepository;
use App\Service\Functions;
use App\Repository\HomeRepository;
use App\Repository\HomeGaleriaRepository;
use App\Repository\TestimoniosRepository;
use App\Repository\HomeServiciosRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
#[Route('/home')]
class HomeController extends AbstractController
{
#[Route('/bienvenido', name: 'app_home_index', methods: ['GET'])]
public function index(
HomeRepository $homeRepository, HomeServiciosRepository $homeServicios,
TestimoniosRepository $testimonios, HomeGaleriaRepository $homeGaleria,
CiudadesRepository $ciudadesRepository, SitiosRepository $sitiosRepository,
CategoriaSitioRepository $categoriaSitioRepository
): Response
{
$count_relacionados = 0;
if ( isset( $_GET['id_categoria'] ) )
{
$data['id_categoria'] = $_GET['id_categoria'];
$relacionados = $sitiosRepository->findBy(
[
'id_categoria' => $_GET['id_categoria'],
'estado' => 'Activo'
]
);
$count_relacionados = count( $relacionados );
}
return $this->render('home/index.html.twig', [
'sitios' => $sitiosRepository->findBy( ['estado' => 'Activo'],['visitas' => 'asc'],4),
'homes' => $homeRepository->find( 1),
'ciudades' => $ciudadesRepository->findBy( ['id_departamento' => 26],['nombre' => 'asc']),
'galeria' => $homeGaleria->findAll(),
'servicios' => $homeServicios->findBy(['estado' => 'Activo' ],['id' => 'desc'],4),
//'categorias' => $categoriaSitioRepository->findBy(['estado' => 'Activo' ],['id' => 'desc'],10),
'categorias' => $categoriaSitioRepository->getByImagesUploaded(),
'count_relacionados' => ($count_relacionados ? $count_relacionados : 1),
]);
}
#[Route('/busqueda_sitios', name: 'busqueda_sitios', methods: ['POST','GET'])]
public function busquedaSitios(Request $request, SitiosRepository $sitiosRepository, CiudadesRepository $ciudadesRepository): Response
{
$data = $request->request->all();
$count_relacionados = 0;
if ( isset( $_GET['id_categoria'] ) )
{
$data['id_categoria'] = $_GET['id_categoria'];
$relacionados = $sitiosRepository->findBy(['id_categoria' => $_GET['id_categoria'], 'estado' => 'Activo']);
$count_relacionados = count( $relacionados );
}
$resultado = $sitiosRepository->findBySearch($data);
return $this->render('home/explore.html.twig', [
'sitios' => $resultado,
'ciudades' => $ciudadesRepository->findBy( ['id_departamento' => 26],['nombre' => 'asc']),
'data' => $data,
'count_relacionados' => $count_relacionados
]);
}
#[Route('/index_rutas', name: 'index_rutas', methods: ['GET'])]
public function menuRutas(): Response
{
return $this->render('components/home_rutas/rutas.html.twig', []);
}
#[Route('/sitio_individual/{id}', name: 'sitio_individual', methods: ['GET'])]
public function sitioIndividual(Request $request, $id, SitiosRepository $sitiosRepository, SitiosGaleriaRepository $sitiosGaleriaRepository, SitiosComodidadesRepository $sitiosComodidadesRepository, SitiosRedesSocialesRepository $sitiosRedesSocialesRepository): Response
{
if( !empty( $id ) ){
$sitio = $sitiosRepository->find($id);
$sitio->setVisitas( $sitio->getVisitas() + 1 );
$sitiosRepository->save($sitio, true);
}
$resultado = $sitiosRepository->find($id);
$galeria = $sitiosGaleriaRepository->findBy(['id_sitio' => $id]);
$comodidades = $sitiosComodidadesRepository->findBy(['id_sitio' => $id]);
$redes = $sitiosRedesSocialesRepository->findBy(['id_sitio' => $id, 'estado' => 'Activo']);
$relacionados = $sitiosRepository->findBy(['id_categoria' => $resultado->getIdCategoria()->getId(), 'estado' => 'Activo']);
$count_galeria = count( $galeria );
$count_relacionados = count( $relacionados );
return $this->render('home/explore_individual.html.twig', [
'redes' => $redes,
'galeria' => $galeria,
'sitio' => $resultado,
'comodidades' => $comodidades,
'relacionados' => $relacionados,
'count_galeria' => $count_galeria,
'count_relacionados' => $count_relacionados
]);
}
#[Route('/obtener_sitios_mapas', name: 'obtener_sitios_mapas', methods: ['GET','POST'])]
public function busquedaSitiosMapsJson(Request $request, SitiosRepository $sitiosRepository): JsonResponse
{
$resultado = [];
$data = $request->request->all();
if ( isset( $_GET['id_categoria'] ) )
{
$data['id_categoria'] = $_GET['id_categoria'];
}
if ( !empty( $data['id_sitio'] ) ){
$sitios = $sitiosRepository->findBy(['id' => $data['id_sitio']]);
}else{
$sitios = $sitiosRepository->findBySearch($data);
}
foreach ( $sitios as $sitio)
{
$resultado [] = array(
'title' => $sitio->getNombre(),
'image' => $sitio->getImagenPrincipal(),
'type' => $sitio->getEstado(),
'price' => $sitio->getValor(),
'address' => $sitio->getDireccion(),
'bedrooms' => 0,
'bathrooms' => 0,
'area' => 0,
'position' => [
'lat' => $sitio->getLatitud(),
'lng' => $sitio->getLongitud()
],
'markerIcon' => "marker-green.png"
);
}
return new JsonResponse( $resultado );
}
/*
title : 'Modern Residence in New York',
image : '1-1-thmb.png',
type : 'For Sale',
price : '$1,550,000',
address : '39 Remsen St, Brooklyn, NY 11201, USA',
bedrooms : '3',
bathrooms : '2',
area : '3430 Sq Ft',
position : {
lat : 40.696047,
lng : -73.997159
},
markerIcon : "marker-green.png"
* */
#[Route('/all', name: 'all_sites', methods: ['GET'])]
public function indexAll(HomeRepository $homeRepository): Response
{
return $this->render('home/index_original.html.twig', [
'homes' => $homeRepository->findBy(['id' => 1]),
]);
}
#[Route('/subir_imagen_cabecera', name: 'add_home_new_galery_image', methods: ['POST'])]
public function subirImagenCabecera(Request $request, Functions $files, HomeGaleriaRepository $homeGaleria, HomeRepository $homeRepository)
{
//$home = $homeRepository->find(1);
$adjunto = $request->files->get('files');
$size_file = (filesize($adjunto) / 1024);
$arrayExtension = ['jpg','png','jpeg','pdf'];
$error_doc = true;
$fecha_hora = date("Y-m-d H:i:s");
if(!empty($adjunto)){
$extension = $adjunto->guessExtension();
if(in_array($extension,$arrayExtension)){
if(($size_file > 0 && $size_file <= 3072)){
$file = $files->uploadFile($adjunto, 'imagen_cabecera');
$galeria = new HomeGaleria();
$galeria->setImagen($file);
$galeria->setFechaHora(\DateTime::createFromFormat('Y-m-d H:i:s', $fecha_hora));
$galeria->setIdHome($homeRepository->find(1));
$error_doc = true;
}else{
$error_doc = false;
}
}else{
$error_doc = false;
}
}
if($error_doc){
$homeGaleria->save($galeria, true);
} else {
$error_doc = false;
}
return new JsonResponse(array('status' => $error_doc));
}
#[Route('/new', name: 'app_home_new', methods: ['GET', 'POST'])]
public function new(Request $request, HomeRepository $homeRepository, Functions $functions): Response
{
$home = new Home();
$form = $this->createForm(HomeType::class, $home);
$form->handleRequest($request);
if ( $form->isSubmitted() )
{
$data = $request->request->all('home');
try {
$imagen = $request->files->get('home')['imagen_cabecera'];
$upload = $functions->uploadFile($imagen, 'imagen_cabecera' );
$home_obj = new Home();
$home_obj->setImagenCabecera( $upload );
$home_obj->setTituloCabecera($data['titulo_cabecera']);
$home_obj->setSubtituloCabecera($data['subtitulo_cabecera']);
$home_obj->setEnlaceCabecera($data['enlace_cabecera']);
$home_obj->setTextoEnlaceCabecera('texto_enlace_cabecera');
$homeRepository->save($home_obj, true);
$functions->addFlash('success', 'Almacenamiento Exitoso.');
}catch ( \Exception $e){
$functions->addFlash('warning', $e->getMessage() );
}catch ( \TypeError $e){
$this->addFlash(
'warning',
'No se ha creado el registro con éxito. '.$e->getmessage()
);
return $this->redirectToRoute('app_home_index');
}
return $this->redirectToRoute('app_home_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('home/new.html.twig', [
'home' => $home,
'form' => $form,
]);
}
#[Route('/{id}/show', name: 'app_home_show', methods: ['GET'])]
public function show(HomeRepository $homeRepository, $id): Response
{
$home = $homeRepository->find($id);
return $this->render('home/show.html.twig', [
'home' => $home,
]);
}
#[Route('/{id}/edit', name: 'app_home_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, $id, HomeRepository $homeRepository, HomeGaleriaRepository $homegaleria, Functions $functions): Response
{
$home = $homeRepository->find($id);
$form = $this->createForm(HomeType::class, $home);
$form->handleRequest($request);
if ($form->isSubmitted())
{
$data = $request->request->all('home');
$imagen = $request->files->get('home')['imagen_cabecera'];
$home_obj = $homeRepository->find($id);
if( !empty( $imagen ) ){
$upload = $functions->uploadFile($imagen, 'imagen_cabecera' );
$home_obj->setImagenCabecera( $upload );
}
$home_obj->setTituloCabecera($data['titulo_cabecera']);
$home_obj->setSubtituloCabecera($data['subtitulo_cabecera']);
$home_obj->setEnlaceCabecera($data['enlace_cabecera']);
$home_obj->setTextoEnlaceCabecera('texto_enlace_cabecera');
$homeRepository->save($home_obj, true);
return $this->redirectToRoute('app_home_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('home/edit.html.twig', [
'home' => $home,
'form' => $form,
'galeria' => $homegaleria->findAll(),
'edit' => false
]);
}
#[Route('/{id}', name: 'app_home_delete', methods: ['POST'])]
public function delete(Request $request, Home $home, HomeRepository $homeRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$home->getId(), $request->request->get('_token'))) {
$homeRepository->remove($home, true);
}
return $this->redirectToRoute('app_home_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/accesDenied', name: 'accesDenied', methods: ['GET'])]
public function accesoDenegado(): Response
{
return $this->render('components/denegado/denied.html.twig', []);
}
}