src/EventListener/RoutingListener.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Company;
  4. use App\Entity\LoginSessions;
  5. use App\Entity\Terminology;
  6. use App\Entity\UserProfile;
  7. use App\Utils\LtsUtils;
  8. use App\Utils\TopNavbar;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. final class RoutingListener
  17. {
  18.     /**
  19.      * @var ContainerInterface
  20.      */
  21.     private $container;
  22.     /**
  23.      * @var mixed;
  24.      */
  25.     private $topNavbar;
  26.     /**
  27.      * @var LtsUtils;
  28.      */
  29.     private $ltsUtils;
  30.     /**
  31.      * @var TranslatorInterface;
  32.      */
  33.     private $translator;
  34.     /**
  35.      * @var Security;
  36.      */
  37.     private $security;
  38.     /**
  39.      * @var mixed;
  40.      */
  41.     private $parameters;
  42.     private $em;
  43.     private $router;
  44.     private $companyId;
  45.     /**
  46.      * Constructor.
  47.      *
  48.      * @param Container $container
  49.      */
  50.     public function __construct(ContainerInterface $containerSecurity $securityEntityManagerInterface $entityManagerTopNavbar $topNavBarLtsUtils $ltsUtilsTranslatorInterface $translatorRouterInterface $router)
  51.     {
  52.         $this->container $container;
  53.         $this->security $security;
  54.         $this->em $entityManager;
  55.         $this->topNavbar $topNavBar;
  56.         $this->ltsUtils $ltsUtils;
  57.         $this->translator $translator;
  58.         $this->router $router;
  59.         $this->companyId $this->container->getParameter('default_company_id');
  60.     }
  61.     /**
  62.      * Routing Listener event.
  63.      */
  64.     public function onKernelRequest(RequestEvent $event)
  65.     {
  66.         $user $this->security->getUser();
  67.         // Set default data
  68.         $this->setDefaultParams();
  69.         // Set UTC timezone
  70.         $this->setTimeZone();
  71.         if ($user) {
  72.             $this->setUserData($user);
  73.             $this->checkForPlanExpiry($event);
  74.             $this->setTwigVariables();
  75.             $this->setCompanyDetails();
  76.             // Set company details
  77.             $this->translator->setLocale($this->parameters['lang']);
  78.             // Set container variables
  79.             $this->container->set('contact'$this);
  80.             // Set Top navigation details
  81.             $this->setTopNavLinks($event$user);
  82.             // Set Demo tour details
  83.             $this->setDemoTourData();
  84.             // some logic to determine the $locale
  85.             // $request->setLocale('Asia/Kolkata');
  86.             // $this->saveLastActiveTime($event);  # last activity time store
  87.         }
  88.     }
  89.     /**
  90.      * This function is to get contact data from contact object.
  91.      *
  92.      * @param string $key
  93.      *
  94.      * @return string
  95.      */
  96.     public function get($key)
  97.     {
  98.         return $this->parameters[$key];
  99.     }
  100.     /**
  101.      * Method setDefaultParams
  102.      * This function is to set default contact data from contact object.
  103.      *
  104.      * @return void
  105.      */
  106.     public function setDefaultParams()
  107.     {
  108.         $this->parameters['id'] = '';
  109.         $this->parameters['userId'] = '';
  110.         $this->parameters['email'] = '';
  111.         $this->parameters['firstName'] = '';
  112.         $this->parameters['surName'] = '';
  113.         $this->parameters['fullName'] = '';
  114.         $this->parameters['companyName'] = '';
  115.         $this->parameters['roles'] = '';
  116.         $this->parameters['isMainAdmin'] = '';
  117.         $this->parameters['profilePic'] = '';
  118.         $this->parameters['defaultDateFormat'] = 'd-m-Y h:i:a';
  119.         $this->parameters['defaultSystemLang'] = 'en';
  120.         $this->parameters['planExpireDate'] = null;
  121.         $this->parameters['plan'] = '';
  122.         $this->parameters['account_status'] = 'active';
  123.         $this->parameters['companyId'] = $this->companyId;
  124.     }
  125.     /**
  126.      * This function is to set contact details in contact object.
  127.      *
  128.      * @param object $user
  129.      */
  130.     public function setUserData($user)
  131.     {
  132.         $userDetails $this->em->getRepository(UserProfile::class)->getUserFullData($user->getId());
  133.         if ($userDetails) {
  134.             $this->parameters['id'] = $userDetails['id'];
  135.             $this->parameters['userId'] = $userDetails['id'];
  136.             $this->parameters['firstName'] = $userDetails['firstName'];
  137.             $this->parameters['email'] = $userDetails['email'];
  138.             $this->parameters['surName'] = $userDetails['lastName'];
  139.             $this->parameters['tier'] = $userDetails['tier'];
  140.             $this->parameters['userProfileImage'] = $userDetails['userProfileImage'];
  141.             $this->parameters['userProfileImageUrl'] = !empty($userDetails['userProfileImage']) ? $this->ltsUtils->getUploadPath('profile_pic'''$userDetails['userProfileImage']) : '';
  142.             $this->parameters['fullName'] = $userDetails['fullName'];
  143.             $this->parameters['companyName'] = $userDetails['companyTitle'];
  144.             $this->parameters['roles'] = $user->getRoles();
  145.             $this->parameters['isMainAdmin'] = $user->isMainAdmin();
  146.             $this->parameters['isSuperAdmin'] = $user->isSuperAdmin();
  147.             $this->parameters['companyId'] = $this->companyId $userDetails['companyId'];
  148.             $this->parameters['encodeCompanyId'] = base64_encode($userDetails['companyId']);
  149.             $this->parameters['userPrivilegeRoles'] = $userDetails['userPrivilegeRoles'];
  150.             $this->parameters['userType'] = $userDetails['userType'];
  151.             $this->parameters['ltsLogo'] = 'data-point-new-logo.png';
  152.             $this->parameters['companyImageUrl'] = !empty($userDetails['companyImage']) ? $this->ltsUtils->getUploadPath('company_logo'$this->parameters['encodeCompanyId'], $userDetails['companyImage']) : '';
  153.             $this->parameters['planExpireDate'] = $userDetails['planExpireDate'];
  154.             $this->parameters['plan'] = $userDetails['plan'];
  155.             $this->parameters['refId'] = $userDetails['refId'];
  156.             $this->parameters['previlageRoles'] = !empty($userDetails['userPrivilegeRoles']) ? $this->ltsUtils->getUserRoles($userDetails['userPrivilegeRoles']) : [];
  157.         }
  158.     }
  159.     /**
  160.      * Set Time zone.
  161.      */
  162.     public function setTimeZone()
  163.     {
  164.         date_default_timezone_set('UTC');
  165.     }
  166.     /**
  167.      * @param type $event
  168.      * @param type $user
  169.      */
  170.     private function setTopNavLinks($event$user)
  171.     {
  172.         $request $event->getRequest();
  173.         $resArray $this->topNavbar->getData($request$user);
  174.         if ($resArray) {
  175.             $this->container->get('twig')->addGlobal('topnav_data'$resArray['topnavData']);
  176.             $this->container->get('twig')->addGlobal('pagetitle_data'$resArray['pageTitleBlock']);
  177.         }
  178.     }
  179.     private function setDemoTourData()
  180.     {
  181.         $this->container->get('twig')->addGlobal('demo_tour_data'$this->ltsUtils->getDemoTourArray());
  182.     }
  183.     /**
  184.      * This function is to set contact details in twig object.
  185.      */
  186.     private function setTwigVariables()
  187.     {
  188.         $this->container->get('twig')->addGlobal('fr_contact'$this->parameters);
  189.         $this->container->get('twig')->addGlobal('ltApp'$this->parameters);
  190.     }
  191.     /**
  192.      * This function is to set contact details in twig object.
  193.      */
  194.     private function setCompanyDetails()
  195.     {
  196.         $this->parameters['defaultDateFormat'] = 'd-m-Y h:i:a';
  197.         $this->parameters['defaultDate'] = 'd-m-Y';
  198.         $this->parameters['defaultTime'] = 'h:i:a';
  199.         $this->parameters['defaultSystemLang'] = 'en';
  200.         $this->parameters['defaultTimezone'] = 'UTC';
  201.         $companyDetails $this->em->getRepository(Terminology::class)->getCompanyDetails($this->companyId);
  202.         $companyDetails['timezone'] = !empty($companyDetails['timezone']) ? $companyDetails['timezone'] : '';
  203.         $this->parameters['lang'] = $companyDetails && !empty($companyDetails['systemLanguage']) ? $companyDetails['systemLanguage'] : 'en';
  204.         $this->parameters['timezone'] = isset($companyDetails['timezone']) ? $companyDetails['timezone'] : '';
  205.         $this->parameters['userRefidPrefix'] = !empty($companyDetails['userRefidPrefix']) ? $companyDetails['userRefidPrefix'] : '';
  206.         $this->parameters['cardRefidPrefix'] =  !empty($companyDetails['cardRefidPrefix']) ? $companyDetails['cardRefidPrefix'] : '';
  207.         $this->parameters['cardRefidStartsFrom'] = !empty($companyDetails['cardRefidStartsFrom']) ? $companyDetails['cardRefidStartsFrom'] : '1';
  208.         $this->parameters['dataTimeFormat'] = $this->ltsUtils->getDateTimeFormat(isset($companyDetails['datetimeFormat']) ? $companyDetails['datetimeFormat'] : null);
  209.         $this->parameters['dataFormat'] = $this->ltsUtils->getDateFormat(isset($companyDetails['dateFormat']) ? $companyDetails['dateFormat'] : null);
  210.         $this->parameters['timeFormat'] = $this->ltsUtils->getTimeFormat(isset($companyDetails['timeFormat']) ? $companyDetails['timeFormat'] : null);
  211.         $this->parameters['companyTerminology'] = $this->ltsUtils->getTerminologyDetails($this->companyId);
  212.         // company plans limit setting
  213.         $this->parameters['companySettings'] = $companyDetails;
  214.         // dateTime formats for twig pages
  215.         $companyDetails['allDateTimeFormat'] = $this->ltsUtils->getDateTimeFormat(isset($companyDetails['datetimeFormat']) ? $companyDetails['datetimeFormat'] : null);
  216.         $companyDetails['allDateFormat'] = $this->ltsUtils->getDateFormat(isset($companyDetails['dateFormat']) ? $companyDetails['dateFormat'] : null);
  217.         $companyDetails['allTimeFormat'] = $this->ltsUtils->getTimeFormat(isset($companyDetails['timeFormat']) ? $companyDetails['timeFormat'] : null);
  218.         // inviduvidal company limitation
  219.         $this->parameters['c1'] = $companyDetails && !empty($companyDetails['c1']) ? $companyDetails['c1'] : 0;
  220.         $this->container->get('twig')->addGlobal('company'$companyDetails);
  221.     }
  222.     /**
  223.      * This function is to save last active time for each login session.
  224.      */
  225.     private function saveLastActiveTime($event)
  226.     {
  227.         $request $event->getRequest();
  228.         $params['routeName'] = $request->get('_route');
  229.         $params['loginUserId'] = $this->get('userId');
  230.         $params['company_id'] = $this->get('companyId');
  231.         if (isset($params['loginUserId']) && $this->em->isOpen()) {
  232.             $this->em->getRepository(LoginSessions::class)->update($params);
  233.         }
  234.     }
  235.     /**
  236.      * @param type $event
  237.      */
  238.     private function checkForPlanExpiry($event)
  239.     {
  240.         if (!empty($this->parameters['planExpireDate']) && !$this->parameters['isSuperAdmin']) {
  241.             if ($this->parameters['planExpireDate']->format('Y-m-d') <= date('Y-m-d') && 'free_trail' == $this->parameters['plan']) {
  242.                 $request $event->getRequest();
  243.                 $requestedUrl $request->getRequestUri();
  244.                 // If public demo accounts after expiring only this url will be allowed.
  245.                 $allowedURLs = ['/settings/accounts''/settings/get-account-settings-data''/settings/get-terminology-data''/settings/logo_upload''/settings/save-account-settings-data''/settings/save-terminology-data'];
  246.                 if (!in_array($requestedUrl$allowedURLs)) {
  247.                     $redirectUrl $this->router->generate('account-settings');
  248.                     $response = new RedirectResponse($redirectUrl);
  249.                     $event->setResponse($response);
  250.                 }
  251.                 $this->parameters['account_status'] = 'in-active';
  252.             }
  253.         }
  254.     }    
  255. }