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