src/Eccube/Repository/ProductRepository.php line 75

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