src/Controller/Front/AgentsController.php line 51

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\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  8. use App\Entity\Agent;
  9. use App\Entity\Estate;
  10. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  11. class AgentsController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/agents", name="agents")
  15.      */
  16.     public function index(): Response
  17.     {   
  18.         $agents =  $this->getDoctrine()->getRepository(Agent::class)->findAllLocalisable();
  19.         $choices  = ['' => ''];
  20.         foreach ($agents as $agent) {
  21.             $choices[$agent->getNomPrenom() .' ('. $agent->getSecteur().')'] = $agent->getId() ;
  22.         }   
  23.         $agents_encoded = Agent::encode($agents);
  24.         shuffle($agents);
  25.         return $this->render('front/agents/index.html.twig', [
  26.             'controller_name' => 'AgentsController',
  27.             'agents' =>$agents,
  28.             'datas' => $agents_encoded,
  29.         ]);
  30.     }
  31.     /**
  32.      * @Route("/agents/{slug}", name="show_agent")
  33.      */
  34.     public function show(string $slug): Response
  35.     {   
  36.         $agent =  $this->getDoctrine()->getRepository(Agent::class)->findOneBySlugAndActive($slug);
  37.         if (!$agent) {
  38.             throw $this->createNotFoundException('Agent introuvable');
  39.         }
  40.         return $this->getShowView($agent);
  41.     }
  42.     /**
  43.      * @Route("/agents/{slug}/{refannonce}", name="show_agent_estate")
  44.      * @ParamConverter("agent", options={"exclude":{"refannonce"}})
  45.      */
  46.     public function showWithEstate(string $slug, $refannonce): Response
  47.     {   
  48.         $agent =  $this->getDoctrine()->getRepository(Agent::class)->findOneBySlugAndActive($slug);
  49.         if (!$agent) {
  50.             throw $this->createNotFoundException('Bien introuvable');
  51.         }
  52.         return $this->getShowView($agent, $refannonce);
  53.     }
  54.     public function getShowView($agent, $refannonce = null): Response
  55.     {   
  56.         try{
  57.             $estates = $this->getDoctrine()->getRepository(Estate::class)->findAllByAgent($agent->getId());
  58.             //Parking/box à la fin 
  59.             $boxes = [];
  60.             $others = [];
  61.             foreach ($estates as $estate) {
  62.                 if ($estate->getTypebien() === "Parking/box") {
  63.                     $boxes[] = $estate;
  64.                 } else {
  65.                     $others[] = $estate;
  66.                 }
  67.             }
  68.             $estates_sorted = array_merge($others, $boxes);
  69.             $encoded_estate = Estate::encode($estates_sorted);
  70.             $count_statut = array_count_values(array_column($encoded_estate, 'statut'));
  71.             
  72.             $totaux['Tous'] = count($encoded_estate);
  73.             $totaux['Vendus'] = array_key_exists('Vendu', $count_statut) ? $count_statut['Vendu'] : 0 ;
  74.             $totaux['Vendus'] += array_key_exists('Sous compromis', $count_statut) ? $count_statut['Sous compromis'] : 0 ;
  75.             $totaux['Ventes'] = $totaux['Tous'] - $totaux['Vendus'] ;
  76.             return $this->render('front/agents/show.html.twig', [
  77.                 'current_page' => 'agents',
  78.                 'agent' => $agent,
  79.                 'refannonce' => $refannonce,
  80.                 'estates' => $encoded_estate,
  81.                 'datas' =>  ['estates' => $encoded_estate],
  82.                 'reviews' => $agent->getReviews(),
  83.                 'totaux' => $totaux
  84.             ]);
  85.         }
  86.         catch(Exception $e ){
  87.             dump($e );
  88.         }
  89.     }
  90.     /**
  91.      * include 
  92.      */
  93.     public function testimonies( $context=null, $agent_id=null ): Response
  94.     {
  95.         $testimonies = $this->getDoctrine()->getRepository(Agent::class)->findTestmonies();
  96.         return $this->render('front/agents/_testimonies.html.twig', [
  97.             'testimonies' => $testimonies,
  98.         ]);
  99.     }
  100. }