src/Service/Filter/Prods.php line 164

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