src/Eccube/Repository/ProductRepository.php line 58

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.hide_off_flag = 0')
  138.             ->andWhere('p.Status = 1');
  139.         // ログインユーザーの会員ランクを取得
  140.         $userRank $searchData['rank'] ?? null;
  141.         // ProductRankRestrictionとの結合を追加
  142.         $qb->leftJoin('Eccube\Entity\ProductRankRestriction''prr''WITH''prr.product = p.id AND prr.rank = :userRank')
  143.             ->setParameter('userRank'$userRank);
  144.         // ユーザーランクに基づいて商品を制限
  145.         if ($userRank) {
  146.             $qb->andWhere('prr.rank IS NULL')
  147.                ->setParameter('userRank'$userRank);
  148.         } else {
  149.             // 未ログインユーザーの場合、制限のない商品のみを表示
  150.             $qb->andWhere('prr.rank IS NULL');
  151.         }
  152.         // category
  153.         $categoryJoin false;
  154.         if (!empty($searchData['category_id']) && $searchData['category_id']) {
  155.             $Categories $searchData['category_id']->getSelfAndDescendants();
  156.             if ($Categories) {
  157.                 $qb
  158.                     ->innerJoin('p.ProductCategories''pct')
  159.                     ->innerJoin('pct.Category''c')
  160.                     ->andWhere($qb->expr()->in('pct.Category'':Categories'))
  161.                     ->setParameter('Categories'$Categories);
  162.                 $categoryJoin true;
  163.             }
  164.         }
  165.         // name
  166.         if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
  167.             $keywords preg_split('/[\s ]+/u'str_replace(['%''_'], ['\\%''\\_'], $searchData['name']), -1PREG_SPLIT_NO_EMPTY);
  168.             foreach ($keywords as $index => $keyword) {
  169.                 $key sprintf('keyword%s'$index);
  170.                 $qb
  171.                     ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR
  172.                         NORMALIZE(p.search_word) LIKE NORMALIZE(:%s) OR
  173.                         EXISTS (SELECT wpc%d FROM \Eccube\Entity\ProductClass wpc%d WHERE p = wpc%d.Product AND NORMALIZE(wpc%d.code) LIKE NORMALIZE(:%s))',
  174.                         $key$key$index$index$index$index$key))
  175.                     ->setParameter($key'%'.$keyword.'%');
  176.             }
  177.         }
  178.         // Order By
  179.         // 価格低い順
  180.         $config $this->eccubeConfig;
  181.         if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['eccube_product_order_price_lower']) {
  182.             // @see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
  183.             $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
  184.             $qb->innerJoin('p.ProductClasses''pc');
  185.             $qb->andWhere('pc.visible = true');
  186.             $qb->groupBy('p.id');
  187.             $qb->orderBy('price02_min''ASC');
  188.             $qb->addOrderBy('p.id''DESC');
  189.         // 価格高い順
  190.         } elseif (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['eccube_product_order_price_higher']) {
  191.             $qb->addSelect('MAX(pc.price02) as HIDDEN price02_max');
  192.             $qb->innerJoin('p.ProductClasses''pc');
  193.             $qb->andWhere('pc.visible = true');
  194.             $qb->groupBy('p.id');
  195.             $qb->orderBy('price02_max''DESC');
  196.             $qb->addOrderBy('p.id''DESC');
  197.         // 新着順
  198.         } elseif (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['eccube_product_order_newer']) {
  199.             // 在庫切れ商品非表示の設定が有効時対応
  200.             // @see https://github.com/EC-CUBE/ec-cube/issues/1998
  201.             if ($this->getEntityManager()->getFilters()->isEnabled('option_nostock_hidden') == true) {
  202.                 $qb->innerJoin('p.ProductClasses''pc');
  203.                 $qb->andWhere('pc.visible = true');
  204.             }
  205.             $qb->orderBy('p.create_date''DESC');
  206.             $qb->addOrderBy('p.id''DESC');
  207.         } else {
  208.             if ($categoryJoin === false) {
  209.                 $qb
  210.                     ->leftJoin('p.ProductCategories''pct')
  211.                     ->leftJoin('pct.Category''c');
  212.             }
  213.             $qb
  214.                 ->addOrderBy('p.id''DESC');
  215.         }
  216.         return $this->queries->customize(QueryKey::PRODUCT_SEARCH$qb$searchData);
  217.     }
  218.     /**
  219.      * get query builder.
  220.      *
  221.      * @param array{
  222.      *         id?:string|int|null,
  223.      *         category_id?:Category,
  224.      *         status?:ProductStatus[],
  225.      *         link_status?:ProductStatus[],
  226.      *         stock_status?:int,
  227.      *         stock?:ProductStock::IN_STOCK|ProductStock::OUT_OF_STOCK,
  228.      *         tag_id?:Tag,
  229.      *         create_datetime_start?:\DateTime,
  230.      *         create_datetime_end?:\DateTime,
  231.      *         create_date_start?:\DateTime,
  232.      *         create_date_end?:\DateTime,
  233.      *         update_datetime_start?:\DateTime,
  234.      *         update_datetime_end?:\DateTime,
  235.      *         update_date_start?:\DateTime,
  236.      *         update_date_end?:\DateTime,
  237.      *         sortkey?:string,
  238.      *         sorttype?:string
  239.      *     } $searchData
  240.      *
  241.      * @return \Doctrine\ORM\QueryBuilder
  242.      */
  243.     public function getQueryBuilderBySearchDataForAdmin($searchData)
  244.     {
  245.         $qb $this->createQueryBuilder('p')
  246.             ->addSelect('pc''pi''tr''ps')
  247.             ->innerJoin('p.ProductClasses''pc')
  248.             ->leftJoin('p.ProductImage''pi')
  249.             ->leftJoin('pc.TaxRule''tr')
  250.             ->leftJoin('pc.ProductStock''ps')
  251.             ->andWhere('pc.visible = :visible')
  252.             ->setParameter('visible'true);
  253.         // id
  254.         if (isset($searchData['id']) && StringUtil::isNotBlank($searchData['id'])) {
  255.             $id preg_match('/^\d{0,10}$/'$searchData['id']) ? $searchData['id'] : null;
  256.             if ($id && $id '2147483647' && $this->isPostgreSQL()) {
  257.                 $id null;
  258.             }
  259.             $qb
  260.                 ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid')
  261.                 ->setParameter('id'$id)
  262.                 ->setParameter('likeid''%'.str_replace(['%''_'], ['\\%''\\_'], $searchData['id']).'%');
  263.         }
  264.         // code
  265.         /*
  266.         if (!empty($searchData['code']) && $searchData['code']) {
  267.             $qb
  268.                 ->innerJoin('p.ProductClasses', 'pc')
  269.                 ->andWhere('pc.code LIKE :code')
  270.                 ->setParameter('code', '%' . $searchData['code'] . '%');
  271.         }
  272.         // name
  273.         if (!empty($searchData['name']) && $searchData['name']) {
  274.             $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
  275.             foreach ($keywords as $keyword) {
  276.                 $qb
  277.                     ->andWhere('p.name LIKE :name')
  278.                     ->setParameter('name', '%' . $keyword . '%');
  279.             }
  280.         }
  281.        */
  282.         // category
  283.         if (!empty($searchData['category_id']) && $searchData['category_id']) {
  284.             $Categories $searchData['category_id']->getSelfAndDescendants();
  285.             if ($Categories) {
  286.                 $qb
  287.                     ->innerJoin('p.ProductCategories''pct')
  288.                     ->innerJoin('pct.Category''c')
  289.                     ->andWhere($qb->expr()->in('pct.Category'':Categories'))
  290.                     ->setParameter('Categories'$Categories);
  291.             }
  292.         }
  293.         // status
  294.         if (!empty($searchData['status']) && $searchData['status']) {
  295.             $qb
  296.                 ->andWhere($qb->expr()->in('p.Status'':Status'))
  297.                 ->setParameter('Status'$searchData['status']);
  298.         }
  299.         // link_status
  300.         if (isset($searchData['link_status']) && !empty($searchData['link_status'])) {
  301.             $qb
  302.                 ->andWhere($qb->expr()->in('p.Status'':Status'))
  303.                 ->setParameter('Status'$searchData['link_status']);
  304.         }
  305.         // stock status
  306.         if (isset($searchData['stock_status'])) {
  307.             $qb
  308.                 ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
  309.                 ->setParameter('StockUnlimited'$searchData['stock_status']);
  310.         }
  311.         // stock status
  312.         if (isset($searchData['stock']) && !empty($searchData['stock'])) {
  313.             switch ($searchData['stock']) {
  314.                 case [ProductStock::IN_STOCK]:
  315.                     $qb->andWhere('pc.stock_unlimited = true OR pc.stock > 0');
  316.                     break;
  317.                 case [ProductStock::OUT_OF_STOCK]:
  318.                     $qb->andWhere('pc.stock_unlimited = false AND pc.stock <= 0');
  319.                     break;
  320.                 default:
  321.                     // 共に選択された場合は全権該当するので検索条件に含めない
  322.             }
  323.         }
  324.         // tag
  325.         if (!empty($searchData['tag_id']) && $searchData['tag_id']) {
  326.             $qb
  327.                 ->innerJoin('p.ProductTag''pt')
  328.                 ->andWhere('pt.Tag = :tag_id')
  329.                 ->setParameter('tag_id'$searchData['tag_id']);
  330.         }
  331.         // crate_date
  332.         if (!empty($searchData['create_datetime_start']) && $searchData['create_datetime_start']) {
  333.             $date $searchData['create_datetime_start'];
  334.             $qb
  335.                 ->andWhere('p.create_date >= :create_date_start')
  336.                 ->setParameter('create_date_start'$date);
  337.         } elseif (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
  338.             $date $searchData['create_date_start'];
  339.             $qb
  340.                 ->andWhere('p.create_date >= :create_date_start')
  341.                 ->setParameter('create_date_start'$date);
  342.         }
  343.         if (!empty($searchData['create_datetime_end']) && $searchData['create_datetime_end']) {
  344.             $date $searchData['create_datetime_end'];
  345.             $qb
  346.                 ->andWhere('p.create_date < :create_date_end')
  347.                 ->setParameter('create_date_end'$date);
  348.         } elseif (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
  349.             $date = clone $searchData['create_date_end'];
  350.             $date $date
  351.                 ->modify('+1 days');
  352.             $qb
  353.                 ->andWhere('p.create_date < :create_date_end')
  354.                 ->setParameter('create_date_end'$date);
  355.         }
  356.         // update_date
  357.         if (!empty($searchData['update_datetime_start']) && $searchData['update_datetime_start']) {
  358.             $date $searchData['update_datetime_start'];
  359.             $qb
  360.                 ->andWhere('p.update_date >= :update_date_start')
  361.                 ->setParameter('update_date_start'$date);
  362.         } elseif (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
  363.             $date $searchData['update_date_start'];
  364.             $qb
  365.                 ->andWhere('p.update_date >= :update_date_start')
  366.                 ->setParameter('update_date_start'$date);
  367.         }
  368.         if (!empty($searchData['update_datetime_end']) && $searchData['update_datetime_end']) {
  369.             $date $searchData['update_datetime_end'];
  370.             $qb
  371.                 ->andWhere('p.update_date < :update_date_end')
  372.                 ->setParameter('update_date_end'$date);
  373.         } elseif (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
  374.             $date = clone $searchData['update_date_end'];
  375.             $date $date
  376.                 ->modify('+1 days');
  377.             $qb
  378.                 ->andWhere('p.update_date < :update_date_end')
  379.                 ->setParameter('update_date_end'$date);
  380.         }
  381.         // Order By
  382.         if (isset($searchData['sortkey']) && !empty($searchData['sortkey'])) {
  383.             $sortOrder = (isset($searchData['sorttype']) && $searchData['sorttype'] == 'a') ? 'ASC' 'DESC';
  384.             $qb->orderBy(self::COLUMNS[$searchData['sortkey']], $sortOrder);
  385.             $qb->addOrderBy('p.update_date''DESC');
  386.             $qb->addOrderBy('p.id''DESC');
  387.         } else {
  388.             $qb->orderBy('p.update_date''DESC');
  389.             $qb->addOrderBy('p.id''DESC');
  390.         }
  391.         return $this->queries->customize(QueryKey::PRODUCT_SEARCH_ADMIN$qb$searchData);
  392.     }
  393.     public function findWithProductTag($tagId$limit 10) {
  394.         $qb $this->createQueryBuilder('p');
  395.         $qb->innerJoin('p.ProductTag''pt')
  396.             ->andWhere('pt.Tag = :tag_id')
  397.             ->andWhere('p.Status = 1')
  398.             ->setParameter('tag_id'$tagId)
  399.             ->orderBy('p.create_date''DESC')
  400.             ->setMaxResults($limit);
  401.         return $qb->getQuery()->getResult();
  402.     }
  403.     public function findNew($limit 10$rank null) {
  404.         $qb $this->createQueryBuilder('p');
  405.         $qb->leftJoin('Eccube\Entity\ProductRankRestriction''prr''WITH''prr.product = p.id AND prr.rank = :userRank')
  406.             ->andWhere('prr.rank IS NULL')
  407.             ->andWhere('p.Status = :status')
  408.             ->andWhere('p.hide_off_flag = 0')
  409.             ->setParameter('userRank'$rank)
  410.             ->setParameter('status'\Eccube\Entity\Master\ProductStatus::DISPLAY_SHOW)
  411.             ->orderBy('p.create_date''DESC')
  412.             ->addOrderBy('p.id''DESC')
  413.             ->setMaxResults($limit);
  414.         return $qb->getQuery()->getResult();
  415.     }
  416.     public function findWithCategory($categoryId$limit 10$rank null) {
  417.         $qb $this->createQueryBuilder('p');
  418.         $qb->innerJoin('p.ProductCategories''pc')
  419.             ->leftJoin('Eccube\Entity\ProductRankRestriction''prr''WITH''prr.product = p.id AND prr.rank = :userRank')
  420.             ->setParameter('userRank'$rank)
  421.             ->andWhere('prr.rank IS NULL')
  422.             ->andWhere('pc.Category = :category_id')
  423.             ->andWhere('p.Status = 1')
  424.             ->andWhere('p.hide_off_flag = 0')
  425.             ->setParameter('category_id'$categoryId)
  426.             ->orderBy('p.create_date''DESC')
  427.             ->addOrderBy('p.id''DESC')
  428.             ->setMaxResults($limit);
  429.         return $qb->getQuery()->getResult();
  430.     }
  431.     public function existsDisplayableProducts($searchData)
  432.     {
  433.         $qb $this->getQueryBuilderBySearchData($searchData)
  434.             ->select('1')
  435.             ->setMaxResults(1)
  436.             ->getQuery();
  437.         // キャッシュにnamespaceを設定
  438.         $cacheDriver $qb->getResultCacheDriver();
  439.         $cacheDriver->setNamespace('Category');
  440.         // 1週間キャッシュ
  441.         $result $qb->useResultCache(true604800)->getOneOrNullResult();
  442.         return !is_null($result);
  443.     }
  444. }