src/Controller/Frontend/HomeController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\CotisationNotification;
  4. use App\Entity\Event;
  5. use App\Entity\Faq;
  6. use App\Entity\FormUser;
  7. use App\Entity\Product;
  8. use App\Form\Frontend\FormUserType;
  9. use DateTimeImmutable;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class HomeController extends AbstractController
  17. {
  18.     #[Route('/'name'customer_home_redirect')]
  19.     public function home(): RedirectResponse
  20.     {
  21.         return $this->redirectToRoute('customer_home');
  22.     }
  23.     #[Route('/home'name'customer_home')]
  24.     public function index(EntityManagerInterface $emRequest $request): Response
  25.     {
  26.         $events $em->getRepository(Event::class)->findNextEvent();
  27.         $faqs $em->getRepository(Faq::class)->findLastFaq();
  28.         /** @var FormUser $formUser */
  29.         $formUser $em->getRepository(FormUser::class)->findLastActualityForm($this->getUser());
  30.         $formUserType null;
  31.         if ($formUser !== null) {
  32.             $formUserType $this->createForm(FormUserType::class, [
  33.                 'questions' => $formUser->getFormUserQuestions()
  34.             ]);
  35.             $formUserType->handleRequest($request);
  36.             if ($formUserType->isSubmitted() && $formUserType->isValid()) {
  37.                 $formUser->setIsDisplayed(true);
  38.                 if ($formUserType->get('save')->isClicked()) {
  39.                     $formUser
  40.                         ->setStatus(FormUser::STATUS_VALIDATED)
  41.                         ->setEndAt(new DateTimeImmutable())
  42.                     ;
  43.                 }
  44.                 if ($formUserType->get('saveDraft')->isClicked()) {
  45.                     $formUser->setStatus(FormUser::STATUS_DRAFT);
  46.                 }
  47.                 $em->persist($formUser);
  48.                 $em->flush();
  49.                 $this->addFlash(
  50.                     'success',
  51.                     'Enregistrement effectuée avec succès'
  52.                 );
  53.                 return $this->redirectToRoute('customer_home');
  54.             }
  55.         }
  56.         return $this->renderForm('frontend/home/index.html.twig', [
  57.             'controller_name' => 'HomeController',
  58.             'menu' => 'home',
  59.             'events' => $events,
  60.             'faqs' => $faqs,
  61.             'formUserType' => $formUserType,
  62.             'formUser' => $formUser
  63.         ]);
  64.     }
  65. }