<?php
namespace Acme\SudcmsBundle\EventSubscriber;
use Acme\SudcmsBundle\Repository\AuthUserRepository;
use Acme\SudcmsBundle\Repository\SiteLangRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
class LocaleSubscriber implements EventSubscriberInterface {
private $session = null;
private $siteLangRepository;
private $currentSitUid = null;
private $parameterBag;
private $twig;
private $request = null;
private $defaultLocale;
private $requestStack;
private $usersRepository;
private $security;
private $entityManager;
public function __construct(string $defaultLocale, EntityManagerInterface $entityManager,
RequestStack $requestStack, SiteLangRepository $siteLangRepository,
ParameterBagInterface $parameterBag, Environment $twig, AuthUserRepository $repoUser,
Security $security) {
$this->siteLangRepository = $siteLangRepository;
$this->repoUser = $repoUser;
$this->parameterBag = $parameterBag;
$this->twig = $twig;
$this->security = $security;
$this->entityManager = $entityManager;
$this->defaultLocale = $defaultLocale;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents() {
return [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 20]],
];
}
public function onKernelRequest(RequestEvent $event) {
$this->request = $request = $event->getRequest();
$this->session = $request->getSession();
if (!$request->hasPreviousSession()) {
return;
}
// permet de savoir si on est en bo ou front
$args = explode("/", $this->request->getPathInfo());
// clean the path values
foreach ($args as $key => $arg) {
if (trim($arg) == "")
unset($args[$key]);
}
// récupération de l'identifiant du site
$args = array_values($args);
if (isset($args[0]) && $args[0] === "admin") {
$this->currentSitUid = $this->session->get('admin_site_uid', 1);
} else {
$domain = explode(".", $this->request->getHost());
// if (is_array($domain) && isset($domain[1]) && $domain[0] != '127') {
// $this->currentSitUid = $this->parameterBag->get($domain[0]);
// } else {
// $this->currentSitUid = $this->parameterBag->get('DEFAULT_ID');
// }
$this->currentSitUid = $this->parameterBag->get('DEFAULT_ID');
}
// dd($this->currentSitUid);
if (!defined('CURRENT_SITE_ID')) {
define('CURRENT_SITE_ID', $this->currentSitUid);
}
// Choix de la BDD
//DOC : https://karoldabrowski.com/blog/dynamic-database-connection-based-on-request-symfony-and-doctrine/
// $connection = $this->entityManager->getConnection();
//
// if(!$connection instanceof MultiDbConnectionWrapper) {
// throw new \RuntimeException('Wrong connection');
// }
// $connection->selectDatabase($this->parameterBag->get('SITE'.$this->currentSitUid.'_DB'));
//
// // try to see if the locale has been set as a _locale routing parameter
if ($locale = $request->query->get('_locale')) {
$request->getSession()->set('_locale', $locale);
$request->setLocale($locale);
} else {
// if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
// vérification que la langue soit autorisée
$this->CheckLanguageSite();
}
private function CheckLanguageSite() {
$languagesBySite = $this->siteLangRepository->getAllLanguagesBySite($this->currentSitUid);
$langCodeActiveBack = $langCodeActiveFront = [];
$langCodeActiveBackFlag = $langCodeActiveFrontFlag = [];
// récupération des langues pour le front et le back
foreach ($languagesBySite as $lng) {
// langue pour le back
if ($lng['lang_active_back'] == 1 && !in_array($lng['lang_code'], $langCodeActiveBack)) {
$langCodeActiveBack [] = $lng;
$langCodeActiveBackFlag [] = $lng["lang_code"];
}
// langues pour le front
if ($lng['lang_active'] == 1 && !in_array($lng['lang_code'], $langCodeActiveFront)) {
$langCodeActiveFront [] = $lng;
$langCodeActiveFrontFlag [] = $lng["lang_code"];
}
}
// ----- ADMIN -----
// récupère la première langue dispo si la lange n'est pas présente
if (!$this->session->has('admin_site_lang')) {
$this->session->set('admin_site_lang', $langCodeActiveBack[0]['lang_code']);
}
// récupère la première langue dispo si la lange n'est pas autorisée
if (!in_array($this->session->get('admin_site_lang'), $langCodeActiveBackFlag)) {
$this->session->set('admin_site_lang', $langCodeActiveBack[0]['lang_code']);
}
$this->twig->addGlobal("languesActivesBack", $langCodeActiveBack);
// ------
// ----- FRONT -----
// récupère la première langue dispo si la lange n'est pas autorisée
if (!in_array($this->request->getLocale(), $langCodeActiveFrontFlag)) {
$this->request->getSession()->set('_locale', $langCodeActiveFront[0]['lang_code']);
$this->request->setLocale($langCodeActiveFront[0]['lang_code']);
$this->langueFront = $langCodeActiveFront[0]['lang_code'];
}
$this->twig->addGlobal("languesActives", $langCodeActiveFront);
// ------
}
}