src/Controller/Block/BlockController.php line 161

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.     #[Route('/{_locale}/block/jslabels'name'block_js_labels'requirements: ['_locale' => '%app.langs%'])]
  63.     public function jsLabelsBlock(): Response
  64.     {
  65.         $response $this->render('block/jslabels.html.twig');
  66.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  67.         return $response;
  68.     }
  69.     #[Route('/{_locale}/block/topmenu/{intname}'name'block_top_menu'requirements: ['_locale' => '%app.langs%'])]
  70.     public function menuBlock(string $intname): Response
  71.     {
  72.         $menu $this->Pages->findBy(["type" => "page""visible" => 1"page" => 0], ["prior" => "DESC"]);
  73.         $response $this->render('block/topmenu.html.twig', [
  74.             'menus' => $menu,
  75.             'intname' => $intname,
  76.         ]);
  77.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  78.         return $response;
  79.     }
  80.     #[Route('/{_locale}/block/footmenu'name'block_foot_menu'requirements: ['_locale' => '%app.langs%'])]
  81.     public function footMenuBlock(): Response
  82.     {
  83.         $menu $this->Pages->findBy(["type" => "page""visible" => 1"page" => 0], ["prior" => "DESC"]);
  84.         $response $this->render('block/footmenu.html.twig', [
  85.             'menus' => $menu,
  86.         ]);
  87.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  88.         return $response;
  89.     }
  90.     #[Route('/{_locale}/block/catmenu/{template}'name'block_catmenu'requirements: ['_locale' => '%app.langs%'])]
  91.     public function catMenuBlock(string $template): Response
  92.     {
  93.         $menu $this->Cats->findBy(["visible" => 1"cat" => 0], ["prior" => "DESC"]);
  94.         foreach ($menu as $k => $cat) {
  95.             $menu[$k]->setCont('');
  96.             $menu[$k]->subcats $this->Cats->findBy(["visible" => 1"cat" => $cat->getId()], ["prior" => "DESC"]);
  97.             if (count($menu[$k]->subcats)) {
  98.                 foreach ($menu[$k]->subcats as $kk => $subcat) {
  99.                     $menu[$k]->subcats[$kk]->setCont('');
  100.                     $menu[$k]->subcats[$kk]->subcats $this->Cats->findBy(["visible" => 1"cat" => $subcat->getId()], ["prior" => "DESC"]);                    
  101.                 }
  102.             } else {
  103.                 unset($menu[$k]->subcats);
  104.             }
  105.         }
  106.         $response $this->render('block/'.$template.'.html.twig', [
  107.             'menu' => $menu,
  108.         ]);
  109.         // $response = Cache::http($response, $this->getParameter('app.http_cache_time'));
  110.         return $response;
  111.     }
  112.     #[Route('/{_locale}/block/speccatmenu/{spec}/{cat_cat}/{cat_id}/{search}'name'block_catmenu_spec'requirements: ['_locale' => '%app.langs%'])]
  113.     public function specCatMenuBlock(string $specint $cat_catint $cat_idstring $searchCats $Cats): Response
  114.     {           
  115.         $cats $Cats->getSpecCatMenu($cat_id$spec$search);
  116.         $catpages = [];
  117.         foreach ($cats as $k => $v) {            
  118.             foreach ($v as $kk => $subcat) {
  119.                 $catpages[$subcat['id']] = $this->Catpages->findBy(["visible" => 1"cat" => $subcat['id']], ["prior" => "DESC"]);
  120.                 if (empty($catpages[$subcat['id']])) {
  121.                     unset($catpages[$subcat['id']]);
  122.                 }
  123.             }
  124.         }
  125.         $response $this->render('block/speccatmenu.html.twig', [
  126.             'menu' => $cats,
  127.             'spec' => $spec,
  128.             'catpages' => $catpages,
  129.             'parent_id' => $cat_cat,
  130.             'cat_id' => $cat_id,
  131.             'search' => $search,
  132.         ]);
  133.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  134.         return $response;
  135.     }
  136.     #[Route('/{_locale}/block/filter/{cat_id}/{cat_intname}/filter-{filters}'name'block_filter'requirements: ['_locale' => '%app.langs%'])]
  137.     #[Route('/{_locale}/block/filter/{spec}/{cat_id}/{cat_intname}/filter-{filters}'name'block_filter_spec'requirements: ['_locale' => '%app.langs%'])]
  138.     public function filterBlock(Request $requestChars $CharsProds $Prodsint $cat_idstring $cat_intname ''string $filters ''string $spec ''): Response
  139.     {
  140.         $chars $Chars->getChars($cat_id);
  141.         $charvals $Chars->getCharVals($cat_id);
  142.         $prods $Prods->getProds($cat_id);
  143.         $chars_selected = [];
  144.         $charvals_selected = [];
  145.         
  146.         if ($filters) {
  147.             preg_match_all("/char(\d+)\-([\d_]+)/"$filters$m);
  148.             if (preg_match("/sale-(\w+)/"$filters$m2)) {
  149.                 $chars_selected['sale'] = $m2[1];
  150.             }
  151.             if (isset($m[1]) && count($m[1])) {
  152.                 foreach ($m[1] as $k => $v) {
  153.                     if (preg_match("/_/"$m[2][$k])) {
  154.                         preg_match_all("/(\d+)/"$m[2][$k], $mm);
  155.                         $chars_selected[$v] = $mm[0];
  156.                     } else {
  157.                         $chars_selected[$v][] = $m[2][$k];
  158.                     }
  159.                 }
  160.             }         
  161.         }
  162.         $response $this->render('block/filter.html.twig', [
  163.             'spec' => $spec,
  164.             'cat_id' => $cat_id,
  165.             'cat_intname' => $cat_intname,
  166.             'chars' => $chars,
  167.             'charvals' => $charvals,
  168.             'chars_selected' => $chars_selected,
  169.             'charvals_selected' => $charvals_selected,
  170.             'prods' => $prods,
  171.         ]);
  172.         if (!isset($_GET['test'])) {
  173.             $response Cache::http($response$this->getParameter('app.http_cache_time'));
  174.         }
  175.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  176.         return $response;
  177.     }
  178.     #[Route('/{_locale}/block/charsselected/{cat_id}/{cat_intname}/filter-{filters}'name'block_chars_selected'requirements: ['_locale' => '%app.langs%'])]
  179.     #[Route('/{_locale}/block/charsselected/{spec}/{cat_id}/{cat_intname}/filter-{filters}'name'block_chars_selected_spec'requirements: ['_locale' => '%app.langs%'])]
  180.     public function charsSelectedBlock(Request $requestFilter $Filterint $cat_idstring $cat_intname ''string $filters ''string $spec ''): Response
  181.     {
  182.         $chars_selected = [];
  183.         $chars_url = [];
  184.         
  185.         if ($filters) {
  186.             preg_match_all("/char(\d+)\-([\d_]+)/"$filters$m);
  187.             if (preg_match("/sale-(\w+)/"$filters$m2)) {
  188.                 $chars_selected['sale'] = $m2[1];
  189.             }
  190.             if (isset($m[1]) && count($m[1])) {
  191.                 foreach ($m[1] as $k => $v) {
  192.                     if (preg_match("/_/"$m[2][$k])) {
  193.                         preg_match_all("/(\d+)/"$m[2][$k], $mm);
  194.                         $chars_selected[$v] = $mm[0];
  195.                     } else {
  196.                         $chars_selected[$v][] = $m[2][$k];
  197.                     }
  198.                 }
  199.             }            
  200.         }
  201.         foreach ($chars_selected as $k => $v) {
  202.             if (is_int($k)) {
  203.                 $char_id = (int) $m[1];                
  204.                 $chars_selected[$char_id] = $v;
  205.                 foreach ($v as $val) {
  206.                     $charval $this->Charvals->find($val);
  207.                     $filters $Filter->generate_filters_exclude($chars_selected$char_id$val);
  208.                     $url $this->generateUrl('prod_list_cat_filter', ['cat_id' => $cat_id'cat_intname' => $cat_intname'filters' => $filters]);
  209.                     $chars_url[$url] =  $charval->getValue();
  210.                 }                
  211.             } else {
  212.                 $char_id $k;                
  213.                 $chars_selected[$char_id] = $v;
  214.                 $val $v;
  215.                 $filters $Filter->generate_filters_exclude($chars_selected$char_id$val);                
  216.                 $url $this->generateUrl('prod_list_cat_filter', ['cat_id' => $cat_id'cat_intname' => $cat_intname'filters' => $filters]);
  217.                 $chars_url[$url] = $v;
  218.             }
  219.         }
  220.         
  221.         $response $this->render('block/chars_selected.html.twig', [
  222.             'spec' => $spec,
  223.             'cat_id' => $cat_id,
  224.             'cat_intname' => $cat_intname,
  225.             'chars_selected' => $chars_selected,
  226.             'chars_url' => $chars_url,
  227.         ]);
  228.         return $response;
  229.     }
  230.     #[Route('/{_locale}/block/timer'name'block_timer'requirements: ['_locale' => '%app.langs%'])]
  231.     public function timersBlock(): Response
  232.     {
  233.         $q "SELECT * FROM timer WHERE visible = 1 and end > ".time()." ORDER BY rand()";
  234.         $stmt $this->em->getConnection()->prepare($q);
  235.         $timers $stmt->executeQuery()->fetchAllAssociative();
  236.         $response $this->render('block/timer.html.twig', [
  237.             'timer' => @$timers[0],
  238.         ]);
  239.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  240.         return $response;
  241.     }
  242.     #[Route('/{_locale}/api/cart/prodsblock/{spec}'name'cart_prods_block'requirements: ['_locale' => '%app.langs%''spec' => 'new|pop'])]
  243.     public function cartProdsBlock(string $spec): Response
  244.     {
  245.         //TODO Alex. Order by rand
  246.         $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)")
  247.             ->setMaxResults(6)
  248.             ->getResult();
  249.         
  250.         foreach ($prods as $k => $prod) {
  251.             $prods[$k]->prices $this->ModelProd->getPrices($prod);
  252.         }
  253.         $response $this->render('cart/block/prods.html.twig', [
  254.             'prods' => $prods,
  255.             'spec' => $spec,
  256.             'cart_items' => $this->ModelCart->getCart(),
  257.         ]);
  258.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  259.         return $response;
  260.     }
  261. }