src/EventListener/RoutingListener.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\DependencyInjection\Container;
  5. use Symfony\Component\Security\Core\Security;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use App\Utils\TopNavbar;
  8. use App\Utils\LtsUtils;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. final class RoutingListener {
  11.     /**
  12.      * @var Container
  13.      */
  14.     private $container;
  15.     /**
  16.      * @var private $ltsUtils;
  17.      */
  18.     private $topNavbar;
  19.     
  20.     /**
  21.      * @var private $ltsUtils;
  22.      */
  23.     private $ltsUtils;
  24.     /**
  25.      * @var private $translator;
  26.      */
  27.     private $translator;
  28.     /**
  29.      * @var private $security;
  30.      */
  31.     private $security;
  32.     /**
  33.      * @var private $parameters;
  34.      */
  35.     private $parameters;
  36.     /**
  37.      * Constructor
  38.      *
  39.      * @param Container $container
  40.      */
  41.     public function __construct(Container $containerSecurity $securityEntityManagerInterface $entityManagerTopNavbar $topNavBarLtsUtils $ltsUtilsTranslatorInterface $translator) {
  42.         $this->container $container;
  43.         $this->security $security;
  44.         $this->em $entityManager;
  45.     $this->topNavbar $topNavBar;
  46.         $this->ltsUtils $ltsUtils;
  47.     $this->translator $translator;
  48.         $this->companyId 1;
  49.     }
  50.     /**
  51.      *
  52.      * @param RequestEvent $event   Event object
  53.      */
  54.     public function onKernelRequest(RequestEvent $event) {
  55.     // Set timezone
  56.     $timezone $this->container->getParameter('timezone');
  57.    
  58.     if($timezone) {
  59.      date_default_timezone_set($timezone);
  60.     }
  61.     // Set default data
  62.     $this->setDefaultParams();
  63.     // Set userdata
  64.         $user $this->security->getUser();
  65.         if ($user && $user->getId()) {
  66.             $this->setUserData($user);
  67.             $this->setTwigVariables();
  68.     }
  69.     // Set company details
  70.         $this->setCompanyDetails();
  71. //    $this->translator->setLocale($this->parameters['app_name']);
  72.     // Set Top navigation details
  73.         $this->setTopNavLinks($event);
  74. //    $request = $event->getRequest();
  75.         $this->container->set('contact'$this);
  76.         // some logic to determine the $locale
  77. //    $request->setLocale('Asia/Kolkata');
  78.     }
  79.     /**
  80.      * This function is to get contact data from contact object
  81.      * @param string $key
  82.      * @return string
  83.      */
  84.     public function get($key) {
  85.         return $this->parameters[$key];
  86.     }
  87.     public function setDefaultParams() {
  88.         $this->parameters['id'] = '';
  89.         $this->parameters['userId'] = '';
  90.         $this->parameters['email'] = '';
  91.         $this->parameters['firstName'] = '';
  92.         $this->parameters['surName'] = '';
  93.         $this->parameters['fullName'] = '';
  94.         $this->parameters['companyName'] = '';
  95.         $this->parameters['roles'] = '';
  96.         $this->parameters['profilePic'] = '';
  97.         $this->parameters['defaultDateFormat'] = 'd-m-Y h:i:a'
  98.         $this->parameters['defaultSystemLang'] = 'en';
  99.     }
  100.     /**
  101.      * This function is to set contact details in contact object
  102.      * @param object $user
  103.      */
  104.     public function setUserData($user) {
  105.         $contact $this->em->getRepository('App:UserProfile')->findOneBy(array('user' => $user->getId()));
  106.         if ($contact) {
  107.             $this->parameters['id'] = $contact->getId();
  108.             $this->parameters['userId'] = $contact->getUser()->getId();
  109.             $this->parameters['email'] = $contact->getUser()->getEmail();
  110.             $this->parameters['firstName'] = $contact->getFirstName();
  111.             $this->parameters['surName'] = $contact->getLastName();
  112.             $this->parameters['fullName'] = $contact->getFirstName() . ' ' $contact->getLastName();
  113.             $this->parameters['companyName'] = $contact->getCompanyName();
  114.             $this->parameters['roles'] = $user->getRoles();
  115.             $this->parameters['profilePic'] = $contact->getUserProfileImage();
  116.         }
  117.     }
  118.     private function setTopNavLinks($event) {
  119.         $request $event->getRequest();
  120.         $resArray $this->topNavbar->getTopNavRoutes($request);
  121.         $this->container->get('twig')->addGlobal('topnav_data'$resArray);
  122.     }
  123.     /**
  124.      * This function is to set contact details in twig object
  125.      */
  126.     private function setTwigVariables() {
  127.         $this->container->get('twig')->addGlobal('user_data'$this->parameters);
  128.     }
  129.     /**
  130.      * This function is to set contact details in twig object
  131.      */
  132.     private function setCompanyDetails()
  133.     {
  134.     $companyDetails $this->em->getRepository('App:Terminology')->getCompanyDetails($this->companyId);
  135.     $this->parameters['lang'] = $companyDetails && $companyDetails['systemLanguage'] != '' $companyDetails['systemLanguage'] : $companyDetails['defaultSystemLang'];
  136.     $this->parameters['dateTimeformat'] = $this->parameters['defaultDateFormat'];
  137.     if($companyDetails && $companyDetails['datetimeFormat'] != ''){
  138.         $dataTimeFormat $this->ltsUtils->getAllDateTimeFormats($companyDetails['datetimeFormat']);
  139.         $this->parameters['dateTimeformat'] = $dataTimeFormat['phpDateTimeFormat'];
  140.         $this->parameters['dataTimeFormats'] = $dataTimeFormat;
  141.     }
  142.     $companyDetails['wo_stages'] = $companyDetails['stageJson'] ? json_decode($companyDetails['stageJson'], true) : $this->ltsUtils->getAllWOStages();
  143.     $this->parameters['wo_stages'] = $companyDetails['wo_stages'];
  144.     $this->container->get('twig')->addGlobal('company'$companyDetails);
  145.     }
  146.     
  147. }