<?php
namespace App\Trinity\ProjectssitesBundle\Controller;
use DateTime;
use PHPUnit\Exception;
use Doctrine\ORM\EntityManager;
use App\CmsBundle\Entity\Media;
use App\CmsBundle\Classes\Postcode;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use App\CmsBundle\Controller\StorageController;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Trinity\ProjectssitesBundle\Entity\Config;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use App\Trinity\ProjectssitesBundle\classes\RealEstate;
use App\Trinity\ProjectssitesBundle\classes\RealEstateOption;
use App\Trinity\ProjectssitesBundle\classes\RealEstateMap;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use App\Trinity\ProjectssitesBundle\Entity\RealEstate as RealEstateEntity;
use App\Trinity\ProjectssitesBundle\Entity\RealEstateOption as RealEstateOptionEntity;
use App\Trinity\ProjectssitesBundle\Entity\RealEstateMap as RealEstateMapEntity;
/**
* Class ProjectensitesController
* @package App\Trinity\ProjectssitesBundle\Controller
*/
class ProjectssitesController extends StorageController
{
/**
* @Route("/admin/realestate", name="admin_mod_projectssites")
* @Template()
*/
public function index(Request $request){
// Initialize StorageController
parent::init($request);
$this->breadcrumbs->addRouteItem('Configuratie', 'admin_mod_projectssites_config');
$this->breadcrumbs->addRouteItem('Real Estate Bundle', 'admin_mod_projectssites');
$realEstates = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstate')->findBy([
'language'=> $this->language,
'settings'=> $this->Settings]);
foreach($realEstates as $realEstate){
$realEstate->realEstateOptions = $realEstate->getRealEstateOptions();;
}
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
return $this->render('@TrinityProjectssites/backend/realestate/overview.html.twig', $this->attributes([
'realEstates' => $realEstates,
'config' => $config,
'settings' => $this->Settings,
]));
}
/**
* @Route("/admin/realestateoptions", name="admin_mod_realestateoptions")
* @Template()
*/
public function realEstateOptions(Request $request){
// Initialize StorageController
parent::init($request);
$this->breadcrumbs->addRouteItem('Configuratie', 'admin_mod_projectssites_config');
$this->breadcrumbs->addRouteItem('Real Estate Bundle', 'admin_mod_projectssites');
$realEstateOptions = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOption')->findBy([
'language'=> $this->language,
'settings'=> $this->Settings]);
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
return $this->render('@TrinityProjectssites/backend/realestateoption/overview.html.twig', $this->attributes([
'realEstateOptions' => $realEstateOptions,
'config' => $config,
'settings' => $this->Settings,
]));
}
/**
* Return link data when required within the link form
*
* @param object Doctrine object
*
* @return array Array with config options
*/
public function getLinkData($em){
return [
// Return data to link form
];
}
/**
* Show dashboard blocks
*
* @return array List of blocks
*/
public function dashboardBlocks(){
// Return block data to show on Trinity dashboard in the following format.
// You can do whatever you want in this function, just return data as below.
return [
[
'title' => 'Mijn lege module',
'class' => '',
'content' => '<div style="text-align:center;padding:20px;">Lege module</div>'
]
];
}
/**
* @Route("/admin/projects/config", name="admin_mod_projectssites_config")
* @Template()
*/
public function editConfig(Request $request, EntityManagerInterface $em, $id = null){
// Initialize StorageController
parent::init($request);
$this->breadcrumbs->addRouteItem('Real Estate Bundle', 'admin_mod_projectssites');
$this->breadcrumbs->addRouteItem('Configuratie', 'admin_mod_projectssites_config');
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
if (empty($config)) {
$config = new Config();
$config->setLanguage($this->language);
$config->setSettings($this->Settings);
}
$errors = [];
$Form = $this->createFormBuilder($config);
$pageChoises = [];
$pages = $this->getDoctrine()->getRepository('CmsBundle:Page')->findBy(['language' => $this->language, 'settings' => $this->Settings]);
foreach($pages as $page){
$pageChoises[$page->getTitle()] = $page;
}
$Form->add('overview_page', ChoiceType::class, array(
'choices' => array_merge(['Kies overzichtspagina' => ''],$pageChoises),
'attr' => ['class' => 'validate'], 'label' => 'Kies overzichtspagina *'
,'row_attr' => ['class' => 'form-floating']));
$Form->add('detail_page_root', ChoiceType::class, array(
'choices' => array_merge(['Kies pagina detail root pagina' => ''],$pageChoises),
'attr' => ['class' => 'validate'], 'label' => 'Kies pagina detail root pagina *'
,'row_attr' => ['class' => 'form-floating']));
$Form->add('google_maps_zoom', ChoiceType::class, array(
'choices' => array_merge(['Kies Google maps zoom level' => ''],array("Level 1"=>1,"Level 2"=>2,"Level 3"=>3,"Level 4"=>4,"Level 5"=>5,"Level 6"=>6,"Level 7"=>7,"Level 8"=>8,"Level 9"=>9,"Level 10"=>10,"Level 11"=>11,"Level 12"=>12,"Level 13"=>13,"Level 14"=>14,"Level 15"=>15,"Level 16"=>16,"Level 17"=>17,"Level 18"=>18)),
'attr' => array('placeholder' => 'Google maps zoom level'),
'row_attr' => ['class' => 'form-floating number']));
$Form->add('is_single_project', CheckboxType::class, array('label' => 'Enkel project', 'required' => false));
$Form->add('submit', SubmitType::class, ['label' => 'Opslaan','row_attr' => ['class' => 'none'], 'attr' => ['class' => 'btn btn-primary']]);
$Form = $Form->getForm();
$Form->handleRequest($request);
if ($Form->isSubmitted() && $Form->isValid()) {
$em->persist($config);
$em->flush($config);
return $this->redirectToRoute('admin_mod_projectssites_config');
}
return $this->render('@TrinityProjectssites/backend/config/edit.html.twig', $this->attributes([
'config' => $config,
'form' => $Form->createView()
]));
}
/**
* @Route("/admin/realestate/maps", name="admin_mod_projectssites_real_estate_map")
* @Template()
*/
public function realEstateMap(Request $request){
// Initialize StorageController
parent::init($request);
$this->breadcrumbs->addRouteItem('Real Estate Bundle', 'admin_mod_projectssites');
$this->breadcrumbs->addRouteItem('Kavels mappen', 'admin_mod_projectssites_real_estate_map');
$realEstateMaps = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateMap')->findBy(['language' => $this->language, 'settings' => $this->Settings]);
return $this->render('@TrinityProjectssites/backend/realestatemap/index.html.twig', $this->attributes([
'realEstateMaps' => $realEstateMaps
]));
}
/**
* @Route("/admin/realestate/map/add/", name="admin_mod_realestate_map_add")
* @Route("/admin/realestate/map/edit/{id}", name="admin_mod_realestate_map_edit")
* @Template()
*/
public function editRealEstateMap(Request $request, $id = null){
// Initialize StorageController
parent::init($request);
$this->breadcrumbs->addRouteItem('Kavels', 'admin_mod_projectssites');
$this->breadcrumbs->addRouteItem('Kavels map Wijzigen', 'admin_mod_realestate_map_edit');
$em = $this->getDoctrine()->getManager();
$realEstateMap = new RealEstateMapEntity();
$realEstateMapClass = new RealEstateMap();
$realEstateMapClass->setLanguage($this->language)
->setSettings($this->Settings)
->setRequest($request)
->setLanguage($this->language)
->setSettings($this->Settings)
->setDoctrine($em);
$Form = $this->createFormBuilder($realEstateMap);
if(!empty($id)){
$realEstateMap = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateMap')->find($id);
if(empty($realEstateMap)){
return $this->redirectToRoute('admin_mod_projectssites_real_estate_map');
}
$Form = $this->createFormBuilder($realEstateMap);
if($realEstateMap->getSettings()->getId() != $this->Settings->getId()){
return $this->redirectToRoute('admin_mod_projectssites_real_estate_map');
}
}
$Form = $realEstateMapClass->setCurrentForm($Form)->getForm();
if(!empty($request)){
if ($Form->isSubmitted() && $Form->isValid()){
if ($realEstateMapClass->getMessageType() == 'success'){
if($_POST["action"] == "submit"){
return $this->redirectToRoute('admin_mod_realestate_map_edit',["id" => $realEstateMap->getId()]);
}else{
return $this->redirectToRoute('admin_mod_projectssites_real_estate_map');
}
}
$this->addFlash($realEstateMapClass->getMessageType(), $realEstateMapClass->getMessage());
}
}
return $this->render('@TrinityProjectssites/backend/realestatemap/edit.html.twig', $this->attributes([
'realEstateMap' => $realEstateMap,
'form' => $Form->createView(),
'maxFileSize' => 10,
'maxMediaFileSize' => $this->Settings->getMaxMediaSizeInKB()
]));
}
/**
* @Route("/admin/realestatemap/{id}/delete", name="admin_realestate_map_remove")
*/
public function deleteRealEstateMap(Request $request, $id = null){
parent::init($request);
$em = $this->getDoctrine()->getManager();
$realestate = $this->getDoctrine()->getRepository('TrinityProjectenBundle:RealEstateMap')->find($id);
$em->remove($realestate);
$em->flush();
return $this->redirectToRoute('admin_mod_projectssites_real_estate_map');
}
/**
* @Route("/ajax/get/address/", name="admin_mod_ajax_get_address")
*/
public function getAddressInfo(Request $request){
parent::init($request);
$addresssInfo = [];
if(isset($_POST['postcode']) && isset($_POST['housenumber'])){
$postcode = $_POST['postcode'];
$housenumber = $_POST['housenumber'];
$postcodeApi = new Postcode($this->Settings);
$addresssInfo = $postcodeApi->fetch($postcode, $housenumber);
}
return new JsonResponse($addresssInfo);
}
/**
* @Route("/ajax/get/konva/", name="admin_mod_ajax_get_konva")
*/
public function ajaxGetMapKonva(Request $request){
parent::init($request);
if(!empty($_POST["mapId"])){
$mapId = $_POST["mapId"];
$realEstateMap = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateMap')->find($mapId);
$convaHtml = $this->renderView('@TrinityProjectssites/frontend/realestate/konva.html.twig', $this->attributes([
'realestatemap' => $realEstateMap,
]));
return new JsonResponse(['status' => true,'realestatemap' => "/".$realEstateMap->getMapimage()->getWebPath(),'convaHtml'=> $convaHtml]);
}
return new JsonResponse(['status' => false]);
}
/**
* @Route("/admin/realestate/add/", name="admin_mod_realestate_add")
* @Route("/admin/realestate/edit/{id}", name="admin_mod_realestate_edit")
* @Template()
*/
public function editRealEstate(Request $request, $id = null){
// Initialize StorageController
parent::init($request);
$this->breadcrumbs->addRouteItem('Kavels', 'admin_mod_projectssites');
if(!empty($id)){
$this->breadcrumbs->addRouteItem('Kavel Wijzigen', 'admin_mod_realestate_edit');
}else{
$this->breadcrumbs->addRouteItem('Kavel Toevoegen', 'admin_mod_realestate_add');
}
$em = $this->getDoctrine()->getManager();
$realEstate = new RealEstateEntity();
$realEstateClass = new RealEstate();
$realEstateClass->setLanguage($this->language)
->setSettings($this->Settings)
->setId($id)
->setRequest($request)
->setLanguage($this->language)
->setSettings($this->Settings)
->setDoctrine($em);
$Form = $this->createFormBuilder($realEstate);
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
if(!empty($id)){
$realEstate = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstate')->find($id);
if(empty($realEstate)){
return $this->redirectToRoute('admin_mod_projectssites');
}
$Form = $this->createFormBuilder($realEstate);
if($realEstate->getSettings()->getId() != $this->Settings->getId()){
return $this->redirectToRoute('admin_mod_projectssites');
}
}
$Form = $realEstateClass->setCurrentForm($Form)->getForm();
if(!empty($request)){
if ($Form->isSubmitted() && $Form->isValid()){
if ($realEstateClass->getMessageType() == 'success'){
if($_POST["action"] == "submit"){
return $this->redirectToRoute('admin_mod_realestate_edit',["id" => $realEstate->getId()]);
}else{
return $this->redirectToRoute('admin_mod_projectssites');
}
}
$this->addFlash($realEstateClass->getMessageType(), $realEstateClass->getMessage());
}
}
$realEstateMap = null;
if(!empty($id)){
$realEstateMap = $realEstate->getRealEstateMap();
}
$realEstateOptionPrices = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionPrice')->findBy(['real_estate' => $id]);
return $this->render('@TrinityProjectssites/backend/realestate/edit.html.twig', $this->attributes([
'realestate' => $realEstate,
'realEstateMap' => $realEstateMap,
'form' => $Form->createView(),
'maxFileSize' => 10,
'maxMediaFileSize' => $this->Settings->getMaxMediaSizeInKB(),
'config' => $config,
'realEstateOptionPrices' => $realEstateOptionPrices,
]));
}
/**
* @Route("/admin/realestateoption/add", name="admin_mod_realestate_option_add")
* @Route("/admin/realestateoption/edit/{id}", name="admin_mod_realestate_option_edit")
* @Template()
*/
public function editRealEstateOption(Request $request,$id = null){
// Initialize StorageController
parent::init($request);
$this->breadcrumbs->addRouteItem('Kavels', 'admin_mod_projectssites');
$this->breadcrumbs->addRouteItem('Kavel optie Wijzigen', 'admin_mod_realestate_edit');
$em = $this->getDoctrine()->getManager();
$realEstateOption = new RealEstateOptionEntity();
$realEstateOptionContent = [];
$realEstateOptionDownload = [];
$realEstateAttribute = [];
$realEstateOptionClass = new RealEstateOption();
$realEstateOptionClass->setLanguage($this->language)
->setSettings($this->Settings)
->setRequest($request)
->setLanguage($this->language)
->setSettings($this->Settings)
->setDoctrine($em);
$Form = $this->createFormBuilder($realEstateOption);
if(!empty($id)){
$realEstateOption = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOption')->find($id);
if(empty($realEstateOption)){
return $this->redirectToRoute('admin_mod_realestateoptions');
}
$Form = $this->createFormBuilder($realEstateOption);
if($realEstateOption->getSettings()->getId() != $this->Settings->getId()){
return $this->redirectToRoute('admin_mod_realestateoptions');
}
$realEstateOptionContent = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionContent')->findBy(['real_estate_option' => $id]);
$realEstateOptionDownload = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionDownload')->findBy(['real_estate_option' => $id]);
$realEstateAttribute = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateAttribute')->findBy(['real_estate_option' => $id]);
}
$Form = $realEstateOptionClass->setCurrentForm($Form)->getForm();
if(!empty($request)){
if ($Form->isSubmitted() && $Form->isValid()){
if ($realEstateOptionClass->getMessageType() == 'success'){
if($_POST["action"] == "submit"){
return $this->redirectToRoute('admin_mod_realestate_option_edit',["id" => $realEstateOption->getId()]);
}else{
return $this->redirectToRoute('admin_mod_realestateoptions');
}
}
$this->addFlash($realEstateOptionClass->getMessageType(), $realEstateOptionClass->getMessage());
}
}
$realEstateOption = $realEstateOptionClass->setFotos($realEstateOption);
return $this->render('@TrinityProjectssites/backend/realestateoption/edit.html.twig', $this->attributes([
'realEstateOption' => $realEstateOption,
'realEstateOptionContent' => $realEstateOptionContent,
'realEstateOptionDownload' => $realEstateOptionDownload,
'realEstateAttribute' => $realEstateAttribute,
'form' => $Form->createView(),
'maxFileSize' => 10,
'maxMediaFileSize' => $this->Settings->getMaxMediaSizeInKB()
]));
}
/**
* Handle uploaded files
*
* @Route("/trinity/projects/upload/", name="trinity_mod_projects_upload")
*/
public function uploadAction(Request $request){
parent::init($request);
if (!empty($_FILES['file'])) {
$file = $_FILES['file'];
$uploadedfile = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], (int)$file['error']);
$em = $this->getDoctrine()->getManager();
$mediadir = $this->getDoctrine()->getRepository('CmsBundle:Mediadir')->findPathByName($em, (!empty($this->Settings->getHost()) ? $this->Settings->getHost() . '/' : '') .
$this->trans('Instructor', [], 'neutral'), $this->language);
$mediafile = new Media();
$mediafile->setParent($mediadir);
$mediafile->setLabel($file['name']);
$mediafile->setDateAdd();
$mediafile->setFile($uploadedfile);
$mediafile->preUpload();
$mediafile->upload();
$em->persist($mediafile);
$em->flush($mediafile);
$mediafile->webPath = $mediafile->getWebPath();
$mediafile->createWebP($this->Settings, $mediafile->getPath(), true);
return new JsonResponse([
'status' => true,
'mediaid' => $mediafile->getId(),
'webpath' => $mediafile->getWebpath(),
]);
}
if (!empty($_FILES['media'])) {
$file = $_FILES['media'];
$uploadedfile = new UploadedFile($file['tmp_name'][0], $file['name'][0], $file['type'][0], (int)$file['error'][0]);
$em = $this->getDoctrine()->getManager();
$mediadir = $this->getDoctrine()->getRepository('CmsBundle:Mediadir')->findPathByName($em, (!empty($this->Settings->getHost()) ? $this->Settings->getHost() . '/' : '') .
$this->trans('Instructor', [], 'neutral'), $this->language);
$mediafile = new Media();
$mediafile->setParent($mediadir);
$mediafile->setLabel($file['name'][0]);
$mediafile->setDateAdd();
$mediafile->setFile($uploadedfile);
$mediafile->preUpload();
$mediafile->upload();
$em->persist($mediafile);
$em->flush($mediafile);
$mediafile->webPath = $mediafile->getWebPath();
$mediafile->createWebP($this->Settings, $mediafile->getPath(), true);
return new JsonResponse([
'status' => true,
'mediaid' => $mediafile->getId(),
'webpath' => $mediafile->getWebpath(),
]);
}
return new JsonResponse([
'status' => false,
'_FILES' => (!empty($_FILES) ? $_FILES : []),
'_POST' => (!empty($_POST) ? $_POST : []),
'error' => 'No file given',
]);
}
/**
* @Route("/admin/realestate/{id}/delete", name="admin_realestate_remove")
*/
public function deleteRealEstate(Request $request, $id = null){
parent::init($request);
$em = $this->getDoctrine()->getManager();
$realEstate = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstate')->find($id);
$em->remove($realEstate);
$em->flush();
return $this->redirectToRoute('admin_mod_projectssites');
}
/**
* @Route("/admin/realestateoption/{id}/delete", name="admin_realestate_option_remove")
*/
public function deleteRealEstateOption(Request $request, $id = null){
parent::init($request);
$em = $this->getDoctrine()->getManager();
$realEstateOption = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOption')->find($id);
$em->remove($realEstateOption);
$em->flush();
return $this->redirectToRoute('admin_mod_realestateoptions');
}
/**
* @Route("/admin/realestateoptioncontent/{optionId}/{id}/delete", name="admin_mod_realestatecontent_delete")
*/
public function deleteRealEstateOptionContent(Request $request, $realEstateId, $optionId, $id = null){
parent::init($request);
$em = $this->getDoctrine()->getManager();
$realestateOptionContent = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionContent')->find($id);
$em->remove($realestateOptionContent);
$em->flush();
return $this->redirectToRoute('admin_mod_realestate_option_edit',["id" => $optionId]);
}
/**
* @Route("/admin/realestateoption/deletemedia/{optionId}/{mediaid}",
* name="admin_mod_realestateoption_media_delete",
* requirements = {
* "id" = "\d+",
* "mediaid" = "\d+"
* }
* )
*/
public function deleteMediaAction(Request $request, $optionId = null, $mediaid = null){
$em = $this->getDoctrine()->getManager();
$realEstateOption = $em->getRepository('TrinityProjectssitesBundle:RealEstateOption')->find($optionId);
if(!empty($realEstateOption)){
if(!empty($realEstateOption->getHeader()) && $realEstateOption->getHeader()->getId() == $mediaid){
$realEstateOption->setHeader(null);
}elseif(!empty($realEstateOption->getThumbnail()) && $realEstateOption->getThumbnail()->getId() == $mediaid){
$realEstateOption->setThumbnail(null);
}else{
$media = $em->getRepository('CmsBundle:Media')->find($mediaid);
$ids = $realEstateOption->removeMediaId($media);
$realEstateOption->emptyFotoGallery();
foreach ($ids as $id) {
$realEstateOption->addFotoGalleryItem($em->getRepository('CmsBundle:Media')->find($id));
}
$realEstateOption->setFotoGallery($realEstateOption->getFotoGalleryCollection());
}
$em->persist($realEstateOption);
$em->flush();
}
return $this->redirectToRoute('admin_mod_realestate_option_edit', ['id' => $optionId]);
}
/**
* @Route("/admin/realestatemap/deletemedia/{mapId}/{mediaId}",
* name="admin_mod_realestate_map_media_delete",
* requirements = {
* "id" = "\d+",
* "mediaid" = "\d+"
* }
* )
*/
public function deleteMediaRealEstateMapAction(Request $request, $mapId = null, $mediaId = null){
$em = $this->getDoctrine()->getManager();
$realEstateMap = $em->getRepository('TrinityProjectssitesBundle:RealEstateMap')->find($mapId);
if(!empty($realEstateMap) && !empty($mediaId)){
if(!empty($realEstateMap->getMapImage()) && $realEstateMap->getMapImage()->getId() == $mediaId){
$realEstateMap->setMapImage(null);
//$media = $em->getRepository('CmsBundle:Media')->find($mediaId);
//$realEstateMap->removeMediaId($media);
}else{
$media = $em->getRepository('CmsBundle:Media')->find($mediaId);
$em->remove($media);
}
$em->persist($realEstateMap);
$em->flush();
}
return $this->redirectToRoute('admin_mod_realestate_map_edit', ["id"=> $mapId]);
}
/**
* @Route("/admin/realestateoptiondownload/{optionId}/{id}/delete", name="admin_mod_realestatedownload_delete")
*/
public function deleteMediaRealEstateDownload(Request $request, $optionId, $id = null){
parent::init($request);
$em = $this->getDoctrine()->getManager();
$realestateOptionDownload = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionDownload')->find($id);
$em->remove($realestateOptionDownload);
$em->flush();
return $this->redirectToRoute('admin_mod_realestate_option_edit',["id" => $optionId]);
}
/**
* @Route("/admin/realestatecontent/deletemedia/{id}/{mediaid}",
* name="admin_mod_realestatecontent_media_delete",
* requirements = {
* "id" = "\d+",
* "mediaid" = "\d+"
* }
* )
*/
public function deleteMediaRealEstateContentAction(Request $request, $id = null, $mediaid = null){
$em = $this->getDoctrine()->getManager();
$realEstateContent = $em->getRepository('TrinityProjectssitesBundle:RealEstateOptionContent')->find($id);
if(!empty($realEstateContent)){
if(!empty($realEstateContent->getImage()) && $realEstateContent->getImage()->getId() == $mediaid){
$realEstateContent->setImage(null);
$media = $em->getRepository('CmsBundle:Media')->find($mediaid);
$em->remove($media);
}
$em->persist($realEstateContent);
$em->flush();
}
return $this->redirectToRoute('admin_mod_realestate_edit', ['id' => $id]);
}
/**
* @Route("/admin/realestateattribute/{optionId}/{id}/delete", name="admin_mod_realestateattribute_delete")
*/
public function deleteRealEstateAttribute(Request $request, $optionId, $id = null){
parent::init($request);
$em = $this->getDoctrine()->getManager();
$realestateAttribute = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateAttribute')->find($id);
$em->remove($realestateAttribute);
$em->flush();
return $this->redirectToRoute('admin_mod_realestate_option_edit',["id" => $optionId]);
}
private function getPriceRange($priceFrom = null, $priceTo = null){
$priceRange = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOption')
->getPriceRange($this->language,$this->Settings);
if(!empty($priceRange)){
$prices = $priceRange[0];
$priceRange = [0,number_format($prices['max_price'],0,',','.')];
}
if(!empty($priceFrom)){
$priceRange = [number_format(floor($priceFrom),0,',','.'), number_format(ceil($prices['max_price']),0,',','.')];
}elseif(!empty($priceTo)){
$priceRange = [0, number_format(ceil($priceTo),0,',','.')];
}
return $priceRange;
}
/**
* Show bundle content to front
*
* @param array $config Array with configuration options
* @param array $params Additional parameters
*
* @return string HTML
*/
public function showAction($config, $params = [], $request){
parent::init($request);
if(isset($config["realestatesmapswidget"]) && $config["realestatesmapswidget"] == "on"){
return $this->widgetMaps($request);
}
if(isset($config["projectssitegooglemapswidget"]) && $config["projectssitegooglemapswidget"] == "on"){
return $this->widgetOverview($request);
}
if(isset($config["projectwidget"]) && $config["projectwidget"] == "on"){
return $this->widgetDetail($request, $params);
}
if(isset($config["realestateswidget"]) && $config["realestateswidget"] == "on"){
return $this->widgetRealEstates($request, $params);
}
if(isset($config["realestateOptionswidget"]) && $config["realestateOptionswidget"] == "on"){
return $this->widgetRealEstateOptions($request, $params);
}
}
private function widgetOverview($request){
$realEstateEntity = new RealEstateEntity();
$realEstateClass = new RealEstate();
$realEstateOptionClass = new RealEstateOption();
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
$response = $this->realEstateFilter(null, null, null, null, null, 30, 1);
$realEstates = $response['realestates'];
$realEstateMap = null;
$realEstateOptionPrices = [];
$index = 0;
foreach ($realEstates as $realEstate) {
$realEstate = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstate')->findOneBy(['id' => $realEstate->getId()]);
$realestateOptions = $realEstate->getRealEstateOptions();
$realEstateMap = $realEstate->getRealEstateMap();
if($config->getIsSingleProject()){
$realEstate->category_txt = $realEstate->getCategories(true)[$realEstate->getCategory()];
$response['realestate_hydrated'][$index]['category_txt'] = $realEstate->category_txt;
$realEstate->status_txt = $realEstate->getStatusses(true)[$realEstate->getStatus()];
$response['realestate_hydrated'][$index]['status_txt'] = $realEstate->status_txt;
}
if(!empty($realestateOptions)){
$realEstate->options = $realestateOptions;
foreach($realestateOptions as $realestateOption){
$realestateOption = $realEstateOptionClass->setDoctrine($this->getDoctrine())->setFotos($realestateOption);
$realEstateOptionPrice = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionPrice')->findOneBy([
'real_estate' => $realEstate->getId(), 'real_estate_option' => $realestateOption->getId()]);
if(!empty($realEstateOptionPrice)){
$realEstateOptionPrices[$realEstate->getId()][$realestateOption->getId()] = $realEstateOptionPrice->getCost();
//dump($realEstate->getId()." ".$realestateOption->getId()." ".$realEstateOptionPrice->getCost());
//dump($realEstates);
}
}
if(!empty($realestateOptions[0])){
$response['realestate_hydrated'][$index]['slug'] = $realestateOptions[0]->getSlug();
$realEstate->foto = $realestateOptions[0]->getThumbnail();
if(!empty($realestateOptions[0]->getThumbnail())){
$response['realestate_hydrated'][$index]['foto'] = $realestateOptions[0]->getThumbnail()->getWebPath();
}
}
//$images = $realestateOptions[0]->getFotoGalleryCollection();
//if(!empty($images)){
//$realEstate->foto = $images[0];
//}
}
$index++;
}
//die();
$priceRange = $this->getPriceRange();
$latitude = 0;
$longitude = 0;
$coords = $this->getCoords("Nederland");
$latitude = $coords['lat'];
$longitude = $coords['long'];
$bundlePath = $this->container->get('kernel')->locateResource('@TrinityProjectssitesBundle');
$projectPath = $this->get('kernel')->getProjectDir();
$marker = '/bundles/trinityprojectssites/img/marker.png';
if(file_exists($projectPath."/public/img/placeholder.jpg")){
$marker = "/img/marker.png";
}
$vars = [
'count' => $response['totalCount'],
'realestate_hydrated' => $response['realestate_hydrated'],
'realestates' => $realEstates,
'marker' => $marker,
'realEstateEntity' => $realEstateEntity,
'config' => $config,
'priceRange' => $priceRange,
'search_lat' => $latitude,
'search_long' => $longitude,
'googleMapsZoomLvl' => $config->getGoogleMapsZoom(),
'realEstateOptionPrices' => $realEstateOptionPrices,
];
$vars['realestatemap'] = $realEstateMap;
return $this->renderView('@TrinityProjectssites/frontend/realestate/widget_overview.html.twig', $vars);
}
private function widgetMaps($request){
$realEstateEntity = new RealEstateEntity();
$realEstateClass = new RealEstate();
$realEstateOptionClass = new RealEstateOption();
$response = $this->realEstateFilter(null, null, null, null, null, 30, 1);
$realEstates = $response['realestates'];
$index = 0;
foreach ($realEstates as $realEstate) {
$realestateOptions = $realEstate->getRealEstateOptions();
if(!empty($realestateOptions)){
$realEstate->options = $realestateOptions;
foreach($realestateOptions as $realestateOption){
$realestateOption = $realEstateOptionClass->setDoctrine($this->getDoctrine())->setFotos($realestateOption);
}
if(!empty($realestateOptions[0])){
$response['realestate_hydrated'][$index]['slug'] = $realestateOptions[0]->getSlug();
$realEstate->foto = $realestateOptions[0]->getThumbnail();
if(!empty($realestateOptions[0]->getThumbnail())){
$response['realestate_hydrated'][$index]['foto'] = $realestateOptions[0]->getThumbnail()->getWebPath();
}
}
//$images = $realestateOptions[0]->getFotoGalleryCollection();
//if(!empty($images)){
//$realEstate->foto = $images[0];
//}
}
$index++;
}
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
$priceRange = $this->getPriceRange();
$latitude = 0;
$longitude = 0;
$coords = $this->getCoords("Nederland");
$latitude = $coords['lat'];
$longitude = $coords['long'];
$bundlePath = $this->container->get('kernel')->locateResource('@TrinityProjectssitesBundle');
$projectPath = $this->get('kernel')->getProjectDir();
$marker = '/bundles/trinityprojectssites/img/marker.png';
if(file_exists($projectPath."/public/img/marker.png")){
$marker = "/img/marker.png";
}
$vars = [
'count' => $response['totalCount'],
'realestate_hydrated' => $response['realestate_hydrated'],
'realestates' => $realEstates,
'realEstateEntity' => $realEstateEntity,
'config' => $config,
'marker' => $marker,
'googleMapsZoomLvl' => $config->getGoogleMapsZoom(),
'priceRange' => $priceRange,
'search_lat' => $latitude,
'search_long' => $longitude,
];
//if($config->getIsSingleProject()){
$realestatemap = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateMap')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
$vars['realestatemap'] = $realestatemap;
//}
return $this->renderView('@TrinityProjectssites/frontend/realestate/widget_maps.html.twig', $vars);
}
function distance($lat1, $lon1, $lat2, $lon2){
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return ($miles * 1.609344);
}
public function realEstateFilter($place, $province, $priceStart, $priceEnd, $status, $maxDistance, $page = null){
$latitude = 0;
$longitude = 0;
$errorMsg = '';
if ($place !== "" && strlen($place) > 1) {
$coords = $this->getCoords($place);
$latitude = $coords['lat'];
$longitude = $coords['long'];
}
$repo = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstate');
$realEstateArray = $repo->search($place, $province, $priceStart, $priceEnd, $status, null,$this->language,$this->Settings);
$realestates = $realEstateArray['realestates'];
$realestatemap = null;
$maxDistance = (int)$maxDistance;
$counter = 0;
foreach ($realestates as $realestate) {
$realestatemap = $realestate->getRealEstateMap();
$realestate = $repo->findOneBy(["id" => $realestate->getId()]);
if ($maxDistance != "0" && $latitude !== 0) {
$calculatedDistance = $this->distance($latitude, $longitude, $realestate->getLatitude(), $realestate->getLongitude());
if($calculatedDistance > $maxDistance){
unset($realestates[array_search($realestate, $realestates)]);
unset($realEstateArray['realestate_hydrated'][$counter]);
}
}
$counter++;
}
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
$vars = [
'realestates' => $realestates,
'config' => $config,
'realestate_hydrated' => $realEstateArray['realestate_hydrated'],
];
$konvaHtml = $this->renderView('@TrinityProjectssites/frontend/realestate/konva.html.twig', $vars);
$vars = [
'realestatemap' => $realestatemap,
'konva' => $konvaHtml,
'realestates' => $realestates,
'realestate_hydrated' => $realEstateArray['realestate_hydrated'],
'errorMsg' => $errorMsg,
'totalCount' => count($realestates),
'search_lat' => $latitude,
'search_long' => $longitude
];
return $vars;
}
/**
* @Route("/ajax/realestate/search/{page}", name="admin_mod_ajax_realestate")
*/
public function searchAction(Request $request, $page = 1){
parent::init($request);
$realEstateEntity = new RealEstateEntity();
$realEstateClass = new RealEstate();
$realEstateOptionClass = new RealEstateOption();
$place = $_POST['place'];
$province = $_POST['province'];
$priceFrom = $_POST['price_from'];
$priceTo = $_POST['price_to'];
$dist = $_POST['max_dist'];
$status = $_POST['status'];
$priceRange = $this->getPriceRange($priceFrom, $priceTo);
$response = $this->realEstateFilter($place, $province, $priceFrom, $priceTo, $status, $dist, $page);
$realestates = $response['realestates'];
$index = 0;
foreach ($realestates as $realEstate) {
$realestateOptions = $realEstate->getRealEstateOptions();
if(!empty($realestateOptions)){
$realEstate->options = $realestateOptions;
foreach($realestateOptions as $realestateOption){
$realestateOption = $realEstateOptionClass->setDoctrine($this->getDoctrine())->setFotos($realestateOption);
}
$realEstate->foto = $realestateOptions[0]->getThumbnail();
$response['realestate_hydrated'][$index]['foto'] = $realestateOptions[0]->getThumbnail()->getWebPath();
//$images = $realestateOptions[0]->getFotoGalleryCollection();
//if(!empty($images)){
//$realEstate->foto = $images[0];
//}
}
$index++;
}
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
$html = $this->renderView('@TrinityProjectssites/frontend/realestate/realestates.html.twig', [
'realestates' => $realestates,
'config' => $config,
]);
$vars = [
'konva' => $response['konva'],
'html' => $html,
'realestates' => $response['realestate_hydrated'],
'error' => $response['errorMsg'],
'count' => $response['totalCount'],
'search_lat' => $response['search_lat'],
'search_long' => $response['search_long'],
'priceRange' => $priceRange,
];
//if($config->getIsSingleProject()){
$realestatemap = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateMap')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
$vars['realestatemap'] = $realestatemap;
//}
return new JsonResponse($vars);
}
function getCoords($placename = null){
$coords = [];
$uri = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $placename . ',NL&key=AIzaSyC042fMu6ZYNvJfBtKOnhEvlPpAs5XNy18';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $uri);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, 1);
if(!empty($response['results'])){
$coords['lat'] = $response['results'][0]['geometry']['location']['lat'];
$coords['long'] = $response['results'][0]['geometry']['location']['lng'];
} else {
$coords['lat'] = 0;
$coords['long'] = 0;
}
return $coords;
}
/**
* @Route("/kavel/{slug}", name="admin_mod_realestate_detail")
*/
private function widgetDetail(Request $request, $slug = null){
$realEstateOptionEntity = new RealEstateOptionEntity();
$realEstateEntity = new RealEstateEntity();
$realEstateOptionClass = new RealEstateOption();
$realEstateOption = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOption')->findOneBy([
'language'=> $this->language,
'settings'=> $this->Settings,
'slug' => $slug]);
if(empty($realEstateOption)){
return $this->widgetOverview($request);
}
$realEstateOption = $realEstateOptionClass->setDoctrine($this->getDoctrine())->setFotos($realEstateOption);
$route = $request->attributes->get('_route');
$parentPage = $this->getDoctrine()->getRepository('CmsBundle:Page')->findOneBy(['slugkey' => $route]);
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
$realEstateOptionContent = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionContent')->findBy(['real_estate_option' => $realEstateOption->getId()]);
$realEstateOptionDownload = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionDownload')->findBy(['real_estate_option' => $realEstateOption->getId()]);
$realEstateAttributes = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateAttribute')->findBy(['real_estate_option' => $realEstateOption->getId()]);
$vars = [
'realEstateAttributes' => $realEstateAttributes,
'realEstateOption' => $realEstateOption,
'realEstateOptionEntity' => $realEstateOptionEntity,
'realEstateEntity' => $realEstateEntity,
'parentPage' => $parentPage,
'config' => $config,
'realEstateOptionContent' => $realEstateOptionContent,
'realEstateOptionDownload' => $realEstateOptionDownload,
];
//if($config->getIsSingleProject()){
$realEstatesHydrated = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstate')->hydratedFindBy([
'language'=> $this->language,
'settings'=> $this->Settings]);
$realEstates = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstate')->findBy([
'language'=> $this->language,
'settings'=> $this->Settings]);
$realEstatesCarousel = [];
foreach ($realEstates as $realEstate) {
$realEstateOptions = $realEstate->getRealEstateOptions();
if(!empty($realEstateOptions)){
$realEstate->options = $realEstateOptions;
$tmpOptions = [];
$tmpRealEstate = $realEstate;
foreach($realEstateOptions as $tmpRealEstateOption){
$tmpRealEstateOption = $realEstateOptionClass->setDoctrine($this->getDoctrine())->setFotos($tmpRealEstateOption);
if($tmpRealEstateOption->getId() != $realEstateOption->getId()){
$tmpOptions[] = $tmpRealEstateOption;
}
}
if(count($tmpOptions) > 0){
$tmpRealEstate->options = $tmpOptions;
$realEstatesCarousel[] = $tmpRealEstate;
}
if(!empty($realEstateOptions[0])){
$images = $realEstateOptions[0]->getFotoGalleryCollection();
if(!empty($images)){
//$realEstate->foto = $images[0];
$realEstate->foto = $realEstateOptions[0]->getThumbnail();
}
}
}
}
$vars['realEstatesCarousel'] = $realEstatesCarousel;
$vars['realestate_hydrated'] = $realEstatesHydrated;
$vars['realEstates'] = $realEstates;
return $this->renderView('@TrinityProjectssites/frontend/realestate/detail.html.twig', $vars);
}
/**
* @Route("/kavel/{slug}", name="admin_mod_realestate_detail")
*/
private function widgetRealEstates(Request $request, $slug = null){
$realEstateOptionClass = new RealEstateOption();
$realEstateOptionEntity = new RealEstateOptionEntity();
$realEstates = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstate')->findBy([
'language'=> $this->language,
'settings'=> $this->Settings]);
$route = $request->attributes->get('_route');
$parentPage = $this->getDoctrine()->getRepository('CmsBundle:Page')->findOneBy(['slugkey' => $route]);
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
$vars = [
'parentPage' => $parentPage,
'config' => $config,
'realEstateOption' => $realEstateOptionEntity,
];
//if($config->getIsSingleProject()){
foreach ($realEstates as $realEstate) {
$realestateOptions = $realEstate->getRealEstateOptions();
if(!empty($realestateOptions)){
$realEstate->options = $realestateOptions;
foreach($realestateOptions as $realestateOption){
$realestateOption = $realEstateOptionClass->setDoctrine($this->getDoctrine())->setFotos($realestateOption);
//$realestateOptionPrice = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionPrice')->get();
$realEstateOptionPrice = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOptionPrice')->findOneBy([
'real_estate' => $realEstate->getId(), 'real_estate_option' => $realestateOption->getId()]);
if(!empty($realEstateOptionPrice)){
$realestateOption->price = $realEstateOptionPrice->getCost();
}
}
$images = $realestateOptions[0]->getFotoGalleryCollection();
if(!empty($images)){
//$realEstate->foto = $images[0];
$realEstate->foto = $realestateOptions[0]->getThumbnail();
}
}
}
$vars['realEstates'] = $realEstates;
//}
return $this->renderView('@TrinityProjectssites/frontend/realestate/widget_realestates.html.twig', $vars);
}
private function widgetRealEstateOptions(Request $request, $slug = null){
$realEstateOptionClass = new RealEstateOption();
$realEstateOptionEntity = new RealEstateOptionEntity();
$realEstateOptions = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:RealEstateOption')->findBy([
'language'=> $this->language,
'settings'=> $this->Settings]);
$route = $request->attributes->get('_route');
$parentPage = $this->getDoctrine()->getRepository('CmsBundle:Page')->findOneBy(['slugkey' => $route]);
$config = $this->getDoctrine()->getRepository('TrinityProjectssitesBundle:Config')->findOneBy(['language' => $this->language, 'settings' => $this->Settings]);
$vars = [
'parentPage' => $parentPage,
'config' => $config,
'realEstateOptionEntity' => $realEstateOptionEntity,
];
//if($config->getIsSingleProject()){
$vars['realEstateOptions'] = $realEstateOptions;
//}
return $this->renderView('@TrinityProjectssites/frontend/realestate/widget_realestate_options.html.twig', $vars);
}
}