src/Controller/HomeController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\ReviewFormRepository;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  8. use Symfony\Component\HttpFoundation\StreamedResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use App\Repository\UsersRepository;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use DateTime;
  15. use App\Repository\QuestionRepository;
  16. use App\Repository\DimensionRepository;
  17. use App\Entity\Users;
  18. use App\Entity\Session;
  19. use App\Entity\Reponse;
  20. use App\Entity\Employee;
  21. class HomeController extends AbstractController
  22. {
  23.     public function __construct(
  24.         QuestionRepository $questionRepository,
  25.         DimensionRepository $dimensionRepository,
  26.         ReviewFormRepository $reviewFormRepository,
  27.         EntityManagerInterface $em
  28.     )
  29.     {
  30.         $this->questionRepository $questionRepository;
  31.         $this->dimensionRepository $dimensionRepository;
  32.         $this->reviewFormRepository $reviewFormRepository;
  33.         $this->em $em;
  34.     }
  35.     #[Route(path'/'name'index')]
  36.     public function index(): Response
  37.     {
  38.         $reviewForm $this->reviewFormRepository->findFormWithDimensionsAndQuestionsByLang('8b6cf598-7f27-11ef-9d7f-1d1f43f3f908''fr');
  39.         return $this->render('index.html.twig', [
  40.             'reviewForm' => $reviewForm
  41.         ]);
  42.       
  43.     }
  44.     #[Route(path'/form/{lang}/{formId}'name'form')]
  45.     public function form($lang$formId): Response
  46.     {
  47.         $reviewForm $this->reviewFormRepository->findFormWithDimensionsAndQuestionsByLang($formId$lang);
  48.         return $this->render('home.html.twig', [
  49.             'reviewForm' => $reviewForm
  50.         ]);
  51.     }
  52.     #[Route(path'/send-form'name'send_form')]
  53.     public function sendForm(Request $request): Response
  54.     {
  55.         $responses json_decode($request->get('questionData'),true);
  56.         $review $this->reviewFormRepository->find(trim($request->get('review')));
  57.         $session = new Session();
  58.         $session->setEmployee(null);
  59.         $session->setCreatedAt(new DateTime('now'));
  60.         $session->setEmail($request->get('email'));
  61.         $session->setName($request->get('firstName'));
  62.         $session->setMessage($request->get('message'));
  63.         $session->setReviewForm($review);
  64.         $session->setLang(strtolower($request->get('lang')));
  65.         $birthdate $request->get('birthdate');
  66.         if ($birthdate) { 
  67.             $birthdateObj \DateTime::createFromFormat('Y-m-d'$birthdate); 
  68.             if ($birthdateObj !== false) { 
  69.                 $session->setBirthdate($birthdateObj); 
  70.             } 
  71.         }
  72.         $session->setEmployeeFunction($request->get('function'));
  73.         foreach ($responses as $key => $response) {
  74.             $idQuestion $response['id'];
  75.             $score $response['score'];
  76.             $score_minimal $response['score_minimal']; 
  77.             $question $this->questionRepository->find($idQuestion);
  78.             $reponse = new Reponse();
  79.             $reponse->setQuestion($question);
  80.             $reponse->setSession($session);
  81.             $reponse->setQuantity(intval($score));
  82.             $reponse->setMinimalQuantity(intval($score_minimal));
  83.             $reponse->setDeleted(false);
  84.             $this->em->persist($reponse);
  85.             
  86.         }
  87.         $session->setStatus(1);
  88.         $this->em->persist($session);
  89.         $this->em->flush();
  90.         return new JsonResponse(200);
  91.     }
  92. }