src/Service/Filter/Prods.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Service\Filter;
  3. use App\Env;
  4. use App\Service\Auth\Auth;
  5. use App\Service\Search\SearchProd;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  10. class Prods
  11. {
  12.     private $prods = [];
  13.     private $pc = [];
  14.     private $prod_count 0;
  15.     public function __construct(
  16.         private RequestStack $rs
  17.         private Auth $Auth
  18.         private EntityManagerInterface $em
  19.         private TagAwareCacheInterface $appCache
  20.         private Env $Env,
  21.         private Security $security,
  22.         private SearchProd $searchProd,
  23.         private CatTree $catTree,
  24.     )
  25.     {
  26.          $this->Auth->setUser($security->getUser());
  27.     }
  28.     public function getFromDb(int $cat 0string $spec ''string $q ''int $id 0)
  29.     {
  30.         $request $this->rs->getCurrentRequest();
  31.         $test $request->get('test');
  32.         $q $q $q $request->get('q');
  33.             
  34.         $prods_temp_q $prods_temp = [];
  35.         
  36.         $opt $this->Auth->isOpt() ? 'opt' '';
  37.         $where "visible = 1 and (num > 0 or num2 > 0 or num3 > 0)";
  38.     
  39.         if ($cat) {
  40.             $where .= " and ".$this->catTree->getCatTreeWhere($cat);
  41.         }
  42.         
  43.         switch($spec) {
  44.             case 'new':
  45.                 $where .= " and `new` = 1";            
  46.                 break;
  47.             case 'pop':
  48.                 $where .= " and `pop` = 1";
  49.                 break;
  50.             case 'onsale':
  51.                 $where .= " and `onsale` = 1";
  52.                 break;
  53.             case 'mix':
  54.                 $where .= " and `mix` = 1";
  55.                 break;    
  56.             case 'action':
  57.                 if($this->Auth->isOpt()) $where .= " and `skidkaopt` > 0";
  58.                 else $where .= " and `skidka` > 0";
  59.                 break;
  60.             case 'search':
  61.                 $lang $this->rs->getCurrentRequest()->getLocale();
  62.                 $ids $this->searchProd->search($lang$q);
  63.                 if (count($ids)) {
  64.                     $where .= " and (id in (".implode(","$ids).") or art like '%$q%')";
  65.                 } else {
  66.                     $where .= " and (id = -1 or art like '%$q%')";
  67.                 }                
  68.                 
  69.                 break;
  70.         }
  71.         
  72.         $select "id, cat, intname, name, art, price, price2, price3, priceopt, priceopt2, priceopt3, skidka, skidka2, skidka3, skidkaopt, skidkaopt2, skidkaopt3, numdiscount, numdiscount2, numdiscount3, chars, numdiscountopt, numdiscountopt2, numdiscountopt3, num, num2, num3, weight, weight2, weight3, inpack, inpack2, inpack3, pop, new, mix, action, onsale, prior, ordered, changed, colors";
  73.         if ($id) {
  74.             $q "SELECT $select FROM prod WHERE id = $id";
  75.             $prods_temp $this->em->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  76.         } elseif ($q) {            
  77.             $q "SELECT $select FROM prod WHERE $where";
  78.             $prods_temp_q $this->em->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  79.         } else {
  80.             $q "SELECT $select FROM prod WHERE $where";
  81.             $prods_temp $this->em->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  82.         }
  83.         
  84.         $this->pc = [];
  85.         $this->prods = [];
  86.         
  87.         if (is_array($prods_temp) && count($prods_temp)) {
  88.             foreach ($prods_temp as $kc => $p) {
  89.                 $p['colors'] = json_decode($p['colors'], true);
  90.                 $this->prods[$p['id']] = $p;
  91.                 $chars explode(";"$p['chars']);
  92.                 foreach ($chars as $char) {
  93.                     $cv explode(":"$char);
  94.                     if ($cv[0] != 'onsale' && $cv[0] != 'pop' && $cv[0] != 'action' && $cv[0] != 'new' && $cv[0] != 'mix' && isset($cv[1]) && $cv[1] > 0) {
  95.                         $this->pc[$cv[0]][] = $cv[1];
  96.                     }
  97.                 }
  98.             }
  99.             unset($prods_temp);
  100.         }        
  101.         
  102.         if (is_array($prods_temp_q) && count($prods_temp_q)) {
  103.             foreach ($prods_temp_q as $kc => $p) {
  104.                 $p['colors'] = json_decode($p['colors'], true);
  105.                 $this->prods[$p['id']] = $p;
  106.                 $chars explode(";"$p['chars']);
  107.                 foreach ($chars as $char) {
  108.                     $cv explode(":"$char);
  109.                     
  110.                     if ($cv[0] != 'onsale' && $cv[0] != 'pop' && $cv[0] != 'action' && $cv[0] != 'new' && $cv[0] != 'mix' && isset($cv[1]) && $cv[1] > 0) {
  111.                         $this->pc[$cv[0]][] = $cv[1];
  112.                     }
  113.                 }
  114.             }
  115.             unset($prods_temp_q);
  116.         }    
  117.         
  118.         // $prod_ids = [];
  119.         // foreach ($this->prods as $k => $v) {
  120.         //     $prod_ids[] = $v['id'];
  121.         // }
  122.         
  123.         // //TODO Alex
  124.         // $this->prods = $this->em->createQuery('SELECT p from App\Entity\Prod p WHERE p.id in (:ids)')
  125.         //     ->setParameter('ids', $prod_ids)
  126.         //     //->setMaxResults(50)
  127.         //     ->getResult();
  128.         return $this->prods;
  129.     }
  130.     public function getProd($id 0)
  131.     {
  132.         $prods $this->getFromDb(0''''$id);
  133.         
  134.         if ($prods) {
  135.             return reset($prods);
  136.         } else {
  137.             return [];
  138.         }        
  139.     }   
  140.     public function getProds(int $cat 0string $spec ''string $search ''): array
  141.     {
  142.         if (empty($this->prods)) {
  143.             return $this->getFromDb($cat$spec$search);
  144.         }
  145.         return $this->prods;
  146.     }   
  147.     public function getPC(int $cat 0string $spec ''): array
  148.     {
  149.         if (empty($this->pc)) {
  150.             $this->getFromDb($cat$spec);
  151.         }
  152.         return $this->pc;
  153.     }
  154.     public function getchars(array $get_params)
  155.     {
  156.         $filter = [];
  157.         foreach ($get_params as $k => $v) {
  158.             if ($k == 'getnum' || $k == 'filter' || $k == 'q') {
  159.                 continue;
  160.             }
  161.             
  162.             if (isset($get_params[$k]) && is_array($get_params[$k])) {
  163.                 $filter[str_replace('char'''$k)] = [];
  164.                 foreach ($get_params[$k] as $vvv) {
  165.                     array_push($filter[str_replace('char'''$k)], $vvv);
  166.                 }
  167.             } else {
  168.                 $filter[str_replace('char'''$k)] = $v;
  169.             }
  170.         }
  171.         return $filter;
  172.     }
  173.     /**
  174.      * 
  175.      * @param array $get_params 
  176.      * @param \App\Entity\Prod[] $prods 
  177.      * @param array $chars 
  178.      * @return array 
  179.      */
  180.     public function filter (array $get_params, array $prods, array $chars = array()) {
  181.         $qwe '';
  182.         if (empty($chars)) {
  183.             $filter $this->getchars($get_params);
  184.         } else {
  185.             $filter $chars;
  186.         }
  187.         foreach($filter as $k => $char) {
  188.             if($char == 'action' && $this->Auth->isOpt()) {
  189.                 $filter[$k] = 'actionopt';
  190.             }
  191.         }
  192.         
  193.         if (isset($filter['sale']) && $filter['sale'] == 1) {
  194.             $filter[$filter['sale']] = "1";
  195.             unset($filter['sale']);
  196.         }
  197.         
  198.         $ids = array();
  199.         if (empty($filter)) {
  200.             return $prods;
  201.         }
  202.         $fc count($filter);
  203.         
  204.         foreach ($prods as $k => $prod) {
  205.             if (empty($prod->getChars())) {
  206.                 continue;
  207.             }
  208.             $filter_vals_founded 0;
  209.             
  210.             foreach ($filter as $c => $cv) {
  211.                 if (is_array($cv) && count($cv) > 1) { // Выбрано нескольно значений одной характеристики
  212.                     foreach ($cv as $ccv) {
  213.                         if (strstr($prod->getChars(), $c.":".$ccv.";")) { // Если хотя бы одно значение найдено, то значение считается найдено
  214.                             $qwe .= $c.":".$ccv.";";
  215.                             $filter_vals_founded++;
  216.                             break;
  217.                         }
  218.                     }
  219.                 } else { // Выбрано ОДНО значение одной характеристики
  220.                     if (is_array($cv)) {
  221.                         $cv $cv[0];
  222.                     }
  223.                     if (strstr($prod->getChars(), $c.":".$cv.";")) {
  224.                         $filter_vals_founded++;
  225.                     }                    
  226.                 }
  227.             }
  228.             if ($filter_vals_founded >= $fc) { // Если найдены все значения характеристик, помещаем товар в массив
  229.                 $ids[] = $prod;
  230.             }    
  231.         }
  232.         
  233.         return $ids;
  234.     }
  235.     /**
  236.      * 
  237.      * @param \App\Entity\Prod[] $prods 
  238.      * @param array $chars 
  239.      * @param mixed $char 
  240.      * @param mixed $charval 
  241.      * @return int 
  242.      */
  243.     public function prodcount (array $prods, array $chars$char$charval)
  244.     {
  245.         $filter $chars;
  246.         $fc count($filter);
  247.         $cnt 0;
  248.         $qwe '';
  249.         
  250.         if ($char == 'action' && $this->Auth->isOpt()) {
  251.             $char 'actionopt';
  252.         }
  253.         
  254.         if (isset($filter['sale'])) {
  255.             $filter[$filter['sale']] = "1";
  256.             unset($filter['sale']);
  257.         }
  258.         
  259.         foreach ($prods as $k => $prod) {    
  260.             if (empty($prod['chars'])) {
  261.                 continue;
  262.             }
  263.             
  264.             if (!empty($filter)) {
  265.                 $filter_vals_founded 0;
  266.             
  267.                 foreach ($filter as $c => $cv) {
  268.                     if (is_array($cv) && count($cv) > 1) { // Выбрано нескольно значений одной характеристики
  269.                         foreach ($cv as $ccv) {
  270.                             if (strstr($prod['chars'], $c.":".$ccv.";") || $c == $char) { // Если хотя бы одно значение найдено, то значение считается найдено
  271.                                 $qwe .= $c.":".$ccv.";";
  272.                                 $filter_vals_founded++;
  273.                                 break;
  274.                             }
  275.                         }
  276.                     } else { // Выбрано ОДНО значение одной характеристики
  277.                         if (is_array($cv)) {
  278.                             $cv $cv[0];
  279.                         }
  280.                         if (strstr($prod['chars'], $c.":".$cv.";") || $c == $char) {
  281.                             $filter_vals_founded++;
  282.                         }                    
  283.                     }
  284.                 }
  285.                 if (strstr($prod['chars'], $char.":".$charval.";") && $filter_vals_founded >= $fc) { // Прибавляем 1 к значению характеристики в фильтре с учетом того, что она соответствуюе всем остальным условиям
  286.                     $cnt++;
  287.                 }    
  288.             } else { // фильтр не задан
  289.                 if (strstr($prod['chars'], $char.":".$charval.";")) {
  290.                     $cnt++;
  291.                 }
  292.             }
  293.         }
  294.         return $cnt;
  295.     }
  296. }