<?php
namespace App\Controller\Block;
use App\DTO\AppDTO;
use App\Entity\Cat;
use App\Entity\Char;
use App\Entity\Page;
use App\Entity\Timer;
use App\Entity\Catpage;
use App\Entity\Charval;
use App\Service\Filter\Filter;
use App\Model\Cart as ModelCart;
use App\Model\Prod as ModelProd;
use App\Repository\CatRepository;
use App\Repository\CharRepository;
use App\Repository\PageRepository;
use App\Repository\TimerRepository;
use App\Repository\CatpageRepository;
use App\Repository\CharvalRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class BlockController extends AbstractController
{
protected EntityManagerInterface $em;
protected AppDTO $app;
protected ModelProd $ModelProd;
protected ModelCart $ModelCart;
//Repositories
protected PageRepository $Pages;
protected CatRepository $Cats;
protected CatpageRepository $Catpages;
protected TimerRepository $Timers;
protected CharRepository $Chars;
protected CharvalRepository $Charvals;
public function __construct(
EntityManagerInterface $em,
private TagAwareCacheInterface $appCache,
AppDTO $app,
ModelProd $ModelProd,
ModelCart $ModelCart
)
{
$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->Catpages = $em->getRepository(Catpage::class);
$this->Timers = $em->getRepository(Timer::class);
$this->Chars = $em->getRepository(Char::class);
$this->Charvals = $em->getRepository(Charval::class);
}
#[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;
}
}