<?php
namespace App\Services;
use App\Services\Util;
use App\Services\ConfigService;
use App\Services\AddressToCoordinates;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Room;
class ProcessEditRoomForm{
private $em;
private $imArray;
private $user;
private $roomId;
public function __construct(EntityManagerInterface $em, $imagesArray = array(), $user, $roomId){
$this->em = $em;
$this->imArray = $imagesArray;
$this->user = $user;
$this->roomId = $roomId;
}
public function editRoom($formData): bool{
//get google api keys
$config = new ConfigService($this->em);
$apiKey = $config->getMapsApiKey();
//get lat long
$address = $formData->get('street')->getData() . ' ' .
$formData->get('streetnomber')->getData() . ' ' .
$formData->get('city')->getData() . ' ' .
$formData->get('country')->getData();
$latLong = AddressToCoordinates::latLong($address, $apiKey);
//make rental contidions
$rt = Util::jsonToArray($formData->get('rentalconditions')->getData());
//var_dump($rt);die();
$room = $this->em->getRepository(Room::class)->findOneBy(['user_id'=>$this->user->getId(),
'id'=>$this->roomId]);
//make latlong
if($latLong != NULL){
$room->setLatLong($latLong);
}
$room->setStreet($formData->get('street')->getData());
$room->setStrNomber($formData->get('streetnomber')->getData());
$room->setFloor($formData->get('floor')->getData());
$room->setDoor($formData->get('door')->getData());
$room->setCity( \ucfirst( \strtolower($formData->get('city')->getData()) ) );
//$room->setPostalCode($formData->get('street')->getData());
$room->setCountry(\ucfirst( \strtolower($formData->get('country')->getData()) ) );
$room->setTotalRooms($formData->get('totalrooms')->getData());
$room->setTotalBathrooms($formData->get('totalbathrooms')->getData());
$room->setBedSize($formData->get('bed_size')->getData());
$room->setLift($formData->get('lift')->getData());
$room->setDoorMan($formData->get('door_man')->getData());
$room->setRoomSize($formData->get('room_size')->getData());
$room->setWindowType($formData->get('window_type')->getData());
$room->setInnerOuterPosition($formData->get('inner_outer_position')->getData());
$room->setTitle($formData->get('title')->getData());
$room->setDescription( Util::removeHtml($formData->get('description')->getData(), 10000) );
$room->setInternalRef($formData->get('internalref')->getData());
$room->setPrice($formData->get('price')->getData());
$room->setDeposit($formData->get('deposit')->getData());
$room->setRentalConditions($rt);
$room->setImages(array_merge($this->imArray, $room->getImages()));
$room->setLastUpdate(\time());
$room->setLang($formData->get('language')->getData());
//$room->setActive('1');
$room->setWifi($formData->get('wifi')->getData());
$room->setLivingRoom($formData->get('livingroom')->getData());
$room->setAvailableFrom( \strtotime($formData->get('availablefrom')->getData()) );
$room->setUserId($this->user);
$this->em->persist($room);
$this->em->flush();
return $room->getId() != NULL ? true : false;
}
}