<?php
namespace App\Controller\Frontend;
use App\Entity\CotisationNotification;
use App\Entity\Event;
use App\Entity\Faq;
use App\Entity\FormUser;
use App\Entity\Product;
use App\Form\Frontend\FormUserType;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
#[Route('/', name: 'customer_home_redirect')]
public function home(): RedirectResponse
{
return $this->redirectToRoute('customer_home');
}
#[Route('/home', name: 'customer_home')]
public function index(EntityManagerInterface $em, Request $request): Response
{
$events = $em->getRepository(Event::class)->findNextEvent();
$faqs = $em->getRepository(Faq::class)->findLastFaq();
/** @var FormUser $formUser */
$formUser = $em->getRepository(FormUser::class)->findLastActualityForm($this->getUser());
$formUserType = null;
if ($formUser !== null) {
$formUserType = $this->createForm(FormUserType::class, [
'questions' => $formUser->getFormUserQuestions()
]);
$formUserType->handleRequest($request);
if ($formUserType->isSubmitted() && $formUserType->isValid()) {
$formUser->setIsDisplayed(true);
if ($formUserType->get('save')->isClicked()) {
$formUser
->setStatus(FormUser::STATUS_VALIDATED)
->setEndAt(new DateTimeImmutable())
;
}
if ($formUserType->get('saveDraft')->isClicked()) {
$formUser->setStatus(FormUser::STATUS_DRAFT);
}
$em->persist($formUser);
$em->flush();
$this->addFlash(
'success',
'Enregistrement effectuée avec succès'
);
return $this->redirectToRoute('customer_home');
}
}
return $this->renderForm('frontend/home/index.html.twig', [
'controller_name' => 'HomeController',
'menu' => 'home',
'events' => $events,
'faqs' => $faqs,
'formUserType' => $formUserType,
'formUser' => $formUser
]);
}
}