src/Service/Filter/Cats.php line 165

Open in your IDE?
  1. <?php
  2. namespace App\Service\Filter;
  3. use App\Env;
  4. use App\Entity\Cat;
  5. use App\Service\Auth\Auth;
  6. use Doctrine\ORM\Mapping\Id;
  7. use App\Repository\CatRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Contracts\Cache\ItemInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  13. class Cats
  14. {
  15.     private RequestStack $rs;
  16.     private Auth $Auth;
  17.     private EntityManagerInterface $em;
  18.     private TagAwareCacheInterface $Cache;
  19.     private CatRepository $Cats;
  20.     private Env $Env;
  21.     private Security $security;
  22.     private $cats = [];
  23.     private $cat_menu = [];
  24.     private $cat_tree = [];
  25.     public function __construct(RequestStack $rsAuth $AuthCatRepository $CatsEntityManagerInterface $emTagAwareCacheInterface $appCacheEnv $EnvSecurity $security)
  26.     {
  27.         $this->rs $rs;
  28.         $this->Auth $Auth;
  29.         $this->Auth->setUser($security->getUser());
  30.         $this->security $security;
  31.         $this->em $em;
  32.         $this->Cache $appCache;
  33.         $this->Env $Env;
  34.         $this->Cats $Cats;
  35.         $this->init();
  36.     }
  37.     public function init()
  38.     {
  39.         $request $this->rs->getCurrentRequest();
  40.         $test $request->get('test');
  41.         $q $request->get('q');
  42.         $cats_array = [
  43.             "cats" => [],
  44.             "cat_menu" => [],
  45.             "cat_tree" => [],
  46.         ];
  47.         $cats_array $this->Cache->get('cat_tree3_'.$request->getLocale(), function(ItemInterface $item) {
  48.             $cats = [];
  49.             $cats_temp = [];
  50.             $cat_menu = [];
  51.             $cat_tree = [];
  52.             
  53.             $item->expiresAfter(3000000);
  54.             $item->tag(['cat']);
  55.             //$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();
  56.             $cats_brom_db $this->Cats->findBy(['visible' => 1], ['prior' => 'DESC''id' => 'ASC']);
  57.             
  58.             foreach ($cats_brom_db as $k => $v) {
  59.                 $cats_temp[] = [
  60.                     "id" => $v->getId(),
  61.                     "intname" => $v->getIntname(),
  62.                     "cat" => $v->getCat(),
  63.                     "name" => $v->getName(),
  64.                     "newlabel" => $v->isNewlabel(),
  65.                 ];
  66.             }
  67.             $this->cat_all($cats$cats_temp0);
  68.             $this->cat_subtree($cat_menu$cats_temp0);
  69.             $this->cat_all_subcats($cat_tree$cat_menu);
  70.             $c = [
  71.                 "cats" => $cats,
  72.                 "cat_menu" => $cat_menu,
  73.                 "cat_tree" => $cat_tree,
  74.             ];
  75.             return $c;
  76.         });
  77.         $this->cats $cats_array['cats'];
  78.         $this->cat_menu $cats_array['cat_menu'];
  79.         $this->cat_tree $cats_array['cat_tree'];
  80.     }
  81.     public function getCats(): array
  82.     {
  83.         return $this->cats;
  84.     }
  85.     public function getCatMenu()
  86.     {
  87.         return $this->cat_menu;
  88.     }
  89.     public function getSpecCatMenu(int $cat_idstring $specstring $q '')
  90.     {        
  91.         $Prods = new Prods($this->rs$this->Auth$this->em$this->Cache$this->Env$this$this->security);
  92.         $prods $Prods->getProds($cat_id$spec$q);
  93.         
  94.         $this->cat_prod_count($prods);
  95.         foreach ($this->cat_menu as $c_id => $subcats) {
  96.             foreach ($subcats as $k => $v) {
  97.                 $this->cat_menu[$c_id][$k]['prods'] = $this->cats[$v['id']][$spec] ?? 0;
  98.             }
  99.         }        
  100.         return $this->cat_menu;
  101.     }
  102.     public function getCatTree()
  103.     {
  104.         return $this->cat_tree;
  105.     }
  106.     private function cat_all(&$cats, &$cats_temp)
  107.     {
  108.         foreach ($cats_temp as $v) {
  109.             $cats[$v['id']] = $v;
  110.         }
  111.     }
  112.     private function cat_subtree(&$cat_menu, &$cats$start)
  113.     {
  114.         foreach ($cats as $k => $v) {
  115.             if ($v['cat'] == $start) {
  116.                 $cat_menu[$start][] = $v;
  117.                 $this->cat_subtree($cat_menu$cats$v['id']);
  118.             }
  119.         }
  120.     }
  121.     private function cat_all_subcats(&$cat_tree, &$cat_menu)
  122.     {        
  123.         foreach ($cat_menu[0] as $k => $c) { // Перебор всех категорий высшего уровня
  124.             $cat_tree[$c['id']] = array();
  125.             if (!isset($cat_menu[$c['id']])) continue;
  126.             foreach ($cat_menu[$c['id']] as $kk => $sc) { // Пребор всех подкатегорий второго уровня                
  127.                 $cat_tree[$sc['id']] = array();
  128.                 $cat_tree[$c['id']][] = $sc['id'];
  129.                 if (!empty($cat_menu[$sc['id']])) {
  130.                     foreach ($cat_menu[$sc['id']] as $kkk => $ssc) { // Перебор категорий третьего уровня
  131.                         $cat_tree[$c['id']][] = $ssc['id'];
  132.                         $cat_tree[$sc['id']][] = $ssc['id'];
  133.                         $cat_tree[$ssc['id']] = array();
  134.                     }
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     public function getCatTreeWhere($start)
  140.     {
  141.         $cats $this->cat_tree;
  142.         $i 0;
  143.         if (count($cats[$start]) < 1) {
  144.             $q "(cat = $start)";
  145.         } else {
  146.             $q "(";
  147.             foreach ($cats[$start] as $k => $cat) {
  148.                 if ($i++ > 0) {
  149.                     $q .= " or ";
  150.                 }
  151.                 $q .= "cat = ".$cat;
  152.             }
  153.             $q .= " or cat = $start)";
  154.         }
  155.         return $q;
  156.     }
  157.     /**
  158.      * 
  159.      * @param array $prods 
  160.      * @return void 
  161.      */
  162.     private function cat_prod_count(array $prods)
  163.     {
  164.         $cat_tree $this->cat_tree;
  165.         foreach ($cat_tree as $k => $v) {
  166.             $cat_tree[$k][] = $k;
  167.         }
  168.         foreach ($this->cats as $cat_id => $cat) {
  169.             $this->cats[$cat_id]['prods'] = 0;
  170.             $this->cats[$cat_id]['new'] = 0;
  171.             $this->cats[$cat_id]['pop'] = 0;
  172.             $this->cats[$cat_id]['action'] = 0;
  173.             $this->cats[$cat_id]['mix'] = 0;
  174.             $this->cats[$cat_id]['onsale'] = 0;
  175.             $this->cats[$cat_id]['search'] = 0;
  176.             foreach ($prods as $prod) {
  177.                 if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id])) {
  178.                     $this->cats[$cat_id]['prods']++;
  179.                 }
  180.                 if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && $prod['new']) {
  181.                     $this->cats[$cat_id]['new']++;
  182.                 }
  183.                 if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && $prod['pop']) {
  184.                     $this->cats[$cat_id]['pop']++;
  185.                 }
  186.                 if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && (($this->Auth->isOpt() && $prod['skidkaopt']) || (!$this->Auth->isOpt() && $prod['skidka']))) {
  187.                     $this->cats[$cat_id]['action']++;
  188.                 }
  189.                 if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && $prod['mix']) {
  190.                     $this->cats[$cat_id]['mix']++;
  191.                 }
  192.                 if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id]) && $prod['onsale']) {
  193.                     $this->cats[$cat_id]['onsale']++;
  194.                 }
  195.                 if (isset($cat_tree[$cat_id]) && in_array($prod['cat'], $cat_tree[$cat_id])) {
  196.                     $this->cats[$cat_id]['search']++;
  197.                 }
  198.             }
  199.         }
  200.     }
  201. }