src/Eccube/Repository/ProductRepository.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Repository;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Eccube\Common\EccubeConfig;
  15. use Eccube\Doctrine\Query\Queries;
  16. use Eccube\Entity\Product;
  17. use Eccube\Entity\ProductStock;
  18. use Eccube\Util\StringUtil;
  19. use Symfony\Bridge\Doctrine\RegistryInterface;
  20. /**
  21.  * ProductRepository
  22.  *
  23.  * This class was generated by the Doctrine ORM. Add your own custom
  24.  * repository methods below.
  25.  */
  26. class ProductRepository extends AbstractRepository
  27. {
  28.     /**
  29.      * @var Queries
  30.      */
  31.     protected $queries;
  32.     /**
  33.      * @var EccubeConfig
  34.      */
  35.     protected $eccubeConfig;
  36.     /**
  37.      * ProductRepository constructor.
  38.      *
  39.      * @param RegistryInterface $registry
  40.      * @param Queries $queries
  41.      * @param EccubeConfig $eccubeConfig
  42.      */
  43.     public function __construct(
  44.         RegistryInterface $registry,
  45.         Queries $queries,
  46.         EccubeConfig $eccubeConfig
  47.     ) {
  48.         parent::__construct($registry,
  49.         Product::class);
  50.         $this->queries $queries;
  51.         $this->eccubeConfig $eccubeConfig;
  52.     }
  53.     /**
  54.      * Find the Product with sorted ClassCategories.
  55.      *
  56.      * @param integer $productId
  57.      *
  58.      * @return Product
  59.      */
  60.     public function findWithSortedClassCategories($productId)
  61.     {
  62.         $qb $this->createQueryBuilder('p');
  63.         $qb->addSelect(['pc''cc1''cc2''pi''pt'])
  64.             ->innerJoin('p.ProductClasses''pc')
  65.             ->leftJoin('pc.ClassCategory1''cc1')
  66.             ->leftJoin('pc.ClassCategory2''cc2')
  67.             ->leftJoin('p.ProductImage''pi')
  68.             ->leftJoin('p.ProductTag''pt')
  69.             ->where('p.id = :id')
  70.             ->andWhere('pc.visible = :visible')
  71.             ->setParameter('id'$productId)
  72.             ->setParameter('visible'true)
  73.             ->orderBy('cc1.sort_no''DESC')
  74.             ->addOrderBy('cc2.sort_no''DESC');
  75.         $product $qb
  76.             ->getQuery()
  77.             ->getSingleResult();
  78.         return $product;
  79.     }
  80.     /**
  81.      * Find the Products with sorted ClassCategories.
  82.      *
  83.      * @param array $ids Product in ids
  84.      * @param string $indexBy The index for the from.
  85.      *
  86.      * @return ArrayCollection|array
  87.      */
  88.     public function findProductsWithSortedClassCategories(array $ids$indexBy null)
  89.     {
  90.         if (count($ids) < 1) {
  91.             return [];
  92.         }
  93.         $qb $this->createQueryBuilder('p'$indexBy);
  94.         $qb->addSelect(['pc''cc1''cc2''pi''pt''tr''ps'])
  95.             ->innerJoin('p.ProductClasses''pc')
  96.             // XXX Joined 'TaxRule' and 'ProductStock' to prevent lazy loading
  97.             ->leftJoin('pc.TaxRule''tr')
  98.             ->innerJoin('pc.ProductStock''ps')
  99.             ->leftJoin('pc.ClassCategory1''cc1')
  100.             ->leftJoin('pc.ClassCategory2''cc2')
  101.             ->leftJoin('p.ProductImage''pi')
  102.             ->leftJoin('p.ProductTag''pt')
  103.             ->where($qb->expr()->in('p.id'$ids))
  104.             ->andWhere('pc.visible = :visible')
  105.             ->setParameter('visible'true)
  106.             ->orderBy('cc1.sort_no''DESC')
  107.             ->addOrderBy('cc2.sort_no''DESC');
  108.         $products $qb
  109.             ->getQuery()
  110.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short'])
  111.             ->getResult();
  112.         return $products;
  113.     }
  114.     /**
  115.      * get query builder.
  116.      *
  117.      * @param  array $searchData
  118.      *
  119.      * @return \Doctrine\ORM\QueryBuilder
  120.      */
  121.     public function getQueryBuilderBySearchData($searchData)
  122.     {
  123.         $qb $this->createQueryBuilder('p')
  124.             ->andWhere('p.Status = 1');
  125.         // category
  126.         $categoryJoin false;
  127.         if (!empty($searchData['category_id']) && $searchData['category_id']) {
  128.             $Categories $searchData['category_id']->getSelfAndDescendants();
  129.             if ($Categories) {
  130.                 $qb
  131.                     ->innerJoin('p.ProductCategories''pct')
  132.                     ->innerJoin('pct.Category''c')
  133.                     ->andWhere($qb->expr()->in('pct.Category'':Categories'))
  134.                     ->setParameter('Categories'$Categories);
  135.                 $categoryJoin true;
  136.             }
  137.         }
  138.         // name
  139.         if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
  140.             $keywords preg_split('/[\s ]+/u'str_replace(['%''_'], ['\\%''\\_'], $searchData['name']), -1PREG_SPLIT_NO_EMPTY);
  141.             foreach ($keywords as $index => $keyword) {
  142.                 $key sprintf('keyword%s'$index);
  143.                 $qb
  144.                     ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR
  145.                         NORMALIZE(p.search_word) LIKE NORMALIZE(:%s) OR
  146.                         EXISTS (SELECT wpc%d FROM \Eccube\Entity\ProductClass wpc%d WHERE p = wpc%d.Product AND NORMALIZE(wpc%d.code) LIKE NORMALIZE(:%s))',
  147.                         $key$key$index$index$index$index$key))
  148.                     ->setParameter($key'%'.$keyword.'%');
  149.             }
  150.         }
  151.         // Order By
  152.         // 価格低い順
  153.         $config $this->eccubeConfig;
  154.         if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['eccube_product_order_price_lower']) {
  155.             //@see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
  156.             $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
  157.             $qb->innerJoin('p.ProductClasses''pc');
  158.             $qb->andWhere('pc.visible = true');
  159.             $qb->groupBy('p.id');
  160. //            $qb->orderBy('price02_min', 'ASC');
  161.             $qb->orderBy('p.default_price''ASC');
  162.             $qb->addOrderBy('p.id''DESC');
  163.         // 価格高い順
  164.         } elseif (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['eccube_product_order_price_higher']) {
  165.             $qb->addSelect('MAX(pc.price02) as HIDDEN price02_max');
  166.             $qb->innerJoin('p.ProductClasses''pc');
  167.             $qb->andWhere('pc.visible = true');
  168.             $qb->groupBy('p.id');
  169. //            $qb->orderBy('price02_max', 'DESC');
  170.             $qb->orderBy('p.default_price''DESC');
  171.             $qb->addOrderBy('p.id''DESC');
  172.         // 新着順
  173.         } elseif (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['eccube_product_order_newer']) {
  174.             // 在庫切れ商品非表示の設定が有効時対応
  175.             // @see https://github.com/EC-CUBE/ec-cube/issues/1998
  176.             if ($this->getEntityManager()->getFilters()->isEnabled('option_nostock_hidden') == true) {
  177.                 $qb->innerJoin('p.ProductClasses''pc');
  178.                 $qb->andWhere('pc.visible = true');
  179.             }
  180.             $qb->orderBy('p.create_date''DESC');
  181.             $qb->addOrderBy('p.id''DESC');
  182.         } else {
  183.             if ($categoryJoin === false) {
  184.                 $qb
  185.                     ->leftJoin('p.ProductCategories''pct')
  186.                     ->leftJoin('pct.Category''c');
  187.             }
  188.             $qb
  189.                 ->addOrderBy('p.id''DESC');
  190.         }
  191.         return $this->queries->customize(QueryKey::PRODUCT_SEARCH$qb$searchData);
  192.     }
  193.     /**
  194.      * get query builder.
  195.      *
  196.      * @param  array $searchData
  197.      *
  198.      * @return \Doctrine\ORM\QueryBuilder
  199.      */
  200.     public function getQueryBuilderBySearchDataForAdmin($searchData)
  201.     {
  202.         $qb $this->createQueryBuilder('p')
  203.             ->addSelect('pc''pi''tr''ps')
  204.             ->innerJoin('p.ProductClasses''pc')
  205.             ->leftJoin('p.ProductImage''pi')
  206.             ->leftJoin('pc.TaxRule''tr')
  207.             ->leftJoin('pc.ProductStock''ps')
  208.             ->andWhere('pc.visible = :visible')
  209.             ->setParameter('visible'true);
  210.         // id
  211.         if (isset($searchData['id']) && StringUtil::isNotBlank($searchData['id'])) {
  212.             $id preg_match('/^\d{0,10}$/'$searchData['id'])  ? $searchData['id'] : null;
  213.             if ($id && $id '2147483647' && $this->isPostgreSQL()) {
  214.                 $id null;
  215.             }
  216.             $qb
  217.                 ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid')
  218.                 ->setParameter('id'$id)
  219.                 ->setParameter('likeid''%'.str_replace(['%''_'], ['\\%''\\_'], $searchData['id']).'%');
  220.         }
  221.         // code
  222.         /*
  223.         if (!empty($searchData['code']) && $searchData['code']) {
  224.             $qb
  225.                 ->innerJoin('p.ProductClasses', 'pc')
  226.                 ->andWhere('pc.code LIKE :code')
  227.                 ->setParameter('code', '%' . $searchData['code'] . '%');
  228.         }
  229.         // name
  230.         if (!empty($searchData['name']) && $searchData['name']) {
  231.             $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
  232.             foreach ($keywords as $keyword) {
  233.                 $qb
  234.                     ->andWhere('p.name LIKE :name')
  235.                     ->setParameter('name', '%' . $keyword . '%');
  236.             }
  237.         }
  238.        */
  239.         // category
  240.         if (!empty($searchData['category_id']) && $searchData['category_id']) {
  241.             $Categories $searchData['category_id']->getSelfAndDescendants();
  242.             if ($Categories) {
  243.                 $qb
  244.                     ->innerJoin('p.ProductCategories''pct')
  245.                     ->innerJoin('pct.Category''c')
  246.                     ->andWhere($qb->expr()->in('pct.Category'':Categories'))
  247.                     ->setParameter('Categories'$Categories);
  248.             }
  249.         }
  250.         // status
  251.         if (!empty($searchData['status']) && $searchData['status']) {
  252.             $qb
  253.                 ->andWhere($qb->expr()->in('p.Status'':Status'))
  254.                 ->setParameter('Status'$searchData['status']);
  255.         }
  256.         // link_status
  257.         if (isset($searchData['link_status']) && !empty($searchData['link_status'])) {
  258.             $qb
  259.                 ->andWhere($qb->expr()->in('p.Status'':Status'))
  260.                 ->setParameter('Status'$searchData['link_status']);
  261.         }
  262.         // stock status
  263.         if (isset($searchData['stock_status'])) {
  264.             $qb
  265.                 ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
  266.                 ->setParameter('StockUnlimited'$searchData['stock_status']);
  267.         }
  268.         // stock status
  269.         if (isset($searchData['stock']) && !empty($searchData['stock'])) {
  270.             switch ($searchData['stock']) {
  271.                 case [ProductStock::IN_STOCK]:
  272.                     $qb->andWhere('pc.stock_unlimited = true OR pc.stock > 0');
  273.                     break;
  274.                 case [ProductStock::OUT_OF_STOCK]:
  275.                     $qb->andWhere('pc.stock_unlimited = false AND pc.stock <= 0');
  276.                     break;
  277.                 default:
  278.                     // 共に選択された場合は全権該当するので検索条件に含めない
  279.             }
  280.         }
  281.         // crate_date
  282.         if (!empty($searchData['create_datetime_start']) && $searchData['create_datetime_start']) {
  283.             $date $searchData['create_datetime_start'];
  284.             $qb
  285.                 ->andWhere('p.create_date >= :create_date_start')
  286.                 ->setParameter('create_date_start'$date);
  287.         } elseif (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
  288.             $date $searchData['create_date_start'];
  289.             $qb
  290.                 ->andWhere('p.create_date >= :create_date_start')
  291.                 ->setParameter('create_date_start'$date);
  292.         }
  293.         if (!empty($searchData['create_datetime_end']) && $searchData['create_datetime_end']) {
  294.             $date $searchData['create_datetime_end'];
  295.             $qb
  296.                 ->andWhere('p.create_date < :create_date_end')
  297.                 ->setParameter('create_date_end'$date);
  298.         } elseif (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
  299.             $date = clone $searchData['create_date_end'];
  300.             $date $date
  301.                 ->modify('+1 days');
  302.             $qb
  303.                 ->andWhere('p.create_date < :create_date_end')
  304.                 ->setParameter('create_date_end'$date);
  305.         }
  306.         // update_date
  307.         if (!empty($searchData['update_datetime_start']) && $searchData['update_datetime_start']) {
  308.             $date $searchData['update_datetime_start'];
  309.             $qb
  310.                 ->andWhere('p.update_date >= :update_date_start')
  311.                 ->setParameter('update_date_start'$date);
  312.         } elseif (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
  313.             $date $searchData['update_date_start'];
  314.             $qb
  315.                 ->andWhere('p.update_date >= :update_date_start')
  316.                 ->setParameter('update_date_start'$date);
  317.         }
  318.         if (!empty($searchData['update_datetime_end']) && $searchData['update_datetime_end']) {
  319.             $date $searchData['update_datetime_end'];
  320.             $qb
  321.                 ->andWhere('p.update_date < :update_date_end')
  322.                 ->setParameter('update_date_end'$date);
  323.         } elseif (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
  324.             $date = clone $searchData['update_date_end'];
  325.             $date $date
  326.                 ->modify('+1 days');
  327.             $qb
  328.                 ->andWhere('p.update_date < :update_date_end')
  329.                 ->setParameter('update_date_end'$date);
  330.         }
  331.         // Order By
  332.         $qb
  333.             ->orderBy('p.update_date''DESC');
  334.         return $this->queries->customize(QueryKey::PRODUCT_SEARCH_ADMIN$qb$searchData);
  335.     }
  336. }