app/Plugin/CMBlogPro42/Repository/BlogRepository.php line 238

Open in your IDE?
  1. <?php
  2. namespace Plugin\CMBlogPro42\Repository;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Eccube\Doctrine\Query\Queries;
  5. use Eccube\Util\StringUtil;
  6. use Eccube\Repository\AbstractRepository;
  7. use Plugin\CMBlogPro42\Entity\Blog;
  8. use Plugin\CMBlogPro42\Repository\ConfigRepository;
  9. use Doctrine\Persistence\ManagerRegistry as RegistryInterface;
  10. /**
  11.  * BlogRepository
  12.  */
  13. class BlogRepository extends AbstractRepository
  14. {
  15.     /**
  16.      * @var ConfigRepository
  17.      */
  18.     protected $configRepository;
  19.     /**
  20.      * ConfigRepository constructor.
  21.      *
  22.      * @param RegistryInterface $registry
  23.      */
  24.     public function __construct(
  25.         RegistryInterface $registry,
  26.         Queries $queries,
  27.         ConfigRepository $configRepository)
  28.     {
  29.         parent::__construct($registryBlog::class);
  30.         $this->queries $queries;
  31.         $this->configRepository $configRepository;
  32.     }
  33.     /**
  34.      * @param int $id
  35.      *
  36.      * @return null|Blog
  37.      */
  38.     public function get($id 1)
  39.     {
  40.         return $this->find($id);
  41.     }
  42.     /**
  43.      * @return Blog[]|ArrayCollection
  44.      */
  45.     public function getList()
  46.     {
  47.         $config $this->configRepository->get();
  48.         $currentDate = new \DateTime();
  49.         return $this
  50.             ->createQueryBuilder('o')->select('o')
  51.             ->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  52.             ->setParameter('date'$currentDate)
  53.             ->andWhere('o.Status = 1')
  54.             ->setMaxResults($config->getDisplayBlock())
  55.             ->orderBy('o.create_date''DESC')
  56.             ->getQuery()->getResult();
  57.     }
  58.     /**
  59.      * @return Blog[]|ArrayCollection
  60.      */
  61.     public function getHeadLine($ROLE_USER)
  62.     {
  63.             $config $this->configRepository->get();
  64.             $currentDate = new \DateTime();
  65.         if($ROLE_USER){
  66.             //会員限定
  67.             return $this
  68.                 ->createQueryBuilder('o')->select('o')
  69.                 ->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  70.                 ->setParameter('date'$currentDate)
  71.                 ->andWhere('o.Status = 1')
  72.                 ->setMaxResults(3)
  73.                 ->orderBy('o.release_date''DESC')
  74.                 ->getQuery()->getResult();    
  75.         }else{
  76.             //一般公開
  77.             return $this
  78.                 ->createQueryBuilder('o')->select('o')
  79.                 ->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  80.                 ->setParameter('date'$currentDate)
  81.                 ->andWhere('o.members != 1')
  82.                 ->andWhere('o.Status = 1')
  83.                 ->setMaxResults(3)
  84.                 ->orderBy('o.release_date''DESC')
  85.                 ->getQuery()->getResult();
  86.         }
  87.     }
  88.     /**
  89.      * @return Blog[]|ArrayCollection
  90.      */
  91.     public function getListNews($id,$ROLE_USER)
  92.     {
  93.         
  94.         $config $this->configRepository->get();
  95.         $currentDate = new \DateTime();
  96.         
  97.         if($ROLE_USER){
  98.             //会員限定
  99.             return $this
  100.                 ->createQueryBuilder('o')->select('o')
  101.                 ->innerJoin('o.BlogCategories''bct')
  102.                 //->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  103.                 //->setParameter('date', $currentDate)
  104.                 ->andWhere('o.Status = 1')
  105.                 ->andWhere('bct.category_id  = :id')
  106.                 ->setParameter('id'$id)
  107.                 ->setMaxResults($config->getDisplayBlock())
  108.                 ->orderBy('o.release_date''DESC')
  109.                 ->getQuery()->getResult();    
  110.         }else{
  111.             //一般公開
  112.             return $this
  113.                 ->createQueryBuilder('o')->select('o')
  114.                 ->innerJoin('o.BlogCategories''bct')
  115.                 //->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  116.                 //->setParameter('date', $currentDate)
  117.                 ->andWhere('o.members != 1')
  118.                 ->andWhere('o.Status = 1')
  119.                 ->andWhere('bct.category_id  = :id')
  120.                 ->setParameter('id'$id)
  121.                 ->setMaxResults($config->getDisplayBlock())
  122.                 ->orderBy('o.release_date''DESC')
  123.                 ->getQuery()->getResult();
  124.         }
  125.     }
  126.     /**
  127.      * @return Blog[]|ArrayCollection
  128.      */
  129.     public function getShareList($id,$ROLE_USER){
  130.         $config $this->configRepository->get();
  131.         $currentDate = new \DateTime();
  132.         if($ROLE_USER){
  133.             //会員限定
  134.             return $this
  135.                 ->createQueryBuilder('o')->select('o')
  136.                 ->innerJoin('o.BlogShares''bct')
  137.                 //->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  138.                 //->setParameter('date', $currentDate)
  139.                 ->andWhere('o.Status = 1')
  140.                 ->andWhere('bct.category_id  = :id')
  141.                 ->setParameter('id'$id)
  142.                 ->orderBy('o.release_date''DESC')
  143.                 ->getQuery()->getResult();        
  144.         }else{
  145.             //一般公開
  146.             return $this
  147.                 ->createQueryBuilder('o')->select('o')
  148.                 ->innerJoin('o.BlogShares''bct')
  149.                 //->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  150.                 //->setParameter('date', $currentDate)
  151.                 ->andWhere('o.members != 1')
  152.                 ->andWhere('o.Status = 1')
  153.                 ->andWhere('bct.category_id  = :id')
  154.                 ->setParameter('id'$id)
  155.                 ->orderBy('o.release_date''DESC')
  156.                 ->getQuery()->getResult();
  157.         }
  158.     }
  159.     /**
  160.      * @return Blog[]|ArrayCollection
  161.      */
  162.     public function getListTop($id,$ROLE_USER)
  163.     {
  164.         $config $this->configRepository->get();
  165.         $currentDate = new \DateTime();
  166.         if($ROLE_USER){
  167.             //会員限定
  168.             return $this
  169.                 ->createQueryBuilder('o')->select('o')
  170.                 ->innerJoin('o.BlogCategories''bct')
  171.                 //->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  172.                 //->setParameter('date', $currentDate)
  173.                 ->andWhere('o.Status = 1')
  174.                 ->andWhere('bct.category_id  = :id')
  175.                 ->setParameter('id'$id)
  176.                 ->setMaxResults(3)
  177.                 ->orderBy('o.release_date''DESC')
  178.                 ->getQuery()->getResult();
  179.         }else{
  180.             //一般公開
  181.             return $this
  182.                 ->createQueryBuilder('o')->select('o')
  183.                 ->innerJoin('o.BlogCategories''bct')
  184.                 //->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  185.                 //->setParameter('date', $currentDate)
  186.                 ->andWhere('o.members != 1')
  187.                 ->andWhere('o.Status = 1')
  188.                 ->andWhere('bct.category_id  = :id')
  189.                 ->setParameter('id'$id)
  190.                 ->setMaxResults(3)
  191.                 ->orderBy('o.release_date''DESC')
  192.                 ->getQuery()->getResult();
  193.         }
  194.     }
  195.     /**
  196.      * get query builder.
  197.      *
  198.      * @param  array $searchData
  199.      *
  200.      * @return \Doctrine\ORM\QueryBuilder
  201.      */
  202.     public function getQueryBuilderBySearchData($searchData)
  203.     {
  204.         $currentDate = new \DateTime();
  205.         $qb $this->createQueryBuilder('o')->select()
  206.         ->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  207.         ->setParameter('date'$currentDate)
  208.         ->andWhere('o.Status = 1');
  209.         //teg
  210.         if (isset($searchData['tag']) && StringUtil::isNotBlank($searchData['tag'])) {
  211.             $qb
  212.                 ->andWhere('o.tag = :tag')
  213.                 ->setParameter(':tag'$searchData['tag']);
  214.         }
  215.         // カテゴリ
  216.         if (isset($searchData['categories']) && StringUtil::isNotBlank($searchData['categories'])) {
  217.             $qb
  218.                 ->innerJoin('o.BlogCategories''bct')
  219.                 ->innerJoin('bct.Category''c')
  220.                 ->andWhere($qb->expr()->in('bct.Category'':categories'))
  221.                 ->setParameter('categories'$searchData['categories']);
  222.         }
  223.         // Order By
  224.         //$qb->orderBy('o.create_date', 'DESC');
  225.         $qb->orderBy('o.release_date''DESC');
  226.         return $this->queries->customize('Blog.getQueryBuilderBySearchData'$qb$searchData);
  227.     }
  228.     public function getQueryBuilderBySearchDataAdmin($searchData)
  229.     {
  230.         $qb $this->createQueryBuilder('o')->select('o');
  231.         // id/タイトル
  232.         if (isset($searchData['id']) && StringUtil::isNotBlank($searchData['id'])) {
  233.             $id preg_match('/^\d{0,10}$/'$searchData['id']) ? $searchData['id'] : null;
  234.             $qb
  235.                 ->andWhere('o.id = :id OR o.title LIKE :likeid')
  236.                 ->setParameter('id'$id)
  237.                 ->setParameter('likeid''%'.str_replace(['%''_'], ['\\%''\\_'], $searchData['id']).'%');
  238.         }
  239.         // ステータス
  240.         if (!empty($searchData['status']) && $searchData['status']) {
  241.             $qb
  242.                 ->andWhere($qb->expr()->in('o.Status'':Status'))
  243.                 ->setParameter('Status'$searchData['status']);
  244.         }        
  245.         // リリース日付
  246.         if (!empty($searchData['releaseStart']) && $searchData['releaseStart']) {
  247.             $date $searchData['releaseStart'];
  248.             $qb
  249.                 ->andWhere('o.release_date >= :releaseStart')
  250.                 ->setParameter('releaseStart'$date);
  251.         }
  252.         if (!empty($searchData['releaseEnd']) && $searchData['releaseEnd']) {
  253.             $date = clone $searchData['releaseEnd'];
  254.             $date $date
  255.                 ->modify('+1 days');
  256.             $qb
  257.                 ->andWhere('o.release_date < :releaseEnd')
  258.                 ->setParameter('releaseEnd'$date);
  259.         }
  260.         // create_date
  261.         if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
  262.             $date $searchData['create_date_start'];
  263.             $qb
  264.                 ->andWhere('o.create_date >= :create_date_start')
  265.                 ->setParameter('create_date_start'$date);
  266.         }
  267.         if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
  268.             $date = clone $searchData['create_date_end'];
  269.             $date $date
  270.                 ->modify('+1 days');
  271.             $qb
  272.                 ->andWhere('o.create_date < :create_date_end')
  273.                 ->setParameter('create_date_end'$date);
  274.         }
  275.         // update_date
  276.         if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
  277.             $date $searchData['update_date_start'];
  278.             $qb
  279.                 ->andWhere('o.update_date >= :update_date_start')
  280.                 ->setParameter('update_date_start'$date);
  281.         }
  282.         if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
  283.             $date = clone $searchData['update_date_end'];
  284.             $date $date
  285.                 ->modify('+1 days');
  286.             $qb
  287.                 ->andWhere('o.update_date < :update_date_end')
  288.                 ->setParameter('update_date_end'$date);
  289.         }
  290.         // カテゴリ
  291.         if (!empty($searchData['category_id']) && $searchData['category_id']) {
  292.             // Not a proper category class so there's no such function
  293.             //$Categories = $searchData['category_id']->getSelfAndDescendants();
  294.             
  295.             $Categories = [$searchData['category_id']];
  296.             if ($Categories) {
  297.                 $qb
  298.                     ->innerJoin('o.BlogCategories''bct')
  299.                     ->innerJoin('bct.Category''c')
  300.                     ->andWhere($qb->expr()->in('bct.Category'':Categories'))
  301.                     ->setParameter('Categories'$Categories);
  302.             }
  303.         }
  304.         // シェア
  305.         if (!empty($searchData['share_id']) && $searchData['share_id']) {
  306.             // Not a proper category class so there's no such function
  307.             //$Categories = $searchData['share_id']->getSelfAndDescendants();
  308.             
  309.             $Categories = [$searchData['share_id']];
  310.             if ($Categories) {
  311.                 $qb
  312.                     ->innerJoin('o.BlogShares''bct')
  313.                     ->innerJoin('bct.Share''c')
  314.                     ->andWhere($qb->expr()->in('bct.Share'':Categories'))
  315.                     ->setParameter('Categories'$Categories);
  316.             }
  317.         }
  318.         // Order By
  319.         $qb->orderBy('o.create_date''DESC');
  320.         return $this->queries->customize('Blog.getQueryBuilderBySearchDataAdmin'$qb$searchData);
  321.     }
  322.     /**
  323.      * @return Blog[]|ArrayCollection
  324.      */
  325.     public function getReleaseDate($searchData)
  326.     {
  327.         $qb $this->createQueryBuilder('o');
  328.         // カテゴリ
  329.         if (isset($searchData['categories']) && StringUtil::isNotBlank($searchData['categories'])) {
  330.             $qb
  331.                 ->innerJoin('o.BlogCategories''bct')
  332.                 ->innerJoin('bct.Category''c')
  333.                 ->andWhere($qb->expr()->in('bct.Category'':categories'))
  334.                 ->setParameter('categories'$searchData['categories']);
  335.         }
  336.         
  337.         $qb
  338.             ->andWhere('o.release_date <= :date OR o.release_date is NULL AND o.create_date <= :date')
  339.             ->setParameter('date', new \DateTime())
  340.             ->andWhere('o.Status = 1')
  341.             ->orderBy('o.release_date''DESC')
  342.             ->select('o.release_date');
  343.         return $qb->getQuery()->getResult();
  344.     }
  345. }