<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Room;
use App\Entity\User;
use App\Entity\LandLord;
use App\Entity\Tennant;
use App\Entity\RentalConditions;
use App\Entity\ListingViews;
use App\Services\ConfigService;
class RoomController extends AbstractController
{
/**
* @Route("{_locale}/room/{url}", name="room", requirements={"_locale": "es|en"})
*/
public function index(string $url = '', Request $request): Response
{
$locale = $request->getLocale();
//find flat by url
$room = $this->getDoctrine()->getRepository(Room::class)->findOneBy(array('url' => $url));
//check if is found
if(!$room){
return $this->redirectToRoute('404');
}
//check if room is deativate
if($room->getActive() == '0'){
return $this->redirectToRoute('inactive_listing');
}
//get publisher data
$publisher = $this->getDoctrine()->getRepository(User::class)->findOneBy(array('id' => $room->getUserId()));
$publisherType = '';
//check if publisher tennant or landlord
$publisherData = $this->getDoctrine()->getRepository(Tennant::class)
->findOneBy(array('user' => $room->getUserId()));
if(\is_null($publisherData)){
$publisherType = 'landlord';
$publisherData = $this->getDoctrine()->getRepository(LandLord::class)
->findOneBy(array('user' => $room->getUserId()));
}
//rental conditions array
$rt = $this->getDoctrine()->getRepository(RentalConditions::class)->findOneBy(array('listingType'=>'room'));
//get maps api key
$config = new ConfigService($this->getDoctrine()->getManager());
$mapsApiKey = $config->getMapsApiKey($this->getDoctrine()->getManager());
//add visitors count
$et = $this->getDoctrine()->getManager();
$views = new ListingViews();
$views->setListingType('room');
$views->setListingId($room->getId());
$views->setVisitDate(\time());
$et->persist($views);
$et->flush();
return $this->render('public/_room.html.twig', [
'room' => $room,
'publisher'=>$publisher,
'publishertype'=>$publisherType,
'publisherdata'=>$publisherData,
'conditions'=>$rt,
'maps_key'=>$mapsApiKey
]);
}
}