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'])
  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.             dump($searchData);
  139.         // category
  140.         $categoryJoin false;
  141.         if (!empty($searchData['category_id']) && $searchData['category_id']) {
  142.             $Categories $searchData['category_id']->getSelfAndDescendants();
  143.             if ($Categories) {
  144.                 $qb
  145.                     ->innerJoin('p.ProductCategories''pct')
  146.                     ->innerJoin('pct.Category''c')
  147.                     ->andWhere($qb->expr()->in('pct.Category'':Categories'))
  148.                     ->setParameter('Categories'$Categories);
  149.                 $categoryJoin true;
  150.             }
  151.         }
  152.         // name
  153.         if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
  154.             $keywords preg_split('/[\s ]+/u'str_replace(['%''_'], ['\\%''\\_'], $searchData['name']), -1PREG_SPLIT_NO_EMPTY);
  155.             foreach ($keywords as $index => $keyword) {
  156.                 $key sprintf('keyword%s'$index);
  157.                 $qb
  158.                     ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR
  159.                         NORMALIZE(p.search_word) LIKE NORMALIZE(:%s) OR
  160.                         EXISTS (SELECT wpc%d FROM \Eccube\Entity\ProductClass wpc%d WHERE p = wpc%d.Product AND NORMALIZE(wpc%d.code) LIKE NORMALIZE(:%s))',
  161.                         $key$key$index$index$index$index$key))
  162.                     ->setParameter($key'%'.$keyword.'%');
  163.             }
  164.         }
  165.         // Order By
  166.         // 価格低い順
  167.         $config $this->eccubeConfig;
  168.         if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['eccube_product_order_price_lower']) {
  169.             // @see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
  170.             $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
  171.             $qb->innerJoin('p.ProductClasses''pc');
  172.             $qb->andWhere('pc.visible = true');
  173.             $qb->groupBy('p.id');
  174.             $qb->orderBy('price02_min''ASC');
  175.             $qb->addOrderBy('p.id''DESC');
  176.         // 価格高い順
  177.         } elseif (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['eccube_product_order_price_higher']) {
  178.             $qb->addSelect('MAX(pc.price02) as HIDDEN price02_max');
  179.             $qb->innerJoin('p.ProductClasses''pc');
  180.             $qb->andWhere('pc.visible = true');
  181.             $qb->groupBy('p.id');
  182.             $qb->orderBy('price02_max''DESC');
  183.             $qb->addOrderBy('p.id''DESC');
  184.         // 新着順
  185.         } elseif (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['eccube_product_order_newer']) {
  186.             // 在庫切れ商品非表示の設定が有効時対応
  187.             // @see https://github.com/EC-CUBE/ec-cube/issues/1998
  188.             if ($this->getEntityManager()->getFilters()->isEnabled('option_nostock_hidden') == true) {
  189.                 $qb->innerJoin('p.ProductClasses''pc');
  190.                 $qb->andWhere('pc.visible = true');
  191.             }
  192.             $qb->orderBy('p.create_date''DESC');
  193.             $qb->addOrderBy('p.id''DESC');
  194.         } else {
  195.             if ($categoryJoin === false) {
  196.                 $qb
  197.                     ->leftJoin('p.ProductCategories''pct')
  198.                     ->leftJoin('pct.Category''c');
  199.             }
  200.             $qb
  201.                 ->addOrderBy('p.id''DESC');
  202.         }
  203.         return $this->queries->customize(QueryKey::PRODUCT_SEARCH$qb$searchData);
  204.     }
  205.     /**
  206.      * get query builder.
  207.      *
  208.      * @param array{
  209.      *         id?:string|int|null,
  210.      *         category_id?:Category,
  211.      *         status?:ProductStatus[],
  212.      *         link_status?:ProductStatus[],
  213.      *         stock_status?:int,
  214.      *         stock?:ProductStock::IN_STOCK|ProductStock::OUT_OF_STOCK,
  215.      *         tag_id?:Tag,
  216.      *         create_datetime_start?:\DateTime,
  217.      *         create_datetime_end?:\DateTime,
  218.      *         create_date_start?:\DateTime,
  219.      *         create_date_end?:\DateTime,
  220.      *         update_datetime_start?:\DateTime,
  221.      *         update_datetime_end?:\DateTime,
  222.      *         update_date_start?:\DateTime,
  223.      *         update_date_end?:\DateTime,
  224.      *         sortkey?:string,
  225.      *         sorttype?:string
  226.      *     } $searchData
  227.      *
  228.      * @return \Doctrine\ORM\QueryBuilder
  229.      */
  230.     public function getQueryBuilderBySearchDataForAdmin($searchData)
  231.     {
  232.         $qb $this->createQueryBuilder('p')
  233.             ->addSelect('pc''pi''tr''ps')
  234.             ->innerJoin('p.ProductClasses''pc')
  235.             ->leftJoin('p.ProductImage''pi')
  236.             ->leftJoin('pc.TaxRule''tr')
  237.             ->leftJoin('pc.ProductStock''ps')
  238.             ->andWhere('pc.visible = :visible')
  239.             ->setParameter('visible'true);
  240.         // id
  241.         if (isset($searchData['id']) && StringUtil::isNotBlank($searchData['id'])) {
  242.             $id preg_match('/^\d{0,10}$/'$searchData['id']) ? $searchData['id'] : null;
  243.             if ($id && $id '2147483647' && $this->isPostgreSQL()) {
  244.                 $id null;
  245.             }
  246.             $qb
  247.                 ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid')
  248.                 ->setParameter('id'$id)
  249.                 ->setParameter('likeid''%'.str_replace(['%''_'], ['\\%''\\_'], $searchData['id']).'%');
  250.         }
  251.         // code
  252.         /*
  253.         if (!empty($searchData['code']) && $searchData['code']) {
  254.             $qb
  255.                 ->innerJoin('p.ProductClasses', 'pc')
  256.                 ->andWhere('pc.code LIKE :code')
  257.                 ->setParameter('code', '%' . $searchData['code'] . '%');
  258.         }
  259.         // name
  260.         if (!empty($searchData['name']) && $searchData['name']) {
  261.             $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
  262.             foreach ($keywords as $keyword) {
  263.                 $qb
  264.                     ->andWhere('p.name LIKE :name')
  265.                     ->setParameter('name', '%' . $keyword . '%');
  266.             }
  267.         }
  268.        */
  269.         // category
  270.         if (!empty($searchData['category_id']) && $searchData['category_id']) {
  271.             $Categories $searchData['category_id']->getSelfAndDescendants();
  272.             if ($Categories) {
  273.                 $qb
  274.                     ->innerJoin('p.ProductCategories''pct')
  275.                     ->innerJoin('pct.Category''c')
  276.                     ->andWhere($qb->expr()->in('pct.Category'':Categories'))
  277.                     ->setParameter('Categories'$Categories);
  278.             }
  279.         }
  280.         // status
  281.         if (!empty($searchData['status']) && $searchData['status']) {
  282.             $qb
  283.                 ->andWhere($qb->expr()->in('p.Status'':Status'))
  284.                 ->setParameter('Status'$searchData['status']);
  285.         }
  286.         // link_status
  287.         if (isset($searchData['link_status']) && !empty($searchData['link_status'])) {
  288.             $qb
  289.                 ->andWhere($qb->expr()->in('p.Status'':Status'))
  290.                 ->setParameter('Status'$searchData['link_status']);
  291.         }
  292.         // stock status
  293.         if (isset($searchData['stock_status'])) {
  294.             $qb
  295.                 ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
  296.                 ->setParameter('StockUnlimited'$searchData['stock_status']);
  297.         }
  298.         // stock status
  299.         if (isset($searchData['stock']) && !empty($searchData['stock'])) {
  300.             switch ($searchData['stock']) {
  301.                 case [ProductStock::IN_STOCK]:
  302.                     $qb->andWhere('pc.stock_unlimited = true OR pc.stock > 0');
  303.                     break;
  304.                 case [ProductStock::OUT_OF_STOCK]:
  305.                     $qb->andWhere('pc.stock_unlimited = false AND pc.stock <= 0');
  306.                     break;
  307.                 default:
  308.                     // 共に選択された場合は全権該当するので検索条件に含めない
  309.             }
  310.         }
  311.         // tag
  312.         if (!empty($searchData['tag_id']) && $searchData['tag_id']) {
  313.             $qb
  314.                 ->innerJoin('p.ProductTag''pt')
  315.                 ->andWhere('pt.Tag = :tag_id')
  316.                 ->setParameter('tag_id'$searchData['tag_id']);
  317.         }
  318.         // crate_date
  319.         if (!empty($searchData['create_datetime_start']) && $searchData['create_datetime_start']) {
  320.             $date $searchData['create_datetime_start'];
  321.             $qb
  322.                 ->andWhere('p.create_date >= :create_date_start')
  323.                 ->setParameter('create_date_start'$date);
  324.         } elseif (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
  325.             $date $searchData['create_date_start'];
  326.             $qb
  327.                 ->andWhere('p.create_date >= :create_date_start')
  328.                 ->setParameter('create_date_start'$date);
  329.         }
  330.         if (!empty($searchData['create_datetime_end']) && $searchData['create_datetime_end']) {
  331.             $date $searchData['create_datetime_end'];
  332.             $qb
  333.                 ->andWhere('p.create_date < :create_date_end')
  334.                 ->setParameter('create_date_end'$date);
  335.         } elseif (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
  336.             $date = clone $searchData['create_date_end'];
  337.             $date $date
  338.                 ->modify('+1 days');
  339.             $qb
  340.                 ->andWhere('p.create_date < :create_date_end')
  341.                 ->setParameter('create_date_end'$date);
  342.         }
  343.         // update_date
  344.         if (!empty($searchData['update_datetime_start']) && $searchData['update_datetime_start']) {
  345.             $date $searchData['update_datetime_start'];
  346.             $qb
  347.                 ->andWhere('p.update_date >= :update_date_start')
  348.                 ->setParameter('update_date_start'$date);
  349.         } elseif (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
  350.             $date $searchData['update_date_start'];
  351.             $qb
  352.                 ->andWhere('p.update_date >= :update_date_start')
  353.                 ->setParameter('update_date_start'$date);
  354.         }
  355.         if (!empty($searchData['update_datetime_end']) && $searchData['update_datetime_end']) {
  356.             $date $searchData['update_datetime_end'];
  357.             $qb
  358.                 ->andWhere('p.update_date < :update_date_end')
  359.                 ->setParameter('update_date_end'$date);
  360.         } elseif (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
  361.             $date = clone $searchData['update_date_end'];
  362.             $date $date
  363.                 ->modify('+1 days');
  364.             $qb
  365.                 ->andWhere('p.update_date < :update_date_end')
  366.                 ->setParameter('update_date_end'$date);
  367.         }
  368.         // Order By
  369.         if (isset($searchData['sortkey']) && !empty($searchData['sortkey'])) {
  370.             $sortOrder = (isset($searchData['sorttype']) && $searchData['sorttype'] == 'a') ? 'ASC' 'DESC';
  371.             $qb->orderBy(self::COLUMNS[$searchData['sortkey']], $sortOrder);
  372.             $qb->addOrderBy('p.update_date''DESC');
  373.             $qb->addOrderBy('p.id''DESC');
  374.         } else {
  375.             $qb->orderBy('p.update_date''DESC');
  376.             $qb->addOrderBy('p.id''DESC');
  377.         }
  378.         return $this->queries->customize(QueryKey::PRODUCT_SEARCH_ADMIN$qb$searchData);
  379.     }
  380. }