<?php
namespace App\EventListener;
use App\Entity\Company;
use App\Entity\LoginSessions;
use App\Entity\Terminology;
use App\Entity\UserProfile;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\EntityManagerInterface;
//use App\Utils\TopNavbar;
use App\Utils\LtsUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
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;
private $em;
/**
* Constructor
*
* @param Container $container
*/
public function __construct(ContainerInterface $container, Security $security, EntityManagerInterface $entityManager, LtsUtils $ltsUtils, TranslatorInterface $translator, RouterInterface $router)
{
$this->container = $container;
$this->security = $security;
$this->em = $entityManager;
// $this->topNavbar = $topNavBar;
$this->ltsUtils = $ltsUtils;
$this->translator = $translator;
$this->router = $router;
}
/**
* Routing Listener event
* @param RequestEvent $event
*/
public function onKernelRequest(RequestEvent $event)
{
$user = $this->security->getUser();
// Set default data
$this->setDefaultParams();
// Set UTC timezone
$this->setTimeZone();
if ($user) {
$this->setUserData($user);
$this->translator->setLocale($this->parameters['lang']);
// Set container variables
$this->container->set('contact', $this);
}
}
/**
* This function is to get contact data from contact object
* @param string $key
* @return string
*/
public function get($key)
{
return $this->parameters[$key];
}
/**
* Method setDefaultParams
* This function is to set default contact data from contact object
* @return void
*/
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['isMainAdmin'] = '';
$this->parameters['profilePic'] = '';
$this->parameters['defaultDateFormat'] = 'd-m-Y h:i:a';
$this->parameters['defaultSystemLang'] = 'en-UK';
$this->parameters['planExpireDate'] = NULL;
$this->parameters['plan'] = '';
$this->parameters['account_status'] = 'active';
}
/**
* This function is to set contact details in contact object
* @param object $user
*/
public function setUserData($user)
{
$userDetails = $this->em->getRepository(UserProfile::class)->getUserFullData($user->getId());
if ($userDetails) {
$this->parameters['id'] = $userDetails['id'];
$this->parameters['userId'] = $userDetails['id'];
$this->parameters['firstName'] = $userDetails['firstName'];
$this->parameters['email'] = $userDetails['email'];
$this->parameters['surName'] = $userDetails['lastName'];
$this->parameters['userProfileImage'] = $userDetails['profileImage'];
$this->parameters['userProfileImageUrl'] = !empty($userDetails['profileImage']) ? $this->ltsUtils->getUploadPath('profile_pic', '', $userDetails['profileImage']) : '';
$this->parameters['fullName'] = $userDetails['fullName'];
$this->parameters['lang'] = !empty($userDetails['systemLanguage']) && $userDetails['systemLanguage'] == 'en-US' ? $userDetails['systemLanguage'] : 'en-UK';
$this->parameters['ltsLogo'] = 'lts.png';
}
}
/**
* Set Time zone
*/
public function setTimeZone()
{
date_default_timezone_set('UTC');
}
/**
*
* @param type $event
* @param type $user
*/
private function setTopNavLinks($event, $user)
{
$request = $event->getRequest();
$resArray = $this->topNavbar->getData($request, $user);
if ($resArray) {
$this->container->get('twig')->addGlobal('topnav_data', $resArray['topnavData']);
$this->container->get('twig')->addGlobal('pagetitle_data', $resArray['pageTitleBlock']);
}
}
/**
*
*/
private function setDemoTourData()
{
$this->container->get('twig')->addGlobal('demo_tour_data', $this->ltsUtils->getDemoTourArray());
}
/**
* This function is to set contact details in twig object
*/
private function setTwigVariables()
{
$this->container->get('twig')->addGlobal('fr_contact', $this->parameters);
$this->container->get('twig')->addGlobal('ltApp', $this->parameters);
}
/**
* This function is to set contact details in twig object
*/
private function setCompanyDetails()
{
$this->parameters['defaultDateFormat'] = 'd-m-Y h:i:a';
$this->parameters['defaultDate'] = 'd-m-Y';
$this->parameters['defaultTime'] = 'h:i:a';
$this->parameters['defaultSystemLang'] = 'en';
$this->parameters['defaultTimezone'] = 'UTC';
$companyDetails = $this->em->getRepository(Terminology::class)->getCompanyDetails($this->companyId);
$companyDetails['timezone'] = !empty($companyDetails['timezone']) ? $companyDetails['timezone'] : '';
$this->parameters['lang'] = $companyDetails && !empty($companyDetails['systemLanguage']) ? $companyDetails['systemLanguage'] : 'en';
$this->parameters['timezone'] = isset($companyDetails['timezone']) ? $companyDetails['timezone'] : '';
$this->parameters['userRefidPrefix'] = !empty($companyDetails['userRefidPrefix']) ? $companyDetails['userRefidPrefix'] : '';
$this->parameters['cardRefidPrefix'] = !empty($companyDetails['hasCardRefId']) && !empty($companyDetails['cardRefidPrefix']) ? $companyDetails['cardRefidPrefix'] : '';
$this->parameters['cardRefidStartsFrom'] = !empty($companyDetails['cardRefidStartsFrom']) ? $companyDetails['cardRefidStartsFrom'] : '1';
$this->parameters['dataTimeFormat'] = $this->ltsUtils->getDateTimeFormat(isset($companyDetails['datetimeFormat']) ? $companyDetails['datetimeFormat'] : null);
$this->parameters['dataFormat'] = $this->ltsUtils->getDateFormat(isset($companyDetails['dateFormat']) ? $companyDetails['dateFormat'] : null);
$this->parameters['timeFormat'] = $this->ltsUtils->getTimeFormat(isset($companyDetails['timeFormat']) ? $companyDetails['timeFormat'] : null);
$this->parameters['companyTerminology'] = $this->ltsUtils->getTerminologyDetails($this->companyId);
# company plans limit setting
$this->parameters['companySettings'] = $companyDetails;
//dateTime formats for twig pages
$companyDetails['allDateTimeFormat'] = $this->ltsUtils->getDateTimeFormat(isset($companyDetails['datetimeFormat']) ? $companyDetails['datetimeFormat'] : null);
$companyDetails['allDateFormat'] = $this->ltsUtils->getDateFormat(isset($companyDetails['dateFormat']) ? $companyDetails['dateFormat'] : null);
$companyDetails['allTimeFormat'] = $this->ltsUtils->getTimeFormat(isset($companyDetails['timeFormat']) ? $companyDetails['timeFormat'] : null);
# inviduvidal company limitation
$this->parameters['c1'] = $companyDetails && !empty($companyDetails['c1']) ? $companyDetails['c1'] : 0;
$this->container->get('twig')->addGlobal('company', $companyDetails);
}
/**
* This function is to save last active time for each login session.
*/
private function saveLastActiveTime($event)
{
$request = $event->getRequest();
$params['routeName'] = $request->get('_route');
$params['loginUserId'] = $this->get('userId');
$params['company_id'] = $this->get('companyId');
if (isset($params['loginUserId']) && $this->em->isOpen()) {
$this->em->getRepository(LoginSessions::class)->update($params);
}
}
/**
*
* @param type $event
*/
private function checkForPlanExpiry($event)
{
if (!empty($this->parameters['planExpireDate']) && !$this->parameters['isSuperAdmin']) {
if ($this->parameters['planExpireDate']->format('Y-m-d') <= date('Y-m-d') && $this->parameters['plan'] == 'free_trail') {
$request = $event->getRequest();
$requestedUrl = $request->getRequestUri();
// If public demo accounts after expiring only this url will be allowed.
$allowedURLs = ['/settings/accounts', '/settings/get-account-settings-data', '/settings/get-terminology-data', '/settings/logo_upload', '/settings/save-account-settings-data', '/settings/save-terminology-data'];
if (!in_array($requestedUrl, $allowedURLs)) {
$redirectUrl = $this->router->generate('account-settings');
$response = new RedirectResponse($redirectUrl);
$event->setResponse($response);
}
$this->parameters['account_status'] = 'in-active';
}
}
}
}