src/Controller/Block/BlockController.php line 277

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Block;
  3. use App\Env;
  4. use App\DTO\AppDTO;
  5. use App\ValueObject\CacheTag;
  6. use App\Entity\Cat;
  7. use App\Entity\Char;
  8. use App\Entity\Page;
  9. use App\Entity\Timer;
  10. use App\Entity\Catpage;
  11. use App\Entity\Charval;
  12. use RecursiveArrayIterator;
  13. use App\Service\Cache\Cache;
  14. use App\Service\Filter\Cats;
  15. use App\Service\Filter\Chars;
  16. use App\Service\Filter\Prods;
  17. use App\Service\Filter\Filter;
  18. use RecursiveIteratorIterator;
  19. use App\Model\Cart as ModelCart;
  20. use App\Model\Prod as ModelProd;
  21. use App\Repository\CatRepository;
  22. use App\Repository\CharRepository;
  23. use App\Repository\PageRepository;
  24. use App\Repository\TimerRepository;
  25. use App\Repository\CatpageRepository;
  26. use App\Repository\CharvalRepository;
  27. use Doctrine\ORM\EntityManagerInterface;
  28. use Doctrine\ORM\Query\ResultSetMapping;
  29. use Symfony\Contracts\Cache\ItemInterface;
  30. use Symfony\Contracts\Cache\CacheInterface;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpFoundation\Response;
  33. use Symfony\Component\Routing\Annotation\Route;
  34. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  35. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  36. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  37. class BlockController extends AbstractController
  38. {
  39.     protected EntityManagerInterface $em;
  40.     protected AppDTO $app;
  41.     protected ModelProd $ModelProd;
  42.     protected ModelCart $ModelCart;
  43.     //Repositories
  44.     protected PageRepository $Pages;
  45.     protected CatRepository $Cats;
  46.     protected CatpageRepository $Catpages;
  47.     protected TimerRepository $Timers;
  48.     protected CharRepository $Chars;
  49.     protected CharvalRepository $Charvals;
  50.     public function __construct(
  51.         EntityManagerInterface $em
  52.         private TagAwareCacheInterface $appCache
  53.         AppDTO $app
  54.         ModelProd $ModelProd
  55.         ModelCart $ModelCart
  56.     )
  57.     {
  58.         $this->em $em;
  59.         $this->app $app;
  60.         $this->ModelProd $ModelProd;
  61.         $this->ModelCart $ModelCart;
  62.         $this->Pages $em->getRepository(Page::class);
  63.         $this->Cats $em->getRepository(Cat::class);
  64.         $this->Catpages $em->getRepository(Catpage::class);
  65.         $this->Timers $em->getRepository(Timer::class);
  66.         $this->Chars $em->getRepository(Char::class);
  67.         $this->Charvals $em->getRepository(Charval::class);
  68.     }
  69.     
  70.     #[Route('/{_locale}/block/topmenu/{intname}'name'block_top_menu'requirements: ['_locale' => '%app.langs%'])]
  71.     public function menuBlock(string $intnameRequest $request): Response
  72.     {
  73.         $menu $this->appCache->get('topmenu_'.$request->getLocale(), function (ItemInterface $item): array {            
  74.             $item->tag([new CacheTag(Page::class)]);
  75.             $menu $this->Pages->findBy(["type" => "page""visible" => 1"page" => 0], ["prior" => "DESC"]);
  76.             return $menu;
  77.         });
  78.         $response $this->render('block/topmenu.html.twig', [
  79.             'menus' => $menu,
  80.             'intname' => $intname,
  81.         ]);
  82.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  83.         return $response;
  84.     }
  85.     #[Route('/{_locale}/block/footmenu'name'block_foot_menu'requirements: ['_locale' => '%app.langs%'])]
  86.     public function footMenuBlock(Request $request): Response
  87.     {
  88.         $menu $this->appCache->get('topmenu_'.$request->getLocale(), function (ItemInterface $item): array {            
  89.             $item->tag([new CacheTag(Page::class)]);
  90.             $menu $this->Pages->findBy(["type" => "page""visible" => 1"page" => 0], ["prior" => "DESC"]);
  91.             return $menu;
  92.         });
  93.         $response $this->render('block/footmenu.html.twig', [
  94.             'menus' => $menu,
  95.         ]);
  96.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  97.         return $response;
  98.     }
  99.     #[Route('/{_locale}/block/catmenu/{template}'name'block_catmenu'requirements: ['_locale' => '%app.langs%'])]
  100.     public function catMenuBlock(string $templateRequest $request): Response
  101.     {
  102.         $menu $this->appCache->get('catmenu_'.$request->getLocale(), function (ItemInterface $item): array {            
  103.             $item->tag([new CacheTag(Page::class)]);
  104.             $menu $this->Cats->findBy(["visible" => 1"cat" => 0], ["prior" => "DESC"]);
  105.             foreach ($menu as $k => $cat) {
  106.                 $menu[$k]->setCont('');
  107.                 $menu[$k]->subcats $this->Cats->findBy(["visible" => 1"cat" => $cat->getId()], ["prior" => "DESC"]);
  108.                 if (count($menu[$k]->subcats)) {
  109.                     foreach ($menu[$k]->subcats as $kk => $subcat) {
  110.                         $menu[$k]->subcats[$kk]->setCont('');
  111.                         $menu[$k]->subcats[$kk]->subcats $this->Cats->findBy(["visible" => 1"cat" => $subcat->getId()], ["prior" => "DESC"]);                    
  112.                     }
  113.                 } else {
  114.                     unset($menu[$k]->subcats);
  115.                 }
  116.             }
  117.             return $menu;
  118.         });
  119.         
  120.         $response $this->render('block/'.$template.'.html.twig', [
  121.             'menu' => $menu,
  122.         ]);
  123.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  124.         return $response;
  125.     }
  126.     #[Route('/{_locale}/block/speccatmenu/{spec}/{cat_cat}/{cat_id}/{search}'name'block_catmenu_spec'requirements: ['_locale' => '%app.langs%'])]
  127.     public function specCatMenuBlock(string $specint $cat_catint $cat_idstring $searchCats $Cats): Response
  128.     {           
  129.         $cats $Cats->getSpecCatMenu($cat_id$spec$search);
  130.         $catpages = [];
  131.         foreach ($cats as $k => $v) {            
  132.             foreach ($v as $kk => $subcat) {
  133.                 $catpages[$subcat['id']] = $this->Catpages->findBy(["visible" => 1"cat" => $subcat['id']], ["prior" => "DESC"]);
  134.                 if (empty($catpages[$subcat['id']])) {
  135.                     unset($catpages[$subcat['id']]);
  136.                 }
  137.             }
  138.         }
  139.         $response $this->render('block/speccatmenu.html.twig', [
  140.             'menu' => $cats,
  141.             'spec' => $spec,
  142.             'catpages' => $catpages,
  143.             'parent_id' => $cat_cat,
  144.             'cat_id' => $cat_id,
  145.             'search' => $search,
  146.         ]);
  147.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  148.         return $response;
  149.     }
  150.     #[Route('/{_locale}/block/filter/{cat_id}/{cat_intname}/filter-{filters}'name'block_filter'requirements: ['_locale' => '%app.langs%'])]
  151.     #[Route('/{_locale}/block/filter/{spec}/{cat_id}/{cat_intname}/filter-{filters}'name'block_filter_spec'requirements: ['_locale' => '%app.langs%'])]
  152.     public function filterBlock(Request $requestChars $CharsProds $Prodsint $cat_idstring $cat_intname ''string $filters ''string $spec ''): Response
  153.     {
  154.         $chars $Chars->getChars($cat_id);
  155.         $charvals $Chars->getCharVals($cat_id);
  156.         $prods $Prods->getProds($cat_id);
  157.         $chars_selected = [];
  158.         $charvals_selected = [];
  159.         
  160.         if ($filters) {
  161.             preg_match_all("/char(\d+)\-([\d_]+)/"$filters$m);
  162.             if (preg_match("/sale-(\w+)/"$filters$m2)) {
  163.                 $chars_selected['sale'] = $m2[1];
  164.             }
  165.             if (isset($m[1]) && count($m[1])) {
  166.                 foreach ($m[1] as $k => $v) {
  167.                     if (preg_match("/_/"$m[2][$k])) {
  168.                         preg_match_all("/(\d+)/"$m[2][$k], $mm);
  169.                         $chars_selected[$v] = $mm[0];
  170.                     } else {
  171.                         $chars_selected[$v][] = $m[2][$k];
  172.                     }
  173.                 }
  174.             }         
  175.         }
  176.         $response $this->render('block/filter.html.twig', [
  177.             'spec' => $spec,
  178.             'cat_id' => $cat_id,
  179.             'cat_intname' => $cat_intname,
  180.             'chars' => $chars,
  181.             'charvals' => $charvals,
  182.             'chars_selected' => $chars_selected,
  183.             'charvals_selected' => $charvals_selected,
  184.             'prods' => $prods,
  185.         ]);
  186.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  187.         return $response;
  188.     }
  189.     #[Route('/{_locale}/block/charsselected/{cat_id}/{cat_intname}/filter-{filters}'name'block_chars_selected'requirements: ['_locale' => '%app.langs%'])]
  190.     #[Route('/{_locale}/block/charsselected/{spec}/{cat_id}/{cat_intname}/filter-{filters}'name'block_chars_selected_spec'requirements: ['_locale' => '%app.langs%'])]
  191.     public function charsSelectedBlock(Request $requestFilter $Filterint $cat_idstring $cat_intname ''string $filters ''string $spec ''): Response
  192.     {
  193.         $chars_selected = [];
  194.         $chars_url = [];
  195.         
  196.         if ($filters) {
  197.             preg_match_all("/char(\d+)\-([\d_]+)/"$filters$m);
  198.             if (preg_match("/sale-(\w+)/"$filters$m2)) {
  199.                 $chars_selected['sale'] = $m2[1];
  200.             }
  201.             if (isset($m[1]) && count($m[1])) {
  202.                 foreach ($m[1] as $k => $v) {
  203.                     if (preg_match("/_/"$m[2][$k])) {
  204.                         preg_match_all("/(\d+)/"$m[2][$k], $mm);
  205.                         $chars_selected[$v] = $mm[0];
  206.                     } else {
  207.                         $chars_selected[$v][] = $m[2][$k];
  208.                     }
  209.                 }
  210.             }            
  211.         }
  212.         foreach ($chars_selected as $k => $v) {
  213.             if (is_int($k)) {
  214.                 $char_id = (int) $m[1];                
  215.                 $chars_selected[$char_id] = $v;
  216.                 foreach ($v as $val) {
  217.                     $charval $this->Charvals->find($val);
  218.                     $filters $Filter->generate_filters_exclude($chars_selected$char_id$val);
  219.                     $url $this->generateUrl('prod_list_cat_filter', ['cat_id' => $cat_id'cat_intname' => $cat_intname'filters' => $filters]);
  220.                     $chars_url[$url] =  $charval->getValue();
  221.                 }                
  222.             } else {
  223.                 $char_id $k;                
  224.                 $chars_selected[$char_id] = $v;
  225.                 $val $v;
  226.                 $filters $Filter->generate_filters_exclude($chars_selected$char_id$val);                
  227.                 $url $this->generateUrl('prod_list_cat_filter', ['cat_id' => $cat_id'cat_intname' => $cat_intname'filters' => $filters]);
  228.                 $chars_url[$url] = $v;
  229.             }
  230.         }
  231.         
  232.         $response $this->render('block/chars_selected.html.twig', [
  233.             'spec' => $spec,
  234.             'cat_id' => $cat_id,
  235.             'cat_intname' => $cat_intname,
  236.             'chars_selected' => $chars_selected,
  237.             'chars_url' => $chars_url,
  238.         ]);
  239.         return $response;
  240.     }
  241.     #[Route('/{_locale}/block/timer'name'block_timer'requirements: ['_locale' => '%app.langs%'])]
  242.     public function timersBlock(): Response
  243.     {
  244.         $timers $this->em->createQueryBuilder()
  245.             ->select('t')
  246.             ->from(Timer::class, 't')
  247.             ->where('t.visible = 1')
  248.             ->andWhere('t.end > :now')
  249.             ->setParameter('now'time())
  250.             ->orderBy('t.end''ASC')
  251.             ->getQuery()
  252.             ->getResult();
  253.         $response $this->render('block/timer.html.twig', [
  254.             'timer' => @$timers[0],
  255.         ]);
  256.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  257.         return $response;
  258.     }
  259.     #[Route('/{_locale}/api/cart/prodsblock/{spec}'name'cart_prods_block'requirements: ['_locale' => '%app.langs%''spec' => 'new|pop'])]
  260.     public function cartProdsBlock(string $spec): Response
  261.     {
  262.         //TODO Alex. Order by rand
  263.         $prods $this->em->createQuery("SELECT p FROM App\Entity\Prod p WHERE p.visible = 1 AND p.".$spec." = 1 AND (p.num > 0 or p.num2 > 0 or p.num3 > 0)")
  264.             ->setMaxResults(6)
  265.             ->getResult();
  266.         
  267.         foreach ($prods as $k => $prod) {
  268.             $prods[$k]->prices $this->ModelProd->getPrices($prod);
  269.         }
  270.         $response $this->render('cart/block/prods.html.twig', [
  271.             'prods' => $prods,
  272.             'spec' => $spec,
  273.             'cart_items' => $this->ModelCart->getCart(),
  274.         ]);
  275.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  276.         return $response;
  277.     }
  278. }