src/EventSubscriber/GlobalVars.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Env;
  4. use App\Func;
  5. use App\DTO\AppDTO;
  6. use App\DTO\PageDTO;
  7. use App\Entity\Cart;
  8. use App\Entity\Sett;
  9. use App\Entity\User;
  10. use App\Entity\Block;
  11. use App\Entity\Order;
  12. use App\Entity\Timer;
  13. use Twig\Environment;
  14. use App\Entity\Banner;
  15. use App\Entity\Labels;
  16. use App\Model\Wishlist;
  17. use App\Entity\Template;
  18. use App\GlobalVars\Blocks;
  19. use App\Service\Auth\Auth;
  20. use App\Entity\Transaction;
  21. use App\GlobalVars\Templates;
  22. use App\Entity\Page as EntityPage;
  23. use App\Repository\PageRepository;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use App\GlobalVars\Sett as GlobalVarsSett;
  26. use Symfony\Contracts\Cache\ItemInterface;
  27. use App\Entity\Translation\PageTranslation;
  28. use App\Entity\Translation\SettTranslation;
  29. use Symfony\Contracts\Cache\CacheInterface;
  30. use App\Entity\Translation\TimerTranslation;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\Security\Core\Security;
  33. use App\GlobalVars\Labels as GlobalVarsLabels;
  34. use App\Entity\Translation\TemplateTranslation;
  35. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  36. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  37. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  38. class GlobalVars implements EventSubscriberInterface
  39. {
  40.     private $no_cache false;
  41.     
  42.     private Environment $twig;
  43.     private EntityManagerInterface $em;
  44.     private CacheInterface $Cache;
  45.     private Auth $Auth;
  46.     private Env $Env;
  47.     private Func $Func;
  48.     private Wishlist $Wishlist;
  49.     private ?AppDTO $app;
  50.     // Temp
  51.     private $_pagename;
  52.     public function __construct(AppDTO $appEnvironment $twigEntityManagerInterface $emTagAwareCacheInterface $appCacheAuth $AuthEnv $EnvFunc $FuncWishlist $WishlistSecurity $security)
  53.     {
  54.         $this->twig $twig;
  55.         $this->em $em;
  56.         $this->app $app;
  57.         $this->Cache $appCache;
  58.         $this->Auth $Auth;
  59.         $this->Auth->setUser($security->getUser());
  60.         $this->Env $Env;
  61.         $this->Func $Func;
  62.         $this->Wishlist $Wishlist;
  63.     }
  64.     public function onKernelController(ControllerEvent $event): void
  65.     {
  66.         $this->setDefaultLocale($event->getRequest());
  67.         $this->setLang($event->getRequest());
  68.         $this->setEnv($event->getRequest());
  69.         $this->setOpt($event->getRequest());        
  70.         $this->setFunc($event->getRequest());
  71.         $this->setArgs($event->getRequest());
  72.         $this->setSett($event->getRequest());
  73.         $this->setLabels($event->getRequest());
  74.         $this->setBlocks($event->getRequest());
  75.         $this->setTemplates($event->getRequest());
  76.         $this->setPage($event->getRequest());
  77.         $this->setBc($event->getRequest());
  78.         $this->setServerVars($event->getRequest());
  79.         $this->setIsCallAvailable($event->getRequest());
  80.         $this->setSearchQueries($event->getRequest());        
  81.         $this->setAuth();
  82.         $this->setWishlistArray();
  83.     }
  84.     private function setDefaultLocale(Request $request)
  85.     {        
  86.         if (!$request->getLocale()) {
  87.             $request->setLocale($request->getDefaultLocale());
  88.         }
  89.     }
  90.     private function setLang(Request $request)
  91.     {
  92.         $this->app->lang $request->getLocale();
  93.     }
  94.     private function setEnv(Request $request)
  95.     {
  96.         $this->twig->addGlobal('env'$this->Env);
  97.     }
  98.     private function setOpt(Request $request)
  99.     {
  100.         if (Env::is_opt()) {
  101.             $this->em->getClassMetadata(EntityPage::class)->setPrimaryTable(['name' => 'opt_page']);
  102.             $this->em->getClassMetadata(PageTranslation::class)->setPrimaryTable(['name' => 'opt_page_translation']);
  103.             $this->em->getClassMetadata(Banner::class)->setPrimaryTable(['name' => 'opt_banner']);            
  104.             $this->em->getClassMetadata(Cart::class)->setPrimaryTable(['name' => 'opt_cart']);            
  105.             $this->em->getClassMetadata(Order::class)->setPrimaryTable(['name' => 'opt_order']);
  106.             $this->em->getClassMetadata(Sett::class)->setPrimaryTable(['name' => 'opt_sett']);
  107.             $this->em->getClassMetadata(SettTranslation::class)->setPrimaryTable(['name' => 'opt_sett_translation']);
  108.             $this->em->getClassMetadata(Template::class)->setPrimaryTable(['name' => 'opt_template']);
  109.             $this->em->getClassMetadata(TemplateTranslation::class)->setPrimaryTable(['name' => 'opt_template_translation']);
  110.             $this->em->getClassMetadata(Timer::class)->setPrimaryTable(['name' => 'opt_timer']);
  111.             $this->em->getClassMetadata(TimerTranslation::class)->setPrimaryTable(['name' => 'opt_timer_translation']);
  112.             $this->em->getClassMetadata(Transaction::class)->setPrimaryTable(['name' => 'opt_transaction']);
  113.             $this->em->getClassMetadata(User::class)->setPrimaryTable(['name' => 'opt_user']);
  114.         }
  115.     }
  116.     private function setFunc(Request $request)
  117.     {
  118.         $this->twig->addGlobal('func'$this->Func);
  119.     }
  120.     private function setArgs(Request $request)
  121.     {
  122.         $args preg_split("/\//"$request->getPathInfo(), -1PREG_SPLIT_NO_EMPTY);
  123.         if (in_array($request->getLocale(), $args)) {
  124.             array_shift($args);
  125.         }
  126.         $this->twig->addGlobal('args'$args);
  127.     }
  128.     private function setSett(Request $request)
  129.     {
  130.         if ($this->no_cache) {
  131.             $this->Cache->delete('sett_'.$request->getLocale());
  132.         }
  133.         
  134.         $sett $this->Cache->get('sett_'.$request->getLocale(), function(ItemInterface $item) {
  135.             $item->expiresAfter(3000000);
  136.             $item->tag(['sett']);
  137.             $Setts $this->em->getRepository(Sett::class);
  138.             $fromDb $Setts->findAll();
  139.             return $this->toArray($fromDb);
  140.         });
  141.         $sett_array = [];
  142.         foreach ($sett as $key => $value)
  143.         {
  144.             $sett_array[$key] = (string) $value;
  145.         }
  146.         
  147.         $this->app->sett = new GlobalVarsSett($sett_array);
  148.         $this->twig->addGlobal('sett'$this->app->sett);
  149.     }
  150.     private function setLabels(Request $request)
  151.     {
  152.         if ($this->no_cache) {
  153.             $this->Cache->delete('labels_'.$request->getLocale());
  154.         }
  155.         
  156.         $labels $this->Cache->get('labels_'.$request->getLocale(), function(ItemInterface $item) {
  157.             $Labels $this->em->getRepository(Labels::class);
  158.             $item->expiresAfter(3000000);
  159.             $item->tag(['labels']);
  160.             $fromDb $Labels->findAll();
  161.             return $this->toArray($fromDb);
  162.         });
  163.         $labels_array = [];
  164.         foreach ($labels as $key => $value)
  165.         {
  166.             $labels_array[$key] = (string) $value;
  167.         }
  168.         $this->app->labels = new GlobalVarsLabels($labels_array);
  169.         $this->twig->addGlobal('labels'$this->app->labels);
  170.     }
  171.     private function setBlocks(Request $request)
  172.     {
  173.         if ($this->no_cache) {
  174.             $this->Cache->delete('blocks_'.$request->getLocale());
  175.         }
  176.         
  177.         $blocks $this->Cache->get('blocks_'.$request->getLocale(), function(ItemInterface $item) {
  178.             $item->expiresAfter(3000000);
  179.             $item->tag(['block']);
  180.             $Blocks $this->em->getRepository(Block::class);
  181.             $fromDb $Blocks->findAll();
  182.             return $this->toArray($fromDb);
  183.         });
  184.         $blocks_array = [];
  185.         foreach ($blocks as $key => $value)
  186.         {
  187.             $blocks_array[$key] = (string) $value;
  188.         }
  189.         
  190.         $this->app->blocks = new Blocks($blocks_array);
  191.         $this->twig->addGlobal('blocks'$this->app->blocks);
  192.     }
  193.     private function setTemplates(Request $request)
  194.     {
  195.         if ($this->no_cache) {
  196.             $this->Cache->delete('templates_'.$request->getLocale());
  197.         }
  198.         
  199.         $templates $this->Cache->get('templates_'.$request->getLocale(), function(ItemInterface $item) {
  200.             $item->expiresAfter(3000000);
  201.             $item->tag(['template']);
  202.             $Templates $this->em->getRepository(Template::class);
  203.             $fromDb $Templates->findAll();
  204.             return $this->toArray($fromDb);
  205.         });
  206.         $templates_array = [];
  207.         foreach ($templates as $key => $value)
  208.         {
  209.             $templates_array[$key] = (string) $value;
  210.         }
  211.         
  212.         $this->app->templates = new Templates($templates_array);
  213.         $this->twig->addGlobal('templates'$this->app->templates);
  214.     }
  215.     private function setPage(Request $request)
  216.     {
  217.         $this->app->page = new PageDTO();
  218.         $this->_pagename $this->pageName($request->getLocale(), $request->getPathInfo());
  219.         $pageFromCache $this->Cache->get('page_'.$this->_pagename.'_'.$request->getLocale(), function(ItemInterface $item) {
  220.             $intname trim(str_replace("_""/"$this->_pagename), "/");            
  221.             $Pages $this->em->getRepository(EntityPage::class);
  222.             $item->expiresAfter(3000000);
  223.             $item->tag(['page']);
  224.             $pageFromDb $Pages->findOneBy(['intname' => $intname]);
  225.             
  226.             return $pageFromDb;
  227.         });
  228.         if ($pageFromCache) {
  229.             $this->app->page->intname $pageFromCache->getIntname();
  230.             $this->app->page->title $pageFromCache->getTitle();
  231.             $this->app->page->descr $pageFromCache->getDescr();
  232.             $this->app->page->kw $pageFromCache->getKw();
  233.             $this->app->page->h1 $pageFromCache->getH1();
  234.             $this->app->page->cont $pageFromCache->getCont();
  235.         }
  236.         
  237.         $this->twig->addGlobal('page'$this->app->page);
  238.     }
  239.     private function setBc(Request $request)
  240.     {
  241.         $this->app->page->bc = ['/'.$request->getLocale().'/' => $this->app->labels->get('home')];
  242.         $this->twig->addGlobal('bc'$this->app->page->bc);
  243.     }
  244.     private function setServerVars(Request $request)
  245.     {
  246.         $this->app->host $_SERVER['HTTP_HOST'];
  247.         $this->twig->addGlobal('host'$_SERVER['HTTP_HOST']);
  248.         $this->twig->addGlobal('request_uri'$_SERVER['REQUEST_URI']);        
  249.     }
  250.     private function setIsCallAvailable(Request $request)
  251.     {
  252.         $isCallAvailable 1;
  253.         if ($this->app->labels->get('tel-timezone')) {
  254.             //date_default_timezone_set($this->app->labels->get('tel-timezone'));
  255.         }
  256.         
  257.         if((date('H') < (int)$this->app->labels->get('tel-start')) || (date('H') >= (int) $this->app->labels->get('tel-end'))) {
  258.             $isCallAvailable 0;
  259.         }
  260.         $this->twig->addGlobal('isCallAvailable'$isCallAvailable);
  261.     }
  262.     private function setSearchQueries(Request $request)
  263.     {
  264.         $queries json_decode($this->app->labels->get('search_queries_'.Env::site()), true);
  265.         $rqueries = [];
  266.         if(is_array($queries)) {
  267.             $c count($queries);
  268.             if ($c 8$c 8;
  269.             $rqueries array_rand($queries$c);
  270.         }
  271.         $this->twig->addGlobal('search_queries'$queries);
  272.         $this->twig->addGlobal('search_queries_rand'$rqueries);
  273.     }
  274.     private function setAuth()
  275.     {
  276.         $this->twig->addGlobal('Auth'$this->Auth);
  277.     }
  278.     private function setWishlistArray()
  279.     {
  280.         $ids $this->Wishlist->getProdIds($this->Auth->userid());
  281.         
  282.         $this->twig->addGlobal('wishlistids'$ids);
  283.     }
  284.     private function toArray(array $entities): array
  285.     {
  286.         $arr = [];
  287.         foreach ($entities as $k => $v) {
  288.             $arr[$v->getIntname()] = $v->getValue();
  289.         }
  290.         return $arr;
  291.     }
  292.     private function pageName(string $localestring $pathInfo): string
  293.     {
  294.         $name str_replace($locale."/"''$pathInfo);
  295.         return str_replace("/""_"$name);
  296.     }
  297.     public static function getSubscribedEvents(): array
  298.     {
  299.         return [
  300.             'kernel.controller' => 'onKernelController',
  301.         ];
  302.     }
  303. }