<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Form\ContactType;
use App\Repository\ContactRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use DateTimeImmutable;
/**
* @Route("/contact")
*/
class ContactController extends AbstractController
{
/**
* @Route("/index", name="app_contact_index", methods={"GET"})
*/
public function index(ContactRepository $contactRepository): Response
{
return $this->render('contact/index.html.twig', [
'contacts' => $contactRepository->findAll(),
]);
}
/**
* @Route("/", name="app_contact_new", methods={"GET", "POST"})
*/
public function new(Request $request, ContactRepository $contactRepository): Response
{
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact->setCreatedAt(new DateTimeImmutable());
$contact->setResolved(false);
$contactRepository->add($contact, true);
$this->addFlash('success', 'info contact bien prise en compte!');
return $this->redirectToRoute('app_home', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('contact/new.html.twig', [
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_contact_show", methods={"GET"})
*/
public function show(Contact $contact): Response
{
return $this->render('contact/show.html.twig', [
'contact' => $contact,
]);
}
/**
* @Route("/{id}/edit", name="app_contact_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Contact $contact, ContactRepository $contactRepository): Response
{
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contactRepository->add($contact, true);
$this->addFlash('success', 'modification info contact bien prise en compte!');
return $this->redirectToRoute('app_contact_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('contact/edit.html.twig', [
'contact' => $contact,
'form' => $form,
]);
}
/**
* @Route("/{id}", name="app_contact_delete", methods={"POST"})
*/
public function delete(Request $request, Contact $contact, ContactRepository $contactRepository): Response
{
if ($this->isCsrfTokenValid('delete' . $contact->getId(), $request->request->get('_token'))) {
$contactRepository->remove($contact, true);
}
$this->addFlash('warning', 'info contact suppression bien prise en compte!');
return $this->redirectToRoute('app_contact_index', [], Response::HTTP_SEE_OTHER);
}
}