src/EventListener/RoutingListener.php line 68

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