<?php
namespace App\Controller;
use App\Func;
use stdClass;
use App\DTO\AppDTO;
use App\Entity\Cat;
use App\Entity\Page;
use App\Entity\Prod;
use App\Entity\Photo;
use App\Entity\Catpage;
use App\Service\Utils\Bc;
use App\Service\Cart\Cart;
use RecursiveArrayIterator;
use App\Service\Filter\Chars;
use App\Service\Filter\Prods;
use App\Model\Cat as ModelCat;
use App\Service\Filter\Filter;
use RecursiveIteratorIterator;
use App\Model\Cart as ModelCart;
use App\Model\Prod as ModelProd;
use App\Repository\CatRepository;
use App\Repository\PageRepository;
use App\Repository\ProdRepository;
use App\Repository\PhotoRepository;
use App\Repository\CatpageRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpFoundation\Request;
use App\Service\Paginator as ServicePaginator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/** @package App\Controller */
class CatalogController extends AbstractASController
{
private CacheInterface $Cache;
private EntityManagerInterface $em;
protected AppDTO $app;
protected ModelProd $ModelProd;
protected Cart $Cart;
protected ModelCart $ModelCart;
// Repositories
protected CatRepository $Cats;
protected ProdRepository $Prods;
protected PageRepository $Pages;
protected CatpageRepository $Catpages;
protected PhotoRepository $Photos;
public function __construct(CacheInterface $Cache, EntityManagerInterface $em, AppDTO $app, ModelProd $ModelProd, Cart $Cart, ModelCart $ModelCart, RequestStack $requestStack)
{
$this->requestStack = $requestStack;
$this->Cache = $Cache;
$this->em = $em;
$this->app = $app;
$this->ModelProd = $ModelProd;
$this->ModelCart = $ModelCart;
$this->Cart = $Cart;
$this->Cats = $this->em->getRepository(Cat::class);
$this->Prods = $this->em->getRepository(Prod::class);
$this->Pages = $this->em->getRepository(Page::class);
$this->Catpages = $this->em->getRepository(Catpage::class);
$this->Photos = $this->em->getRepository(Photo::class);
}
#[Route('/catalog', name: 'app_catalog')]
public function index(): Response
{
return $this->redirectToRoute('home');
}
#[Route(path: '/catalog/cat-{cat_id}-{cat_intname}', name: 'prod_list_cat_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route(path: '/catalog/cat-{cat_id}-{cat_intname}/filter-{filters}', name: 'prod_list_cat_filte_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route(path: '/catalog/{spec}/cat-{cat_id}-{cat_intname}/filter-{filters}', name: 'prod_list_cat_spec_filter_no_locale', defaults: ['_locale' => '%app.default_lang%'], requirements: ['spec' => 'new|action|pop|onsale|mix|search'])]
#[Route(path: '/catalog/{spec}/cat-{cat_id}-{cat_intname}', name: 'prod_list_cat_spec_no_locale', defaults: ['_locale' => '%app.default_lang%'], requirements: ['spec' => 'new|action|pop|onsale|mix|search'])]
#[Route(path: '/catalog/{spec}', name: 'prod_list_spec_no_locale', defaults: ['_locale' => '%app.default_lang%'], requirements: ['spec' => 'new|action|pop|onsale|mix|search'])]
#[Route(path: '/{_locale}/catalog/cat-{cat_id}-{cat_intname}', name: 'prod_list_cat', requirements: ['_locale' => '%app.langs%'])]
#[Route(path: '/{_locale}/catalog/cat-{cat_id}-{cat_intname}/filter-{filters}', name: 'prod_list_cat_filter', requirements: ['_locale' => '%app.langs%'])]
#[Route(path: '/{_locale}/catalog/{spec}/cat-{cat_id}-{cat_intname}/filter-{filters}', name: 'prod_list_cat_spec_filter', requirements: ['_locale' => '%app.langs%', 'spec' => 'new|action|pop|onsale|mix|search'])]
#[Route(path: '/{_locale}/catalog/{spec}/cat-{cat_id}-{cat_intname}', name: 'prod_list_cat_spec', requirements: ['_locale' => '%app.langs%', 'spec' => 'new|action|pop|onsale|mix|search'])]
#[Route(path: '/{_locale}/catalog/{spec}', name: 'prod_list_spec', requirements: ['_locale' => '%app.langs%', 'spec' => 'new|action|pop|onsale|mix|search'])]
public function prodList(Request $request, SessionInterface $session, ModelCat $ModelCat, Prods $ModelProd, int $cat_id = 0, string $cat_intname = '', string $spec = '', string $filters = ''): Response
{
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc = ["/" => $this->app->labels->get('home')];
} else {
$bc = ["/" . $request->getLocale() . "/" => $this->app->labels->get('home')];
}
switch ($spec) {
case 'new':
$bctype = '/new';
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc["/catalog/new"] = $this->app->labels->get('main-mobile-buttons-1');
} else {
$bc["/" . $request->getLocale() . "/catalog/new"] = $this->app->labels->get('main-mobile-buttons-1');
}
break;
case 'action':
$bctype = '/action';
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc["/catalog/action"] = $this->app->labels->get('main-mobile-buttons-2');
} else {
$bc["/" . $request->getLocale() . "/catalog/action"] = $this->app->labels->get('main-mobile-buttons-2');
}
break;
case 'pop':
$bctype = '/pop';
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc["/catalog/pop"] = $this->app->labels->get('main-mobile-buttons-4');
} else {
$bc["/" . $request->getLocale() . "/catalog/pop"] = $this->app->labels->get('main-mobile-buttons-4');
}
break;
case 'mix':
$bctype = '/mix';
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc["/catalog/mix"] = $this->app->labels->get('main-mobile-buttons-3');
} else {
$bc["/" . $request->getLocale() . "/catalog/mix"] = $this->app->labels->get('main-mobile-buttons-3');
}
break;
case 'onsale':
$bctype = '/onsale';
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc["/catalog/onsale"] = $this->app->labels->get('catalog-bc-sale');
} else {
$bc["/" . $request->getLocale() . "/catalog/onsale"] = $this->app->labels->get('catalog-bc-sale');
}
break;
case 'search':
$bctype = '/search';
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc["/catalog/search?q=" . $request->get('q')] = $this->app->labels->get('catalog-bc-1') . ": \"" . $request->get('q') . "\"";
} else {
$bc["/" . $request->getLocale() . "/catalog/search?q=" . $request->get('q')] = $this->app->labels->get('catalog-bc-1') . ": \"" . $request->get('q') . "\"";
}
break;
case '':
$bctype = '';
break;
}
$bcc = [];
$is_filter = 0;
$noindex = 0;
$catpage = null;
$showcont = 1;
if ($request->get('filter')) {
return $this->redirect($this->_makeFilterUrl($request, $spec));
}
if ($request->get('results') && $request->get('results') != $session->get('results')) {
$session->set('results', $request->get('results'));
}
if ($request->get('view') === 'list' || $request->get('view') === 'grid') {
setcookie("view_mode", $_GET['view'], time() + 86400 * 365, '/');
}
$results = $session->get('results') ?? 30;
$start = (int) $request->query->get('start');
$view_mode = $request->get('view') ?? $request->cookies->get('view_mode') ?? 'list';
$prods = $ModelProd->getProds($cat_id, $spec, $request->get('q', ''));
if ($spec) {
$page = $this->Pages->findOneBy(['intname' => 'catalog/' . $spec]);
} else {
$page = $this->Pages->findOneBy(['intname' => 'catalog']);
}
if (!$page) {
$page = new Page();
}
if ($cat_id) {
$cat_row = $row = $this->Cats->find($cat_id);
if (!$row->isVisible() || $row->getIntname() != $cat_intname) {
throw $this->createNotFoundException('Page not found');
}
$page->setName($row->getName());
$page->setCont($row->getCont());
$page->setCont2($row->getCont2());
$page->setTitle(!empty($row->getTitle()) ? $row->getTitle() : Func::mess_from_tmp($this->app->templates->get('cat_title'), array("name" => $row->getName())));
$page->setName($row->getName());
$page->setH1($row->getH1() ? $row->getH1() : $row->getName());
$page->setKw($row->getKw());
$page->setDescr(!empty($row->getDescr()) ? $row->getDescr() : Func::mess_from_tmp($this->app->templates->get('cat_desc'), array("name" => $row->getName())));
// if ($charval) {
// $Charval = new Model_Charval($charval);
// $charval_row = $Charval->get($charval);
// $view->page->title = $charval_row->value.". ".$view->page->title;
// $view->page->h1 = $charval_row->value.". ".$view->page->h1;
// $view->page->kw = $charval_row->value.", ".$view->page->kw;
// $url->g['filter'] = 1;
// $url->g['char'.$char] = array("0" => $charval);
// }
$tree = array_reverse($ModelCat->get_cat_tree_up($cat_id));
$bctype = $spec ? '/' . $spec : '';
foreach ($tree as $k => $v) {
$cat_row = $this->Cats->find($v['id']);
$searchq = '';
if ($request->query->get('q')) {
$searchq = '?q=' . $request->query->get('q');
}
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc["/catalog" . $bctype . "/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname() . $searchq] = $cat_row->getName();
} else {
$bc["/" . $request->getLocale() . "/catalog" . $bctype . "/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname() . $searchq] = $cat_row->getName();
}
$relatives = $this->Cats->findBy(['cat' => $cat_row->getCat(), 'visible' => 1]);
foreach ($relatives as $k => $v) {
if (!isset($bcc["/" . $request->getLocale() . "/catalog/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()])) {
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bcc["/catalog" . $bctype . "/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()] = [];
} else {
$bcc["/" . $request->getLocale() . "/catalog" . $bctype . "/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()] = [];
}
}
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bcc["/catalog" . $bctype . "/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()]["/" . $request->getLocale() . "/catalog/cat-" . $v->getId() . "-" . $v->getIntname()] = $v->getName();
} else {
$bcc["/" . $request->getLocale() . "/catalog" . $bctype . "/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()]["/" . $request->getLocale() . "/catalog/cat-" . $v->getId() . "-" . $v->getIntname()] = $v->getName();
}
}
}
}
if ($filters) {
$is_filter = 1;
$sale = 0;
preg_match_all("/char(\d+)\-([\d_]+)/", $filters, $m);
$chars = [];
if (preg_match("/sale-(\w+)/", $filters, $m2)) {
$sale = $m2[1];
}
foreach ($m[1] as $k => $v) {
if (preg_match("/_/", $m[2][$k])) {
preg_match_all("/(\d+)/", $m[2][$k], $mm);
$chars[$v] = $mm[0];
} else {
$chars[$v][] = $m[2][$k];
}
}
$catpage = $this->Catpages->findOneBy(['intname' => str_replace("/".$request->getLocale()."/", "/", $request->getPathInfo())]);
if ($catpage) {
$is_catpage = 1;
$page->setName($catpage->getName());
$page->setTitle($catpage->getTitle());
$page->setH1($catpage->getH1());
$page->setKw($catpage->getKw());
$page->setDescr($catpage->getDescr());
$page->setCont($catpage->getCont());
$page->setCont2($catpage->getCont2());
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc["/" . $catpage->getIntname()] = $catpage->getName();
} else {
$bc["/" . $request->getLocale() . "/" . $catpage->getIntname()] = $catpage->getName();
}
}
$chars['sale'] = $sale;
$prods = $this->Prods->filter($prods, $chars);
}
if ($request->get('order') == 'prior') {
uasort($prods, '\App\Repository\ProdRepository::sortArrayByPriorDesc');
} elseif ($request->get('order') == 'changed' || (empty($request->get('order')) && $spec == 'new')) {
uasort($prods, '\App\Repository\ProdRepository::sortArrayByChangedAsc');
} elseif ($request->get('order') && $request->get('desc_asc') == 'asc') {
uasort($prods, '\App\Repository\ProdRepository::sortArrayByPriceAsc');
} elseif ($request->get('order') && $request->get('desc_asc') == 'desc') {
uasort($prods, '\App\Repository\ProdRepository::sortArrayByPriceDesc');
} else {
uasort($prods, '\App\Repository\ProdRepository::sortArrayBySkidkaDesc');
}
$cnt = count($prods);
$show_prods = [];
$k = 1;
$i = 1;
$prodstable = ['cat' => ($cat_id ? $page->getName() : ''), 'cnt' => count($prods), 'minprice' => (count($prods) ? 999999999 : 0), 'maxprice' => 0, 'avgprice' => 0];
foreach ($prods as $prod) {
if ($start != 0 && $i++ <= $start) continue;
if ($k <= $results) {
// $prod->prices = $this->ModelProd->getPrices($prod);
// $prod->photos = $this->Photos->findBy(['type' => 'prod', 'par' => $prod->getId()]);
$show_prods[] = $prod;
}
$k++;
(($prod['price'] > 0) && ($prodstable['minprice'] > $prod['price'])) ? $prodstable['minprice'] = round($prod['price']) : false;
(($prod['price'] > 0) && ($prodstable['maxprice'] < $prod['price'])) ? $prodstable['maxprice'] = round($prod['price']) : false;
$prodstable['avgprice'] += $prod['price'];
}
if ($prodstable['cnt']) {
$prodstable['avgprice'] = round($prodstable['avgprice'] / $prodstable['cnt'], 2);
} else {
$prodstable['avgprice'] = 0;
}
if ($start) {
$page = new Page();
if (isset($row) && $row) {
$page->setTitle(Func::mess_from_tmp($this->app->templates->get('cat_page_title'), array("page" => round($start / $results) + 1, "name" => $row->getName())));
$page->setDescr(Func::mess_from_tmp($this->app->templates->get('cat_page_descr'), array("page" => round($start / $results) + 1, "name" => $row->getName())));
} else {
$page->setTitle(Func::mess_from_tmp($this->app->templates->get('cat_page_title'), array("page" => round($start / $results) + 1, "name" => "")));
$page->setDescr(Func::mess_from_tmp($this->app->templates->get('cat_page_descr'), array("page" => round($start / $results) + 1, "name" => "")));
}
$page->setCont('');
$page->setCont2('');
$showcont = 0;
}
/* For SEO. Indexing */
$route = $request->get('_route');
$canonical = '';
if ($request->get('start')) {
$canonical = "https://".$request->getHost().$request->getPathInfo()."?start=".$request->get('start');
} else {
$canonical = "https://".$request->getHost().$request->getPathInfo();
}
if ($request->get('start', -1) == 0 || $request->get('start') == 1 || $request->get('start') == 'x') {
if ($spec && isset($row)) {
return $this->redirectToRoute($request->get('_route'), ['cat_id' => $row->getId(), "cat_intname" => $row->getIntname(), "spec" => $spec], 301);
} elseif (isset($row)) {
return $this->redirectToRoute($request->get('_route'), ['cat_id' => $row->getId(), "cat_intname" => $row->getIntname()], 301);
}
}
if ($start > 0 && count($show_prods) == 0) {
$this->createNotFoundException();
}
if (!empty($spec)) {
$noindex = 1;
}
if ($is_filter || strpos($request->getUri(), 'filter') !== false) {
if ($catpage) {
$noindex = 0;
} else {
$noindex = 1;
}
}
if (count($request->query->all()) > 1) {
$noindex = 1;
}
if (!empty($request->query->all()) && $start % 30 != 0) {
$noindex = 1;
}
if (!empty($request->get('results'))) {
$noindex = 1;
}
if (!empty($request->get('view'))) {
$noindex = 1;
}
/* End For SEO */
// Render Subcats
$subcat_list_rendered = '';
if (empty($spec)) {
$cats = $this->Cats->findBy(['visible' => 1, 'cat' => $cat_id], ["prior" => "desc", "name" => "asc"]);
if (count($cats)) {
$subcat_list_rendered = $this->render('catalog/subcat-list.html.twig', [
'cats' => $cats,
]);
}
}
// Ids in Cart
$cartids = [];
foreach ($this->Cart->getCart() as $k => $v) {
$cartids[] = $v['id'];
}
$cat = $this->Cats->find($cat_id);
if ($request->get('test')) {
foreach ($show_prods as $k => $v) {
echo $v['id']."-".$v['price']."<br>";
}
}
if ($start) {
if ($cat) {
$cat->setCont('');
}
$showcont = 0;
}
$show_prods = $this->Prods->prodArrayToEntity($show_prods);
if ($request->get('order') == 'prior') {
uasort($show_prods, '\App\Repository\ProdRepository::sortByPriorDesc');
} elseif ($request->get('order') == 'changed' || (empty($request->get('order')) && $spec == 'new')) {
uasort($show_prods, '\App\Repository\ProdRepository::sortByChangedAsc');
} elseif ($request->get('order') && $request->get('desc_asc') == 'asc') {
uasort($show_prods, '\App\Repository\ProdRepository::sortByPriceAsc');
} elseif ($request->get('order') && $request->get('desc_asc') == 'desc') {
uasort($show_prods, '\App\Repository\ProdRepository::sortByPriceDesc');
} else {
uasort($show_prods, '\App\Repository\ProdRepository::sortBySkidkaDesc');
}
foreach ($show_prods as $k => $v) {
$show_prods[$k]->setPrices($this->ModelProd->getPrices($v));
}
return $this->render('catalog/list.html.twig', [
'subcat_list_rendered' => $subcat_list_rendered,
'spec' => $spec,
'cat_id' => $cat_id,
'cat_intname' => $cat_intname,
'filters' => $filters,
'prods' => $show_prods,
'noindex' => $noindex,
'cat' => $cat,
'results' => $results,
'view_mode' => $view_mode,
'page' => $page,
'cartids' => $cartids,
'paginator' => new ServicePaginator($request->attributes->get('_route'), $cnt, $results, $start),
'bc' => $bc,
'bcc' => $bcc,
'canonical' => $canonical,
'showcont' => $showcont,
'prodstable' => $prodstable,
'cart_items' => $this->ModelCart->getCart(),
]);
}
#[Route('/catalog/prod-{prod_id}', name: 'prod_cont_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route('/{_locale}/catalog/prod-{prod_id}', name: 'prod_cont', requirements: ['_locale' => '%app.langs%'])]
public function prodCont(Request $request, ModelCat $ModelCat, int $prod_id = 0): Response
{
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc = ["/" => $this->app->labels->get('home')];
} else {
$bc = ["/" . $request->getLocale() . "/" => $this->app->labels->get('home')];
}
$bcc = [];
$prod = $this->Prods->find($prod_id);
if (!$prod) {
throw $this->createNotFoundException('Page not found');
}
if ($prod) {
$prod->setPrices($this->ModelProd->getPrices($prod));
}
$photos = $this->Photos->findBy(['type' => 'prod', 'par' => $prod->getId()]);
$cat_id = $prod->getCat();
$tree = array_reverse($ModelCat->get_cat_tree_up($cat_id));
foreach ($tree as $k => $v) {
$cat_row = $this->Cats->find($v['id']);
$searchq = '';
if ($request->query->get('q')) {
$searchq = '?q=' . $request->query->get('q');
}
$relatives = $this->Cats->findBy(['cat' => $cat_row->getCat(), 'visible' => 1]);
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bc["/catalog/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()] = $cat_row->getName();
} else {
$bc["/" . $request->getLocale() . "/catalog/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()] = $cat_row->getName();
}
foreach ($relatives as $k => $v) {
if (!isset($bcc["/" . $request->getLocale() . "/catalog/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()])) {
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bcc["/catalog/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()] = [];
} else {
$bcc["/" . $request->getLocale() . "/catalog/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()] = [];
}
}
if ($request->getLocale() == $this->getParameter('app.default_lang')) {
$bcc["/catalog/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()]["/catalog/cat-" . $v->getId() . "-" . $v->getIntname()] = $v->getName();
} else {
$bcc["/" . $request->getLocale() . "/catalog/cat-" . $cat_row->getId() . "-" . $cat_row->getIntname()]["/" . $request->getLocale() . "/catalog/cat-" . $v->getId() . "-" . $v->getIntname()] = $v->getName();
}
}
}
$prod_chars = $this->Prods->getprodchars($prod_id);
$prod_childs = $this->Prods->getprodchilds($prod_id);
//TODO. Alex. SEO
$page = new Page();
$page->setTitle(Func::mess_from_tmp($this->app->templates->get('prod_title'), array("name" => $prod->getName())));
$page->setDescr(Func::mess_from_tmp($this->app->templates->get('prod_descr'), array("name" => $prod->getName())));
$page->setCont('');
$page->setCont2('');
return $this->render('catalog/cont.html.twig', [
'page' => $page,
'prod' => $prod,
'photos' => $photos,
'bc' => $bc,
'bcc' => $bcc,
'chars' => $prod_chars,
'childs' => $prod_childs,
'cart_items' => $this->ModelCart->getCart(),
]);
}
#[Route('/catalog/prod/ajax/{prod_id}', name: 'prod_ajax_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route('/{_locale}/catalog/prod/ajax/{prod_id}', name: 'prod_ajax', requirements: ['_locale' => '%app.langs%'])]
public function prodAjaxCont(Request $request, int $prod_id = 0): Response
{
$prod = $this->Prods->find($prod_id);
if ($prod) {
$prod->setPrices($this->ModelProd->getPrices($prod));
}
$photos = $this->Photos->findBy(['type' => 'prod', 'par' => $prod->getId()]);
$photosarray = [];
foreach ($photos as $photo) {
$obj = new stdClass();
$obj->id = $photo->getId();
$photosarray[] = $obj;
}
$newprod = [
'name' => $prod->getName(),
'labels' => $this->render('catalog/block/prod-labels.html.twig', [
'prodone' => $prod,
])->getContent(),
'price' => $this->render('catalog/block/prod-price.html.twig', [
'prodone' => $prod,
])->getContent(),
'inpack' => $this->render('catalog/block/prod-inpack.html.twig', [
'prodone' => $prod,
])->getContent(),
'weight' => $this->render('catalog/block/prod-weight.html.twig', [
'prodone' => $prod,
])->getContent(),
'form' => $this->render('catalog/block/prod-form.html.twig', [
'prodone' => $prod,
])->getContent(),
'wishlist' => $this->render('catalog/block/prod-wishlist.html.twig', [
'prodone' => $prod,
])->getContent(),
'photos' => $photosarray,
];
return $this->render('catalog/ajax.html.twig', [
'prod' => json_encode($newprod),
]);
}
#[Route(path: '/catalog/getnum/{cat_id}', name: 'getnum_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route(path: '/catalog/{spec}/getnum/{cat_id}', name: 'getnum_spec_no_locale', defaults: ['_locale' => '%app.default_lang%'], requirements: ['spec' => 'new|action|pop|onsale|mix|search'])]
#[Route(path: '/{_locale}/catalog/getnum/{cat_id}', name: 'getnum', requirements: ['_locale' => '%app.langs%'])]
#[Route(path: '/{_locale}/catalog/{spec}/getnum/{cat_id}', name: 'getnum_spec', requirements: ['_locale' => '%app.langs%', 'spec' => 'new|action|pop|onsale|mix|search'])]
public function getNum(Request $request, Prods $ModelProd, Chars $ModelChar, int $cat_id = 0, string $spec = ''): Response
{
$result = [];
$prods = $ModelProd->getProds($cat_id, $spec);
$chars = $ModelChar->getChars($cat_id);
$charvals = $ModelChar->getCharVals($cat_id);
$get_chars = $ModelProd->getchars($request->query->all());
//$result = array("num" => count($ModelProd->filter($request->query->all(), $prods)));
$prods_filtered = $this->Prods->filter($prods, $get_chars);
$result = array("num" => count($prods_filtered));
foreach ($chars as $k => $char) {
foreach ($charvals[$char->getId()] as $k => $cv) {
$result['prodcount'][$cv->getId()] = $ModelProd->prodcount($prods, $get_chars, $char->getId(), $cv->getId());
}
$result['prodcount']['action'] = $ModelProd->prodcount($prods, $get_chars, 'action', 1);
$result['prodcount']['new'] = $ModelProd->prodcount($prods, $get_chars, 'new', 1);
$result['prodcount']['pop'] = $ModelProd->prodcount($prods, $get_chars, 'pop', 1);
$result['prodcount']['mix'] = $ModelProd->prodcount($prods, $get_chars, 'mix', 1);
}
if (is_array($get_chars)) {
$result['chars'] = array_keys($get_chars);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($get_chars));
foreach ($it as $v) {
$result['charvals'][] = $v;
}
} else {
$result['charvals'] = array();
$result['chars'] = array();
}
return $this->json($result);
}
private function _makeFilterUrl(Request $request, string $spec): string
{
$url_redir = $request->getPathInfo() . Filter::generate_url($request->query->all());
return $url_redir;
}
}