<?php
namespace App\Controller;
use App\Repository\ReviewFormRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\UsersRepository;
use Doctrine\ORM\EntityManagerInterface;
use DateTime;
use App\Repository\QuestionRepository;
use App\Repository\DimensionRepository;
use App\Entity\Users;
use App\Entity\Session;
use App\Entity\Reponse;
use App\Entity\Employee;
class HomeController extends AbstractController
{
public function __construct(
QuestionRepository $questionRepository,
DimensionRepository $dimensionRepository,
ReviewFormRepository $reviewFormRepository,
EntityManagerInterface $em
)
{
$this->questionRepository = $questionRepository;
$this->dimensionRepository = $dimensionRepository;
$this->reviewFormRepository = $reviewFormRepository;
$this->em = $em;
}
#[Route(path: '/', name: 'index')]
public function index(): Response
{
$reviewForm = $this->reviewFormRepository->findFormWithDimensionsAndQuestionsByLang('8b6cf598-7f27-11ef-9d7f-1d1f43f3f908', 'fr');
return $this->render('index.html.twig', [
'reviewForm' => $reviewForm
]);
}
#[Route(path: '/form/{lang}/{formId}', name: 'form')]
public function form($lang, $formId): Response
{
$reviewForm = $this->reviewFormRepository->findFormWithDimensionsAndQuestionsByLang($formId, $lang);
return $this->render('home.html.twig', [
'reviewForm' => $reviewForm
]);
}
#[Route(path: '/send-form', name: 'send_form')]
public function sendForm(Request $request): Response
{
$responses = json_decode($request->get('questionData'),true);
$review = $this->reviewFormRepository->find(trim($request->get('review')));
$session = new Session();
$session->setEmployee(null);
$session->setCreatedAt(new DateTime('now'));
$session->setEmail($request->get('email'));
$session->setName($request->get('firstName'));
$session->setMessage($request->get('message'));
$session->setReviewForm($review);
$session->setLang(strtolower($request->get('lang')));
$birthdate = $request->get('birthdate');
if ($birthdate) {
$birthdateObj = \DateTime::createFromFormat('Y-m-d', $birthdate);
if ($birthdateObj !== false) {
$session->setBirthdate($birthdateObj);
}
}
$session->setEmployeeFunction($request->get('function'));
foreach ($responses as $key => $response) {
$idQuestion = $response['id'];
$score = $response['score'];
$score_minimal = $response['score_minimal'];
$question = $this->questionRepository->find($idQuestion);
$reponse = new Reponse();
$reponse->setQuestion($question);
$reponse->setSession($session);
$reponse->setQuantity(intval($score));
$reponse->setMinimalQuantity(intval($score_minimal));
$reponse->setDeleted(false);
$this->em->persist($reponse);
}
$session->setStatus(1);
$this->em->persist($session);
$this->em->flush();
return new JsonResponse(200);
}
}