<?php
namespace App\EventListener;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Entity\Currency;
use App\Entity\Eshop;
use App\Entity\User;
use App\Entity\Website;
class RequestListener
{
protected $container;
protected $doctrine;
protected $appKernel;
public function __construct(KernelInterface $appKernel,
ManagerRegistry $doctrine,
$defaultLocale = 'cs')
{
$this->appKernel = $appKernel;
$this->doctrine = $doctrine;
if($_SERVER['HTTP_HOST'] == 'www.easycommerc.cz' OR $_SERVER['HTTP_HOST'] == 'easycommerc.cz') {
$defaultLocale = 'cs';
} elseif($_SERVER['HTTP_HOST'] == 'www.easycommerc.com' OR $_SERVER['HTTP_HOST'] == 'easycommerc.com') {
$defaultLocale = 'en';
} elseif($_SERVER['HTTP_HOST'] == 'www.easycommerc.eu' OR $_SERVER['HTTP_HOST'] == 'easycommerc.eu') {
$defaultLocale = 'en';
}
$this->defaultLocale = $defaultLocale;
}
/**
* @throws \ReflectionException
*/
public function __invoke(RequestEvent $event): void
{
$request = $event->getRequest();
if (!$event->isMainRequest()) {
// don't do anything if it's not the master request
return;
}
$session = $request->getSession();
// print('<br> QQW RequestListener');
//code to get db credentials from master database and stored in varaiables
$user = null;
/* we setup e-shop and its user */
if(($request->attributes->get('_route') == 'eshop')
OR ($request->attributes->get('_route') == 'eshop3d')
OR ($request->attributes->get('_route') == 'basket3d')
OR ($request->attributes->get('_route') == 'eshopCategory')
OR ($request->attributes->get('_route') == 'eshopProduct')
OR ($request->attributes->get('_route') == 'eshopBasket')
OR ($request->attributes->get('_route') == 'eshopLogin')
OR ($request->attributes->get('_route') == 'eshopRegister')
OR ($request->attributes->get('_route') == 'eshopHomeContact')
OR ($request->attributes->get('_route') == 'eshopEditContact')
OR ($request->attributes->get('_route') == 'eshopOrderContact')
OR ($request->attributes->get('_route') == 'payment')
OR ($request->attributes->get('_route') == 'customer')
OR ($request->attributes->get('_route') == 'thankyou')
OR ($request->attributes->get('_route') == 'eshopPage')
OR ($request->attributes->get('_route') == 'search')
OR ($request->attributes->get('_route') == 'eshopRequest')
OR ($request->attributes->get('_route') == 'getBasket')
OR ($request->attributes->get('_route') == 'addBasket')
OR ($request->attributes->get('_route') == 'apiEshopProduct')
OR ($request->attributes->get('_route') == 'eshopForgottenPassword')
) {
//print('<br>qqw setting e-shop');
//die();
$em = $this->doctrine->getManager();
$eshop = $em->getRepository(Eshop::class)->getEshop($request->attributes->get('eshopId'));
$session->set('eshop', $eshop);
/* we load e-shop currencies into session */
if(empty($session->get('eshopCurrencyList'))) {
//print("<br>QQW e-shop currencies: ");
$eshopCurrencyList = $eshop->getCurrencies();
$session->set('eshopCurrencyList', $eshopCurrencyList);
}
/* we load default currency into session */
if(empty($session->get('eshopCurrency'))) {
//print("<br>QQW e-shop default currency: ");
$eshopCurrency = $em->getRepository(Currency::class)->getCurrency($eshop->getPreferredCurrencyId());
$session->set('eshopCurrency', $eshopCurrency);
}
$user = $em->getRepository(User::class)->getUser($session->get('eshop')->getUserId());
}
/* we setup website and its user */
if($request->attributes->get('_route') == 'web'
OR $request->attributes->get('_route') == 'webGalleries'
OR $request->attributes->get('_route') == 'webGallery'
OR $request->attributes->get('_route') == 'webLogin'
OR $request->attributes->get('_route') == 'webRegister'
OR $request->attributes->get('_route') == 'webHomeContact'
OR $request->attributes->get('_route') == 'page') {
//print('<br>qqw we setup website: '.$request->attributes->get('webId'));
$em = $this->doctrine->getManager();
$website = $em->getRepository(Website::class)->getWebsite($request->attributes->get('webId'));
$session->set('website', $website);
$user = $em->getRepository(User::class)->getUser($session->get('website')->getUserId());
}
/* we set user oriented services */
if($request->attributes->get('_route') == 'gallery3D'
OR $request->attributes->get('_route') == 'freeWorld3D'
OR $request->attributes->get('_route') == 'form'
) {
//print('<br>qqw we setup user: '.$request->attributes->get('userId'));
$em = $this->doctrine->getManager();
$user = $em->getRepository(User::class)->getUser($request->attributes->get('userId'));
}
//print('<br>qqw request listener');
if(empty($user) && !empty($session->get('user'))) {
//print('<br> set user from session');
$user = $session->get('user');
}
/* we set user entity manager */
if(!empty($user)) {
$em = $this->doctrine->getManager();
$connection = $this->appKernel->getContainer()->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
$connection->close();
$refConn = new \ReflectionObject($connection);
$refParams = $refConn->getProperty('params');
$refParams->setAccessible('public'); //we have to change it for a moment
$params = $refParams->getValue($connection);
$params['dbname'] = $user->getDatabaseName();
$params['user'] = $user->getDatabaseUser();
$params['password'] = $user->getDatabasePassword();
$refParams->setAccessible('private');
$refParams->setValue($connection, $params);
$this->doctrine->resetManager('dynamic_em');
}
/* if current user has set db host */
/*
if(!empty($session->get('user'))) {
print('<br>qqw set dem from user');
$connection = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
$connection->close();
$refConn = new \ReflectionObject($connection);
$refParams = $refConn->getProperty('_params');
$refParams->setAccessible('public'); //we have to change it for a moment
$params = $refParams->getValue($connection);
$params['dbname'] = $session->get('user')->getDatabaseName();
$params['user'] = $session->get('user')->getDatabaseUser();
$params['password'] = $session->get('user')->getDatabasePassword();
$refParams->setAccessible('private');
$refParams->setValue($connection, $params);
$this->container->get('doctrine')->resetEntityManager('dynamic_em');
} elseif(!empty($session->get('eshop'))) {
print('<br>qqw set dem from eshop');
$em = $this->container->get('doctrine')->getManager();
//$eshop = $em->getRepository('AppBundle:Eshop')->getCurrentEshop($currentUserId);
$user = $em->getRepository('AppBundle:User')->getUser($session->get('eshop')->getUserId());
$connection = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
$connection->close();
$refConn = new \ReflectionObject($connection);
$refParams = $refConn->getProperty('_params');
$refParams->setAccessible('public'); //we have to change it for a moment
$params = $refParams->getValue($connection);
$params['dbname'] = $user->getDatabaseName();
$params['user'] = $user->getDatabaseUser();
$params['password'] = $user->getDatabasePassword();
$refParams->setAccessible('private');
$refParams->setValue($connection, $params);
$this->container->get('doctrine')->resetEntityManager('dynamic_em');
} elseif(!empty($session->get('website'))) {
print('<br>qqw set dem from website');
$em = $this->container->get('doctrine')->getManager();
$user = $em->getRepository('AppBundle:User')->getUser($session->get('website')->getUserId());
$connection = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
$connection->close();
$refConn = new \ReflectionObject($connection);
$refParams = $refConn->getProperty('_params');
$refParams->setAccessible('public'); //we have to change it for a moment
$params = $refParams->getValue($connection);
$params['dbname'] = $user->getDatabaseName();
$params['user'] = $user->getDatabaseUser();
$params['password'] = $user->getDatabasePassword();
$refParams->setAccessible('private');
$refParams->setValue($connection, $params);
$this->container->get('doctrine')->resetEntityManager('dynamic_em');
}
*/
//$em = $this->container->get('doctrine')->getManager();
//print('<br>locale listener:');
// $request = $event->getRequest();
// if (!$request->hasPreviousSession()) {
// return;
// }
//
// // try to see if the locale has been set as a _locale routing parameter
// if ($locale = $request->attributes->get('_locale')) {
// $request->getSession()->set('_locale', $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));
// }
//
// if(!empty($request->getSession()->get('lang'))) {
// $locale = $request->getSession()->get('lang')->getLangKey();
// } else {
// $locale = $this->defaultLocale;
// //$locale = 'cz';
// }
// //$locale = 'cs';
// print('<br> QQW LocaleListener locale 3: '.$locale);
//
// $request->setLocale($locale);
}
}