<?php
namespace App\Controller\Front;
use App\Repository\Configuration\SurveyRepository;
use App\Repository\LanguagesRepository;
use App\Service\LanguagesManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/')]
class HomeController extends AbstractController
{
#[Route('/', name: 'front_home')]
public function index(Request $request,
SurveyRepository $surveyRepository,
LanguagesManager $languagesManager): Response
{
return $this->render('front/home.html.twig', [
'surveys' => $surveyRepository->getAllQb(
[
'fk' => ['survey.label'],
'fk_description' => ['survey.description'],
'fk_media' => ['survey.vignette_form_request'],
'id_language' => $languagesManager->getLanguageUser($request),
'IsEnable' => 1,
'order_by_position' => true
]
)->getResult()
]);
}
#[Route('/change_locale', name:'change_locale')]
public function changeLocale(Request $request, LanguagesRepository $languagesRepository, $lang ="fr")
{
$old_locale = $request->getLocale();
$id = $request->request->get('id');
if($id) $selectLanguage = $languagesRepository->findOneById($id);
else $selectLanguage = $languagesRepository->findOneByCode($lang);
if($selectLanguage) {
$targetLocale = $selectLanguage->getCode();
$request->setLocale($targetLocale);
$request->getSession()->set('_locale', $targetLocale);
}
$referer = $request->headers->get('referer');
if($old_locale != "[a-z]{2}") $referer = str_replace('/'.$old_locale, '/'.$targetLocale, $referer);
else {
$referer = str_replace('/'.$request->getDefaultLocale(), '/'.$targetLocale, $referer);
}
return new Response($referer);
}
}