<?php
namespace App\Controller;
use App\Entity\Prospects;
use App\Form\ProspectsType;
use App\Repository\ProspectsRepository;
use App\Repository\ReferentsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Datetime;
/**
* @Route("/prospects")
*/
class ProspectsController extends AbstractController
{
/**
* @Route("/", name="app_prospects_index", methods={"GET"})
*/
public function index(ProspectsRepository $prospectsRepository): Response
{
return $this->render('prospects/index.html.twig', [
'prospects' => $prospectsRepository->findAll(),
]);
}
/**
* @Route("/new/{code}", name="app_prospects_new", methods={"GET", "POST"})
*/
public function new(Request $request, ProspectsRepository $prospectsRepository,$code, ReferentsRepository $referentsRepo): Response
{
$referent = $referentsRepo->findOneBy(array('Code'=>$code));
if(empty($referent))
{
dd("Erreur de code contacter l'administrateur");
}
$prospect = new Prospects();
$form = $this->createForm(ProspectsType::class, $prospect);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$prospExist = $prospectsRepository->findOneBy(array('Email'=>$prospect->getEmail()));
if(empty($prospExist))
{
$prospect->setDateNaissance(new Datetime($_POST['date']));
$prospect->setReferents($referent);
$prospectsRepository->add($prospect, true);
}
return $this->redirectToRoute('app_prospects_new', ['code'=>$code,'message'=>'success'], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('prospects/new.html.twig', [
'prospect' => $prospect,
'form' => $form,
'code' => $code
]);
}
/**
* @Route("/{id}", name="app_prospects_show", methods={"GET"})
*/
public function show(Prospects $prospect): Response
{
return $this->render('prospects/show.html.twig', [
'prospect' => $prospect,
]);
}
/**
* @Route("/{id}/edit", name="app_prospects_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Prospects $prospect, ProspectsRepository $prospectsRepository): Response
{
$form = $this->createForm(ProspectsType::class, $prospect);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$prospectsRepository->add($prospect, true);
return $this->redirectToRoute('app_prospects_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('prospects/edit.html.twig', [
'prospect' => $prospect,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_prospects_delete", methods={"POST"})
*/
public function delete(Request $request, Prospects $prospect, ProspectsRepository $prospectsRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$prospect->getId(), $request->request->get('_token'))) {
$prospectsRepository->remove($prospect, true);
}
return $this->redirectToRoute('app_prospects_index', [], Response::HTTP_SEE_OTHER);
}
}