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 string $part 
  210.      * @return Prod[] 
  211.      */
  212.     public function findByArt(string $part): array
  213.     {
  214.         return $this->createQueryBuilder('p')
  215.             ->where('p.art LIKE :part')
  216.             ->andWhere('p.visible = 1')
  217.             ->setParameter('part''%' $part '%')
  218.             ->getQuery()
  219.             ->getResult();
  220.     }
  221.     /**
  222.      * 
  223.      * @param array $ids 
  224.      * @return Prod[] 
  225.      */
  226.     public function prodArrayToEntity(array $prods): array
  227.     {
  228.         $ids = [];
  229.     
  230.         foreach ($prods as $k => $v) {
  231.             $ids[] = $v['id'];
  232.         }
  233.         if (empty($ids)) {
  234.             return [];
  235.         }
  236.             
  237.         $prods $this->findBy(['id' => $ids]);
  238.         foreach ($prods as $k => $prod) {
  239.             $prods[$k]->prices $this->ModelProd->getPrices($prod);
  240.         }
  241.         
  242.         return $prods;
  243.     }
  244.     public static function sortByChangedAsc($a$b
  245.     {
  246.         if ($a->getChanged() == $b->getChanged()) {
  247.             return 0;
  248.         }
  249.         return ($a->getChanged() < $b->getChanged()) ? : -1;
  250.     }
  251.     public static function sortByChangedDesc($a$b
  252.     {
  253.         if ($a->getChanged() == $b->getChanged()) {
  254.             return 0;
  255.         }
  256.         return ($a->getChanged() > $b->getChanged()) ? : -1;
  257.     }
  258.     
  259.     public static function sortByPriorDesc($a$b)
  260.     {
  261.         if ($a->getPrior() == $b->getPrior()) {
  262.             return 0;
  263.         }
  264.         return ($a->getPrior() > $b->getPrior()) ? -1;
  265.     }
  266.     
  267.     public static function sortByPriceDesc($a$b
  268.     {
  269.         if ($a->getPrice() == $b->getPrice()) {
  270.             return 0;
  271.         }
  272.         return ($a->getPrice() > $b->getPrice()) ? -1;
  273.     }
  274.     
  275.     public static function sortByPriceAsc($a$b
  276.     {
  277.         if ($a->getPrice() == $b->getPrice()) {
  278.             return 0;
  279.         }
  280.         return ($a->getPrice() < $b->getPrice()) ? -1;
  281.     }
  282.     public static function sortBySkidkaDesc($a$b
  283.     {
  284.         if ($a->getSkidka() == $b->getSkidka()) {
  285.             return 0;
  286.         }
  287.         return ($a->getSkidka() > $b->getSkidka()) ? -1;
  288.     }
  289.     public static function sortArrayByChangedAsc($a$b
  290.     {
  291.         if ($a['changed'] == $b['changed']) {
  292.             return 0;
  293.         }
  294.         return ($a['changed'] < $b['changed']) ? : -1;
  295.     }
  296.     public static function sortArrayByChangedDesc($a$b
  297.     {
  298.         if ($a['changed'] == $b['changed']) {
  299.             return 0;
  300.         }
  301.         return ($a['changed'] > $b['changed']) ? : -1;
  302.     }
  303.     
  304.     public static function sortArrayByPriorDesc($a$b)
  305.     {
  306.         if ($a['prior'] == $b['prior']) {
  307.             return 0;
  308.         }
  309.         return ($a['prior'] > $b['prior']) ? -1;
  310.     }
  311.     
  312.     public static function sortArrayByPriceDesc($a$b
  313.     {
  314.         if ($a['price'] == $b['price']) {
  315.             return 0;
  316.         }
  317.         return ($a['price'] > $b['price']) ? -1;
  318.     }
  319.     
  320.     public static function sortArrayByPriceAsc($a$b
  321.     {
  322.         if ($a['price'] == $b['price']) {
  323.             return 0;
  324.         }
  325.         return ($a['price'] < $b['price']) ? -1;
  326.     }
  327.     public static function sortArrayBySkidkaDesc($a$b
  328.     {
  329.         if ($a['skidka'] == $b['skidka']) {
  330.             return 0;
  331.         }
  332.         return ($a['skidka'] > $b['skidka']) ? -1;
  333.     }
  334.     public function getDoubles(): array
  335.     {
  336.         $prods = array();
  337.         $q "select id, name, art, count(art) as count_art
  338.             from `prod`
  339.             group by art
  340.             having count(art) > 1";
  341.         $prods $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  342.         
  343.         return $prods;
  344.     }
  345.     public function getNotInCart(): array
  346.     {
  347.         $prods = array();
  348.         $q "select p.id, p.external_id, p.name, p.art, c.id as cid
  349.             from `prod` as p
  350.             left join `cart` as c on c.prod_id = p.id
  351.             where c.id is NULL";
  352.         $prods $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  353.         
  354.         return $prods;
  355.     }
  356.     public function deleteWithDependencies(int $id)
  357.     {
  358.         $q "delete from prod where id = $id";
  359.         $this->getEntityManager()->getConnection()->prepare($q)->executeQuery();
  360.         $q "delete from prodchar where prod = $id";
  361.         $this->getEntityManager()->getConnection()->prepare($q)->executeQuery();
  362.         $q "delete from prod_translation where object_id = $id";
  363.         $this->getEntityManager()->getConnection()->prepare($q)->executeQuery();        
  364.     }
  365.     public function getProdPics(): array
  366.     {
  367.         $prods = array();
  368.         $q "select id, pic from `prod`";
  369.         $pc $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  370.         foreach ($pc as $r) {
  371.             $prods[] = $r;
  372.         }
  373.         return $prods;
  374.     }
  375.     public function getAllIdsAndArts(): array
  376.     {
  377.         $prods = array();
  378.         $q "select id, art from `prod`";
  379.         $pc $this->getEntityManager()->getConnection()->prepare($q)->executeQuery()->fetchAllAssociative();
  380.         foreach ($pc as $r) {
  381.             $prods[] = $r;
  382.         }
  383.         return $prods;
  384.     }
  385.     public function findOneProductsInLocale($ids$locale)
  386.     {
  387.         $qb $this->createQueryBuilder('p')
  388.             ->select('p')
  389.             ->where('p.id in (:id)')
  390.             ->setParameter('id'$ids);
  391.         ;
  392.         $query $qb->getQuery()->useQueryCache(false);
  393.         $query->setHint(
  394.             \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
  395.             'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
  396.         );
  397.         // force Gedmo Translatable to not use current locale
  398.         $query->setHint(
  399.             \Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
  400.             $locale
  401.         );
  402.         $query->setHint(
  403.             \Gedmo\Translatable\TranslatableListener::HINT_FALLBACK,
  404.             1
  405.         );
  406.         
  407.         return $query->getResult();
  408.     }
  409. }