src/Service/TimezoneListener.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\DependencyInjection\ContainerInterface
  5. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Twig\Environment;
  8. class TimezoneListener
  9. {   
  10.     protected $tokenStorage;
  11.     protected $container;
  12.     private $twig;
  13.     
  14.     public function __construct(TokenStorageInterface $tokenStorageEnvironment $twigContainerInterface $container
  15.     {
  16.        $this->tokenStorage $tokenStorage;
  17.        $this->container $container;
  18.        $this->twig $twig;
  19.     }
  20.     /**
  21.      *
  22.      * @param RequestEvent $event
  23.      */
  24.     public function onKernelRequest(RequestEvent $event) {
  25.         
  26.         if (!$event->isMainRequest()) {
  27.             // don't do anything if it's not the master request
  28.             return;
  29.         }
  30.         
  31.         $token $this->tokenStorage->getToken();
  32.         // set time zone
  33.         if (!defined('_USER_TIMEZONE_')) {
  34.             
  35.             $hasTimezone 0;
  36.             if ($token != null && (($user $token->getUser()) instanceof \App\Entity\User)) {
  37.                 //echo 'fine';exit;
  38.                 $companyTimezone $this->getUserTimeZone();
  39.                 $hasTimezone 1;
  40.             }
  41.             
  42.             if($hasTimezone == 1)
  43.                 define('_USER_TIMEZONE_'$companyTimezone['timezone']);
  44.             else
  45.                 define('_USER_TIMEZONE_'$this->container->getParameter('default_timezone'));
  46.         }
  47.         date_default_timezone_set(_USER_TIMEZONE_);
  48.         
  49.         $this->twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone(_USER_TIMEZONE_);
  50.     }
  51.     public function getUserTimeZone($crmId NULL)
  52.     {
  53.         $em $this->container->get('doctrine')->getManager();
  54.         if ($crmId == NULL) {
  55.             $user $this->tokenStorage->getToken()->getUser();
  56.             $crmId $user->getActiveCrm();
  57.         }
  58.         
  59.         $userCompany $em->getRepository(\App\Entity\UserCrms::class)->find($crmId);
  60.         if ($userCompany instanceof \App\Entity\UserCrms) {
  61.             
  62.             $timeZone $em->getRepository(\App\Entity\UserTimezone::class)->find($userCompany->getTimezoneId());
  63.             if ($timeZone instanceof \App\Entity\UserTimezone) {
  64.                 $arr = array(
  65.                     'timezone' => $timeZone->getTimezone(),
  66.                     'offset' => '(' $timeZone->getUtcOffset() . ')',
  67.                     'title' => $timeZone->getTitle()
  68.                 );
  69.                 return $arr;
  70.             }
  71.         }
  72.         $arr = array(
  73.             'timezone' => 'Australia/Sydney',
  74.             'offset' => '(UTC/GMT +11:00)',
  75.             'title' => 'Australia/Sydney'
  76.         );
  77.         return $arr;
  78.     }
  79.     
  80. }