src/Controller/Front/AcheterController.php line 66

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use App\Entity\Estate;
  8. use App\Entity\Banner;
  9. use App\Entity\SousType;
  10. use App\Filter\EstateFilterType;
  11. use App\Manager\SearchManager;
  12. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  13. use Setono\GoogleAnalyticsServerSideTrackingBundle\Factory\HitBuilderFactoryInterface;
  14. use Symfony\Component\HttpKernel\KernelInterface;
  15. use Symfony\Bundle\FrameworkBundle\Console\Application;
  16. use Symfony\Component\Console\Input\ArrayInput;
  17. use Symfony\Component\Console\Output\BufferedOutput;
  18. class AcheterController  extends AbstractController
  19. {
  20.     private $searchManager;
  21.     private $hitBuilderFactory;
  22.     public const LIMIT_PER_PAGE = 99999999999;
  23.     public function __construct(SearchManager $searchManager, HitBuilderFactoryInterface $hitBuilderFactory)
  24.     {
  25.         $this->searchManager = $searchManager;
  26.         $this->hitBuilderFactory = $hitBuilderFactory;
  27.     }
  28.     /**
  29.      * @Route("/acheter", name="acheter")
  30.      */
  31.     public function index(Request $request): Response
  32.     {
  33.         $datas = $this->getDatas($request);
  34.         if($request->isXmlHttpRequest()) {
  35.             $output = [
  36.                 'success' => true,
  37.                 'datas' => $datas['datas'],
  38.             ];
  39.             return $this->json($output);
  40.         }
  41.         else{
  42.             $banners = $this->getDoctrine()->getRepository(Banner::class)->findAllRandom();
  43.             $datas['user'] = $this->getUser();
  44.             $datas['banners']=  Banner::encode($banners);
  45.             $datas['current_route'] = 'acheter';
  46.             $datas['theme_class'] = 'primary';
  47.             $datas['with_map'] = 1;
  48.             $datas['with_carrousel'] = 1;
  49.             return $this->render('front/acheter/index.html.twig', $datas);
  50.         }
  51.     }
  52.     /**
  53.      * @Route("/commerces", name="commerces")
  54.      */
  55.     public function commerces(Request $request): Response
  56.     {
  57.         $datas = $this->getDatas($request, 'commerces');
  58.         if($request->isXmlHttpRequest()) {
  59.             $output = [
  60.                 'success' => true,
  61.                 'datas' => $datas['datas'],
  62.             ];
  63.             return $this->json($output);
  64.         }
  65.         else{
  66.             $banners = $this->getDoctrine()->getRepository(Banner::class)->findAllRandom();
  67.             $datas['user'] = $this->getUser();
  68.             $datas['banners']=  Banner::encode($banners);
  69.             $datas['current_route'] = 'commerces';
  70.             $datas['theme_class'] = 'secondary';
  71.             $datas['with_map'] = 0;
  72.             $datas['with_carrousel'] = 1;
  73.             return $this->render('front/acheter/index.html.twig', $datas);
  74.         }
  75.     }
  76.     /**
  77.      * @Route("/acheter/{numeromandat}", name="acheter_estate_old", requirements={"numeromandat"="\d+"})
  78.      */
  79.     public function redirectAcheterFromNumeromandat(int $numeromandat): Response
  80.     {
  81.         $estate = $this->getDoctrine()
  82.             ->getRepository(Estate::class)
  83.             ->findOneBy(['numeromandat' => $numeromandat]);
  84.         if (!$estate) {
  85.             throw $this->createNotFoundException("Aucun bien trouvé pour le mandat $numeromandat");
  86.         }
  87.         if( in_array($estate->getTypebien() , array_keys(estate::getTypes('commerces')) ) ){
  88.             return $this->redirectToRoute('commerces_estate', ['refannonce' => $estate->getRefannonce()], Response::HTTP_MOVED_PERMANENTLY);
  89.         }
  90.         return $this->redirectToRoute('acheter_estate', [
  91.             'refannonce' => $estate->getRefannonce()
  92.         ], Response::HTTP_MOVED_PERMANENTLY);
  93.     }
  94.     /**
  95.      * @Route("/acheter/{refannonce}", name="acheter_estate", requirements={"refannonce"="[^0-9]\w*"})     * 
  96.      */
  97.     public function indexWithEstate(Estate $estate, Request $request): Response
  98.     {   
  99.         // Vérifie si l'agent lié au bien est actif
  100.         if (!$estate->getAgent() || !$estate->getAgent()->getIsActif()) {
  101.             throw $this->createNotFoundException('Bien introuvable');
  102.         }
  103.         if( in_array($estate->getTypebien() , array_keys(estate::getTypes('commerces')) ) ){
  104.             return $this->redirectToRoute('commerces_estate', ['refannonce' => $estate->getRefannonce()], Response::HTTP_MOVED_PERMANENTLY);
  105.         }
  106.         $datas = $this->getDatas($request);
  107.         $banners = $this->getDoctrine()->getRepository(Banner::class)->findAllRandom();
  108.         $datas['estate'] = $estate;
  109.         $datas['user'] = $this->getUser();
  110.         $datas['banners']=  Banner::encode($banners);
  111.         $datas['current_route'] = 'acheter';
  112.         $datas['theme_class'] = 'primary';
  113.         $datas['with_map'] = 1;
  114.         $datas['with_carrousel'] = 1;
  115.         return $this->render('front/acheter/index.html.twig', $datas);
  116.     }
  117.     /**
  118.      * @Route("/commerces/{numeromandat}", name="commerces_estate_old", requirements={"numeromandat"="\d+"})
  119.      */
  120.     public function redirectCommerceFromNumeromandat(int $numeromandat): Response
  121.     {
  122.         $estate = $this->getDoctrine()
  123.             ->getRepository(Estate::class)
  124.             ->findOneBy(['numeromandat' => $numeromandat]);
  125.         if (!$estate) {
  126.             throw $this->createNotFoundException("Aucun bien trouvé pour le mandat $numeromandat");
  127.         }
  128.         
  129.         return $this->redirectToRoute('commerces_estate', [
  130.             'refannonce' => $estate->getRefannonce()
  131.         ], Response::HTTP_MOVED_PERMANENTLY);
  132.     }
  133.     /**
  134.      * @Route("/commerces/{refannonce}", name="commerces_estate", requirements={"refannonce"="[^0-9]\w*"})
  135.      */
  136.     public function commercesWithEstate(Estate $estate, Request $request): Response
  137.     {   
  138.         // Vérifie si l'agent lié au bien est actif
  139.         if (!$estate->getAgent() || !$estate->getAgent()->getIsActif()) {
  140.             throw $this->createNotFoundException('Bien introuvable');
  141.         }
  142.         if( in_array($estate->getTypebien() , array_keys(estate::getTypes('acheter')) ) ){
  143.             return $this->redirectToRoute('acheter_estate', ['refannonce' => $estate->getRefannonce()], Response::HTTP_MOVED_PERMANENTLY);
  144.         }
  145.         $datas = $this->getDatas($request,'commerces');
  146.         // dump($datas);
  147.         $banners = $this->getDoctrine()->getRepository(Banner::class)->findAllRandom();
  148.         $datas['estate'] = $estate;
  149.         $datas['user'] = $this->getUser();
  150.         $datas['banners'] =  Banner::encode($banners);
  151.         $datas['current_route'] = 'commerces';
  152.         $datas['theme_class'] = 'primary';
  153.         $datas['with_map'] = 0;
  154.         $datas['with_carrousel'] = 1;
  155.         return $this->render('front/acheter/index.html.twig', $datas);
  156.     }
  157.     /**
  158.      * @Route("/hit_estate", name="hit_estate")
  159.      */    
  160.     public function hitEstate(Request $request): Response
  161.     {
  162.         if($request->isXmlHttpRequest() && $request->request->has('refannonce')) {
  163.             $eventHitBuilder = $this->hitBuilderFactory->createEventHitBuilder();
  164.             $eventHitBuilder->setEventCategory($request->request->get('familly_type'));
  165.             $eventHitBuilder->setEventAction('Modale');
  166.             $eventHitBuilder->setEventLabel($request->request->get('refannonce'));
  167.             $locationUrl = sprintf("%s/%s/%s", $request->getSchemeAndHttpHost(), $request->request->get('familly_type'), $request->request->get('refannonce'));
  168.             $referrer = sprintf("%s/%s", $request->getSchemeAndHttpHost(), $request->request->get('familly_type'));
  169.             // $pageViewHitBuilder = $this->hitBuilderFactory->createPageViewHitBuilder();
  170.             $eventHitBuilder->setDocumentLocationUrl($locationUrl);
  171.             $eventHitBuilder->setDocumentReferrer($referrer);
  172.             // $send_hits = $this->run_command($kernel, 'setono:google-analytics:send-hits');
  173.             $HTTP_HOST = $request->server->get('HTTP_HOST');
  174.             return $this->json([
  175.                 'eventHitBuilder' => $eventHitBuilder->getData(),
  176.                 // 'pageViewHitBuilder' => $pageViewHitBuilder,
  177.                 // 'send_hits' => $send_hits
  178.             ]); 
  179.         }    
  180.         else{
  181.             throw new AccessDeniedHttpException("L'action demandée est impossible");
  182.         }
  183.     }
  184.     private  function run_command( $kernel, $command , $options=null) 
  185.     { 
  186.         $application = new Application($kernel, $command);
  187.         $application->setAutoExit(false);
  188.         $input = new ArrayInput([
  189.             'command' => $command,
  190.         ]);
  191.         if(is_array($options)){
  192.             foreach($options as $key => $value){
  193.                 $application->addArgument($key, $value);
  194.             }
  195.         }
  196.         // You can use NullOutput() if you don't need the output
  197.         $output = new BufferedOutput();
  198.         $application->run($input, $output);
  199.         // return the output, don't use if you used NullOutput()
  200.         $content = $output->fetch();
  201.         // return new Response(""), if you used NullOutput()
  202.         return new Response($content);
  203.     }
  204.     private function getDatas($request, $familly_type='acheter')
  205.     {   
  206.         // try{
  207.             $session = $request->getSession();
  208.             $offset = max(0, $request->request->getInt('offset', 0));
  209.             if ($request->isMethod('POST') && $request->request->has('sort_by')) {
  210.                 $session->set('sort_by', $request->request->get('sort_by'));
  211.             }
  212.             $sort_by = $session->get('sort_by', 'numeromandat-DESC');
  213.             
  214.             $sous_types = $this->getDoctrine()->getRepository(SousType::class)->getChoices();
  215.             $filter = $this->get('form.factory')->create(EstateFilterType::class, null, ['familly_type' => $familly_type, 'sous_types' => $sous_types]);
  216.             if ($request->request->has($filter->getName())) {
  217.                 $filter->submit($request->request->get($filter->getName()));
  218.             }
  219.             
  220.             $limit = self::LIMIT_PER_PAGE;
  221.             $paginator =  $this->getDoctrine()->getRepository(Estate::class)->findAllWithAgent($familly_type, $filter, $sort_by, $limit, $offset);
  222.             $total = count($paginator);
  223.             
  224.             if($filter->isSubmitted() && $total > 0 ){
  225.                 //Enregistrement de la recherche   
  226.                 $user = $this->getUser();
  227.                 $this->searchManager->save($request, $filter->getData(), $familly_type, $total , $user );
  228.                 //GA hit
  229.                 $filter_data = $filter->getData();
  230.                 $statut = $filter_data['statut_name'];
  231.                 $statut_name_choices = [
  232.                     "available,under_offer" =>"En vente" ,
  233.                     "sold" => "Vendu"
  234.                 ];
  235.                 //track
  236.                 $hitBuilder = $this->hitBuilderFactory->createEventHitBuilder();
  237.                 $hitBuilder->setEventCategory($familly_type);
  238.                 $hitBuilder->setEventAction('Filtre');
  239.                 $hitBuilder->setEventLabel($statut_name_choices[$statut]);
  240.             }
  241.             $all_prices = $this->getDoctrine()->getRepository(Estate::class)->getAllPriceByFamillyType($familly_type);
  242.             $range_prices = Estate::createRange($all_prices);
  243.             $estates = Estate::encode($paginator);
  244.             // $estates = Estate::getIds($paginator);
  245.             $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';
  246.             
  247.             $datas = [
  248.                 'filter' => $filter->createView(),
  249.                 'datas' => [
  250.                     'sort_by' => $sort_by,
  251.                     'filter' => $filter,
  252.                     'familly_type' => $familly_type,
  253.                     'total' => count($paginator),
  254.                     'estates' => $estates,
  255.                     'range_prices' => $range_prices,
  256.                     'next' => min(count($paginator), $offset + self::LIMIT_PER_PAGE),
  257.                     'less' => count($paginator) - min(count($paginator), $offset + self::LIMIT_PER_PAGE),
  258.                     'ribon_right' => ['Sous offre', 'Sous compromis', 'Vendu'],
  259.                     'show_price' => ['En estimation', 'En vente', 'Sous offre'],
  260.                     'show_favorite'=> ['En vente'],
  261.                     'col_class'=> $col_class,
  262.                     // 'with_carrousel' => $familly_type == 'commerces' ? true : false,
  263.                     'with_carrousel' => $familly_type == true,
  264.                 ]
  265.             ];
  266.             return $datas;
  267.         // }
  268.         // catch(\Exception $e){
  269.         //     dump($e->getMessage());
  270.         //     die;
  271.         // }  
  272.     }
  273. }