<?php
namespace Notaris\FrontendBundle\Controller;
use Notaris\AdminBundle\Entity\Module;
use Notaris\AdminBundle\Entity\NotaryInfo;
use Notaris\AdminBundle\Entity\SvenBannerGalleryRepository;
use Notaris\AdminBundle\NotarisAdminBundle;
use Pdp\Cache;
use Pdp\CurlHttpClient;
use Pdp\Parser;
use Pdp\Manager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class DefaultController extends AbstractController
{
public function indexAction(Request $request, $slug = null): Response
{
$localelistener = $this->get('notaris.locale.listener');
$notary = $localelistener->getNotary();
$isMultiLanguage = $localelistener->getIsMulti();
$host = $localelistener->getHost();
if($notary == false):
//default home page
$manager = $this->getDoctrine()->getManager();
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
// file_get_contents('https://immo.notaris.be/immoplatform-public-service_v1/api/authentication/guest');
//
// $cookies = array();
// foreach ($http_response_header as $hdr) {
// if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {
// $cookies []= $matches[1];
// }
// }
//
// // Create a stream
// $opts = [
// "http" => [
// "method" => "GET",
// "header" => "Cookie: " . implode("; ", $cookies)
// ]
// ];
//
// $context = stream_context_create($opts);
//
// $immoRequest = file_get_contents('https://immo.notaris.be/immoplatform-public-service_v1/api/properties/reference/' . '206890', false, $context);
// $immoResponse = json_decode($immoRequest);
// var_dump($immoResponse->response->id);exit;
//get home banners
$banners_repo = $manager->getRepository('Notaris\AdminBundle\Entity\HomeBanner');
$banners = $banners_repo->findActiveByLanguage($locale);
$news_repo = $manager->getRepository('Notaris\AdminBundle\Entity\News');
$news = $news_repo->getAllHomepageNews($locale);
//FEDNOTLINKS ophalen
$columntypes = array('home1', 'home2', 'home3');
/** @var $repo_svenlinks \Notaris\AdminBundle\Entity\ModuleLinksRepository */
$repo_svenlinks = $manager->getRepository('Notaris\AdminBundle\Entity\ModuleLinks');
$fednotmodule = array();
foreach ($columntypes as $columntype):
$left = $repo_svenlinks->getModuleColumn($columntype, 'L', $locale);
$center = $repo_svenlinks->getModuleColumn($columntype, 'C', $locale);
$right = $repo_svenlinks->getModuleColumn($columntype, 'R', $locale);
$fednotmodule[$columntype] = array('left'=>$left,'center'=>$center,'right'=>$right);
endforeach;
//NOTARIS-TV
$video_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Video');
$recent_videos = $video_repo->getRecentVideos($locale,1);
//THEMES
/** @var $subsite_repo \Notaris\AdminBundle\Entity\SubsiteRepository */
$subsite_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Subsite');
$themes = $subsite_repo->getThemesInfo($locale);
//JOBS
$jobs_repo = $manager->getRepository('Notaris\AdminBundle\Entity\NotaryJobGeneral');
$jobs = $jobs_repo->findByLocale($locale);
$totaljobs = false;
if(isset($jobs[0])){
$totaljobs = $jobs[0]->getTotal();
}
//IMMO
/* @var $general_repo /Notaris\AdminBundle\Entity\General */
$general_repo = $manager->getRepository('Notaris\AdminBundle\Entity\General');
$totalImmoItem = $general_repo->findOneBy(['item' => 'totalImmos']);
$totalImmos = $totalImmoItem->getValue();
//random immo image ophalen
/* @var $immo_repo /Notaris\AdminBundle\Entity\Immo */
$immo_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Immo');
$immo_image = $immo_repo->randomImage();
$totalimmo = ($totalImmos) ? $totalImmos : 0;
return $this->render('@NotarisFrontend/default/index.html.twig', [
'banners'=>$banners,
'news' => $news,
'fednotmodule'=>$fednotmodule,
'videos'=>$recent_videos,
'themes'=>$themes,
'totalJobs'=>$totaljobs,
'jobs'=>$jobs,
'totalImmo' => $totalimmo,
'immoImage'=> $immo_image
]);
else:
//INFO: vroeger moest er taalkeuze zijn als er geen taalcookie was, nu toont hij de moedertaal van de sven
// $langCookie = "NotarySven_Lang_".$notary->getId();
// if((isset($_COOKIE[$langCookie]) AND $_COOKIE[$langCookie] == $notary->getLocale()) OR $localelistener->getSvenChoosenLang() != ''):
// $isMultiLanguage = false;
// endif;
// if($isMultiLanguage == false):
return $this->svenAction($request, $host, $slug);
// else:
// return $this->redirect($this->generateUrl('notaris_frontend_sven_choose_language'));
// endif;
endif;
foreach ($addresses as $address) {
if($address->getSlug() == $currentslug){
$office_id = $address->getEe();
if(!$address->getEe()){
$office_id = $address->getSlug();
}
if(!$address->getSkedifyId()){
$address = $skedifyService->getOfficesByExternalId($office_id, $address);
}
if($address and $address->getSkedifyId()) {
$showSkedify = $skedifyService->getSpecificOffice($address->getSkedifyId());
}else{
$showSkedify = false;
}
}
}
}
public function downloadAction($dd,$filename)
{
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
if($locale == 'de'):
$locale = 'fr';
endif;
$manager = $this->getDoctrine()->getManager();
$attachment_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Attachment');
$filename = urldecode($filename);
$dd = urldecode($dd);
$attachment = $attachment_repo->findOneByFilename('/'.$dd .'/'. $filename);
$file = $this->get('kernel')->getRootDir() . "/../web/uploads/$locale" . $attachment->getFilename() ;
$response = new BinaryFileResponse($file);
BinaryFileResponse::trustXSendfileTypeHeader();
$response->headers->set('Content-type', $attachment->getFileType());
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $attachment->getOriginalFilename() );
return $response;
}
public function clearAction(){
opcache_reset();
echo "opcache reset";
}
public function svenAction(Request $request, $host, $slug)
{
$localelistener = $this->get('notaris.locale.listener');
$locale = $localelistener->getLocale();
$address_info = false;
$manager = $this->getDoctrine()->getManager();
/** @var $svenAddress_repo \Notaris\AdminBundle\Entity\SvenAddressInfoRepository */
$svenAddress_repo = $manager->getRepository('Notaris\AdminBundle\Entity\SvenAddressInfo');
/** @var $address_info \Notaris\AdminBundle\Entity\SvenAddressInfo */
/** @var $notary \Notaris\AdminBundle\Entity\Notary */
$notary = $localelistener->getNotary();
if($notary):
//keep chosen address in session
$session = $request->getSession();
if(sizeof($notary->getAddresses()) > 1){
if($slug){
/** @var $notaryInfo_repo \Notaris\AdminBundle\Entity\NotaryInfoRepository */
$notaryInfo_repo = $manager->getRepository('Notaris\AdminBundle\Entity\NotaryInfo');
$address_info = $svenAddress_repo->getAddressFromNotary($notary, $slug, $localelistener->getLocale());
$info = $notaryInfo_repo->getInfoFromNotaryAddress($localelistener->getLocale(),$address_info->getAddress());
if(!$info){
//when there is no info on address, added it her, only first time
$info = new NotaryInfo();
$info->setIsActive(true);
$info->setLocale($localelistener->getLocale());
$info->setAddress($address_info->getAddress());
$manager->persist($info);
$manager->flush();
}
$session->set('sven_address_slug', $address_info->getAddress()->getSlug());
}
if(!$address_info){
//msk
$sven_color_repo = $manager->getRepository('Notaris\AdminBundle\Entity\SvenColor');
$default_color = $sven_color_repo->findDefault();
return $this->render('@NotarisFrontend/sven/msk.html.twig',
array(
'isKantoor'=>false,
'showMskSplash'=> true,
'notary'=>$notary,
'defaultColor'=>$default_color
)
);
}
}else{
$session->remove('sven_address_slug');
/** @var $info \Notaris\AdminBundle\Entity\NotaryInfo */
$info = $localelistener->getNotaryInfo();
$address_info = $svenAddress_repo->getAddressFromNotary($notary, $info->getAddress()->getSlug(), $localelistener->getLocale());
}
else:
return $this->redirect($this->container->getParameter('url_'. $locale));
endif;
if(!$info){
//redirct to default site
return $this->redirect($this->container->getParameter('url_'. $locale));
}
//service
$svenService = $this->get('notaris.sven.service');
$svenPage = $svenService->getSvenPage($notary, $info, $address_info);
/** @var $svenBannerGalleryRepository \Notaris\AdminBundle\Entity\SvenBannerGallery */
$svenBannerGalleryRepository = $manager->getRepository('Notaris\AdminBundle\Entity\SvenBannerGallery');
$backgroundImageGallery = $svenBannerGalleryRepository->findOneBy([
'name' => 'Start-screen images',
'locale' => $notary->getLocale(),
]);
//period for notary people
/** @var $notabasePeriodRepository \Notaris\AdminBundle\Entity\NotabasePeriodRepository */
$notabasePeriodRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabasePeriod');
$periods = $notabasePeriodRepository->createQueryBuilder('p')
->select('p')
->groupBy('p.personNumber, p.replacedNumber')
->where('p.officeNumber = :officeNumber')
->andWhere('p.active = 1')
->andWhere('p.endDate IS NULL')
->setParameter('officeNumber', $notary->getEe())
->getQuery()
->getResult();
return $this->render('@NotarisFrontend/sven/homepage.html.twig', [
'isKantoor' => true,
'notary' => $notary,
'address_info' => $address_info,
'banners' => $svenPage['banners'],
'groupinfos' => $svenPage['groupinfos'],
'build' => $svenPage['build'],
'backgroundImageGallery' => $backgroundImageGallery->getAssets(),
'periods' => $periods,
'socialMedia' => $svenPage['socialMedia'],
'navigation' => $svenPage['navigation']
]);
}
public function skedifyAction(Request $request, $addressslug = null){
$office_id = false;
$notary = false;
$showSkedify = true;
$manager = $this->getDoctrine()->getManager();
$skedifyService = $this->get('skedify_service');
$address_info = null;
/** @var $address_info \Notaris\AdminBundle\Entity\SvenAddressInfo */
$svenAddressInfo_repo = $manager->getRepository('Notaris\AdminBundle\Entity\SvenAddressInfo');
if($addressslug){
//via notaris.be with slug
/** @var $svenAddress_repo \Notaris\AdminBundle\Entity\SvenAddressRepository */
$svenAddress_repo = $manager->getRepository('Notaris\AdminBundle\Entity\SvenAddress');
/** @var $address \Notaris\AdminBundle\Entity\SvenAddress */
$address = $svenAddress_repo->findOneBySlug($addressslug);
if($address){
$notary = $address->getNotary();
$office_id = $address->getEe();
if(!$office_id){
$office_id = $address->getSlug();
}
// if(!$address->getSkedifyId()){
// $address = $skedifyService->getOfficesByExternalId($office_id, $address);
// }
// if($address and $address->getSkedifyId()) {
// $showSkedify = $skedifyService->getSpecificOffice($address->getSkedifyId());
// }else{
// $showSkedify = false;
// }
// $showSkedify = $address->getIsSkedifyActive();
}
}else{
//via sven domain
$localelistener = $this->get('notaris.locale.listener');
$notary = $localelistener->getNotary();
if($notary){
$session = $request->getSession();
$currentslug = $session->get('sven_address_slug');
$addresses = $notary->getAddresses();
if(sizeof($addresses) > 1){
if($currentslug){
/** @var $notaryInfo_repo \Notaris\AdminBundle\Entity\NotaryInfoRepository */
$notaryInfo_repo = $manager->getRepository('Notaris\AdminBundle\Entity\NotaryInfo');
$address_info = $svenAddressInfo_repo->getAddressFromNotary($notary, $currentslug, $localelistener->getLocale());
}
foreach ($addresses as $address) {
if($address->getSlug() == $currentslug){
$office_id = $address->getEe();
if(!$address->getEe()){
$office_id = $address->getSlug();
}
// if(!$address->getSkedifyId()){
// $address = $skedifyService->getOfficesByExternalId($office_id, $address);
// }
//
// if($address and $address->getSkedifyId()) {
// $showSkedify = $skedifyService->getSpecificOffice($address->getSkedifyId());
// }else{
// $showSkedify = false;
// }
// $showSkedify = $address->getIsSkedifyActive();
}
}
}else{
$info = $localelistener->getNotaryInfo();
$address = $info->getAddress();
$office_id = $address->getEe();
/** @var $svenAddress_repo \Notaris\AdminBundle\Entity\SvenAddressInfoRepository */
$svenAddress_repo = $manager->getRepository('Notaris\AdminBundle\Entity\SvenAddressInfo');
$address_info = $svenAddress_repo->getAddressFromNotary($notary, $info->getAddress()->getSlug(), $localelistener->getLocale());
// if(!$address->getSkedifyId()){
//// $address = $skedifyService->getOfficesByExternalId($office_id, $address);
//// }
////
//// if($address and $address->getSkedifyId()) {
//// $showSkedify = $skedifyService->getSpecificOffice($address->getSkedifyId());
//// }else{
//// $showSkedify = false;
//// }
// $showSkedify = $address->getIsSkedifyActive();
}
}
}
//back link
$backlink = $request->headers->get('referer');
$host = parse_url($backlink, PHP_URL_HOST);
if ($backlink == '') {
$backlink = '/';
} elseif($request->getHttpHost() != $host) {
// Check if referer has a sven domain
$repo_notary_domain = $manager->getRepository('Notaris\AdminBundle\Entity\NotaryDomain');
$pslManager = new Manager(new Cache(), new CurlHttpClient());
try {
$rules = $pslManager->getRules();
$url = $rules->resolve($host);
$domain = $repo_notary_domain->findNotaryByDomain($url->getRegistrableDomain());
if(!$domain) {
$backlink = $this->generateUrl('notaris_frontend_homepage');
}
} catch(\Exception $exception) {
$backlink = $this->generateUrl('notaris_frontend_homepage');
}
}
if(!$addressslug) {
//only on sven domain
if (!empty($notary) && empty($address_info)) {
$info = $localelistener->getNotaryInfo();
/** @var $svenAddress_repo \Notaris\AdminBundle\Entity\SvenAddressInfoRepository */
$svenAddress_repo = $manager->getRepository('Notaris\AdminBundle\Entity\SvenAddressInfo');
$address_info = $svenAddress_repo->getAddressFromNotary($notary, $info->getAddress()->getSlug(), $localelistener->getLocale());
}
if (!empty($notary) && !empty($address_info)) {
$info = $localelistener->getNotaryInfo();
$svenService = $this->get('notaris.sven.service');
$svenPage = $svenService->getSvenPage($notary, $info, $address_info);
return $this->render('@NotarisFrontend/sven/skedify.html.twig', [
'office_id' => $office_id,
'showSkedify' => $showSkedify,
'notary' => $notary,
'backlink' => $backlink,
'address_info' => $address_info,
'socialMedia' => $svenPage['socialMedia'],
'build' => $svenPage['build'],
]);
}
}
return $this->render('@NotarisFrontend/default/skedify.html.twig', [
'office_id'=> $office_id, 'showSkedify' => $showSkedify, 'notary' => $notary, 'backlink' => $backlink
]);
}
//@TODO: remove old code
// public function getAction(Request $request){
//
// $response = new \stdClass();
// $response->success = false;
//
// $submit_name = '';
//
//// if($request->get('name')):
//// echo "Hello submit " . $request->get('name'); exit;
// $submit_name = $request->get('name','');
//// endif;
//
//// if($request->get('ajax_name')):
////// echo "Hello submit " . $request->get('ajax_name'); exit;
//// $response->success = true;
//// $response->name = $request->get('ajax_name');
//// return $this->json($response);
//// endif;
//
// return array('submitName'=>$submit_name);
// }
// /**
// * @param $data
// * @return Response
// */
// protected function json($data)
// {
// $response = new Response(json_encode($data));
// $response->headers->set('Content-Type', 'application/json');
//
// return $response;
// }
public function staticFaqAction(Request $request,)
{
$filename = __DIR__ . '/../../../../web/not-so-faq-' . "nl" . '.html';
if (!file_exists($filename)) {
throw $this->createNotFoundException('Pagina niet gevonden');
}
return new \Symfony\Component\HttpFoundation\Response(file_get_contents($filename));
}
public function barometerAction(Request $request)
{
return $this->render('@NotarisFrontend/default/barometer.html.twig');
}
}