<?php
namespace App\Controller\Front;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use App\Entity\Agent;
use App\Entity\Estate;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class AgentsController extends AbstractController
{
/**
* @Route("/agents", name="agents")
*/
public function index(): Response
{
$agents = $this->getDoctrine()->getRepository(Agent::class)->findAllLocalisable();
$choices = ['' => ''];
foreach ($agents as $agent) {
$choices[$agent->getNomPrenom() .' ('. $agent->getSecteur().')'] = $agent->getId() ;
}
$agents_encoded = Agent::encode($agents);
shuffle($agents);
return $this->render('front/agents/index.html.twig', [
'controller_name' => 'AgentsController',
'agents' =>$agents,
'datas' => $agents_encoded,
]);
}
/**
* @Route("/agents/{slug}", name="show_agent")
*/
public function show(string $slug): Response
{
$agent = $this->getDoctrine()->getRepository(Agent::class)->findOneBySlugAndActive($slug);
if (!$agent) {
throw $this->createNotFoundException('Agent introuvable');
}
return $this->getShowView($agent);
}
/**
* @Route("/agents/{slug}/{refannonce}", name="show_agent_estate")
* @ParamConverter("agent", options={"exclude":{"refannonce"}})
*/
public function showWithEstate(string $slug, $refannonce): Response
{
$agent = $this->getDoctrine()->getRepository(Agent::class)->findOneBySlugAndActive($slug);
if (!$agent) {
throw $this->createNotFoundException('Bien introuvable');
}
return $this->getShowView($agent, $refannonce);
}
public function getShowView($agent, $refannonce = null): Response
{
try{
$estates = $this->getDoctrine()->getRepository(Estate::class)->findAllByAgent($agent->getId());
//Parking/box à la fin
$boxes = [];
$others = [];
foreach ($estates as $estate) {
if ($estate->getTypebien() === "Parking/box") {
$boxes[] = $estate;
} else {
$others[] = $estate;
}
}
$estates_sorted = array_merge($others, $boxes);
$encoded_estate = Estate::encode($estates_sorted);
$count_statut = array_count_values(array_column($encoded_estate, 'statut'));
$totaux['Tous'] = count($encoded_estate);
$totaux['Vendus'] = array_key_exists('Vendu', $count_statut) ? $count_statut['Vendu'] : 0 ;
$totaux['Vendus'] += array_key_exists('Sous compromis', $count_statut) ? $count_statut['Sous compromis'] : 0 ;
$totaux['Ventes'] = $totaux['Tous'] - $totaux['Vendus'] ;
return $this->render('front/agents/show.html.twig', [
'current_page' => 'agents',
'agent' => $agent,
'refannonce' => $refannonce,
'estates' => $encoded_estate,
'datas' => ['estates' => $encoded_estate],
'reviews' => $agent->getReviews(),
'totaux' => $totaux
]);
}
catch(Exception $e ){
dump($e );
}
}
/**
* include
*/
public function testimonies( $context=null, $agent_id=null ): Response
{
$testimonies = $this->getDoctrine()->getRepository(Agent::class)->findTestmonies();
return $this->render('front/agents/_testimonies.html.twig', [
'testimonies' => $testimonies,
]);
}
}