src/Repository/ProdRepository.php line 239

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Cart;
  4. use App\Entity\Prod;
  5. use App\Model\Prod as ModelProd;
  6. use App\RepositoryInterface\ProdRepositoryInterface;
  7. use App\Service\Auth\Auth;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  10. /**
  11.  * @extends ServiceEntityRepository<Prod>
  12.  *
  13.  * @method Prod|null find($id, $lockMode = null, $lockVersion = null)
  14.  * @method Prod|null findOneBy(array $criteria, array $orderBy = null)
  15.  * @method Prod[]    findAll()
  16.  * @method Prod[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  17.  */
  18. class ProdRepository extends ServiceEntityRepository implements ProdRepositoryInterface
  19. {
  20.     private ModelProd $ModelProd;
  21.     public function __construct(ManagerRegistry $registryModelProd $ModelProd, protected CharRepository $Chars, protected CharvalRepository $Charvals)
  22.     {
  23.         $this->ModelProd $ModelProd;
  24.         parent::__construct($registryProd::class);
  25.     }
  26.     public function save(): void
  27.     {
  28.         $this->getEntityManager()->flush();
  29.     }
  30.     public function add(Prod $entitybool $flush false): void
  31.     {
  32.         $this->getEntityManager()->persist($entity);
  33.         if ($flush) {
  34.             $this->getEntityManager()->flush();
  35.         }
  36.     }
  37.     public function remove(Prod $entitybool $flush false): void
  38.     {
  39.         $this->getEntityManager()->remove($entity);
  40.         if ($flush) {
  41.             $this->getEntityManager()->flush();
  42.         }
  43.     }
  44.     
  45.     public function get(int $id): ?Prod {
  46.         return $this->find($id);
  47.     }
  48.     public function getNewProds(int $results): array
  49.     {
  50.         return $this->getEntityManager()->createQuery('SELECT p FROM App\Entity\Prod p WHERE p.visible = 1 and p.skidka > 0 and (p.num != 0 or p.num2 != 0 or p.num3 != 0) ORDER BY p.changed desc')
  51.             ->setMaxResults(12)
  52.             ->getResult();
  53.     }
  54.     public function getallforexport(array $cats_for_export$opt 0): array
  55.     {
  56.         $cats = [];
  57.         foreach ($cats_for_export as $cat) {
  58.             $cats[$cat->getId()] = $cat->getId();
  59.         }
  60.         $prods = array();        
  61.         $prods $this->getEntityManager()->createQuery("SELECT p FROM \App\Entity\Prod p WHERE p.visible = 1 AND p.price > 0 AND p.num > 0 AND p.name != '' and p.cat in (".implode(","$cats).") ORDER By p.id")->getResult();
  62.         return $prods;
  63.     }
  64.     
  65.     public function getprodchars(int $prod_id): array
  66.     {
  67.         $prod_chars = array();
  68.         
  69.         $q "select c.id as cid, c.name as cname, c.izm, cv.id as cvid, cv.value as value, pc.charval as val, pc.value as text 
  70.             from `prodchar` as pc 
  71.             left join `char` as c on c.id = pc.`char` 
  72.             left join `charval` as cv on cv.id = pc.charval 
  73.             where c.inprod = 1 and pc.prod = '".$prod_id."' and cv.value != ''";
  74.         $prod_chars $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  75.         foreach ($prod_chars as $k => $v) {
  76.             $char $this->Chars->find($v['cid']);
  77.             $charval $this->Charvals->find($v['cvid']);
  78.             $prod_chars[$k]['cname'] = $char->getName();
  79.             $prod_chars[$k]['value'] = $charval->getValue();
  80.         }
  81.         
  82.         return $prod_chars;
  83.     }
  84.     public function getprodchars_all(): array
  85.     {
  86.         $prod_chars = array();
  87.         $q "select pc.prod as pid, c.id as cid, c.name as cname, c.izm, cv.value as value, pc.charval as val, pc.value as text 
  88.             from `prodchar` as pc 
  89.             left join `char` as c on c.id = pc.`char` 
  90.             left join `charval` as cv on cv.id = pc.charval
  91.             where cv.value != ''";
  92.         $pc $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  93.         foreach ($pc as $r) {
  94.             $char $this->Chars->find($r['cid']);
  95.             $charval $this->Charvals->find($r['cvid']);
  96.             $r['cname'] = $char->getName();
  97.             $r['value'] = $charval->getValue();
  98.             $prod_chars[$r['pid']][$r['cid']] = $r;            
  99.         }
  100.         return $prod_chars;
  101.     }
  102.     public function getprodchilds(int $idbool $opt false): array
  103.     {
  104.         $opt $opt 'opt' '';
  105.         
  106.         $prods = [];
  107.         $prod_ids = [];
  108.         /** @var \App\Repository\CartRepository $Cart */
  109.         $Cart $this->getEntityManager()->getRepository(Cart::class);
  110.         $cart $Cart->findOneBy(['prod' => $id]);
  111.         //find findgetone(array("where" => "prod = ".$id));
  112.         if ($cart && $cart->getOrderId()) {
  113.             $q "select p.id, p.art, p.name, p.num, p.num2, p.num3, p.price".$opt." as price, p.price2".$opt." as price2, p.price3".$opt." as price3, p.colors, p.skidka".$opt." as skidka, p.skidka".$opt."2 as skidka2, p.skidka".$opt."3 as skidka3, p.inpack 
  114.                     from cart as c
  115.                     left join prod as p on p.id = c.prod_id 
  116.                     where c.order_id = ".$cart->getOrderId()." and p.visible = 1 and (p.num > 0 or p.num2 > 0 or p.num3 > 0) and p.id != ".$id." limit 6";
  117.             $pc $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  118.             foreach ($pc as $r) {
  119.                 $r['colors'] = json_decode($r['colors']);
  120.                 $prods[$r['id']] = $r;
  121.                 //$prods[$r['id']]->prices = $this->ModelProd->getPrices($r);
  122.             }
  123.         }
  124.         foreach ($prods as $k => $prod) {
  125.             $prod_ids[] = $prod['id'];
  126.         }
  127.         
  128.         if (count($prod_ids) == 0) {
  129.             return [];
  130.         }
  131.         $prods $this->findBy(['id' => $prod_ids]);
  132.         foreach ($prods as $k => $prod) {
  133.             $prods[$k]->prices $this->ModelProd->getPrices($prod);
  134.         }
  135.         
  136.         return $prods;
  137.     }
  138.     function filter_selected ($char$val
  139.     {
  140.         $filter $this->getchars();
  141.     
  142.         if ($filter[$char] && ((is_array($filter[$char]) && in_array($val$filter[$char])) || $filter[$char] == $val)) {
  143.             return true;
  144.         } else {
  145.             return false;
  146.         }
  147.     }
  148.     function filter ($prods$chars = array(), bool $is_opt false) {
  149.         $qwe '';
  150.         if (empty($chars)) {
  151.             return $prods;
  152.         }
  153.         $filter $chars;
  154.         foreach($filter as $k => $char) {
  155.             if($char == 'action' && $is_opt) {
  156.                 $filter[$k] = 'actionopt';
  157.             }
  158.         }
  159.         
  160.         if (isset($filter['sale']) && $filter['sale']) {
  161.             $filter[$filter['sale']] = "1";
  162.             unset($filter['sale']);
  163.         } else {
  164.             unset($filter['sale']);
  165.         }
  166.         
  167.         $ids = array();
  168.         
  169.         if (empty($filter)) {
  170.             return $prods;
  171.         }
  172.         
  173.         $fc count($filter);
  174.         
  175.         foreach ($prods as $k => $prod) {
  176.             if (empty($prod['chars'])) {
  177.                 continue;
  178.             }
  179.             $filter_vals_founded 0;
  180.             
  181.             foreach ($filter as $c => $cv) {
  182.                 if (is_array($cv) && count($cv) > 1) { // Выбрано нескольно значений одной характеристики
  183.                     foreach ($cv as $ccv) {
  184.                         if (strstr($prod['chars'], $c.":".$ccv.";")) { // Если хотя бы одно значение найдено, то значение считается найдено
  185.                             $qwe .= $c.":".$ccv.";";
  186.                             $filter_vals_founded++;
  187.                             break;
  188.                         }
  189.                     }
  190.                 } else { // Выбрано ОДНО значение одной характеристики
  191.                     if (is_array($cv)) {
  192.                         $cv $cv[0];
  193.                     }
  194.                     if (strstr($prod['chars'], $c.":".$cv.";")) {
  195.                         $filter_vals_founded++;
  196.                     }                    
  197.                 }
  198.             }
  199.             if ($filter_vals_founded >= $fc) { // Если найдены все значения характеристик, помещаем товар в массив
  200.                 $ids[] = $prod;
  201.             }    
  202.         }
  203.                 
  204.         return $ids;
  205.     }
  206.     
  207.     /**
  208.      * 
  209.      * @param array $ids 
  210.      * @return Prod[] 
  211.      */
  212.     public function prodArrayToEntity(array $prods): array
  213.     {
  214.         $ids = [];
  215.     
  216.         foreach ($prods as $k => $v) {
  217.             $ids[] = $v['id'];
  218.         }
  219.         $prods $this->findBy(['id' => $ids]);
  220.         foreach ($prods as $k => $prod) {
  221.             $prods[$k]->prices $this->ModelProd->getPrices($prod);
  222.         }
  223.         
  224.         return $prods;
  225.     }
  226.     public static function sortByChangedAsc($a$b
  227.     {
  228.         if ($a->getChanged() == $b->getChanged()) {
  229.             return 0;
  230.         }
  231.         return ($a->getChanged() < $b->getChanged()) ? : -1;
  232.     }
  233.     public static function sortByChangedDesc($a$b
  234.     {
  235.         if ($a->getChanged() == $b->getChanged()) {
  236.             return 0;
  237.         }
  238.         return ($a->getChanged() > $b->getChanged()) ? : -1;
  239.     }
  240.     
  241.     public static function sortByPriorDesc($a$b)
  242.     {
  243.         if ($a->getPrior() == $b->getPrior()) {
  244.             return 0;
  245.         }
  246.         return ($a->getPrior() > $b->getPrior()) ? -1;
  247.     }
  248.     
  249.     public static function sortByPriceDesc($a$b
  250.     {
  251.         if ($a->getPrice() == $b->getPrice()) {
  252.             return 0;
  253.         }
  254.         return ($a->getPrice() > $b->getPrice()) ? -1;
  255.     }
  256.     
  257.     public static function sortByPriceAsc($a$b
  258.     {
  259.         if ($a->getPrice() == $b->getPrice()) {
  260.             return 0;
  261.         }
  262.         return ($a->getPrice() < $b->getPrice()) ? -1;
  263.     }
  264.     public static function sortBySkidkaDesc($a$b
  265.     {
  266.         if ($a->getSkidka() == $b->getSkidka()) {
  267.             return 0;
  268.         }
  269.         return ($a->getSkidka() > $b->getSkidka()) ? -1;
  270.     }
  271.     public static function sortArrayByChangedAsc($a$b
  272.     {
  273.         if ($a['changed'] == $b['changed']) {
  274.             return 0;
  275.         }
  276.         return ($a['changed'] < $b['changed']) ? : -1;
  277.     }
  278.     public static function sortArrayByChangedDesc($a$b
  279.     {
  280.         if ($a['changed'] == $b['changed']) {
  281.             return 0;
  282.         }
  283.         return ($a['changed'] > $b['changed']) ? : -1;
  284.     }
  285.     
  286.     public static function sortArrayByPriorDesc($a$b)
  287.     {
  288.         if ($a['prior'] == $b['prior']) {
  289.             return 0;
  290.         }
  291.         return ($a['prior'] > $b['prior']) ? -1;
  292.     }
  293.     
  294.     public static function sortArrayByPriceDesc($a$b
  295.     {
  296.         if ($a['price'] == $b['price']) {
  297.             return 0;
  298.         }
  299.         return ($a['price'] > $b['price']) ? -1;
  300.     }
  301.     
  302.     public static function sortArrayByPriceAsc($a$b
  303.     {
  304.         if ($a['price'] == $b['price']) {
  305.             return 0;
  306.         }
  307.         return ($a['price'] < $b['price']) ? -1;
  308.     }
  309.     public static function sortArrayBySkidkaDesc($a$b
  310.     {
  311.         if ($a['skidka'] == $b['skidka']) {
  312.             return 0;
  313.         }
  314.         return ($a['skidka'] > $b['skidka']) ? -1;
  315.     }
  316.     public function getDoubles(): array
  317.     {
  318.         $prods = array();
  319.         $q "select id, name, art, count(art) as count_art
  320.             from `prod`
  321.             group by art
  322.             having count(art) > 1";
  323.         $prods $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  324.         
  325.         return $prods;
  326.     }
  327.     public function getNotInCart(): array
  328.     {
  329.         $prods = array();
  330.         $q "select p.id, p.external_id, p.name, p.art, c.id as cid
  331.             from `prod` as p
  332.             left join `cart` as c on c.prod_id = p.id
  333.             where c.id is NULL";
  334.         $prods $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  335.         
  336.         return $prods;
  337.     }
  338.     public function deleteWithDependencies(int $id)
  339.     {
  340.         $q "delete from prod where id = $id";
  341.         $this->getEntityManager()->getConnection()->prepare($q)->executeQuery();
  342.         $q "delete from prodchar where prod = $id";
  343.         $this->getEntityManager()->getConnection()->prepare($q)->executeQuery();
  344.         $q "delete from prod_translation where object_id = $id";
  345.         $this->getEntityManager()->getConnection()->prepare($q)->executeQuery();        
  346.     }
  347.     public function getProdPics(): array
  348.     {
  349.         $prods = array();
  350.         $q "select id, pic from `prod`";
  351.         $pc $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  352.         foreach ($pc as $r) {
  353.             $prods[] = $r;
  354.         }
  355.         return $prods;
  356.     }
  357.     public function findOneProductsInLocale($ids$locale)
  358.     {
  359.         $qb $this->createQueryBuilder('p')
  360.             ->select('p')
  361.             ->where('p.id in (:id)')
  362.             ->setParameter('id'$ids);
  363.         ;
  364.         $query $qb->getQuery()->useQueryCache(false);
  365.         $query->setHint(
  366.             \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
  367.             'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
  368.         );
  369.         // force Gedmo Translatable to not use current locale
  370.         $query->setHint(
  371.             \Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
  372.             $locale
  373.         );
  374.         $query->setHint(
  375.             \Gedmo\Translatable\TranslatableListener::HINT_FALLBACK,
  376.             1
  377.         );
  378.         
  379.         return $query->getResult();
  380.     }
  381. }