<?php
namespace App\Services;
use App\Services\Util;
use App\Services\ConfigService;
use App\Services\AddressToCoordinates;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Flat;
class ProcessEditFlatForm{
private $em;
private $imArray;
private $user;
private $flatId;
public function __construct(EntityManagerInterface $em, $imagesArray = array(), $user, $flatId){
$this->em = $em;
$this->imArray = $imagesArray;
$this->user = $user;
$this->flatId = $flatId;
}
public function editFlat($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();
$flat = $this->em->getRepository(Flat::class)->findOneBy(['user'=>$this->user->getId(),
'id'=>$this->flatId]);
//make latlong
if($latLong != NULL){
$flat->setLatLong($latLong);
}
$flat->setStreet($formData->get('street')->getData());
$flat->setStreetNo($formData->get('streetnomber')->getData());
$flat->setFlatNo($formData->get('floor')->getData());
$flat->setFloor($formData->get('floor')->getData());
$flat->setDoorNo($formData->get('door')->getData());
$flat->setCity( \ucfirst( \strtolower($formData->get('city')->getData()) ) );
$flat->setCountry(\ucfirst( \strtolower($formData->get('country')->getData()) ) );
$flat->setTotalRooms($formData->get('totalrooms')->getData());
$flat->setTotalBathrooms($formData->get('totalbathrooms')->getData());
$flat->setLift($formData->get('lift')->getData());
$flat->setSquareMeter($formData->get('squaremeters')->getData());
$flat->setTitle($formData->get('title')->getData());
$flat->setDescription( Util::removeHtml($formData->get('description')->getData(), 10000) );
$flat->setInternalRef($formData->get('internalref')->getData());
$flat->setPrice($formData->get('price')->getData());
$flat->setDeposit($formData->get('deposit')->getData());
$flat->setRentalConditions($rt);
$flat->setImages(array_merge($this->imArray, $flat->getImages()));
$flat->setLastUpdate(\time());
$flat->setActive('1');
$flat->setLang($formData->get('language')->getData());
$flat->setWifi($formData->get('wifi')->getData());
$flat->setLivingRoom($formData->get('livingroom')->getData());
$flat->setAvailableFrom( \strtotime($formData->get('availablefrom')->getData()) );
//$flat->setUser($this->user);
$this->em->persist($flat);
$this->em->flush();
return $flat->getId() != NULL ? true : false;
}
}