src/EventSubscriber/GlobalVars.php line 76

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\Entity\Translation\BannerTranslation;
  24. use App\Repository\PageRepository;
  25. use Doctrine\ORM\EntityManagerInterface;
  26. use App\GlobalVars\Sett as GlobalVarsSett;
  27. use Symfony\Contracts\Cache\ItemInterface;
  28. use App\Entity\Translation\PageTranslation;
  29. use App\Entity\Translation\SettTranslation;
  30. use Symfony\Contracts\Cache\CacheInterface;
  31. use App\Entity\Translation\TimerTranslation;
  32. use Symfony\Component\HttpFoundation\Request;
  33. use Symfony\Component\Security\Core\Security;
  34. use App\GlobalVars\Labels as GlobalVarsLabels;
  35. use App\Entity\Translation\TemplateTranslation;
  36. use App\Service\Filter\Cats;
  37. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  38. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  39. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  40. class GlobalVars implements EventSubscriberInterface
  41. {
  42.     private $no_cache false;
  43.     
  44.     private Environment $twig;
  45.     private EntityManagerInterface $em;
  46.     private CacheInterface $Cache;
  47.     private Auth $Auth;
  48.     private Env $Env;
  49.     private Func $Func;
  50.     private Wishlist $Wishlist;
  51.     private ?AppDTO $app;
  52.     // Temp
  53.     private $_pagename;
  54.     public function __construct(AppDTO $appEnvironment $twigEntityManagerInterface $emTagAwareCacheInterface $appCacheAuth $AuthEnv $EnvFunc $FuncWishlist $WishlistSecurity $security, private Cats $Cats)
  55.     {
  56.         $this->twig $twig;
  57.         $this->em $em;
  58.         $this->app $app;
  59.         $this->Cache $appCache;
  60.         $this->Auth $Auth;
  61.         $this->Auth->setUser($security->getUser());
  62.         $this->Env $Env;
  63.         $this->Func $Func;
  64.         $this->Wishlist $Wishlist;
  65.     }
  66.     public function onKernelController(ControllerEvent $event): void
  67.     {
  68.         $this->setDefaultLocale($event->getRequest());
  69.         $this->setLang($event->getRequest());
  70.         $this->setEnv($event->getRequest());
  71.         $this->setOpt($event->getRequest());        
  72.         $this->setFunc($event->getRequest());
  73.         $this->setArgs($event->getRequest());
  74.         $this->setSett($event->getRequest());
  75.         $this->setLabels($event->getRequest());
  76.         $this->setBlocks($event->getRequest());
  77.         $this->setTemplates($event->getRequest());
  78.         $this->setTimer($event->getRequest());
  79.         $this->setCatmenu($event->getRequest());
  80.         $this->setCatmenuTree($event->getRequest());
  81.         $this->setTopMenu($event->getRequest());
  82.         $this->setMainMenu($event->getRequest());
  83.         $this->setFootMenu($event->getRequest());
  84.         $this->setPage($event->getRequest());
  85.         $this->setBc($event->getRequest());
  86.         $this->setServerVars($event->getRequest());
  87.         $this->setIsCallAvailable($event->getRequest());
  88.         $this->setSearchQueries($event->getRequest());        
  89.         $this->setAuth();
  90.         $this->setWishlistArray();
  91.     }
  92.     private function setDefaultLocale(Request $request)
  93.     {        
  94.         if (!$request->getLocale()) {
  95.             $request->setLocale($request->getDefaultLocale());
  96.         }
  97.     }
  98.     private function setLang(Request $request)
  99.     {
  100.         $this->app->lang $request->getLocale();
  101.     }
  102.     private function setEnv(Request $request)
  103.     {
  104.         $this->twig->addGlobal('env'$this->Env);
  105.     }
  106.     private function setOpt(Request $request)
  107.     {
  108.         if (Env::is_opt()) {
  109.             $this->em->getClassMetadata(EntityPage::class)->setPrimaryTable(['name' => 'opt_page']);
  110.             $this->em->getClassMetadata(PageTranslation::class)->setPrimaryTable(['name' => 'opt_page_translation']);
  111.             $this->em->getClassMetadata(Banner::class)->setPrimaryTable(['name' => 'opt_banner']);            
  112.             $this->em->getClassMetadata(BannerTranslation::class)->setPrimaryTable(['name' => 'opt_banner_translation']);
  113.             $this->em->getClassMetadata(Cart::class)->setPrimaryTable(['name' => 'opt_cart']);            
  114.             $this->em->getClassMetadata(Order::class)->setPrimaryTable(['name' => 'opt_order']);
  115.             $this->em->getClassMetadata(Sett::class)->setPrimaryTable(['name' => 'opt_sett']);
  116.             $this->em->getClassMetadata(SettTranslation::class)->setPrimaryTable(['name' => 'opt_sett_translation']);
  117.             $this->em->getClassMetadata(Template::class)->setPrimaryTable(['name' => 'opt_template']);
  118.             $this->em->getClassMetadata(TemplateTranslation::class)->setPrimaryTable(['name' => 'opt_template_translation']);
  119.             $this->em->getClassMetadata(Timer::class)->setPrimaryTable(['name' => 'opt_timer']);
  120.             $this->em->getClassMetadata(TimerTranslation::class)->setPrimaryTable(['name' => 'opt_timer_translation']);
  121.             $this->em->getClassMetadata(Transaction::class)->setPrimaryTable(['name' => 'opt_transaction']);
  122.             $this->em->getClassMetadata(User::class)->setPrimaryTable(['name' => 'opt_user']);
  123.         }
  124.     }
  125.     private function setFunc(Request $request)
  126.     {
  127.         $this->twig->addGlobal('func'$this->Func);
  128.     }
  129.     private function setArgs(Request $request)
  130.     {
  131.         $args preg_split("/\//"$request->getPathInfo(), -1PREG_SPLIT_NO_EMPTY);
  132.         if (in_array($request->getLocale(), $args)) {
  133.             array_shift($args);
  134.         }
  135.         $this->twig->addGlobal('args'$args);
  136.     }
  137.     private function setSett(Request $request)
  138.     {
  139.         if ($this->no_cache) {
  140.             $this->Cache->delete('sett_'.$request->getLocale());
  141.         }
  142.         
  143.         $sett $this->Cache->get('sett_'.$request->getLocale(), function(ItemInterface $item) {
  144.             $item->expiresAfter(3000000);
  145.             $item->tag(['sett']);
  146.             $Setts $this->em->getRepository(Sett::class);
  147.             $fromDb $Setts->findAll();
  148.             return $this->toArray($fromDb);
  149.         });
  150.         $sett_array = [];
  151.         foreach ($sett as $key => $value)
  152.         {
  153.             $sett_array[$key] = (string) $value;
  154.         }
  155.         
  156.         $this->app->sett = new GlobalVarsSett($sett_array);
  157.         $this->twig->addGlobal('sett'$this->app->sett);
  158.     }
  159.     private function setLabels(Request $request)
  160.     {
  161.         if ($this->no_cache) {
  162.             $this->Cache->delete('labels_'.$request->getLocale());
  163.         }
  164.         
  165.         $labels $this->Cache->get('labels_'.$request->getLocale(), function(ItemInterface $item) {
  166.             $Labels $this->em->getRepository(Labels::class);
  167.             $item->expiresAfter(3000000);
  168.             $item->tag(['labels']);
  169.             $fromDb $Labels->findAll();
  170.             return $this->toArray($fromDb);
  171.         });
  172.         $labels_array = [];
  173.         foreach ($labels as $key => $value)
  174.         {
  175.             $labels_array[$key] = (string) $value;
  176.         }
  177.         $this->app->labels = new GlobalVarsLabels($labels_array);
  178.         $this->twig->addGlobal('labels'$this->app->labels);
  179.     }
  180.     private function setBlocks(Request $request)
  181.     {
  182.         if ($this->no_cache) {
  183.             $this->Cache->delete('blocks_'.$request->getLocale());
  184.         }
  185.         
  186.         // $blocks = $this->Cache->get('blocks_'.$request->getLocale(), function(ItemInterface $item) {
  187.             // $item->expiresAfter(3000000);
  188.             // $item->tag(['block']);
  189.             $Blocks $this->em->getRepository(Block::class);
  190.             $fromDb $Blocks->findAll();
  191.             // return $this->toArray($fromDb);
  192.             $blocks $this->toArray($fromDb);
  193.         // });
  194.         $blocks_array = [];
  195.         foreach ($blocks as $key => $value)
  196.         {
  197.             $blocks_array[$key] = (string) $value;
  198.         }
  199.         
  200.         $this->app->blocks = new Blocks($blocks_array);
  201.         $this->twig->addGlobal('blocks'$this->app->blocks);
  202.     }
  203.     private function setTemplates(Request $request)
  204.     {
  205.         if ($this->no_cache) {
  206.             $this->Cache->delete('templates_'.$request->getLocale());
  207.         }
  208.         
  209.         $templates $this->Cache->get('templates_'.$request->getLocale(), function(ItemInterface $item) {
  210.             $item->expiresAfter(3000000);
  211.             $item->tag(['template']);
  212.             $Templates $this->em->getRepository(Template::class);
  213.             $fromDb $Templates->findAll();
  214.             return $this->toArray($fromDb);
  215.         });
  216.         $templates_array = [];
  217.         foreach ($templates as $key => $value)
  218.         {
  219.             $templates_array[$key] = (string) $value;
  220.         }
  221.         
  222.         $this->app->templates = new Templates($templates_array);
  223.         $this->twig->addGlobal('templates'$this->app->templates);
  224.     }
  225.     private function setTimer(Request $request)
  226.     {
  227.         $timer $this->Cache->get('timer_'.$request->getLocale(), function(ItemInterface $item) {
  228.             $item->expiresAfter(3000000);
  229.             $item->tag(['timer']);
  230.             $timers $this->em->createQueryBuilder()
  231.                 ->select('t')
  232.                 ->from(Timer::class, 't')
  233.                 ->where('t.visible = 1')
  234.                 ->andWhere('t.end > :now')
  235.                 ->setParameter('now'time())
  236.                 ->orderBy('t.end''ASC')
  237.                 ->getQuery()
  238.                 ->getResult();
  239.         
  240.             return isset($timers[0]) ? $timers[0] : null;
  241.         });
  242.         
  243.         $this->twig->addGlobal('timer'$timer);
  244.     }
  245.     private function setCatMenu(Request $request)
  246.     {
  247.         $catmenu $this->Cache->get('catmenu_'.$request->getLocale(), function (ItemInterface $item): array {            
  248.             $item->expiresAfter(3000000);
  249.             $item->tag(['cat']);
  250.             $Cats $this->em->getRepository(\App\Entity\Cat::class);
  251.             $menu $Cats->findBy(["visible" => 1"cat" => 0], ["prior" => "DESC"]);
  252.             foreach ($menu as $k => $cat) {
  253.                 $menu[$k]->setCont('');
  254.                 $menu[$k]->subcats $Cats->findBy(["visible" => 1"cat" => $cat->getId()], ["prior" => "DESC"]);
  255.                 if (count($menu[$k]->subcats)) {
  256.                     foreach ($menu[$k]->subcats as $kk => $subcat) {
  257.                         $menu[$k]->subcats[$kk]->setCont('');
  258.                         $menu[$k]->subcats[$kk]->subcats $Cats->findBy(["visible" => 1"cat" => $subcat->getId()], ["prior" => "DESC"]);                    
  259.                     }
  260.                 } else {
  261.                     unset($menu[$k]->subcats);
  262.                 }
  263.             }
  264.             return $menu;
  265.         });        
  266.         
  267.         $this->twig->addGlobal('catmenu'$catmenu);
  268.     }
  269.     private function setCatMenuTree(Request $request)
  270.     {
  271.         if (strstr($request->getPathInfo(), 'catalog')) {
  272.             return;
  273.         }
  274.         $cats $this->Cats->getCatMenu();
  275.         $catpages = [];
  276.         //TODO. Ð¡Ð´ÐµÐ»Ð°Ñ‚ÑŒ Ð²Ñ‹Ð±Ð¾Ñ€ÐºÑƒ catpages
  277.         // foreach ($cats as $k => $v) {            
  278.         //     foreach ($v as $kk => $subcat) {
  279.         //         $catpages[$subcat['id']] = $this->Catpages->findBy(["visible" => 1, "cat" => $subcat['id']], ["prior" => "DESC"]);
  280.         //         if (empty($catpages[$subcat['id']])) {
  281.         //             unset($catpages[$subcat['id']]);
  282.         //         }
  283.         //     }
  284.         // }
  285.         $catmenu $this->Cache->get('catmenu_'.$request->getLocale(), function (ItemInterface $item): array {            
  286.             $item->expiresAfter(3000000);
  287.             $item->tag(['cat']);
  288.             $Cats $this->em->getRepository(\App\Entity\Cat::class);
  289.             $menu $Cats->findBy(["visible" => 1"cat" => 0], ["prior" => "DESC"]);
  290.             foreach ($menu as $k => $cat) {
  291.                 $menu[$k]->setCont('');
  292.                 $menu[$k]->subcats $Cats->findBy(["visible" => 1"cat" => $cat->getId()], ["prior" => "DESC"]);
  293.                 if (count($menu[$k]->subcats)) {
  294.                     foreach ($menu[$k]->subcats as $kk => $subcat) {
  295.                         $menu[$k]->subcats[$kk]->setCont('');
  296.                         $menu[$k]->subcats[$kk]->subcats $Cats->findBy(["visible" => 1"cat" => $subcat->getId()], ["prior" => "DESC"]);                    
  297.                     }
  298.                 } else {
  299.                     unset($menu[$k]->subcats);
  300.                 }
  301.             }
  302.             return $menu;
  303.         });        
  304.         
  305.         $this->twig->addGlobal('speccatmenu_menu'$cats);
  306.         $this->twig->addGlobal('speccatmenu_spec''default');
  307.         $this->twig->addGlobal('speccatmenu_parent_id'0);
  308.         $this->twig->addGlobal('speccatmenu_cat_id'0);
  309.         $this->twig->addGlobal('speccatmenu_search''');
  310.     }
  311.     private function setTopMenu(Request $request)
  312.     {
  313.         $footmenu $this->Cache->get('topmenu_'.$request->getLocale(), function (ItemInterface $item): array {            
  314.             $item->expiresAfter(3000000);
  315.             $item->tag(['page']);
  316.             $Pages $this->em->getRepository(EntityPage::class);
  317.             $menu $Pages->findBy(["type" => "page""visible" => 1"page" => 0], ["prior" => "DESC"]);
  318.             return $menu;
  319.         });
  320.         
  321.         $this->twig->addGlobal('topmenu'$footmenu);
  322.     }
  323.     private function setMainMenu(Request $request)
  324.     {
  325.         $menu $this->Cache->get('mainmenu_'.$request->getLocale(), function (ItemInterface $item): array {            
  326.             $item->expiresAfter(3000000);
  327.             $item->tag(['page']);
  328.             $Pages $this->em->getRepository(EntityPage::class);
  329.             $menu $Pages->findBy(["type" => "page""visible" => 1"page" => 0], ["prior" => "DESC"]);
  330.             return $menu;
  331.         });
  332.         
  333.         $this->twig->addGlobal('mainmenu'$menu);
  334.     }
  335.     private function setFootMenu(Request $request)
  336.     {
  337.         $menu $this->Cache->get('footmenu_'.$request->getLocale(), function (ItemInterface $item): array {            
  338.             $item->expiresAfter(3000000);
  339.             $item->tag(['page']);
  340.             $Pages $this->em->getRepository(EntityPage::class);
  341.             $menu $Pages->findBy(["type" => "page""visible" => 1"page" => 0], ["prior" => "DESC"]);
  342.             return $menu;
  343.         });
  344.         
  345.         $this->twig->addGlobal('footmenu'$menu);
  346.     }
  347.     private function setPage(Request $request)
  348.     {
  349.         $this->app->page = new PageDTO();
  350.         $this->_pagename $this->pageName($request->getLocale(), $request->getPathInfo());
  351.         $pageFromCache $this->Cache->get('page_'.$this->_pagename.'_'.$request->getLocale(), function(ItemInterface $item) {
  352.             $intname trim(str_replace("_""/"$this->_pagename), "/");            
  353.             $Pages $this->em->getRepository(EntityPage::class);
  354.             $item->expiresAfter(3000000);
  355.             $item->tag(['page']);
  356.             $pageFromDb $Pages->findOneBy(['intname' => $intname]);
  357.             
  358.             return $pageFromDb;
  359.         });
  360.         if ($pageFromCache) {
  361.             $this->app->page->intname $pageFromCache->getIntname();
  362.             $this->app->page->title $pageFromCache->getTitle();
  363.             $this->app->page->descr $pageFromCache->getDescr();
  364.             $this->app->page->kw $pageFromCache->getKw();
  365.             $this->app->page->h1 $pageFromCache->getH1();
  366.             $this->app->page->cont $pageFromCache->getCont();
  367.         }
  368.         
  369.         $this->twig->addGlobal('page'$this->app->page);
  370.     }
  371.     private function setBc(Request $request)
  372.     {
  373.         $this->app->page->bc = ['/'.$request->getLocale().'/' => $this->app->labels->get('home')];
  374.         $this->twig->addGlobal('bc'$this->app->page->bc);
  375.     }
  376.     private function setServerVars(Request $request)
  377.     {
  378.         $this->app->host $_SERVER['HTTP_HOST'];
  379.         $this->twig->addGlobal('host'$_SERVER['HTTP_HOST']);
  380.         $this->twig->addGlobal('request_uri'$_SERVER['REQUEST_URI']);        
  381.     }
  382.     private function setIsCallAvailable(Request $request)
  383.     {
  384.         $isCallAvailable 1;
  385.         if ($this->app->labels->get('tel-timezone')) {
  386.             //date_default_timezone_set($this->app->labels->get('tel-timezone'));
  387.         }
  388.         
  389.         if((date('H') < (int)$this->app->labels->get('tel-start')) || (date('H') >= (int) $this->app->labels->get('tel-end'))) {
  390.             $isCallAvailable 0;
  391.         }
  392.         $this->twig->addGlobal('isCallAvailable'$isCallAvailable);
  393.     }
  394.     private function setSearchQueries(Request $request)
  395.     {
  396.         $queries json_decode($this->app->labels->get('search_queries_'.Env::site()), true);
  397.         $rqueries = [];
  398.         if(is_array($queries)) {
  399.             $c count($queries);
  400.             if ($c 8$c 8;
  401.             $rqueries array_rand($queries$c);
  402.         }
  403.         $this->twig->addGlobal('search_queries'$queries);
  404.         $this->twig->addGlobal('search_queries_rand'$rqueries);
  405.     }
  406.     private function setAuth()
  407.     {
  408.         $this->twig->addGlobal('Auth'$this->Auth);
  409.     }
  410.     private function setWishlistArray()
  411.     {
  412.         $ids $this->Wishlist->getProdIds($this->Auth->userid());
  413.         
  414.         $this->twig->addGlobal('wishlistids'$ids);
  415.     }
  416.     private function toArray(array $entities): array
  417.     {
  418.         $arr = [];
  419.         foreach ($entities as $k => $v) {
  420.             $arr[$v->getIntname()] = $v->getValue();
  421.         }
  422.         return $arr;
  423.     }
  424.     private function pageName(string $localestring $pathInfo): string
  425.     {
  426.         $name str_replace($locale."/"''$pathInfo);
  427.         return str_replace("/""_"$name);
  428.     }
  429.     public static function getSubscribedEvents(): array
  430.     {
  431.         return [
  432.             'kernel.controller' => 'onKernelController',
  433.         ];
  434.     }
  435. }