src/Trinity/WebshopBundle/Repository/ProductRepository.php line 1903

Open in your IDE?
  1. <?php
  2. namespace App\Trinity\WebshopBundle\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. use App\Trinity\WebshopBundle\Entity\Category;
  5. use App\CmsBundle\Classes\SqlFormatter;
  6. /**
  7.  * ProductRepository
  8.  */
  9. class ProductRepository extends EntityRepository
  10. {
  11.     public function filter($doCount false$Webshop null$offset 0$limit 0$filter = []){
  12.         $em $this->getEntityManager();
  13.         $q          = (isset($filter['q']) && !empty($filter['q']) ? $filter['q'] : null);
  14.         $type       = (isset($filter['type']) && $filter['type'] != '' $filter['type'] : null);
  15.         $status     = (isset($filter['status']) && $filter['status'] != '' $filter['status'] : null);
  16.         $featured   = (isset($filter['featured']) && $filter['featured'] != '' $filter['featured'] : null);
  17.         $category   = (isset($filter['category']) && $filter['category'] != '' $filter['category'] : null);
  18.         $tax        = (isset($filter['tax']) &&!empty($filter['tax']) && $filter['tax'] != '' $filter['tax'] : null);
  19.         
  20.         $stock      = (isset($filter['stock']) && $filter['stock'] != '' $filter['stock'] : null);
  21.         $sale       = (isset($filter['sale']) && $filter['sale'] != '' $filter['sale'] : null);
  22.         $sales      = (isset($filter['sales']) && $filter['sales'] != '' $filter['sales'] : null);
  23.         $media      = (isset($filter['media']) && $filter['media'] != '' $filter['media'] : null);
  24.         $visibility = (isset($filter['visibility']) && $filter['visibility'] != '' $filter['visibility'] : null);
  25.         
  26.         $sort       = (!empty($filter['sort']) && $filter['sort'] != '' $filter['sort'] : 'p.label');
  27.         $order      = (!empty($filter['order']) && $filter['order'] == 'desc' 'desc' 'asc');
  28.         // Force $q to array
  29.         if(!is_array($q)){
  30.             $q = [$q];
  31.         }
  32.         $partDone = [];
  33.         $queries = [];
  34.         foreach($q as $part){
  35.             // Cleanup
  36.             $part preg_replace('/^(.*?)\'.*?$/''$1'$part);
  37.             $part str_replace('\'''_'$part);
  38.             if(empty($part) || strlen($part) <= || in_array($part$partDone)) continue;
  39.             $partDone[] = $part;
  40.             $queries[] = "
  41.             (
  42.                 p.label LIKE '%" $part "%' OR
  43.                 p.ean LIKE '%" $part "%' OR
  44.                 p.sku LIKE '%" $part "%' OR
  45.                 p.number LIKE '%" $part "%'
  46.             )
  47.             ";
  48.         }
  49.         if($doCount && empty($category) && $stock == null && ($type == null) && $tax == null && $sale == null && $sales == null && $visibility == null && $status == null && $featured == null && $media == null && empty($queries)){
  50.             $sql "
  51.             SELECT count(p.id)
  52.             FROM TrinityWebshopBundle:Product p
  53.             WHERE 1 = 1
  54.             " . ($Webshop "AND p.webshop = :webshopid" "") . "
  55.             ";
  56.         }else{
  57.             if($category != null){
  58.                 $sql "
  59.                 SELECT " . (!$doCount "wp" "count(wp.id)") . "
  60.                 FROM TrinityWebshopBundle:WebshopProduct wp
  61.                 JOIN wp.product p
  62.                 WHERE 1 = 1
  63.                 " . ($Webshop "AND p.webshop = :webshopid" "") . "
  64.                 " . ($category != null "
  65.                 AND wp.category  = " $category "
  66.                 " "") . "
  67.                 " . ($stock != null ? ($stock == 
  68.                         "AND (p.stock = 0 OR (p.stock = 1 AND (p.stock_amount - p.out_of_stock_quantity) > 0))"
  69.                         :
  70.                         "AND p.toggle_stock = 0 OR (p.stock = 1 AND ((p.stock_amount - p.out_of_stock_quantity) <= 0 or (p.child_stock_amount - p.out_of_stock_quantity) <= 0))"
  71.                         )
  72.                     :
  73.                 "") . "
  74.                 " . ($type != null "
  75.                 AND p.type = " $type "
  76.                 " "") . "
  77.                 " . ($tax != null "
  78.                 AND p.tax = " $tax "
  79.                 " "") . "
  80.                 " . ($sale != null "
  81.                     AND p.price_sale " . ($sale " > 0" "<= 0") . "
  82.                     AND (p.price_sale < p.price_incl OR p.price_sale > p.price_incl)
  83.                 " "") . "
  84.                 " . ($sales != null "
  85.                     AND (SELECT COUNT(op) FROM TrinityWebshopBundle:OrderProduct op WHERE op.product = p) >= " $sales "
  86.                 " "") . "
  87.                 " . ($visibility === '1' "
  88.                 AND p.visible = 1
  89.                 AND p.enabled = 1
  90.                 " "") . "
  91.                 " . ($visibility === '0' "
  92.                 AND (p.visible = 0
  93.                 OR p.enabled = 0)
  94.                 " "") . "
  95.                 " . ($status != null "
  96.                 AND p.enabled = " $status "
  97.                 " "") . "
  98.                 " . ($featured != null "
  99.                 AND p.featured = " $featured "
  100.                 " "") . "
  101.                 " . (empty($media) && !is_null($media) ? "AND wp.has_media != 1" "") . "
  102.                 " . (!empty($queries) ? " AND " implode(" AND "$queries) : "") . "
  103.                 " . (!$doCount "GROUP BY p.id" "") . "
  104.             
  105.                 ORDER BY {$sort} {$order}
  106.                 ";
  107.             }else{
  108.                 $sql "
  109.                 SELECT " . (!$doCount "p" "count(p.id)") . "
  110.                 FROM TrinityWebshopBundle:Product p
  111.                 " . ($category != null " JOIN p.category cp JOIN cp.category c " "") . "
  112.                 " . (empty($media) && !is_null($media) ? "LEFT JOIN p.media m" "") . "
  113.                 WHERE 1 = 1
  114.                 " . ($Webshop "AND p.webshop = :webshopid" "") . "
  115.                 " . ($category != null "
  116.                 AND c.id  = " $category "
  117.                 " "") . "
  118.                 " . ($stock != null ? ($stock == 
  119.                         "AND (p.stock = 0 OR (p.stock = 1 AND (p.stock_amount - p.out_of_stock_quantity) > 0))"
  120.                         :
  121.                         "AND p.toggle_stock = 0 OR (p.stock = 1 AND ((p.stock_amount - p.out_of_stock_quantity) <= 0))"
  122.                         )
  123.                     :
  124.                 "") . "
  125.                 " . ($type != null "
  126.                 AND p.type = " $type "
  127.                 " "") . "
  128.                 " . ($tax != null "
  129.                 AND p.tax = " $tax "
  130.                 " "") . "
  131.                 " . ($sale != null "
  132.                     AND p.price_sale " . ($sale " > 0" "<= 0") . "
  133.                     AND (p.price_sale < p.price_incl OR p.price_sale > p.price_incl)
  134.                 " "") . "
  135.                 " . ($sales != null "
  136.                     AND (SELECT COUNT(op) FROM TrinityWebshopBundle:OrderProduct op WHERE op.product = p) >= " $sales "
  137.                 " "") . "
  138.                 " . ($visibility === '1' "
  139.                 AND p.visible = 1
  140.                 AND p.enabled = 1
  141.                 " "") . "
  142.                 " . ($visibility === '0' "
  143.                 AND (p.visible = 0
  144.                 OR p.enabled = 0)
  145.                 " "") . "
  146.                 " . ($status != null "
  147.                 AND p.enabled = " $status "
  148.                 " "") . "
  149.                 " . ($featured != null "
  150.                 AND p.featured = " $featured "
  151.                 " "") . "
  152.                 " . (empty($media) && !is_null($media) ? "AND p.has_media = 0" "") . "
  153.                 " . (!empty($queries) ? " AND " implode(" AND "$queries) : "") . "
  154.             
  155.                 " . (!$doCount "GROUP BY p.id" "") . "
  156.                 ORDER BY {$sort} {$order}
  157.                 ";
  158.             }
  159.             /*$sql = "
  160.             SELECT " . (!$doCount ? "p" : "count(p.id)") . "
  161.                 FROM TrinityWebshopBundle:Product p
  162.                 LEFT JOIN p.product_link pl
  163.             " . ($category != null ? " JOIN p.category cp JOIN cp.category c " : "") . "
  164.             " . (!empty($media) ? "JOIN p.media m" : "") . "
  165.             " . (empty($media) && !is_null($media) ? "LEFT JOIN p.media m" : "") . "
  166.             WHERE 1 = 1
  167.             " . ($Webshop ? "AND p.webshop = :webshopid" : "") . "
  168.             " . ($category != null ? "
  169.             AND c.id  = " . $category . "
  170.             " : "") . "
  171.             " . ($stock != null ? "
  172.             AND p.stock_amount " . ($stock ? " > 0" : "<= 0") . "
  173.             " : "") . "
  174.             " . ($type != null ? "
  175.             AND p.type = " . $type . "
  176.             " : "") . "
  177.             " . ($tax != null ? "
  178.             AND p.tax = " . $tax . "
  179.             " : "") . "
  180.             " . ($sale != null ? "
  181.                 AND p.price_sale " . ($sale ? " > 0" : "<= 0") . "
  182.                 AND (p.price_sale < p.price_incl OR p.price_sale > p.price_incl)
  183.             " : "") . "
  184.             " . ($sales != null ? "
  185.                 AND (SELECT COUNT(op) FROM TrinityWebshopBundle:OrderProduct op WHERE op.product = p) >= " . $sales . "
  186.             " : "") . "
  187.             " . ($visibility === '1' ? "
  188.             AND p.visible = 1
  189.             AND p.enabled = 1
  190.             " : "") . "
  191.             " . ($visibility === '0' ? "
  192.             AND (p.visible = 0
  193.             OR p.enabled = 0)
  194.             " : "") . "
  195.             " . ($status != null ? "
  196.             AND p.enabled = " . $status . "
  197.             " : "") . "
  198.             " . ($featured != null ? "
  199.             AND p.featured = " . $featured . "
  200.             " : "") . "
  201.             " . (empty($media) && !is_null($media) ? "AND m.id IS NULL" : "") . "
  202.             " . (!empty($queries) ? " AND " . implode(" AND ", $queries) : "") . "
  203.         
  204.             " . (!$doCount ? "GROUP BY p.id" : "") . "
  205.             ORDER BY {$sort} {$order}
  206.             ";*/
  207.         }
  208.         // dump($sql);die();
  209.         /*if(!$doCount){
  210.             dump($sql);
  211.             die();
  212.         }*/
  213.         $query $em->createQuery($sql);
  214.         if ($Webshop) {
  215.             $query->setParameter('webshopid', (!empty($Webshop) ? $Webshop 0));
  216.         }
  217.         if($doCount){
  218.             try{
  219.                 return $query->getSingleScalarResult();
  220.             }catch(\Exception $e){
  221.                 return 0;
  222.             }
  223.         }
  224.         if(!empty($limit)){
  225.             try{
  226.                 $all_results $query->setFirstResult($offset)->setMaxResults($limit)->getResult();
  227.                 foreach($all_results as $i => $r){
  228.                     if($r instanceof \App\Trinity\WebshopBundle\Entity\WebshopProduct){
  229.                         $all_results[$i] = $r->getProduct();
  230.                     }
  231.                 }
  232.                 return $all_results;
  233.             }catch(\Exception $e){
  234.                 return [];
  235.             }
  236.         }
  237.         $all_results $query->getResult();
  238.         foreach($all_results as $i => $r){
  239.             if($r instanceof \App\Trinity\WebshopBundle\Entity\WebshopProduct){
  240.                 $all_results[$i] = $r->getProduct();
  241.             }
  242.         }
  243.         return $all_results;
  244.     }
  245.     public function getPriceRange($Webshop null$Category null){
  246.         $em $this->getEntityManager();
  247.         $sql "
  248.         SELECT MAX(p.sell_price_incl) as max_price, MIN(p.sell_price_incl) as min_price
  249.         FROM TrinityWebshopBundle:Product p
  250.         JOIN p.category cp
  251.         JOIN p.product_link wp
  252.         JOIN cp.category c
  253.         " . ($Webshop "JOIN c.webshop w" "") . "
  254.         WHERE p.visible = 1
  255.         AND p.sell_price_incl >= 1
  256.         AND p.enabled = 1
  257.         AND (p.stock_amount > 0 or p.child_stock_amount > 0 or p.stock = 0 or p.visible_no_stock = 1)
  258.         " . ($Webshop "AND w.id LIKE :webshopid" "") . "
  259.         " . ($Category && $Category instanceof Category "AND c.id LIKE :categoryid" "") . "
  260.         " . ($Category && is_array($Category) ? "AND wp.category IN (:categoryids)" ""). "
  261.         ";
  262.         $query $em->createQuery($sql);
  263.         if ($Webshop) {
  264.             $query->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  265.         }
  266.         if ($Category && $Category instanceof Category) {
  267.             $query->setParameter('categoryid', (!empty($Category) ? $Category->getId() : 0));
  268.         } elseif ($Category && is_array($Category)) {
  269.             $query->setParameter('categoryids'$Category\Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
  270.         }
  271.         return $query->getResult();
  272.     }
  273.     public function countBy($Webshop null$q null$type null){
  274.         $em $this->getEntityManager();
  275.         $query $em->createQuery(
  276.             "SELECT count(p)
  277.             FROM TrinityWebshopBundle:Product p
  278.             JOIN p.category cp
  279.             JOIN cp.category c
  280.             " . ($Webshop "JOIN c.webshop w" "") . "
  281.             WHERE 1 = 1
  282.             " . ($Webshop "AND w.id LIKE :webshopid" "") . "
  283.             " . ($type "
  284.             AND p.type = " $type "
  285.             " "") . "
  286.             " . ($q "
  287.             AND (
  288.                 p.label LIKE '%" $q "%' OR
  289.                 p.ean LIKE '%" $q "%' OR
  290.                 p.sku LIKE '%" $q "%' OR
  291.                 p.number LIKE '%" $q "%' OR
  292.                 c.label LIKE '%" $q "%'
  293.             )
  294.             " "") . "
  295.             "
  296.         );
  297.         if ($Webshop) {
  298.             $query->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  299.         }
  300.         return $query->getSingleScalarResult();
  301.     }
  302.     public function findOrders($id$q null$doCount false$offset null$limit null){
  303.         $em $this->getEntityManager();
  304.         if(!empty($q)){
  305.             $sql "SELECT " . ($doCount "count(o)" "o") . "
  306.             FROM TrinityWebshopBundle:Order o
  307.             JOIN o.products p
  308.             JOIN p.product op
  309.             WHERE op.id = " $id "
  310.             AND (
  311.                 o.order_id LIKE '%" $q "%' OR 
  312.                 o.firstname LIKE '%" $q "%' OR 
  313.                 o.lastname LIKE '%" $q "%' OR 
  314.                 o.email LIKE '%" $q "%'
  315.             )
  316.             ORDER BY o.date DESC
  317.             ";
  318.         }else{
  319.             $sql "SELECT " . ($doCount "count(o)" "o") . "
  320.             FROM TrinityWebshopBundle:Order o
  321.             JOIN o.products p
  322.             JOIN p.product op
  323.             WHERE op.id = " $id "
  324.             ORDER BY o.date DESC
  325.             ";
  326.         }
  327.         
  328.         $query $em->createQuery($sql);
  329.         if($doCount){
  330.             return (int)$query->getSingleScalarResult();
  331.         }
  332.         return $query->setFirstResult($offset)->setMaxResults($limit)->getResult();
  333.     }
  334.     public function getUsingSkuOtherShops($sku$Product$Webshop null){
  335.         $em $this->getEntityManager();
  336.         $query $em->createQuery(
  337.             "SELECT p
  338.             FROM TrinityWebshopBundle:Product p
  339.             WHERE 1 = 1
  340.             AND p.sku = '$sku'
  341.             " . ($Product "AND p.id != " $Product->getId() . "" "") . "
  342.             " . ($Webshop "AND p.webshop = :webshopid" "") . "
  343.             "
  344.         );
  345.         if ($Webshop) {
  346.             $query->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  347.         }
  348.         $results $query->getResult();
  349.         return $results;
  350.     }
  351.     public function getUsingEanOtherShops($sku$Product$Webshop null){
  352.         $em $this->getEntityManager();
  353.         $query $em->createQuery(
  354.             "SELECT p
  355.             FROM TrinityWebshopBundle:Product p
  356.             WHERE 1 = 1
  357.             AND p.sku = '$sku'
  358.             " . ($Product "AND p.id != " $Product->getId() . "" "") . "
  359.             " . ($Webshop "AND p.webshop = :webshopid" "") . "
  360.             "
  361.         );
  362.         if ($Webshop) {
  363.             $query->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  364.         }
  365.         $results $query->getResult();
  366.         return $results;
  367.     }
  368.     public function countWithImage($Webshop null){
  369.         $em $this->getEntityManager();
  370.         $query $em->createQuery(
  371.             "SELECT count(p)
  372.             FROM TrinityWebshopBundle:Product p
  373.             JOIN p.category cp
  374.             JOIN cp.category c
  375.             JOIN p.media m
  376.             " . ($Webshop "JOIN c.webshop w" "") . "
  377.             WHERE 1 = 1
  378.             " . ($Webshop "AND w.id LIKE :webshopid" "") . "
  379.             
  380.             "
  381.         );
  382.         if ($Webshop) {
  383.             $query->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  384.         }
  385.         return $query->getSingleScalarResult();
  386.     }
  387.     public function selector($Webshop$q ''$c ''){
  388.         $em $this->getEntityManager();
  389.         $query $em->createQuery(
  390.             "SELECT p
  391.             FROM TrinityWebshopBundle:Product p
  392.             JOIN p.category cp
  393.             JOIN cp.category c
  394.             JOIN c.webshop w
  395.             WHERE w.id LIKE :webshopid
  396.             " . ($q "
  397.             AND (
  398.                 p.label LIKE '%" $q "%' OR
  399.                 p.ean LIKE '%" $q "%' OR
  400.                 p.sku LIKE '%" $q "%' OR
  401.                 p.number LIKE '%" $q "%' OR
  402.                 c.label LIKE '%" $q "%'
  403.             )
  404.             " "") . "
  405.             " . ($c "
  406.             AND c.id = " $c "
  407.             " "") . "
  408.             ORDER BY p.label ASC
  409.             "
  410.         );
  411.         $query->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  412.         return $query->getResult();
  413.     }
  414.     public function getBy($Webshop$offset$limit$q null$type null){
  415.         $em $this->getEntityManager();
  416.         $query $em->createQuery(
  417.             "SELECT p
  418.             FROM TrinityWebshopBundle:Product p
  419.             JOIN p.category cp
  420.             JOIN cp.category c
  421.             " . ($Webshop "JOIN c.webshop w" "") . "
  422.             WHERE 1 = 1
  423.             " . ($Webshop "AND w.id LIKE :webshopid" "") . "
  424.             " . ($type "
  425.             AND p.type = " $type "
  426.             " "") . "
  427.             " . ($q "
  428.             AND (
  429.                 p.label LIKE '%" $q "%' OR
  430.                 p.ean LIKE '%" $q "%' OR
  431.                 p.sku LIKE '%" $q "%' OR
  432.                 p.number LIKE '%" $q "%' OR
  433.                 c.label LIKE '%" $q "%'
  434.             )
  435.             " "") . "
  436.             ORDER BY p.label"
  437.         );
  438.         if ($Webshop) {
  439.             $query->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  440.         }
  441.         return $query->setFirstResult($offset)->setMaxResults($limit)->getResult();
  442.     }
  443.     public function findByCategoryAndSlug($category$slug$preview false){
  444.         $em $this->getEntityManager();
  445.         $sql "
  446.         SELECT p
  447.         FROM TrinityWebshopBundle:Product p
  448.         JOIN p.category cp
  449.         JOIN cp.category c
  450.         JOIN p.language pl
  451.         JOIN c.webshop w
  452.         JOIN w.language l WITH l.id = pl.id
  453.         WHERE c.id = " $category->getId() . "
  454.         AND p.slug = '" $slug "'
  455.         ";
  456.         $query $em->createQuery($sql);
  457.         try {
  458.             return $query->getSingleResult();
  459.         } catch (\Exception $e) {}
  460.         return null;
  461.     }
  462.     public function findBySpecs($specs$Product){
  463.         $em $this->getEntityManager();
  464.         $joins = [];
  465.         $ands = [];
  466.         $orderbys = [];
  467.         if(!empty($specs['spec'])){
  468.             foreach($specs['spec'] as $specid => $value){
  469.                 $joins[] = " JOIN p.spec_values s" $specid;
  470.                 $ands[] = " AND s" $specid ".spec = " $specid "";
  471.                 $ands[] = " AND s" $specid ".value = '" $value "'";
  472.                 $orderbys[] = "s" $specid ".position ASC";
  473.             }
  474.         }/*else{
  475.             $joins[] = " JOIN p.spec_values sx";
  476.             $orderbys[] = "sx.position ASC";
  477.         }*/
  478.         $sql 'SELECT p
  479.             FROM TrinityWebshopBundle:Product p
  480.             JOIN p.linked_to l
  481.             ' implode(' '$joins) . '
  482.             WHERE l.id = :product
  483.             AND p.enabled = 1
  484.             ' implode(' '$ands) . '
  485.             ' . (!empty($orderbys) ? 'ORDER BY ' implode(', '$orderbys) : '') . '
  486.             ';
  487.         $query $em->createQuery($sql)
  488.                 ->setParameter('product'$Product);
  489.         return $query->getResult();
  490.     }
  491.     public function findAnyBySpecs($specs$price_range = []){
  492.         $em $this->getEntityManager();
  493.         $joins = [];
  494.         $ands = [];
  495.         $orderbys = [];
  496.         if(!empty($specs['spec'])){
  497.             foreach($specs['spec'] as $specid => $value){
  498.                 $joins[] = " JOIN p.spec_values s" $specid;
  499.                 $ands[] = " AND s" $specid ".spec = " $specid "";
  500.                 $ands[] = " AND s" $specid ".value = '" $value "'";
  501.                 $orderbys[] = "s" $specid ".position ASC";
  502.             }
  503.         }/*else{
  504.             $joins[] = " JOIN p.spec_values sx";
  505.             $orderbys[] = "sx.position ASC";
  506.         }*/
  507.         $sql 'SELECT p
  508.             FROM TrinityWebshopBundle:Product p
  509.             ' implode(' '$joins) . '
  510.             WHERE 1 = 1
  511.             AND p.enabled = 1
  512.             ' implode(' '$ands) . '
  513.             ' . (!empty($price_range) ? 'AND ((p.sell_price_incl BETWEEN ' $price_range[0] . ' AND ' $price_range[1] . '))' '') . '
  514.             ' . (!empty($orderbys) ? 'ORDER BY ' implode(', '$orderbys) : '') . '
  515.             ';
  516.         $query $em->createQuery($sql);
  517.         return $query->getResult();
  518.     }
  519.     public function getFeatured($Webshop$limit 0$offset 0){
  520.         $em $this->getEntityManager();
  521.         $query $em->createQuery(
  522.             'SELECT p
  523.             FROM TrinityWebshopBundle:Product p
  524.             JOIN p.category cp
  525.             JOIN cp.category c
  526.             JOIN c.webshop w
  527.             WHERE w.id LIKE :webshopid
  528.             AND p.featured = 1
  529.             AND p.visible = 1
  530.             AND p.enabled = 1
  531.             GROUP BY p.id
  532.             ORDER BY p.label'
  533.         )->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  534.         if($limit 0){
  535.             return $query->setFirstResult($offset)->setMaxResults($limit)->getResult();
  536.         }else{
  537.             return $query->getResult();
  538.         }
  539.     }
  540.     public function getNew($Webshop$limit 0$offset 0){
  541.         $em $this->getEntityManager();
  542.         /*$products = $this->createQueryBuilder('p')
  543.             ->select('p')
  544.             ->join('p.category', 'c')
  545.             ->join('c.webshop', 'w')
  546.             ->where('w = :webshop')
  547.             ->setParameter('webshop', $Webshop)
  548.             ->orderBy('p.id', 'desc')
  549.             ->setMaxResults($limit)
  550.             ->setFirstResult($offset)
  551.             ->getQuery()
  552.             ->getSql();
  553.             dump($products);die();*/
  554.         $query $em->createQuery(
  555.             'SELECT p
  556.             FROM TrinityWebshopBundle:Product p
  557.             JOIN p.category cp
  558.             JOIN cp.category c
  559.             JOIN c.webshop w
  560.             WHERE w.id LIKE :webshopid
  561.             AND p.visible = 1
  562.             AND p.enabled = 1
  563.             GROUP BY p.id
  564.             ORDER BY p.id DESC'
  565.         )->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  566.         if($limit 0){
  567.             return $query->setFirstResult($offset)->setMaxResults($limit)->getResult();
  568.         }else{
  569.             return $query->getResult();
  570.         }
  571.     }
  572.     public function getBestViewed($Webshop$limit 0$offset 0){
  573.         $em $this->getEntityManager();
  574.         $query $em->createQuery(
  575.             'SELECT p
  576.             FROM TrinityWebshopBundle:Product p
  577.             JOIN p.category cp
  578.             JOIN cp.category c
  579.             JOIN c.webshop w
  580.             WHERE w.id LIKE :webshopid
  581.             AND p.visible = 1
  582.             AND p.enabled = 1
  583.             GROUP BY p.id
  584.             ORDER BY p.views DESC'
  585.         )->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  586.         if($limit 0){
  587.             return $query->setFirstResult($offset)->setMaxResults($limit)->getResult();
  588.         }else{
  589.             return $query->getResult();
  590.         }
  591.     }
  592.     public function findByCategoryFront($category_id$sort$limit 0$offset 0){
  593.         $em $this->getEntityManager();
  594.         $s = ['p.id desc'];
  595.         if(!empty($sort)){
  596.             $s = [];
  597.             foreach($sort as $k => $v){
  598.                 $s[] = "p.$k $v";
  599.             }
  600.         }
  601.         if(!is_array($category_id)){
  602.             $category_id = [$category_id];
  603.         }
  604.         $query $em->createQuery(
  605.             'SELECT p
  606.             FROM TrinityWebshopBundle:Product p
  607.             JOIN p.category cp JOIN cp.category c WITH c.id IN (' implode(','$category_id) . ')
  608.             WHERE p.enabled = 1
  609.             AND p.visible = 1
  610.             GROUP BY p.id
  611.             ORDER BY ' implode(','$s)
  612.         );
  613.         if($limit 0){
  614.             return $query->setFirstResult($offset)->setMaxResults($limit)->getResult();
  615.         }else{
  616.             return $query->getResult();
  617.         }
  618.     }
  619.     public function searchSimple($q ''){
  620.         $em $this->getEntityManager();
  621.         $query $em->createQuery(
  622.             "
  623.             SELECT p
  624.             FROM TrinityWebshopBundle:Product p
  625.             WHERE p.label LIKE '%" $q "%'
  626.             OR p.sku LIKE '%" $q "%'
  627.             OR p.number LIKE '%" $q "%'
  628.             "
  629.         );
  630.         return $query->getResult();
  631.     }
  632.     public function getBestSold($Webshop$limit 0$offset 0){
  633.         $em $this->getEntityManager();
  634.         $query $em->createQuery(
  635.             'SELECT p as Product, COUNT(op.id) AS num
  636.             FROM TrinityWebshopBundle:Product p
  637.             JOIN p.category cp
  638.             JOIN cp.category c
  639.             JOIN c.webshop w
  640.             JOIN TrinityWebshopBundle:OrderProduct op WITH op.product = p
  641.             WHERE w.id LIKE :webshopid
  642.             AND p.enabled = 1
  643.             AND p.visible = 1
  644.             GROUP BY p.id
  645.             ORDER BY num DESC'
  646.         )->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  647.         if($limit 0){
  648.             return $query->setFirstResult($offset)->setMaxResults($limit)->getResult();
  649.         }else{
  650.             return $query->getResult();
  651.         }
  652.     }
  653.     public function getBestSoldAllowed($Webshop$limit 0$offset 0){
  654.         $em $this->getEntityManager();
  655.         $query $em->createQuery(
  656.             'SELECT p as Product, COUNT(op.id) AS num
  657.             FROM TrinityWebshopBundle:Product p
  658.             JOIN p.category cp
  659.             JOIN cp.category c
  660.             JOIN c.webshop w
  661.             JOIN TrinityWebshopBundle:OrderProduct op WITH op.product = p
  662.             WHERE w.id LIKE :webshopid
  663.             AND p.visible = 1
  664.             AND p.enabled = 1
  665.             GROUP BY p.id
  666.             ORDER BY num DESC'
  667.         )->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  668.         if($limit 0){
  669.             return $query->setFirstResult($offset)->setMaxResults($limit)->getResult();
  670.         }else{
  671.             return $query->getResult();
  672.         }
  673.     }
  674.     
  675.     public function countProducts(){
  676.         $em $this->getEntityManager();
  677.         $qb $em->createQueryBuilder();
  678.         $qb->select('count(product.id)');
  679.         $qb->from('TrinityWebshopBundle:Product','product');
  680.         $count $qb->getQuery()->getSingleScalarResult();
  681.         
  682.         return $count;
  683.     }
  684.     public function search($Webshop$filters = array(), $order null$orderDir 'asc'$noLimit false$ignoreStock false$page 1$limit 9$offset 0$thisIsFilter false){
  685.         $em $this->getEntityManager();
  686.         if(isset($filters['q']) && is_array($filters['q'])){
  687.             $filters['q'] = implode(' '$filters['q']);
  688.         }
  689.         if(isset($filters['q']) && strpos($filters['q'], 'linkableproducts') !== false){
  690.             $ignoreStock true;
  691.         }
  692.         $featured = (!empty($filters['featured']) && $filters['featured'] === true);
  693.         $debug false;
  694.         if(isset($filters['debug'])){
  695.             $debug true;
  696.             unset($filters['debug']);
  697.         }
  698.         $join = [];
  699.         $joinNorForCount = [];
  700.         $and = [];
  701.         $or = [];
  702.         $start 0;
  703.         if(empty($limit) || !is_numeric($limit)){
  704.             $limit 9// 9 is default, can be overridden in Trinity
  705.         }
  706.         // Check category
  707.         if(isset($filters['page']) && $page <= 1){
  708.             $page = (int)$filters['page'];
  709.         }
  710.         $webshop_id = (!empty($Webshop) ? $Webshop->getId() : 0);
  711.         $ignore_ids = [];
  712.         if(!empty($filters['ignore_ids'])){
  713.             $ignore_ids $filters['ignore_ids'];
  714.             if(!is_array($ignore_ids)){
  715.                 $ignore_ids = [$ignore_ids];
  716.             }
  717.             unset($filters['ignore_ids']);
  718.         }
  719.         $showHidden false;
  720.         if(!empty($filters['q'])){
  721.             if(preg_match('/linkableproducts=1/'$filters['q'])){
  722.                 $filters['q'] = preg_replace('/&linkableproducts=1/'''$filters['q']);
  723.                 $showHidden true;
  724.             }
  725.         }
  726.         $ranges       = [];
  727.         $ranges_joins = [];
  728.         $ranges_where = [];
  729.         $fn 0;
  730.         if(!empty($filters['filters'])){
  731.             foreach($filters['filters'] as $spec_id => $value){
  732.                 if(is_array($value) && isset($value['range'])){
  733.                     foreach($value as $t => $v){
  734.                         if(!empty($v[0]) && !empty($v[1]) && $v[0] != 'undefined' && $v[1] != 'undefined'){
  735.                             $ranges_joins[] = "JOIN p.spec_values sv{$fn}x1 WITH sv{$fn}x1.spec = '" $spec_id "'";
  736.                             $ranges_where[] = "AND (sv{$fn}x1.value BETWEEN " implode(' AND '$v) . " OR sv{$fn}x1.value = '' OR sv{$fn}x1.value IS NULL)";
  737.                             $fn++;
  738.                         }
  739.                     }
  740.                 }
  741.             }
  742.         }
  743.         $sliders = (!empty($filters['sliders']) ? $filters['sliders'] : []);
  744.         $sliders_range = (!empty($filters['sliders_range']) ? $filters['sliders_range'] : []);
  745.         if(!empty($filters['filters'])){
  746.             foreach($filters['filters'] as $spec_id => $value){
  747.                 if(in_array($spec_id$sliders) && (int)$value[0] > 0){
  748.                     $v = [0, (int)$value[0]];
  749.                     $ranges_joins[] = "JOIN p.spec_values sv{$fn}x1 WITH sv{$fn}x1.spec = '" $spec_id "'";
  750.                     $ranges_where[] = "AND (sv{$fn}x1.value BETWEEN " implode(' AND '$v) . ")";
  751.                     $fn++;
  752.                 }
  753.                 if(in_array($spec_id$sliders_range)){
  754.                     $v = [(int)$value['min'], (int)$value['max']];
  755.                     $ranges_joins[] = "JOIN p.spec_values sv{$fn}x1 WITH sv{$fn}x1.spec = '" $spec_id "'";
  756.                     $ranges_where[] = "AND (sv{$fn}x1.value BETWEEN " implode(' AND '$v) . ")";
  757.                     $fn++;
  758.                 }
  759.                 /*if(is_array($value) && isset($value['range'])){
  760.                     foreach($value as $t => $v){
  761.                         if(!empty($v[0]) && !empty($v[1]) && $v[0] != 'undefined' && $v[1] != 'undefined'){
  762.                             $ranges_joins[] = "JOIN p.spec_values sv{$fn}x1 WITH sv{$fn}x1.spec = '" . $spec_id . "'";
  763.                             $ranges_where[] = "AND (sv{$fn}x1.value BETWEEN " . implode(' AND ', $v) . " OR sv{$fn}x1.value = '' OR sv{$fn}x1.value IS NULL)";
  764.                             $fn++;
  765.                         }
  766.                     }
  767.                 }*/
  768.             }
  769.         }
  770.         $linkableproducts = (!empty($_GET['linkableproducts']) ? true false);
  771.         $search_raw = (!empty($filters['q']) ? (!is_array($filters['q']) ? [$filters['q']] : $filters['q']) : null);
  772.         $visible    = (!empty($filters['visible']) ? $filters['visible'] : null);
  773.         $featured   = (!empty($filters['featured']) ? $filters['featured'] : null);
  774.         $category   = (!empty($filters['category']) ? $filters['category'] : null);
  775.         $relation   = (!empty($filters['relation']) ? $filters['relation'] : null);
  776.         $newproducts = (!empty($filters['newproducts']) ? $filters['newproducts'] : null);
  777.         $search = [];
  778.         if(!empty($search_raw)){
  779.             foreach($search_raw as $sr){
  780.                 $sr explode(' '$sr);
  781.                 foreach($sr as $srp){
  782.                     $search[] = $srp;
  783.                 }
  784.             }
  785.         }
  786.         $typeFilter "((p.type = 0 or p.type = 3 or p.type = 4) or ((p.type = 1 or p.type = 2) and p.child_stock_amount > 0))";
  787.         if(!empty($_GET['linkableproducts']) && (empty($_GET['link']) || $_GET['link'] != 'relations')){
  788.             $visible false;
  789.             $typeFilter "((p.type = 0 or p.type = 3 or p.type = 4))";
  790.         }
  791.         $tags = [];
  792.         if(!empty($filters['tags'])){
  793.             $tags $filters['tags'];
  794.         }
  795.         $sql "
  796.         SELECT p
  797.         
  798.         FROM TrinityWebshopBundle:Product p
  799.         JOIN p.product_link wp
  800.         " . (!empty($category) && ($category instanceof Category || is_array($category)) ? "LEFT JOIN p.category cp WITH (cp.category = wp.category)" "") . "
  801.         " . (!empty($relation) ? "JOIN p.relation_to cp WITH (cp.id = " $relation->getId() . ")" "") . "
  802.         " . (!empty($tags) ? "JOIN p.tags pt WITH (pt.id IN (" implode(', '$tags) . "))" "") . "
  803.         " . (!empty($ranges_joins) ? implode("\n"$ranges_joins) : "") . "
  804.         WHERE 1 = 1
  805.         AND " . ($linkableproducts "1 = 1" "((p.type = 0 and ((p.stock_amount - p.out_of_stock_quantity) > 0 or p.stock = 0 or p.visible_no_stock = 1)) or p.type > 0)") . "
  806.         AND " $typeFilter "
  807.         " . (!empty($ranges_where) ? implode("\n"$ranges_where) : "") . "
  808.         ";
  809.         if(!empty($ignore_ids)){
  810.             $sql .= "\nAND p.id NOT IN (" implode(','$ignore_ids) . ")";
  811.         }
  812.         $sql .= "\nAND p.enabled = 1";
  813.         $s = [];
  814.         if($search){
  815.             foreach($search as $q){
  816.                 $q str_replace('\'''_'$q);
  817.                 if($thisIsFilter){
  818.                     $s[] = "((p.label LIKE '%" $q "%') OR (p.number LIKE '%" $q "%') OR (wp.specs LIKE '%" $q "%') OR (wp.child_specs LIKE '%" $q "%') OR p.extra_search_tags LIKE '%" $q "%')";
  819.                 }else{
  820.                     $s[] = "((wp.specs LIKE '%" $q "%' OR (
  821.                                                                                 p.label LIKE '%" $q "%'
  822.                                                                             OR
  823.                                                                                 p.extra_search_tags LIKE '%" $q "%'
  824.                                                                             OR
  825.                                                                                 p.label_sub LIKE '%" $q "%'
  826.                                                                             OR
  827.                                                                                 p.compatibility LIKE '%" $q "%'
  828.                                                                             OR
  829.                                                                                 p.ean LIKE '%" $q "%'
  830.                                                                             OR
  831.                                                                                 p.number LIKE '%" $q "%'
  832.                                                                             OR
  833.                                                                                 p.sku LIKE '%" $q "%'
  834.                                                                             )) OR (wp.child_specs LIKE '%" $q "%' OR (
  835.                                                                                 p.label LIKE '%" $q "%'
  836.                                                                             OR
  837.                                                                                 p.extra_search_tags LIKE '%" $q "%'
  838.                                                                             OR
  839.                                                                                 p.label_sub LIKE '%" $q "%'
  840.                                                                             OR
  841.                                                                                 p.compatibility LIKE '%" $q "%'
  842.                                                                             OR
  843.                                                                                 p.ean LIKE '%" $q "%'
  844.                                                                             OR
  845.                                                                                 p.number LIKE '%" $q "%'
  846.                                                                             OR
  847.                                                                                 p.sku LIKE '%" $q "%'
  848.                                                                             )))";
  849.                 }
  850.             }
  851.             $sql .= "\nAND (" implode(" AND "$s) . ")";
  852.         }
  853.         if($visible){
  854.             $sql .= "\nAND p.visible = 1";
  855.         }
  856.         if($featured){
  857.             $sql .= "\nAND p.featured = 1";
  858.         }
  859.         if ($newproducts) {
  860.             $sql .= "\nAND p.new = 1";
  861.             $sql .= "\nAND (p.new_from is null OR p.new_from <= NOW())";
  862.             $sql .= "\nAND (p.new_till is null OR p.new_till > NOW())";
  863.         }
  864.         if($category && $category instanceof Category){
  865.             $sql .= "\nAND wp.category = " $category->getId();
  866.         }
  867.         if($category && is_array($category)){
  868.             $sql .= "\nAND wp.category IN (" implode(', '$category) . ")";
  869.         }
  870.         if($webshop_id && is_numeric($webshop_id)){
  871.             $sql .= "\nAND wp.webshop = " $webshop_id;
  872.         }
  873.         if(!empty($filters['price']) && is_array($filters['price']) && (!isset($filters['ignore_pricing']) || $filters['ignore_pricing'] == false)){
  874.             // $sql .= "\nAND ((p.sell_price_incl BETWEEN " . $filters['price'][0] . " AND " . $filters['price'][1] . ") OR p.sell_price_incl < 1)";
  875.             $sql .= "\nAND ((p.sell_price_incl BETWEEN " $filters['price'][0] . " AND " $filters['price'][1] . "))";
  876.         }
  877.         $strict_search = [265];
  878.         if(!empty($filters['filters'])){
  879.             foreach($filters['filters'] as $spec_id => $value){
  880.                 if(in_array($spec_id$sliders) || in_array($spec_id$sliders_range)){
  881.                     continue;
  882.                 }
  883.                 if(!isset($value['range']) && $spec_id != 'q'){
  884.                     if(!is_array($value)) $value = [$value];
  885.                     foreach($value as $t => $v){
  886.                         $v str_replace('\'''_'$v);
  887.                         $v json_encode($v);
  888.                         $v str_replace('"'''$v);
  889.                         $v str_replace('\\''\\\\'$v);
  890.                         if(in_array($spec_id$strict_search) || $thisIsFilter){
  891.                             $sql .= "\nAND (((wp.specs LIKE '%\"" $spec_id "\":%' AND (wp.specs LIKE '%\"" $v "\"%'))) OR ((wp.child_specs LIKE '%\"" $spec_id "\":%' AND (wp.child_specs LIKE '%\"" $v "\"%'))))";
  892.                         }else{
  893.                             $sql .= "\nAND (((wp.specs LIKE '%\"" $spec_id "\":%' AND (wp.specs LIKE '%\"%" $v "%\"%' OR wp.specs LIKE '%," $v "%' OR wp.specs LIKE '%" $v ",%')) OR (
  894.                                                                         p.label LIKE '%" $v "%'
  895.                                                                     OR
  896.                                                                         p.extra_search_tags LIKE '%" $v "%'
  897.                                                                     OR
  898.                                                                         p.label_sub LIKE '%" $v "%'
  899.                                                                     OR
  900.                                                                         p.compatibility LIKE '%" $v "%'
  901.                                                                     OR
  902.                                                                         p.ean LIKE '%" $v "%'
  903.                                                                     OR
  904.                                                                         p.number LIKE '%" $v "%'
  905.                                                                     OR
  906.                                                                         p.sku LIKE '%" $v "%'
  907.                                                                     )) OR ((wp.child_specs LIKE '%\"" $spec_id "\":%' AND (wp.child_specs LIKE '%\"%" $v "%\"%' OR wp.child_specs LIKE '%," $v "%' OR wp.child_specs LIKE '%" $v ",%')) OR (
  908.                                                                         p.label LIKE '%" $v "%'
  909.                                                                     OR
  910.                                                                         p.extra_search_tags LIKE '%" $v "%'
  911.                                                                     OR
  912.                                                                         p.label_sub LIKE '%" $v "%'
  913.                                                                     OR
  914.                                                                         p.compatibility LIKE '%" $v "%'
  915.                                                                     OR
  916.                                                                         p.ean LIKE '%" $v "%'
  917.                                                                     OR
  918.                                                                         p.number LIKE '%" $v "%'
  919.                                                                     OR
  920.                                                                         p.sku LIKE '%" $v "%'
  921.                                                                     )))";
  922.                         }
  923.                     }
  924.                 }
  925.             }
  926.         }
  927.         /*foreach($search as $v){
  928.             $sql .= "\nAND ";
  929.         }*/
  930.         $sql_order 'wp.pos asc'// Default
  931.         $custom_order false;
  932.         if(!empty($order)){
  933.             if(substr($order03) == 'pos'){
  934.                 if(preg_match('/asc|desc/'strtolower($order))){
  935.                     $sql_order "cp." $order;
  936.                 }else{
  937.                     $sql_order "cp." $order " " $orderDir;
  938.                 }
  939.                 if (!str_contains($order'price_incl')) {
  940.                     $sql_order .= " , cp.id " $orderDir;
  941.                 }
  942.             }else{
  943.                 if(preg_match('/asc|desc/'strtolower($order))){
  944.                     $sql_order "p." $order;
  945.                 }else{
  946.                     $sql_order "p." $order " " $orderDir;
  947.                 }
  948.                 if (!str_contains($order'price_incl')) {
  949.                     $sql_order .= ", p.id " $orderDir;
  950.                 }
  951.                 $custom_order true;
  952.             }
  953.         }
  954.         if(!$custom_order && empty($category)){
  955.             $sql_order "p.id desc";
  956.         }
  957.         $sql_order str_replace('cp.pos''wp.pos'$sql_order);
  958.         if(!empty($filters['q']) && $sql_order == 'cp.pos asc'){
  959.             $fq explode(' '$filters['q']);
  960.             $sql_order "";
  961.             foreach($fq as $fqp){
  962.                 if(empty($sql_order)){
  963.                     $sql_order .= "FIND_IN_SET('" $fqp "', p.label)";
  964.                 }else{
  965.                     $sql_order .= ",FIND_IN_SET('" $fqp "', p.label)";
  966.                 }
  967.             }
  968.         }
  969.         // $sql .= "\nGROUP BY p.id";
  970.         $sql .= "\nORDER BY " $sql_order;
  971.         // dump($sql);die();
  972. /*if(isset($filters['dev']) && $filters['dev'] == true){
  973.     dump($sql);die();
  974. }*/
  975. // dump($sql);die();
  976.         if (!empty($relation)) {
  977.             /*dump($sql);die();
  978.             $query = $em->createQuery($sql);
  979.             echo ( '<pre>' . print_r( SqlFormatter::format($query->getSql()), 1 ) . '</pre>' );
  980.             dump(count($query->getResult()));
  981.             die();*/
  982.         }
  983.         // dump($sql);die();
  984.         /*$query = $em->createQuery($sql);
  985.         echo ( '<pre>' . print_r( SqlFormatter::format($query->getSql()), 1 ) . '</pre>' );
  986.         dump(count($query->getResult()));
  987.         die();*/
  988. // dump($sql);die();
  989.         $query $em->createQuery($sql);
  990. // die($query->getSql());
  991.         if($debug){
  992.             die($query->getSql());
  993.         }
  994.         // $em = $this->getEntityManager();
  995.         /*$sql = "SELECT p
  996.                 FROM TrinityWebshopBundle:Product p
  997.                 " . implode("\n", $join) . "
  998.                 " . implode("\n", $joinNorForCount) . "
  999.                 " . ($Webshop ? "JOIN c.webshop w WITH w.id = " . $webshop_id : "") . "
  1000.                 WHERE p.id > 0
  1001.                 AND p.enabled = 1
  1002.                 AND 
  1003.                 (
  1004.                     (
  1005.                         (p.type = 1 OR p.type = 2)
  1006.                         " . implode("\n", $filterValuesLinked) . "
  1007.                     )
  1008.                     OR
  1009.                     (
  1010.                         (p.type != 1 AND p.type != 2)
  1011.                         " . implode("\n", $filterValuesSimple) . "
  1012.                     )
  1013.                 )
  1014.                 AND c.label != 'Ongesorteerd'
  1015.                 " . ($featured ? "AND p.featured = 1" : "") . "
  1016.                 " . (!$ignoreStock ? "
  1017.                 AND ((p.type = 0 and ((p.stock_amount - p.out_of_stock_quantity) > 0 or p.stock = 0 or p.visible_no_stock = 1)) or p.type > 0)
  1018.                 AND ((p.type = 0 or p.type = 3 or p.type = 4) or ((p.type = 1 or p.type = 2) and p.child_stock_amount > 0))
  1019.                 " : "") . 
  1020.                                 (!empty($and) ? "
  1021.                                 AND
  1022.                                 (
  1023.                                 " . implode("\nAND ", $and) . "
  1024.                                 )
  1025.                 " : '') .
  1026.                                 (!empty($or) ? "
  1027.                                 AND
  1028.                                 (
  1029.                                 " .  implode("\nOR ", $or) . "
  1030.                                 )
  1031.                 " : '') 
  1032.                                 . (!empty($specFilter) ? "AND (" . implode("\nOR ", $specFilter) . ")" : "") . "
  1033.                 ORDER BY " . (!empty($order) ? (substr($order, 0, 3) == 'pos' ? "cp." . $order : "p." . $order) : "cp.pos asc");
  1034.         $query = $em->createQuery($sql);*/
  1035.         if(!empty($page)){
  1036.             $start = (($page $limit) - $limit);
  1037.         }else if(!empty($filters['limit'])){
  1038.             $start = (int)$filters['limit']['start'];
  1039.             $limit = (int)$filters['limit']['limit'];
  1040.         }
  1041.         if($offset 0){
  1042.             $start $offset;
  1043.         }
  1044.         if($noLimit == false){
  1045.             $query->setMaxResults($limit)->setFirstResult($start);
  1046.         }
  1047.         // Doctrine Paginator experiment
  1048.         $results = new \Doctrine\ORM\Tools\Pagination\Paginator($query$fetchJoinCollection true);
  1049.         // $results->setUseOutputWalkers(false);
  1050.         $total count($results);
  1051.         $resultCount count($results->getIterator()->getArrayCopy());
  1052.         $results $results->getIterator()->getArrayCopy();
  1053.         
  1054.         if(!empty($filters['filters'])){
  1055.             // Walk through filtered results
  1056.             foreach($results as $k => $res){
  1057.                 // Only for grouped or configurable products
  1058.                 if($res->getType() > 0){
  1059.                     // Walk through linked products
  1060.                     foreach($res->getLinkedProducts() as $lp){
  1061.                         // See if spec values are given to product
  1062.                         if($lp->getSpecValues()->count() > 0){
  1063.                             // Walk through spec values
  1064.                             foreach($lp->getSpecValues() as $sv){
  1065.                                 $Spec $sv->getSpec();
  1066.                                 if($Spec){
  1067.                                     try {
  1068.                                         // Limit product display on colors
  1069.                                         if($Spec->getType() == 'color' || $Spec->getCode() == 'color'){
  1070.                                             if(array_key_exists($Spec->getId(), $filters['filters'])){
  1071.                                                 // If color matches the filtered colors
  1072.                                                 if(in_array($sv->getValue(), $filters['filters'][$Spec->getId()])){
  1073.                                                     // Set linked product as display product
  1074.                                                     $results[$k]->setDisplayProduct($lp);
  1075.                                                     break(3);
  1076.                                                 }
  1077.                                             }
  1078.                                         }
  1079.                                     } catch (\Exception $ex) {
  1080.                                         //dump($ex);
  1081.                                     }
  1082.                                 }
  1083.                             }
  1084.                         }
  1085.                     }
  1086.                 }
  1087.             }
  1088.         }
  1089.         return [
  1090.             'total'   => $total,
  1091.             'current' => $resultCount,
  1092.             'start'   => $start,
  1093.             'limit'   => $limit,
  1094.             'pages'   => (int)ceil($total $limit),
  1095.             'page'    => (int)$page,
  1096.             'results' => $results
  1097.         ];
  1098.     }
  1099.     public function searchInApi($Webshop$filters = array(), $order null$orderDir 'asc'$noLimit false$ignoreStock false$page 1$limit 9$offset 0$thisIsFilter false){
  1100.         $em $this->getEntityManager();
  1101.         if(isset($filters['q']) && is_array($filters['q'])){
  1102.             $filters['q'] = implode(' '$filters['q']);
  1103.         }
  1104.         if(isset($filters['q']) && strpos($filters['q'], 'linkableproducts') !== false){
  1105.             $ignoreStock true;
  1106.         }
  1107.         $featured = (!empty($filters['featured']) && $filters['featured'] === true);
  1108.         $debug false;
  1109.         if(isset($filters['debug'])){
  1110.             $debug true;
  1111.             unset($filters['debug']);
  1112.         }
  1113.         $join = [];
  1114.         $joinNorForCount = [];
  1115.         $and = [];
  1116.         $or = [];
  1117.         $start 0;
  1118.         if(empty($limit) || !is_numeric($limit)){
  1119.             $limit 9// 9 is default, can be overridden in Trinity
  1120.         }
  1121.         // Check category
  1122.         if(isset($filters['page']) && $page <= 1){
  1123.             $page = (int)$filters['page'];
  1124.         }
  1125.         $webshop_id = (!empty($Webshop) ? $Webshop->getId() : 0);
  1126.         $ignore_ids = [];
  1127.         if(!empty($filters['ignore_ids'])){
  1128.             $ignore_ids $filters['ignore_ids'];
  1129.             if(!is_array($ignore_ids)){
  1130.                 $ignore_ids = [$ignore_ids];
  1131.             }
  1132.             unset($filters['ignore_ids']);
  1133.         }
  1134.         $showHidden false;
  1135.         if(!empty($filters['q'])){
  1136.             if(preg_match('/linkableproducts=1/'$filters['q'])){
  1137.                 $filters['q'] = preg_replace('/&linkableproducts=1/'''$filters['q']);
  1138.                 $showHidden true;
  1139.             }
  1140.         }
  1141.         $ranges       = [];
  1142.         $ranges_joins = [];
  1143.         $ranges_where = [];
  1144.         $fn 0;
  1145.         if(!empty($filters['filters'])){
  1146.             foreach($filters['filters'] as $spec_id => $value){
  1147.                 if(is_array($value) && isset($value['range'])){
  1148.                     foreach($value as $t => $v){
  1149.                         if(!empty($v[0]) && !empty($v[1]) && $v[0] != 'undefined' && $v[1] != 'undefined'){
  1150.                             $ranges_joins[] = "JOIN p.spec_values sv{$fn}x1 WITH sv{$fn}x1.spec = '" $spec_id "'";
  1151.                             $ranges_where[] = "AND (sv{$fn}x1.value BETWEEN " implode(' AND '$v) . " OR sv{$fn}x1.value = '' OR sv{$fn}x1.value IS NULL)";
  1152.                             $fn++;
  1153.                         }
  1154.                     }
  1155.                 }
  1156.             }
  1157.         }
  1158.         $sliders = (!empty($filters['sliders']) ? $filters['sliders'] : []);
  1159.         $sliders_range = (!empty($filters['sliders_range']) ? $filters['sliders_range'] : []);
  1160.         if(!empty($filters['filters'])){
  1161.             foreach($filters['filters'] as $spec_id => $value){
  1162.                 if(in_array($spec_id$sliders) && (int)$value[0] > 0){
  1163.                     $v = [0, (int)$value[0]];
  1164.                     $ranges_joins[] = "JOIN p.spec_values sv{$fn}x1 WITH sv{$fn}x1.spec = '" $spec_id "'";
  1165.                     $ranges_where[] = "AND (sv{$fn}x1.value BETWEEN " implode(' AND '$v) . ")";
  1166.                     $fn++;
  1167.                 }
  1168.                 if(in_array($spec_id$sliders_range)){
  1169.                     $v = [(int)$value['min'], (int)$value['max']];
  1170.                     $ranges_joins[] = "JOIN p.spec_values sv{$fn}x1 WITH sv{$fn}x1.spec = '" $spec_id "'";
  1171.                     $ranges_where[] = "AND (sv{$fn}x1.value BETWEEN " implode(' AND '$v) . ")";
  1172.                     $fn++;
  1173.                 }
  1174.                 /*if(is_array($value) && isset($value['range'])){
  1175.                     foreach($value as $t => $v){
  1176.                         if(!empty($v[0]) && !empty($v[1]) && $v[0] != 'undefined' && $v[1] != 'undefined'){
  1177.                             $ranges_joins[] = "JOIN p.spec_values sv{$fn}x1 WITH sv{$fn}x1.spec = '" . $spec_id . "'";
  1178.                             $ranges_where[] = "AND (sv{$fn}x1.value BETWEEN " . implode(' AND ', $v) . " OR sv{$fn}x1.value = '' OR sv{$fn}x1.value IS NULL)";
  1179.                             $fn++;
  1180.                         }
  1181.                     }
  1182.                 }*/
  1183.             }
  1184.         }
  1185.         $linkableproducts = (!empty($_GET['linkableproducts']) ? true false);
  1186.         $search_raw = (!empty($filters['q']) ? (!is_array($filters['q']) ? [$filters['q']] : $filters['q']) : null);
  1187.         $visible    = (!empty($filters['visible']) ? $filters['visible'] : null);
  1188.         $featured   = (!empty($filters['featured']) ? $filters['featured'] : null);
  1189.         $category   = (!empty($filters['category']) ? $filters['category'] : null);
  1190.         $relation   = (!empty($filters['relation']) ? $filters['relation'] : null);
  1191.         $newproducts = (!empty($filters['newproducts']) ? $filters['newproducts'] : null);
  1192.         $search = [];
  1193.         if(!empty($search_raw)){
  1194.             foreach($search_raw as $sr){
  1195.                 $sr explode(' '$sr);
  1196.                 foreach($sr as $srp){
  1197.                     $search[] = $srp;
  1198.                 }
  1199.             }
  1200.         }
  1201.         $typeFilter "((p.type = 0 or p.type = 3 or p.type = 4) or ((p.type = 1 or p.type = 2) and p.child_stock_amount > 0))";
  1202.         if(!empty($_GET['linkableproducts']) && (empty($_GET['link']) || $_GET['link'] != 'relations')){
  1203.             $visible false;
  1204.             $typeFilter "((p.type = 0 or p.type = 3 or p.type = 4))";
  1205.         }
  1206.         $tags = [];
  1207.         if(!empty($filters['tags'])){
  1208.             $tags $filters['tags'];
  1209.         }
  1210.         $sql "
  1211.         SELECT p
  1212.         
  1213.         FROM TrinityWebshopBundle:Product p
  1214.         JOIN p.product_link wp
  1215.         " . (!empty($category) && ($category instanceof Category || is_array($category)) ? "JOIN p.category cp WITH (cp.category = wp.category)" "") . "
  1216.         " . (!empty($relation) ? "JOIN p.relation_to cp WITH (cp.id = " $relation->getId() . ")" "") . "
  1217.         " . (!empty($tags) ? "JOIN p.tags pt WITH (pt.id IN (" implode(', '$tags) . "))" "") . "
  1218.         " . (!empty($ranges_joins) ? implode("\n"$ranges_joins) : "") . "
  1219.         WHERE 1 = 1
  1220.         AND " . ($linkableproducts "1 = 1" "((p.type = 0 and ((p.stock_amount - p.out_of_stock_quantity) > 0 or p.stock = 0 or p.visible_no_stock = 1)) or p.type > 0)") . "
  1221.         AND " $typeFilter "
  1222.         " . (!empty($ranges_where) ? implode("\n"$ranges_where) : "") . "
  1223.         ";
  1224.         if(!empty($ignore_ids)){
  1225.             $sql .= "\nAND p.id NOT IN (" implode(','$ignore_ids) . ")";
  1226.         }
  1227.         $s = [];
  1228.         if($search){
  1229.             foreach($search as $q){
  1230.                 $q str_replace('\'''_'$q);
  1231.                 if($thisIsFilter){
  1232.                     $s[] = "((p.label LIKE '%" $q "%') OR (p.number LIKE '%" $q "%') OR (wp.specs LIKE '%" $q "%') OR (wp.child_specs LIKE '%" $q "%') OR p.extra_search_tags LIKE '%" $q "%')";
  1233.                 }else{
  1234.                     $s[] = "((wp.specs LIKE '%" $q "%' OR (
  1235.                                                                                 p.label LIKE '%" $q "%'
  1236.                                                                             OR
  1237.                                                                                 p.extra_search_tags LIKE '%" $q "%'
  1238.                                                                             OR
  1239.                                                                                 p.label_sub LIKE '%" $q "%'
  1240.                                                                             OR
  1241.                                                                                 p.compatibility LIKE '%" $q "%'
  1242.                                                                             OR
  1243.                                                                                 p.ean LIKE '%" $q "%'
  1244.                                                                             OR
  1245.                                                                                 p.number LIKE '%" $q "%'
  1246.                                                                             OR
  1247.                                                                                 p.sku LIKE '%" $q "%'
  1248.                                                                             )) OR (wp.child_specs LIKE '%" $q "%' OR (
  1249.                                                                                 p.label LIKE '%" $q "%'
  1250.                                                                             OR
  1251.                                                                                 p.extra_search_tags LIKE '%" $q "%'
  1252.                                                                             OR
  1253.                                                                                 p.label_sub LIKE '%" $q "%'
  1254.                                                                             OR
  1255.                                                                                 p.compatibility LIKE '%" $q "%'
  1256.                                                                             OR
  1257.                                                                                 p.ean LIKE '%" $q "%'
  1258.                                                                             OR
  1259.                                                                                 p.number LIKE '%" $q "%'
  1260.                                                                             OR
  1261.                                                                                 p.sku LIKE '%" $q "%'
  1262.                                                                             )))";
  1263.                 }
  1264.             }
  1265.             $sql .= "\nAND (" implode(" AND "$s) . ")";
  1266.         }
  1267.         if($visible){
  1268.             $sql .= "\nAND p.visible = 1";
  1269.         }
  1270.         if($featured){
  1271.             $sql .= "\nAND p.featured = 1";
  1272.         }
  1273.         if ($newproducts) {
  1274.             $sql .= "\nAND p.new = 1";
  1275.             $sql .= "\nAND (p.new_from is null OR p.new_from <= NOW())";
  1276.             $sql .= "\nAND (p.new_till is null OR p.new_till > NOW())";
  1277.         }
  1278.         if($category && $category instanceof Category){
  1279.             $sql .= "\nAND wp.category = " $category->getId();
  1280.         }
  1281.         if($category && is_array($category)){
  1282.             $sql .= "\nAND wp.category IN (" implode(', '$category) . ")";
  1283.         }
  1284.         if($webshop_id && is_numeric($webshop_id)){
  1285.             $sql .= "\nAND wp.webshop = " $webshop_id;
  1286.         }
  1287.         if(!empty($filters['price']) && is_array($filters['price']) && (!isset($filters['ignore_pricing']) || $filters['ignore_pricing'] == false)){
  1288.             // $sql .= "\nAND ((p.sell_price_incl BETWEEN " . $filters['price'][0] . " AND " . $filters['price'][1] . ") OR p.sell_price_incl < 1)";
  1289.             $sql .= "\nAND ((p.sell_price_incl BETWEEN " $filters['price'][0] . " AND " $filters['price'][1] . "))";
  1290.         }
  1291.         $strict_search = [265];
  1292.         if(!empty($filters['filters'])){
  1293.             foreach($filters['filters'] as $spec_id => $value){
  1294.                 if(in_array($spec_id$sliders) || in_array($spec_id$sliders_range)){
  1295.                     continue;
  1296.                 }
  1297.                 if(!isset($value['range']) && $spec_id != 'q'){
  1298.                     if(!is_array($value)) $value = [$value];
  1299.                     foreach($value as $t => $v){
  1300.                         $v str_replace('\'''_'$v);
  1301.                         if(in_array($spec_id$strict_search) || $thisIsFilter){
  1302.                             $sql .= "\nAND (((wp.specs LIKE '%\"" $spec_id "\":%' AND (wp.specs LIKE '%\"" $v "\"%'))) OR ((wp.child_specs LIKE '%\"" $spec_id "\":%' AND (wp.child_specs LIKE '%\"" $v "\"%'))))";
  1303.                         }else{
  1304.                             $sql .= "\nAND (((wp.specs LIKE '%\"" $spec_id "\":%' AND (wp.specs LIKE '%\"%" $v "%\"%' OR wp.specs LIKE '%," $v "%' OR wp.specs LIKE '%" $v ",%')) OR (
  1305.                                                                         p.label LIKE '%" $v "%'
  1306.                                                                     OR
  1307.                                                                         p.extra_search_tags LIKE '%" $v "%'
  1308.                                                                     OR
  1309.                                                                         p.label_sub LIKE '%" $v "%'
  1310.                                                                     OR
  1311.                                                                         p.compatibility LIKE '%" $v "%'
  1312.                                                                     OR
  1313.                                                                         p.ean LIKE '%" $v "%'
  1314.                                                                     OR
  1315.                                                                         p.number LIKE '%" $v "%'
  1316.                                                                     OR
  1317.                                                                         p.sku LIKE '%" $v "%'
  1318.                                                                     )) OR ((wp.child_specs LIKE '%\"" $spec_id "\":%' AND (wp.child_specs LIKE '%\"%" $v "%\"%' OR wp.child_specs LIKE '%," $v "%' OR wp.child_specs LIKE '%" $v ",%')) OR (
  1319.                                                                         p.label LIKE '%" $v "%'
  1320.                                                                     OR
  1321.                                                                         p.extra_search_tags LIKE '%" $v "%'
  1322.                                                                     OR
  1323.                                                                         p.label_sub LIKE '%" $v "%'
  1324.                                                                     OR
  1325.                                                                         p.compatibility LIKE '%" $v "%'
  1326.                                                                     OR
  1327.                                                                         p.ean LIKE '%" $v "%'
  1328.                                                                     OR
  1329.                                                                         p.number LIKE '%" $v "%'
  1330.                                                                     OR
  1331.                                                                         p.sku LIKE '%" $v "%'
  1332.                                                                     )))";
  1333.                         }
  1334.                     }
  1335.                 }
  1336.             }
  1337.         }
  1338.         /*foreach($search as $v){
  1339.             $sql .= "\nAND ";
  1340.         }*/
  1341.         $sql_order 'cp.pos asc'// Default
  1342.         $custom_order false;
  1343.         if(!empty($order)){
  1344.             if(substr($order03) == 'pos'){
  1345.                 if(preg_match('/asc|desc/'strtolower($order))){
  1346.                     $sql_order "cp." $order;
  1347.                 }else{
  1348.                     $sql_order "cp." $order " " $orderDir;
  1349.                 }
  1350.                 if (!str_contains($order'price_incl')) {
  1351.                     $sql_order .= " , cp.id " $orderDir;
  1352.                 }
  1353.             }else{
  1354.                 if(preg_match('/asc|desc/'strtolower($order))){
  1355.                     $sql_order "p." $order;
  1356.                 }else{
  1357.                     $sql_order "p." $order " " $orderDir;
  1358.                 }
  1359.                 if (!str_contains($order'price_incl')) {
  1360.                     $sql_order .= ", p.id " $orderDir;
  1361.                 }
  1362.                 $custom_order true;
  1363.             }
  1364.         }
  1365.         if(!$custom_order && empty($category)){
  1366.             $sql_order "p.id desc";
  1367.         }
  1368.         if(!empty($filters['q']) && $sql_order == 'cp.pos asc'){
  1369.             $fq explode(' '$filters['q']);
  1370.             $sql_order "";
  1371.             foreach($fq as $fqp){
  1372.                 if(empty($sql_order)){
  1373.                     $sql_order .= "FIND_IN_SET('" $fqp "', p.label)";
  1374.                 }else{
  1375.                     $sql_order .= ",FIND_IN_SET('" $fqp "', p.label)";
  1376.                 }
  1377.             }
  1378.         }
  1379.         // $sql .= "\nGROUP BY p.id";
  1380.         $sql .= "\nORDER BY " $sql_order;
  1381.         // dump($sql);die();
  1382. /*if(isset($filters['dev']) && $filters['dev'] == true){
  1383.     dump($sql);die();
  1384. }*/
  1385. // dump($sql);die();
  1386.         if (!empty($relation)) {
  1387.             /*dump($sql);die();
  1388.             $query = $em->createQuery($sql);
  1389.             echo ( '<pre>' . print_r( SqlFormatter::format($query->getSql()), 1 ) . '</pre>' );
  1390.             dump(count($query->getResult()));
  1391.             die();*/
  1392.         }
  1393.         // dump($sql);die();
  1394.         /*$query = $em->createQuery($sql);
  1395.         echo ( '<pre>' . print_r( SqlFormatter::format($query->getSql()), 1 ) . '</pre>' );
  1396.         dump(count($query->getResult()));
  1397.         die();*/
  1398. // dump($sql);die();
  1399.         $query $em->createQuery($sql);
  1400. // die($query->getSql());
  1401.         if($debug){
  1402.             die($query->getSql());
  1403.         }
  1404.         // $em = $this->getEntityManager();
  1405.         /*$sql = "SELECT p
  1406.                 FROM TrinityWebshopBundle:Product p
  1407.                 " . implode("\n", $join) . "
  1408.                 " . implode("\n", $joinNorForCount) . "
  1409.                 " . ($Webshop ? "JOIN c.webshop w WITH w.id = " . $webshop_id : "") . "
  1410.                 WHERE p.id > 0
  1411.                 AND p.enabled = 1
  1412.                 AND 
  1413.                 (
  1414.                     (
  1415.                         (p.type = 1 OR p.type = 2)
  1416.                         " . implode("\n", $filterValuesLinked) . "
  1417.                     )
  1418.                     OR
  1419.                     (
  1420.                         (p.type != 1 AND p.type != 2)
  1421.                         " . implode("\n", $filterValuesSimple) . "
  1422.                     )
  1423.                 )
  1424.                 AND c.label != 'Ongesorteerd'
  1425.                 " . ($featured ? "AND p.featured = 1" : "") . "
  1426.                 " . (!$ignoreStock ? "
  1427.                 AND ((p.type = 0 and ((p.stock_amount - p.out_of_stock_quantity) > 0 or p.stock = 0 or p.visible_no_stock = 1)) or p.type > 0)
  1428.                 AND ((p.type = 0 or p.type = 3 or p.type = 4) or ((p.type = 1 or p.type = 2) and p.child_stock_amount > 0))
  1429.                 " : "") . 
  1430.                                 (!empty($and) ? "
  1431.                                 AND
  1432.                                 (
  1433.                                 " . implode("\nAND ", $and) . "
  1434.                                 )
  1435.                 " : '') .
  1436.                                 (!empty($or) ? "
  1437.                                 AND
  1438.                                 (
  1439.                                 " .  implode("\nOR ", $or) . "
  1440.                                 )
  1441.                 " : '') 
  1442.                                 . (!empty($specFilter) ? "AND (" . implode("\nOR ", $specFilter) . ")" : "") . "
  1443.                 ORDER BY " . (!empty($order) ? (substr($order, 0, 3) == 'pos' ? "cp." . $order : "p." . $order) : "cp.pos asc");
  1444.         $query = $em->createQuery($sql);*/
  1445.         if(!empty($page)){
  1446.             $start = (($page $limit) - $limit);
  1447.         }else if(!empty($filters['limit'])){
  1448.             $start = (int)$filters['limit']['start'];
  1449.             $limit = (int)$filters['limit']['limit'];
  1450.         }
  1451.         if($offset 0){
  1452.             $start $offset;
  1453.         }
  1454.         if($noLimit == false){
  1455.             $query->setMaxResults($limit)->setFirstResult($start);
  1456.         }
  1457.         // Doctrine Paginator experiment
  1458.         $results = new \Doctrine\ORM\Tools\Pagination\Paginator($query$fetchJoinCollection true);
  1459.         // $results->setUseOutputWalkers(false);
  1460.         $total count($results);
  1461.         $resultCount count($results->getIterator()->getArrayCopy());
  1462.         $results $results->getIterator()->getArrayCopy();
  1463.         
  1464.         if(!empty($filters['filters'])){
  1465.             // Walk through filtered results
  1466.             foreach($results as $k => $res){
  1467.                 // Only for grouped or configurable products
  1468.                 if($res->getType() > 0){
  1469.                     // Walk through linked products
  1470.                     foreach($res->getLinkedProducts() as $lp){
  1471.                         // See if spec values are given to product
  1472.                         if($lp->getSpecValues()->count() > 0){
  1473.                             // Walk through spec values
  1474.                             foreach($lp->getSpecValues() as $sv){
  1475.                                 $Spec $sv->getSpec();
  1476.                                 if($Spec){
  1477.                                     try {
  1478.                                         // Limit product display on colors
  1479.                                         if($Spec->getType() == 'color' || $Spec->getCode() == 'color'){
  1480.                                             if(array_key_exists($Spec->getId(), $filters['filters'])){
  1481.                                                 // If color matches the filtered colors
  1482.                                                 if(in_array($sv->getValue(), $filters['filters'][$Spec->getId()])){
  1483.                                                     // Set linked product as display product
  1484.                                                     $results[$k]->setDisplayProduct($lp);
  1485.                                                     break(3);
  1486.                                                 }
  1487.                                             }
  1488.                                         }
  1489.                                     } catch (\Exception $ex) {
  1490.                                         //dump($ex);
  1491.                                     }
  1492.                                 }
  1493.                             }
  1494.                         }
  1495.                     }
  1496.                 }
  1497.             }
  1498.         }
  1499.         return [
  1500.             'total'   => $total,
  1501.             'current' => $resultCount,
  1502.             'start'   => $start,
  1503.             'limit'   => $limit,
  1504.             'pages'   => (int)ceil($total $limit),
  1505.             'page'    => (int)$page,
  1506.             'results' => $results
  1507.         ];
  1508.     }
  1509.     public function getOneBy($slug ''$Webshop$Category null){
  1510.         $em $this->getEntityManager();
  1511.         $query $this->createQueryBuilder('p')
  1512.             ->innerJoin('p.category''c')
  1513.             ->innerJoin('c.webshop''wp')
  1514.             ->where('wp.id = :webshopid')
  1515.             ->setParameter('webshopid', (!empty($Webshop) ? (!empty($Webshop) ? $Webshop->getId() : 0) : 0));
  1516.         if(!empty($slug)) $query->andWhere('p.slug = \'' $slug '\'');
  1517.         if($Category instanceof \App\Trinity\WebshopBundle\Entity\Category){
  1518.             $query->andWhere('c.id = :categoryid')
  1519.             ->setParameter('categoryid'$Category->getId());
  1520.         }
  1521.         return $query->getQuery()->getSingleResult();
  1522.     }
  1523.     public function gatherProducts($cats){
  1524.         $em $this->getEntityManager();
  1525.         $sql "SELECT    cp
  1526.                 FROM    TrinityWebshopBundle:CategoryProduct cp
  1527.                 JOIN     cp.category c
  1528.                 WHERE     c.id IN (" implode(', '$cats) . ")";
  1529.         $query $em->createQuery($sql);
  1530.         return $query->getResult();
  1531.     }
  1532.     public function recentlyChanged($Webshop$limit 4){
  1533.         $em $this->getEntityManager();
  1534.         // $query = $em->createQuery("SELECT ph.date,a as Product FROM TrinityWebshopBundle:ProductHistory ph JOIN TrinityWebshopBundle:Product a WITH ph.Product = a JOIN a.category cp JOIN cp.category c WHERE (ph.label LIKE '%gewijzigd%') AND c.webshop = " . (!empty($Webshop) ? $Webshop->getId() : 0) . " GROUP BY ph.Product ORDER BY ph.date DESC");
  1535.         $query $em->createQuery("SELECT ph.date,a as Product FROM TrinityWebshopBundle:ProductHistory ph JOIN TrinityWebshopBundle:Product a WITH ph.Product = a JOIN a.category cp JOIN cp.category c WHERE (ph.label LIKE '%gewijzigd%') AND c.webshop = " . (!empty($Webshop) ? $Webshop->getId() : 0) . " ORDER BY ph.date DESC");
  1536.         return $query->setMaxResults($limit)->getResult();
  1537.     }
  1538.     public function recentlyAdded($Webshop$limit 4){
  1539.         $em $this->getEntityManager();
  1540.         // $query = $em->createQuery("SELECT ph.date,a as Product FROM TrinityWebshopBundle:ProductHistory ph JOIN TrinityWebshopBundle:Product a WITH ph.Product = a JOIN a.category cp JOIN cp.category c WHERE (ph.label LIKE '%geïmporteerd%' OR ph.label LIKE '%toegevoegd%') AND c.webshop = " . (!empty($Webshop) ? $Webshop->getId() : 0) . " GROUP BY ph.Product ORDER BY ph.date DESC");
  1541.         $query $em->createQuery("SELECT ph.date,a as Product FROM TrinityWebshopBundle:ProductHistory ph JOIN TrinityWebshopBundle:Product a WITH ph.Product = a JOIN a.category cp JOIN cp.category c WHERE (ph.label LIKE '%geïmporteerd%' OR ph.label LIKE '%toegevoegd%') AND c.webshop = " . (!empty($Webshop) ? $Webshop->getId() : 0) . " ORDER BY ph.date DESC");
  1542.         return $query->setMaxResults($limit)->getResult();
  1543.     }
  1544.     public function recentReviews($Webshop$limit 4){
  1545.         $em $this->getEntityManager();
  1546.         $query $em->createQuery("SELECT r FROM TrinityWebshopBundle:Review r JOIN r.product p JOIN p.category cp JOIN cp.category c WHERE c.webshop = " . (!empty($Webshop) ? $Webshop->getId() : 0) . " GROUP BY r.id ORDER BY r.date DESC");
  1547.         return $query->setMaxResults($limit)->getResult();
  1548.     }
  1549.     public function getReviewsByWebshop($Webshop){
  1550.         $em $this->getEntityManager();
  1551.         $query $em->createQuery("SELECT r FROM TrinityWebshopBundle:Review r JOIN r.product p JOIN p.category cp JOIN cp.category c WHERE c.webshop = " . (!empty($Webshop) ? $Webshop->getId() : 0) . " GROUP BY r.id ORDER BY r.date DESC");
  1552.         return $query->getResult();
  1553.     }
  1554.     public function bestSold($Webshop$limit 4){
  1555.         $em $this->getEntityManager();
  1556.         $query $em->createQuery("SELECT p as Product, COUNT(op.id) AS num FROM TrinityWebshopBundle:Product p JOIN TrinityWebshopBundle:OrderProduct op WITH op.product = p GROUP BY p.id ORDER BY num DESC");
  1557.         return $query->setMaxResults($limit)->getResult();
  1558.     }
  1559.     public function discounted($Webshop$limit 4){
  1560.         $list = [];
  1561.         $em $this->getEntityManager();
  1562.         $sql "SELECT        p
  1563.                 FROM        TrinityWebshopBundle:Product p
  1564.                 JOIN        p.promotions pm
  1565.                 WHERE        (
  1566.                                 pm.date_start IS NULL
  1567.                             OR
  1568.                                 pm.date_start <= '" date('Y-m-d H:i:s') . "'
  1569.                             )
  1570.                 AND            (
  1571.                                 pm.date_end IS NULL
  1572.                             OR
  1573.                                 pm.date_end >= '" date('Y-m-d H:i:s') . "'
  1574.                             )
  1575.                 GROUP BY    p.id
  1576.                 ";
  1577.         $query $em->createQuery($sql);
  1578.         $list $query->setMaxResults($limit)->getResult();
  1579.         if(count($list) < $limit){
  1580.             $sql "SELECT        p
  1581.                     FROM        TrinityWebshopBundle:Product p
  1582.                     JOIN        p.category c
  1583.                     JOIN        c.promotions pm
  1584.                     WHERE        (
  1585.                                     pm.date_start IS NULL
  1586.                                 OR
  1587.                                     pm.date_start <= '" date('Y-m-d H:i:s') . "'
  1588.                                 )
  1589.                     AND            (
  1590.                                     pm.date_end IS NULL
  1591.                                 OR
  1592.                                     pm.date_end >= '" date('Y-m-d H:i:s') . "'
  1593.                                 )
  1594.                     AND c.webshop = " . (!empty($Webshop) ? $Webshop->getId() : 0) . "
  1595.                     GROUP BY    p.id
  1596.                     ";
  1597.             $query $em->createQuery($sql);
  1598.             $list array_merge($list$query->setMaxResults($limit count($list))->getResult());
  1599.         }
  1600.         return $list;
  1601.     }
  1602.     public function getByWebshop($Webshop){
  1603.         $em $this->getEntityManager();
  1604.         $query $em->createQuery(
  1605.             'SELECT p
  1606.             FROM TrinityWebshopBundle:Product p
  1607.             JOIN p.category cp
  1608.             JOIN cp.category c
  1609.             JOIN c.webshop w
  1610.             WHERE w.id LIKE :webshopid'
  1611.         )->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  1612.         return $query->getResult();
  1613.     }
  1614.     public function getByDiscounted($Webshop$limit 9){
  1615.         $sql "SELECT p
  1616.                 FROM TrinityWebshopBundle:Product p
  1617.                 JOIN p.category cp
  1618.                 JOIN cp.category c
  1619.                 JOIN c.webshop w
  1620.                 WHERE w.id LIKE :webshopid
  1621.                 AND p.enabled = 1
  1622.                 AND p.visible = 1
  1623.                 AND p.discount = 1
  1624.                 ORDER BY p.label ASC";
  1625.                 echo ( '<pre>' print_r$sql) . '</pre>' );
  1626.         $em $this->getEntityManager();
  1627.         $query $em->createQuery($sql)
  1628.         ->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  1629.         $query->setMaxResults($limit);
  1630.         return $query->getResult();
  1631.     }
  1632.     public function findLowStock(){
  1633.         $em $this->getEntityManager();
  1634.         $query $em->createQuery(
  1635.             "
  1636.             SELECT p
  1637.             FROM TrinityWebshopBundle:Product p
  1638.             WHERE p.stock = 1
  1639.             AND p.stock_amount < p.notify_on_stock_below
  1640.             "
  1641.         );
  1642.         return $query->getResult();
  1643.     }
  1644.     public function findSimpleForLinking($Webshop$q){
  1645.         $em $this->getEntityManager();
  1646.         $query $em->createQuery(
  1647.             '
  1648.             SELECT p
  1649.             FROM TrinityWebshopBundle:Product p
  1650.             JOIN p.category cp
  1651.             JOIN cp.category c
  1652.             JOIN c.webshop w
  1653.             WHERE w.id LIKE :webshopid
  1654.             AND p.label LIKE \'%' $q '%\'
  1655.             AND p.type = 0
  1656.             '
  1657.         )->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0));
  1658.         return $query->getResult();
  1659.     }
  1660.     public function findByLabelInWebshop($Webshop$label){
  1661.         $em $this->getEntityManager();
  1662.         $query $em->createQuery(
  1663.             'SELECT p
  1664.             FROM TrinityWebshopBundle:Product p
  1665.             JOIN p.category cp
  1666.             JOIN cp.category c
  1667.             JOIN c.webshop w
  1668.             WHERE w.id LIKE :webshopid
  1669.             AND p.label LIKE :label'
  1670.         )
  1671.         ->setParameter('webshopid', (!empty($Webshop) ? $Webshop->getId() : 0))
  1672.         ->setParameter('label'$label);
  1673.         return $query->getOneOrNullResult();
  1674.     }
  1675.     /**
  1676.      * Count unlinked products (likely after language value added to product)
  1677.      *
  1678.      * @return integer
  1679.      */
  1680.     public function countUnlinked(){
  1681.         $sql "SELECT  COUNT(P)
  1682.                 FROM    TrinityWebshopBundle:Product AS P
  1683.                 WHERE   P.language IS NULL
  1684.                 ";
  1685.         return (int)$this->getEntityManager()->createQuery($sql)->getSingleScalarResult();
  1686.     }
  1687.     /**
  1688.      * Count unlinked products (likely after language value added to product)
  1689.      *
  1690.      * @return integer
  1691.      */
  1692.     public function countReviews($product_id$page$perpage$moderate false){
  1693.         $sql "SELECT  COUNT(R)
  1694.                 FROM    TrinityWebshopBundle:Review AS R
  1695.                 WHERE   R.product = " $product_id "
  1696.                 AND     R.parent IS NULL
  1697.                 " . ($moderate "AND R.moderated = 1" '') . "
  1698.                 ";
  1699.         return (int)$this->getEntityManager()->createQuery($sql)->getSingleScalarResult();
  1700.     }
  1701.     /**
  1702.      * Count unlinked products (likely after language value added to product)
  1703.      *
  1704.      * @return integer
  1705.      */
  1706.     public function fetchReviews($product_id$page$perpage$moderate false){
  1707.         $sql "SELECT  R
  1708.                 FROM    TrinityWebshopBundle:Review AS R
  1709.                 WHERE   R.product = " $product_id "
  1710.                 AND     R.parent IS NULL
  1711.                 " . ($moderate "AND R.moderated = 1" '') . "
  1712.                 ORDER BY R.date DESC
  1713.                 ";
  1714.         $query $this->getEntityManager()->createQuery($sql);
  1715.         $query->setMaxResults($perpage);
  1716.         $query->setFirstResult(($page $perpage) - $perpage);
  1717.         return $query->getResult();
  1718.     }
  1719.     /**
  1720.      * Get product labels based on base and optional parent ID
  1721.      *
  1722.      * @return array
  1723.      */
  1724.     public function findLabelsByBase($webshop_id$base_id$parent_id null){
  1725.         $sql "SELECT      GROUP_CONCAT(CONCAT(P.id,'|',P.label)) AS list,GROUP_CONCAT(CONCAT(P.id,'|',R.id)) AS relations
  1726.                 FROM        TrinityWebshopBundle:Product AS P
  1727.                 LEFT JOIN    P.relation_to R
  1728.                 WHERE       P.webshop = " $webshop_id "
  1729.                 AND         P.base = " $base_id "
  1730.                 ORDER BY    P.label ASC
  1731.                 ";
  1732. // dump($sql);
  1733.         $query $this->getEntityManager()->createQuery($sql);
  1734.         $result $query->getResult();
  1735. // dump($result);die();
  1736.         $return = [];
  1737.         if(!empty($result[0]['list'])){
  1738.             $return_raw explode(','$result[0]['list']);
  1739.             foreach($return_raw as $r){
  1740.                 $r explode('|'$r);
  1741.                 $return[$r[0]] = ['label' => $r[1], 'relation' => null];
  1742.             }
  1743.         }
  1744.         if(!empty($result[0]['relations'])){
  1745.             $return_raw explode(','$result[0]['relations']);
  1746.             foreach($return_raw as $r){
  1747.                 $r explode('|'$r);
  1748.                 if(!empty($return[$r[0]])){
  1749.                     $return[$r[0]]['relation'] = $r[1];
  1750.                 }
  1751.             }
  1752.         }
  1753.         // dump($return);die();
  1754.         return $return;
  1755.     }
  1756. }