src/Controller/Block/BlockController.php line 84

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