src/Controller/Block/BlockController.php line 66

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