src/EventListener/RoutingListener.php line 66

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