<?php
namespace App\Controller;
use App\Repository\ReviewFormRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
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;
}
// Contrôleur Symfony
#[Route(path: '/', name: 'index')]
public function index(Request $request): Response
{
$lang = $request->query->get('_locale', $request->getSession()->get('_locale', 'fr'));
$request->getSession()->set('_locale', $lang);
$reviewForm = $this->reviewFormRepository->findFormWithDimensionsAndQuestionsByLang('8b6cf598-7f27-11ef-9d7f-1d1f43f3f908', $lang);
return $this->render('index.html.twig', [
'reviewForm' => $reviewForm
]);
}
#[Route('/change-langage/{params}', name: 'change_language')]
public function changeLangage($params, RequestStack $requestStack, SessionInterface $session): Response
{
// Liste des langues valides
$validLocales = ['en', 'fr', 'nl'];
// Vérifier si la langue est valide, sinon utiliser 'en' comme langue par défaut
if (!in_array($params, $validLocales)) {
$params = 'en';
}
// Stocker la langue sélectionnée dans la session
$session->set('_locale', $params);
// Récupérer la requête actuelle
$request = $requestStack->getCurrentRequest();
$previousUrl = $request->headers->get('referer'); // URL du référent (page précédente)
return $this->redirectToRoute('index');
// Ajouter ou mettre à jour le paramètre de langue dans l'URL de la page précédente
$urlComponents = parse_url($previousUrl);
parse_str($urlComponents['query'] ?? '', $query);
$query['_locale'] = $params;
$urlComponents['query'] = http_build_query($query);
// Reconstruire l'URL complète avec le paramètre de langue ajouté
$newUrl = $urlComponents['scheme'] . '://' . $urlComponents['host'] . ($urlComponents['path'] ?? '') . '?' . $urlComponents['query'];
// Rediriger vers l'URL modifiée avec la nouvelle langue
return new RedirectResponse($newUrl);
}
#[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'];
$comment = $response['comment'];
$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);
$reponse->setComment($comment);
$this->em->persist($reponse);
}
$session->setStatus(1);
$this->em->persist($session);
$this->em->flush();
return new JsonResponse(200);
}
}