<?php
namespace App\Controller\Backend;
use App\Entity\EntityStatus;
use App\Entity\Entreprise;
use App\Entity\User;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Entity\Contact;
use App\Form\Common\ContactCompleteType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/admin')]
class ContactController extends AbstractController
{
#[Route(path: '/contacts/{id}/show', name: 'show_contact')]
public function show(Contact $contact, UserRepository $repository): Response
{
$userExists = $repository->findOneBy([
'contact' => $contact
]);
return $this->render('backend/contact/show.html.twig', [
'controller_name' => 'ContactController',
'menu' => 'contact',
'contact' => $contact,
'userExists' => $userExists
]);
}
#[Route(path: '/contacts/add-from-dashboard', name: 'add_contact_from_dashboard', methods: ['GET', 'POST'])]
#[Route(path: '/contacts/{id}/add', name: 'add_contact_from_firm', methods: ['GET', 'POST'])]
#[IsGranted('ROLE_ADD_ENTITY', message: 'Vous n\'avez pas les droits nécessaire pour accéder à cette fonctionnalité')]
public function add(
Request $request,
EntityManagerInterface $em,
?Entreprise $entreprise = null,
?int $fromDashboard = 0
): Response
{
$contact = new Contact();
$route = $request->attributes->get('_route');
if (!is_null($entreprise)) {
$contact->setFirm($entreprise);
}
$form = $this->createForm(ContactCompleteType::class, $contact, [
'create_from_dashboard' => $route === 'add_contact_from_dashboard'
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (!is_null($entreprise)) {
$contact->setFirm($entreprise);
}
$contact->setStatus(Contact::STATUS_ENABLED);
$em->persist($contact);
$em->flush();
$this->addFlash(
'success',
'Enregistrement effectué avec succès'
);
if ($route === 'add_contact_from_firm' && !is_null($entreprise)) {
return $this->redirectToRoute('show_entreprise', [
'id' => $entreprise->getId()
]);
} else {
return $this->redirectToRoute('show_contact', [
'id' => $form->getData()->getId()
]);
}
}
return $this->render('backend/contact/edit.html.twig', [
'controller_name' => 'ContactController',
'menu' => 'contact',
'contactForm' => $form->createView(),
'action' => 'creation',
'route' => $route,
'entreprise' => $entreprise
]);
}
#[Route(path: '/contacts/{id}/edit', name: 'edit_contact', methods: ['GET', 'POST'])]
#[Route(path: '/contacts/{id}/edit', name: 'edit_contact_from_firm', methods: ['GET', 'POST'])]
#[IsGranted('ROLE_UPDATE_ENTITY', message: 'Vous n\'avez pas les droits nécessaire pour accéder à cette fonctionnalité')]
public function edit(
Contact $contact,
Request $request,
EntityManagerInterface $em
)
{
$route = $request->attributes->get('_route');
$form = $this->createForm(ContactCompleteType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->flush();
$this->addFlash(
'success',
'Enregistrement effectué avec succès'
);
if ($route === 'edit_contact_from_firm') {
return $this->redirectToRoute('show_entreprise', [
'id' => $contact->getFirm()->getId()
]);
} else {
return $this->redirectToRoute('show_contact', [
'id' => $contact->getId()
]);
}
}
return $this->renderForm('backend/contact/edit.html.twig', [
'controller_name' => 'ContactController',
'menu' => 'contact',
'contactForm' => $form,
'action' => 'edition',
'contact' => $contact,
'contactData' => $contact->getPrenom().' ' .$contact->getNom(),
'route' => $route
]);
}
#[Route('/contacts/{id}/archive', name: 'archive_contact', methods: ['DELETE', 'POST'])]
#[IsGranted('ROLE_DELETE_ENTITY', message: 'Vous n\'avez pas les droits nécessaire pour accéder à cette fonctionnalité')]
public function archive(Contact $contact, Request $request, EntityManagerInterface $em): JsonResponse
{
$data = json_decode($request->getContent(), true);
if ($this->isCsrfTokenValid('contact_'.$contact->getId(), $data['_token'])) {
$contact->setStatus(EntityStatus::STATUS_ARCHIVED);
if ($contact->getUser() instanceof User) {
$contact->getUser()->setStatus(EntityStatus::STATUS_ARCHIVED);
}
$em->persist($contact);
$em->flush();
return new JsonResponse([
'status' => 'ok',
'message' => 'Archivage effectuée avec succès'
]);
}
return new JsonResponse([
'status' => 'nok',
'message' => 'Erreur lors de l\'archivage. Merci de contacter un administrateur.'
]);
}
}