<?php
namespace App\Controller\Front;
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\Estate;
use App\Entity\Banner;
use App\Entity\SousType;
use App\Filter\EstateFilterType;
use App\Manager\SearchManager;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Setono\GoogleAnalyticsServerSideTrackingBundle\Factory\HitBuilderFactoryInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
class AcheterController extends AbstractController
{
private $searchManager;
private $hitBuilderFactory;
public const LIMIT_PER_PAGE = 99999999999;
public function __construct(SearchManager $searchManager, HitBuilderFactoryInterface $hitBuilderFactory)
{
$this->searchManager = $searchManager;
$this->hitBuilderFactory = $hitBuilderFactory;
}
/**
* @Route("/acheter", name="acheter")
*/
public function index(Request $request): Response
{
$datas = $this->getDatas($request);
if($request->isXmlHttpRequest()) {
$output = [
'success' => true,
'datas' => $datas['datas'],
];
return $this->json($output);
}
else{
$banners = $this->getDoctrine()->getRepository(Banner::class)->findAllRandom();
$datas['user'] = $this->getUser();
$datas['banners']= Banner::encode($banners);
$datas['current_route'] = 'acheter';
$datas['theme_class'] = 'primary';
$datas['with_map'] = 1;
$datas['with_carrousel'] = 1;
return $this->render('front/acheter/index.html.twig', $datas);
}
}
/**
* @Route("/commerces", name="commerces")
*/
public function commerces(Request $request): Response
{
$datas = $this->getDatas($request, 'commerces');
if($request->isXmlHttpRequest()) {
$output = [
'success' => true,
'datas' => $datas['datas'],
];
return $this->json($output);
}
else{
$banners = $this->getDoctrine()->getRepository(Banner::class)->findAllRandom();
$datas['user'] = $this->getUser();
$datas['banners']= Banner::encode($banners);
$datas['current_route'] = 'commerces';
$datas['theme_class'] = 'secondary';
$datas['with_map'] = 0;
$datas['with_carrousel'] = 1;
return $this->render('front/acheter/index.html.twig', $datas);
}
}
/**
* @Route("/acheter/{numeromandat}", name="acheter_estate_old", requirements={"numeromandat"="\d+"})
*/
public function redirectAcheterFromNumeromandat(int $numeromandat): Response
{
$estate = $this->getDoctrine()
->getRepository(Estate::class)
->findOneBy(['numeromandat' => $numeromandat]);
if (!$estate) {
throw $this->createNotFoundException("Aucun bien trouvé pour le mandat $numeromandat");
}
if( in_array($estate->getTypebien() , array_keys(estate::getTypes('commerces')) ) ){
return $this->redirectToRoute('commerces_estate', ['refannonce' => $estate->getRefannonce()], Response::HTTP_MOVED_PERMANENTLY);
}
return $this->redirectToRoute('acheter_estate', [
'refannonce' => $estate->getRefannonce()
], Response::HTTP_MOVED_PERMANENTLY);
}
/**
* @Route("/acheter/{refannonce}", name="acheter_estate", requirements={"refannonce"="[^0-9]\w*"}) *
*/
public function indexWithEstate(Estate $estate, Request $request): Response
{
// Vérifie si l'agent lié au bien est actif
if (!$estate->getAgent() || !$estate->getAgent()->getIsActif()) {
throw $this->createNotFoundException('Bien introuvable');
}
if( in_array($estate->getTypebien() , array_keys(estate::getTypes('commerces')) ) ){
return $this->redirectToRoute('commerces_estate', ['refannonce' => $estate->getRefannonce()], Response::HTTP_MOVED_PERMANENTLY);
}
$datas = $this->getDatas($request);
$banners = $this->getDoctrine()->getRepository(Banner::class)->findAllRandom();
$datas['estate'] = $estate;
$datas['user'] = $this->getUser();
$datas['banners']= Banner::encode($banners);
$datas['current_route'] = 'acheter';
$datas['theme_class'] = 'primary';
$datas['with_map'] = 1;
$datas['with_carrousel'] = 1;
return $this->render('front/acheter/index.html.twig', $datas);
}
/**
* @Route("/commerces/{numeromandat}", name="commerces_estate_old", requirements={"numeromandat"="\d+"})
*/
public function redirectCommerceFromNumeromandat(int $numeromandat): Response
{
$estate = $this->getDoctrine()
->getRepository(Estate::class)
->findOneBy(['numeromandat' => $numeromandat]);
if (!$estate) {
throw $this->createNotFoundException("Aucun bien trouvé pour le mandat $numeromandat");
}
return $this->redirectToRoute('commerces_estate', [
'refannonce' => $estate->getRefannonce()
], Response::HTTP_MOVED_PERMANENTLY);
}
/**
* @Route("/commerces/{refannonce}", name="commerces_estate", requirements={"refannonce"="[^0-9]\w*"})
*/
public function commercesWithEstate(Estate $estate, Request $request): Response
{
// Vérifie si l'agent lié au bien est actif
if (!$estate->getAgent() || !$estate->getAgent()->getIsActif()) {
throw $this->createNotFoundException('Bien introuvable');
}
if( in_array($estate->getTypebien() , array_keys(estate::getTypes('acheter')) ) ){
return $this->redirectToRoute('acheter_estate', ['refannonce' => $estate->getRefannonce()], Response::HTTP_MOVED_PERMANENTLY);
}
$datas = $this->getDatas($request,'commerces');
// dump($datas);
$banners = $this->getDoctrine()->getRepository(Banner::class)->findAllRandom();
$datas['estate'] = $estate;
$datas['user'] = $this->getUser();
$datas['banners'] = Banner::encode($banners);
$datas['current_route'] = 'commerces';
$datas['theme_class'] = 'primary';
$datas['with_map'] = 0;
$datas['with_carrousel'] = 1;
return $this->render('front/acheter/index.html.twig', $datas);
}
/**
* @Route("/hit_estate", name="hit_estate")
*/
public function hitEstate(Request $request): Response
{
if($request->isXmlHttpRequest() && $request->request->has('refannonce')) {
$eventHitBuilder = $this->hitBuilderFactory->createEventHitBuilder();
$eventHitBuilder->setEventCategory($request->request->get('familly_type'));
$eventHitBuilder->setEventAction('Modale');
$eventHitBuilder->setEventLabel($request->request->get('refannonce'));
$locationUrl = sprintf("%s/%s/%s", $request->getSchemeAndHttpHost(), $request->request->get('familly_type'), $request->request->get('refannonce'));
$referrer = sprintf("%s/%s", $request->getSchemeAndHttpHost(), $request->request->get('familly_type'));
// $pageViewHitBuilder = $this->hitBuilderFactory->createPageViewHitBuilder();
$eventHitBuilder->setDocumentLocationUrl($locationUrl);
$eventHitBuilder->setDocumentReferrer($referrer);
// $send_hits = $this->run_command($kernel, 'setono:google-analytics:send-hits');
$HTTP_HOST = $request->server->get('HTTP_HOST');
return $this->json([
'eventHitBuilder' => $eventHitBuilder->getData(),
// 'pageViewHitBuilder' => $pageViewHitBuilder,
// 'send_hits' => $send_hits
]);
}
else{
throw new AccessDeniedHttpException("L'action demandée est impossible");
}
}
private function run_command( $kernel, $command , $options=null)
{
$application = new Application($kernel, $command);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => $command,
]);
if(is_array($options)){
foreach($options as $key => $value){
$application->addArgument($key, $value);
}
}
// You can use NullOutput() if you don't need the output
$output = new BufferedOutput();
$application->run($input, $output);
// return the output, don't use if you used NullOutput()
$content = $output->fetch();
// return new Response(""), if you used NullOutput()
return new Response($content);
}
private function getDatas($request, $familly_type='acheter')
{
// try{
$session = $request->getSession();
$offset = max(0, $request->request->getInt('offset', 0));
if ($request->isMethod('POST') && $request->request->has('sort_by')) {
$session->set('sort_by', $request->request->get('sort_by'));
}
$sort_by = $session->get('sort_by', 'numeromandat-DESC');
$sous_types = $this->getDoctrine()->getRepository(SousType::class)->getChoices();
$filter = $this->get('form.factory')->create(EstateFilterType::class, null, ['familly_type' => $familly_type, 'sous_types' => $sous_types]);
if ($request->request->has($filter->getName())) {
$filter->submit($request->request->get($filter->getName()));
}
$limit = self::LIMIT_PER_PAGE;
$paginator = $this->getDoctrine()->getRepository(Estate::class)->findAllWithAgent($familly_type, $filter, $sort_by, $limit, $offset);
$total = count($paginator);
if($filter->isSubmitted() && $total > 0 ){
//Enregistrement de la recherche
$user = $this->getUser();
$this->searchManager->save($request, $filter->getData(), $familly_type, $total , $user );
//GA hit
$filter_data = $filter->getData();
$statut = $filter_data['statut_name'];
$statut_name_choices = [
"available,under_offer" =>"En vente" ,
"sold" => "Vendu"
];
//track
$hitBuilder = $this->hitBuilderFactory->createEventHitBuilder();
$hitBuilder->setEventCategory($familly_type);
$hitBuilder->setEventAction('Filtre');
$hitBuilder->setEventLabel($statut_name_choices[$statut]);
}
$all_prices = $this->getDoctrine()->getRepository(Estate::class)->getAllPriceByFamillyType($familly_type);
$range_prices = Estate::createRange($all_prices);
$estates = Estate::encode($paginator);
// $estates = Estate::getIds($paginator);
$col_class = $familly_type == 'commerces' ? 'col-xs-12 col-md-6 col-lg-4 col-xl-3' : 'col-md-6 col-lg-12 col-xl-6';
$datas = [
'filter' => $filter->createView(),
'datas' => [
'sort_by' => $sort_by,
'filter' => $filter,
'familly_type' => $familly_type,
'total' => count($paginator),
'estates' => $estates,
'range_prices' => $range_prices,
'next' => min(count($paginator), $offset + self::LIMIT_PER_PAGE),
'less' => count($paginator) - min(count($paginator), $offset + self::LIMIT_PER_PAGE),
'ribon_right' => ['Sous offre', 'Sous compromis', 'Vendu'],
'show_price' => ['En estimation', 'En vente', 'Sous offre'],
'show_favorite'=> ['En vente'],
'col_class'=> $col_class,
// 'with_carrousel' => $familly_type == 'commerces' ? true : false,
'with_carrousel' => $familly_type == true,
]
];
return $datas;
// }
// catch(\Exception $e){
// dump($e->getMessage());
// die;
// }
}
}