<?php
namespace App\EventListener;
use App\Entity\TcContacts;
use App\Entity\Terminology;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\EntityManagerInterface;
use App\Utils\TopNavbar;
use App\Utils\LtsUtils;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
final class RoutingListener
{
/**
* @var Container
*/
private $container;
/**
* @var private $ltsUtils;
*/
private $topNavbar;
/**
* @var private $ltsUtils;
*/
private $ltsUtils;
/**
* @var private $translator;
*/
private $translator;
/**
* @var private $security;
*/
private $security;
/**
* @var private $parameters;
*/
private $parameters;
/**
* Constructor
*
* @param Container $container
*/
public function __construct(Container $container, Security $security, EntityManagerInterface $entityManager, TopNavbar $topNavBar, LtsUtils $ltsUtils, TranslatorInterface $translator, EventDispatcherInterface $dispatcher,ClientRegistry $clientRegistry)
{
$this->container = $container;
$this->security = $security;
$this->em = $entityManager;
$this->topNavbar = $topNavBar;
$this->ltsUtils = $ltsUtils;
$this->translator = $translator;
$this->dispatcher = $dispatcher;
$this->clientRegistry = $clientRegistry;
$this->companyId = 1;
}
public function onKernelRequest(RequestEvent $event)
{
$companyDetails = $this->em->getRepository(Terminology::class)->getCompanyDetails($this->companyId);
$timezone = $companyDetails['timezone'] ? $companyDetails['timezone'] : $this->container->getParameter('timezone');
if ($timezone) {
date_default_timezone_set($timezone);
}
$user = $this->security->getUser();
if ($user) {
$this->setUserData($user);
$this->setTwigVariables();
}
// Set company details
$this->setCompanyDetails();
$this->tokenValidate();
$this->translator->setLocale($this->parameters['lang']);
// Set Top navigation details
$this->setTopNavLinks($event);
// $request = $event->getRequest();
$this->container->set('contact', $this);
// some logic to determine the $locale
// $request->setLocale('Asia/Kolkata');
// $dispatcher is an instance of the EventDispatcher
$this->dispatcher->addListener('kernel.request', array( $this->container->get('contact'), 'onKernelRequest'));
}
/**
* This function is to get contact data from contact object
* @param string $key
* @return string
*/
public function get($key)
{
return $this->parameters[$key];
}
public function tokenValidate()
{
return true;
}
/**
* This function is to set contact details in contact object
* @param object $user
*/
public function setUserData($user)
{
$contact = $this->em->getRepository(TcContacts::class)->findOneBy(['user' => $user->getId()]);
if ($contact) {
$this->parameters['id'] = $contact->getId();
$this->parameters['userId'] = $contact->getUser()->getId();
$this->parameters['firstName'] = $contact->getFirstName();
$this->parameters['surName'] = $contact->getSurName();
$this->parameters['fullName'] = $contact->getFirstName() . ' ' . $contact->getSurName();
$this->parameters['companyName'] = $contact->getCompanyName();
$this->parameters['roles'] = $user->getRoles();
}
}
// private function setTopNavLinks($event) {
// $request = $event->getRequest();
// $resArray = $this->topNavbar->getTopNavRoutes($request->get('_route'), $request->attributes->get('module'), $request->attributes->get('page'));
// $this->container->get('twig')->addGlobal('topnav_data', $resArray);
// }
private function setTopNavLinks($event)
{
$request = $event->getRequest();
$resArray = $this->topNavbar->getTopNavRoutes($request);
$this->container->get('twig')->addGlobal('topnav_data', $resArray);
}
/**
* This function is to set contact details in twig object
*/
private function setTwigVariables()
{
$this->container->get('twig')->addGlobal('fr_contact', $this->parameters);
}
/**
* This function is to set contact details in twig object
*/
private function setCompanyDetails()
{
$this->parameters['defaultDateFormat'] = 'd-m-Y h:i:a';
$this->parameters['defaultSystemLang'] = 'en';
$this->parameters['defaultTimezone'] = 'Europe/London';
$companyDetails = $this->em->getRepository(Terminology::class)->getCompanyDetails($this->companyId);
$companyDetails['timezone'] = $companyDetails['timezone'] ? $companyDetails['timezone'] : $this->container->getParameter('timezone');
$this->parameters['lang'] = $companyDetails && $companyDetails['systemLanguage'] != '' ? $companyDetails['systemLanguage'] : $companyDetails['defaultSystemLang'];
$this->parameters['dateTimeformat'] = $this->parameters['defaultDateFormat'];
$this->parameters['timezone'] = $companyDetails && $companyDetails['timezone'] != '' ? $companyDetails['timezone'] : $this->container->getParameter('timezone');
if ($companyDetails && $companyDetails['datetimeFormat'] != '') {
$dataTimeFormat = $this->ltsUtils->getAllDateTimeFormats($companyDetails['datetimeFormat']);
$this->parameters['dateTimeformat'] = $dataTimeFormat['phpDateTimeFormat'];
$this->parameters['dataTimeFormats'] = $dataTimeFormat;
}
// dd($companyDetails);
$this->container->get('twig')->addGlobal('company', $companyDetails);
}
}