src/Eccube/Repository/ProductRepository.php line 63

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