<?php
namespace App\Service\Filter;
use App\Env;
use App\Entity\Cat;
use App\Service\Auth\Auth;
use Doctrine\ORM\Mapping\Id;
use App\Repository\CatRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
class Cats
{
private RequestStack $rs;
private Auth $Auth;
private EntityManagerInterface $em;
private TagAwareCacheInterface $Cache;
private CatRepository $Cats;
private Env $Env;
private Security $security;
private $cats = [];
private $cat_menu = [];
private $cat_tree = [];
public function __construct(RequestStack $rs, Auth $Auth, CatRepository $Cats, EntityManagerInterface $em, TagAwareCacheInterface $appCache, Env $Env, Security $security)
{
$this->rs = $rs;
$this->Auth = $Auth;
$this->Auth->setUser($security->getUser());
$this->security = $security;
$this->em = $em;
$this->Cache = $appCache;
$this->Env = $Env;
$this->Cats = $Cats;
$this->init();
}
public function init()
{
$request = $this->rs->getCurrentRequest();
$test = $request->get('test');
$q = $request->get('q');
$cats_array = [
"cats" => [],
"cat_menu" => [],
"cat_tree" => [],
];
$cats_array = $this->Cache->get('cat_tree3_'.$request->getLocale(), function(ItemInterface $item) {
$cats = [];
$cats_temp = [];
$cat_menu = [];
$cat_tree = [];
$item->expiresAfter(3000000);
$item->tag(['cat']);
//$cats_temp = $this->em->createQuery('SELECT c FROM App\Entity\Cat c WHERE c.visible = 1 ORDER BY c.prior DESC, c.name ASC')->getResult();
$cats_brom_db = $this->Cats->findBy(['visible' => 1], ['prior' => 'DESC', 'id' => 'ASC']);
foreach ($cats_brom_db as $k => $v) {
$cats_temp[] = [
"id" => $v->getId(),
"intname" => $v->getIntname(),
"cat" => $v->getCat(),
"name" => $v->getName(),
"newlabel" => $v->isNewlabel(),
];
}
$this->cat_all($cats, $cats_temp, 0);
$this->cat_subtree($cat_menu, $cats_temp, 0);
$this->cat_all_subcats($cat_tree, $cat_menu);
$c = [
"cats" => $cats,
"cat_menu" => $cat_menu,
"cat_tree" => $cat_tree,
];
return $c;
});
$this->cats = $cats_array['cats'];
$this->cat_menu = $cats_array['cat_menu'];
$this->cat_tree = $cats_array['cat_tree'];
}
public function getCats(): array
{
return $this->cats;
}
public function getCatMenu()
{
return $this->cat_menu;
}
public function getSpecCatMenu(int $cat_id, string $spec, string $q = '')
{
$Prods = new Prods($this->rs, $this->Auth, $this->em, $this->Cache, $this->Env, $this, $this->security);
$prods = $Prods->getProds($cat_id, $spec, $q);
$this->cat_prod_count($prods);
foreach ($this->cat_menu as $c_id => $subcats) {
foreach ($subcats as $k => $v) {
$this->cat_menu[$c_id][$k]['prods'] = $this->cats[$v['id']][$spec] ?? 0;
}
}
return $this->cat_menu;
}
public function getCatTree()
{
return $this->cat_tree;
}
private function cat_all(&$cats, &$cats_temp)
{
foreach ($cats_temp as $v) {
$cats[$v['id']] = $v;
}
}
private function cat_subtree(&$cat_menu, &$cats, $start)
{
foreach ($cats as $k => $v) {
if ($v['cat'] == $start) {
$cat_menu[$start][] = $v;
$this->cat_subtree($cat_menu, $cats, $v['id']);
}
}
}
private function cat_all_subcats(&$cat_tree, &$cat_menu)
{
foreach ($cat_menu[0] as $k => $c) { // Перебор всех категорий высшего уровня
$cat_tree[$c['id']] = array();
if (!isset($cat_menu[$c['id']])) continue;
foreach ($cat_menu[$c['id']] as $kk => $sc) { // Пребор всех подкатегорий второго уровня
$cat_tree[$sc['id']] = array();
$cat_tree[$c['id']][] = $sc['id'];
if (!empty($cat_menu[$sc['id']])) {
foreach ($cat_menu[$sc['id']] as $kkk => $ssc) { // Перебор категорий третьего уровня
$cat_tree[$c['id']][] = $ssc['id'];
$cat_tree[$sc['id']][] = $ssc['id'];
$cat_tree[$ssc['id']] = array();
}
}
}
}
}
public function getCatTreeWhere($start)
{
$cats = $this->cat_tree;
$i = 0;
if (count($cats[$start]) < 1) {
$q = "(cat = $start)";
} else {
$q = "(";
foreach ($cats[$start] as $k => $cat) {
if ($i++ > 0) {
$q .= " or ";
}
$q .= "cat = ".$cat;
}
$q .= " or cat = $start)";
}
return $q;
}
/**
*
* @param array $prods
* @return void
*/
private function cat_prod_count(array $prods)
{
$cat_tree = $this->cat_tree;
foreach ($cat_tree as $k => $v) {
$cat_tree[$k][] = $k;
}
foreach ($this->cats as $cat_id => $cat) {
$this->cats[$cat_id]['prods'] = 0;
$this->cats[$cat_id]['new'] = 0;
$this->cats[$cat_id]['pop'] = 0;
$this->cats[$cat_id]['action'] = 0;
$this->cats[$cat_id]['mix'] = 0;
$this->cats[$cat_id]['onsale'] = 0;
$this->cats[$cat_id]['search'] = 0;
foreach ($prods as $prod) {
if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id])) {
$this->cats[$cat_id]['prods']++;
}
if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && $prod['new']) {
$this->cats[$cat_id]['new']++;
}
if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && $prod['pop']) {
$this->cats[$cat_id]['pop']++;
}
if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && (($this->Auth->isOpt() && $prod['skidkaopt']) || (!$this->Auth->isOpt() && $prod['skidka']))) {
$this->cats[$cat_id]['action']++;
}
if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && $prod['mix']) {
$this->cats[$cat_id]['mix']++;
}
if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && $prod['onsale']) {
$this->cats[$cat_id]['onsale']++;
}
if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id])) {
$this->cats[$cat_id]['search']++;
}
}
}
}
}