<?php
namespace App\Controller\Block;
use App\Env;
use App\Entity\Cat;
use App\Entity\Char;
use App\Entity\Charval;
use App\Entity\Page;
use App\DTO\AppDTO;
use App\Entity\Timer;
use RecursiveArrayIterator;
use App\Service\Cache\Cache;
use App\Service\Filter\Cats;
use App\Service\Filter\Chars;
use App\Service\Filter\Prods;
use App\Service\Filter\Filter;
use RecursiveIteratorIterator;
use App\Repository\CatRepository;
use App\Repository\CharRepository;
use App\Repository\PageRepository;
use App\Repository\TimerRepository;
use App\Repository\CharvalRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\ResultSetMapping;
use App\Model\Prod as ModelProd;
use App\Model\Cart as ModelCart;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
class BlockController extends AbstractController
{
protected EntityManagerInterface $em;
protected CacheInterface $Cache;
protected AppDTO $app;
protected ModelProd $ModelProd;
protected ModelCart $ModelCart;
//Repositories
protected PageRepository $Pages;
protected CatRepository $Cats;
protected TimerRepository $Timers;
protected CharRepository $Chars;
protected CharvalRepository $Charvals;
public function __construct(EntityManagerInterface $em, CacheInterface $Cache, AppDTO $app, ModelProd $ModelProd, ModelCart $ModelCart)
{
$this->Cache = $Cache;
$this->em = $em;
$this->app = $app;
$this->ModelProd = $ModelProd;
$this->ModelCart = $ModelCart;
$this->Pages = $em->getRepository(Page::class);
$this->Cats = $em->getRepository(Cat::class);
$this->Timers = $em->getRepository(Timer::class);
$this->Chars = $em->getRepository(Char::class);
$this->Charvals = $em->getRepository(Charval::class);
}
#[Route('/{_locale}/block/jslabels', name: 'block_js_labels', requirements: ['_locale' => '%app.langs%'])]
public function jsLabelsBlock(): Response
{
$response = $this->render('block/jslabels.html.twig');
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
#[Route('/{_locale}/block/topmenu/{intname}', name: 'block_top_menu', requirements: ['_locale' => '%app.langs%'])]
public function menuBlock(string $intname): Response
{
$menu = $this->Pages->findBy(["type" => "page", "visible" => 1, "page" => 0], ["prior" => "DESC"]);
$response = $this->render('block/topmenu.html.twig', [
'menus' => $menu,
'intname' => $intname,
]);
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
#[Route('/{_locale}/block/footmenu', name: 'block_foot_menu', requirements: ['_locale' => '%app.langs%'])]
public function footMenuBlock(): Response
{
$menu = $this->Pages->findBy(["type" => "page", "visible" => 1, "page" => 0], ["prior" => "DESC"]);
$response = $this->render('block/footmenu.html.twig', [
'menus' => $menu,
]);
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
#[Route('/{_locale}/block/catmenu/{template}', name: 'block_catmenu', requirements: ['_locale' => '%app.langs%'])]
public function catMenuBlock(string $template): Response
{
$menu = $this->Cats->findBy(["visible" => 1, "cat" => 0], ["prior" => "DESC"]);
foreach ($menu as $k => $cat) {
$menu[$k]->setCont('');
$menu[$k]->subcats = $this->Cats->findBy(["visible" => 1, "cat" => $cat->getId()], ["prior" => "DESC"]);
if (count($menu[$k]->subcats)) {
foreach ($menu[$k]->subcats as $kk => $subcat) {
$menu[$k]->subcats[$kk]->setCont('');
$menu[$k]->subcats[$kk]->subcats = $this->Cats->findBy(["visible" => 1, "cat" => $subcat->getId()], ["prior" => "DESC"]);
}
} else {
unset($menu[$k]->subcats);
}
}
$response = $this->render('block/'.$template.'.html.twig', [
'menu' => $menu,
]);
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
#[Route('/{_locale}/block/speccatmenu/{spec}/{cat_cat}/{cat_id}/{search}', name: 'block_catmenu_spec', requirements: ['_locale' => '%app.langs%'])]
public function specCatMenuBlock(string $spec, int $cat_cat, int $cat_id, string $search, Cats $Cats): Response
{
$cats = $Cats->getSpecCatMenu($cat_id, $spec, $search);
$response = $this->render('block/speccatmenu.html.twig', [
'menu' => $cats,
'spec' => $spec,
'parent_id' => $cat_cat,
'cat_id' => $cat_id,
'search' => $search,
]);
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
#[Route('/{_locale}/block/filter/{cat_id}/{cat_intname}/filter-{filters}', name: 'block_filter', requirements: ['_locale' => '%app.langs%'])]
#[Route('/{_locale}/block/filter/{spec}/{cat_id}/{cat_intname}/filter-{filters}', name: 'block_filter_spec', requirements: ['_locale' => '%app.langs%'])]
public function filterBlock(Request $request, Chars $Chars, Prods $Prods, int $cat_id, string $cat_intname = '', string $filters = '', string $spec = ''): Response
{
$chars = $Chars->getChars($cat_id);
$charvals = $Chars->getCharVals($cat_id);
$prods = $Prods->getProds($cat_id);
$chars_selected = [];
$charvals_selected = [];
if ($filters) {
preg_match_all("/char(\d+)\-([\d_]+)/", $filters, $m);
if (preg_match("/sale-(\w+)/", $filters, $m2)) {
$chars_selected['sale'] = $m2[1];
}
if (isset($m[1]) && count($m[1])) {
foreach ($m[1] as $k => $v) {
if (preg_match("/_/", $m[2][$k])) {
preg_match_all("/(\d+)/", $m[2][$k], $mm);
$chars_selected[$v] = $mm[0];
} else {
$chars_selected[$v][] = $m[2][$k];
}
}
}
}
$response = $this->render('block/filter.html.twig', [
'spec' => $spec,
'cat_id' => $cat_id,
'cat_intname' => $cat_intname,
'chars' => $chars,
'charvals' => $charvals,
'chars_selected' => $chars_selected,
'charvals_selected' => $charvals_selected,
'prods' => $prods,
]);
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
#[Route('/{_locale}/block/charsselected/{cat_id}/{cat_intname}/filter-{filters}', name: 'block_chars_selected', requirements: ['_locale' => '%app.langs%'])]
#[Route('/{_locale}/block/charsselected/{spec}/{cat_id}/{cat_intname}/filter-{filters}', name: 'block_chars_selected_spec', requirements: ['_locale' => '%app.langs%'])]
public function charsSelectedBlock(Request $request, Filter $Filter, int $cat_id, string $cat_intname = '', string $filters = '', string $spec = ''): Response
{
$chars_selected = [];
$chars_url = [];
if ($filters) {
preg_match_all("/char(\d+)\-([\d_]+)/", $filters, $m);
if (preg_match("/sale-(\w+)/", $filters, $m2)) {
$chars_selected['sale'] = $m2[1];
}
if (isset($m[1]) && count($m[1])) {
foreach ($m[1] as $k => $v) {
if (preg_match("/_/", $m[2][$k])) {
preg_match_all("/(\d+)/", $m[2][$k], $mm);
$chars_selected[$v] = $mm[0];
} else {
$chars_selected[$v][] = $m[2][$k];
}
}
}
}
foreach ($chars_selected as $k => $v) {
if (is_int($k)) {
$char_id = (int) $m[1];
$chars_selected[$char_id] = $v;
foreach ($v as $val) {
$charval = $this->Charvals->find($val);
$filters = $Filter->generate_filters_exclude($chars_selected, $char_id, $val);
$url = $this->generateUrl('prod_list_cat_filter', ['cat_id' => $cat_id, 'cat_intname' => $cat_intname, 'filters' => $filters]);
$chars_url[$url] = $charval->getValue();
}
} else {
$char_id = $k;
$chars_selected[$char_id] = $v;
$val = $v;
$filters = $Filter->generate_filters_exclude($chars_selected, $char_id, $val);
$url = $this->generateUrl('prod_list_cat_filter', ['cat_id' => $cat_id, 'cat_intname' => $cat_intname, 'filters' => $filters]);
$chars_url[$url] = $v;
}
}
$response = $this->render('block/chars_selected.html.twig', [
'spec' => $spec,
'cat_id' => $cat_id,
'cat_intname' => $cat_intname,
'chars_selected' => $chars_selected,
'chars_url' => $chars_url,
]);
return $response;
}
#[Route('/{_locale}/block/timer', name: 'block_timer', requirements: ['_locale' => '%app.langs%'])]
public function timersBlock(): Response
{
$q = "SELECT * FROM timer WHERE visible = 1 and end > ".time()." ORDER BY rand()";
$stmt = $this->em->getConnection()->prepare($q);
$timers = $stmt->executeQuery()->fetchAllAssociative();
$response = $this->render('block/timer.html.twig', [
'timer' => @$timers[0],
]);
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
#[Route('/{_locale}/api/cart/prodsblock/{spec}', name: 'cart_prods_block', requirements: ['_locale' => '%app.langs%', 'spec' => 'new|pop'])]
public function cartProdsBlock(string $spec): Response
{
//TODO Alex. Order by rand
$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)")
->setMaxResults(6)
->getResult();
foreach ($prods as $k => $prod) {
$prods[$k]->prices = $this->ModelProd->getPrices($prod);
}
$response = $this->render('cart/block/prods.html.twig', [
'prods' => $prods,
'spec' => $spec,
'cart_items' => $this->ModelCart->getCart(),
]);
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
}