<?php
namespace App\EventListener;
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 Symfony\Contracts\Translation\TranslatorInterface;
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) {
$this->container = $container;
$this->security = $security;
$this->em = $entityManager;
$this->topNavbar = $topNavBar;
$this->ltsUtils = $ltsUtils;
$this->translator = $translator;
$this->companyId = 1;
}
/**
*
* @param RequestEvent $event Event object
*/
public function onKernelRequest(RequestEvent $event) {
// Set timezone
$timezone = $this->container->getParameter('timezone');
if($timezone) {
date_default_timezone_set($timezone);
}
// Set default data
$this->setDefaultParams();
// Set userdata
$user = $this->security->getUser();
if ($user && $user->getId()) {
$this->setUserData($user);
$this->setTwigVariables();
}
// Set company details
$this->setCompanyDetails();
// $this->translator->setLocale($this->parameters['app_name']);
// 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');
}
/**
* 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 setDefaultParams() {
$this->parameters['id'] = '';
$this->parameters['userId'] = '';
$this->parameters['email'] = '';
$this->parameters['firstName'] = '';
$this->parameters['surName'] = '';
$this->parameters['fullName'] = '';
$this->parameters['companyName'] = '';
$this->parameters['roles'] = '';
$this->parameters['profilePic'] = '';
$this->parameters['defaultDateFormat'] = 'd-m-Y h:i:a';
$this->parameters['defaultSystemLang'] = 'en';
}
/**
* This function is to set contact details in contact object
* @param object $user
*/
public function setUserData($user) {
$contact = $this->em->getRepository('App:UserProfile')->findOneBy(array('user' => $user->getId()));
if ($contact) {
$this->parameters['id'] = $contact->getId();
$this->parameters['userId'] = $contact->getUser()->getId();
$this->parameters['email'] = $contact->getUser()->getEmail();
$this->parameters['firstName'] = $contact->getFirstName();
$this->parameters['surName'] = $contact->getLastName();
$this->parameters['fullName'] = $contact->getFirstName() . ' ' . $contact->getLastName();
$this->parameters['companyName'] = $contact->getCompanyName();
$this->parameters['roles'] = $user->getRoles();
$this->parameters['profilePic'] = $contact->getUserProfileImage();
}
}
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('user_data', $this->parameters);
}
/**
* This function is to set contact details in twig object
*/
private function setCompanyDetails()
{
$companyDetails = $this->em->getRepository('App:Terminology')->getCompanyDetails($this->companyId);
$this->parameters['lang'] = $companyDetails && $companyDetails['systemLanguage'] != '' ? $companyDetails['systemLanguage'] : $companyDetails['defaultSystemLang'];
$this->parameters['dateTimeformat'] = $this->parameters['defaultDateFormat'];
if($companyDetails && $companyDetails['datetimeFormat'] != ''){
$dataTimeFormat = $this->ltsUtils->getAllDateTimeFormats($companyDetails['datetimeFormat']);
$this->parameters['dateTimeformat'] = $dataTimeFormat['phpDateTimeFormat'];
$this->parameters['dataTimeFormats'] = $dataTimeFormat;
}
$companyDetails['wo_stages'] = $companyDetails['stageJson'] ? json_decode($companyDetails['stageJson'], true) : $this->ltsUtils->getAllWOStages();
$this->parameters['wo_stages'] = $companyDetails['wo_stages'];
$this->container->get('twig')->addGlobal('company', $companyDetails);
}
}