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

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$limit null )
  46.     {
  47.         $config $this->configRepository->get();
  48.         $sortId '';
  49.         if (!is_null($config->getSort())) {
  50.             $sortId $config->getSort()->getId();
  51.         }
  52.         
  53.         $currentDate = new \DateTime();
  54.         $query $this
  55.                 ->createQueryBuilder('o')->select('o')
  56.                 ->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  57.                 ->setParameter('date'$currentDate)
  58.                 ->andWhere('o.Status = 1')
  59.                 ->setMaxResults($limit);
  60.         if ( $sortId == 1) {
  61.             $query->orderBy('o.release_date''DESC');
  62.         } elseif ( $sortId == ) {
  63.             $query->orderBy('o.create_date''DESC');
  64.         } elseif ( $sortId == ) {
  65.             $query->orderBy('o.update_date''DESC');
  66.         } else {
  67.             $query->orderBy('o.create_date''DESC');
  68.         }
  69.                 
  70.         return $query->getQuery()->getResult();
  71.     }
  72.     /**
  73.      * get query builder.
  74.      *
  75.      * @param  array $searchData
  76.      *
  77.      * @return \Doctrine\ORM\QueryBuilder
  78.      */
  79.     public function getQueryBuilderBySearchData($searchData)
  80.     {
  81.         $config $this->configRepository->get();
  82.         $sortId '';
  83.         if (!is_null($config->getSort())) {
  84.             $sortId $config->getSort()->getId();
  85.         }
  86.         $currentDate = new \DateTime();
  87.         $qb $this->createQueryBuilder('o')->select('o')
  88.         ->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  89.         ->setParameter('date'$currentDate)
  90.         ->andWhere('o.Status = 1');
  91.         // カテゴリ
  92.         if (isset($searchData['categories']) && StringUtil::isNotBlank($searchData['categories'])) {
  93.             $qb
  94.                 ->innerJoin('o.BlogCategories''bct')
  95.                 ->innerJoin('bct.Category''c')
  96.                 ->andWhere($qb->expr()->in('bct.Category'':categories'))
  97.                 ->setParameter('categories'$searchData['categories']);
  98.         }
  99.         // タグで検索
  100.         if (isset($searchData['tag']) && StringUtil::isNotBlank($searchData['tag'])) {
  101.             $qb
  102.                 ->andWhere('o.tag LIKE :tag')
  103.                 ->setParameter('tag''%'$searchData['tag'] .'%');
  104.         }
  105.         // タグで検索
  106.         if (isset($searchData['date']) && StringUtil::isNotBlank($searchData['date'])) {
  107.             
  108.             $currentDate = new \DateTime();
  109.             $qb
  110.                 ->andWhere('o.release_date >= :max_date AND o.release_date <= :min_date AND o.release_date <= :currentDate')
  111.                 ->setParameter('max_date'date('Y-m-01 00:00:00'strtotime($searchData['date'])))
  112.                 ->setParameter('min_date'date('Y-m-t 23:59:59'strtotime($searchData['date'])))
  113.                 ->setParameter('currentDate'$currentDate);
  114.         }
  115.         if ( $sortId == 1) {
  116.             $qb->orderBy('o.release_date''DESC');
  117.         } elseif ( $sortId == ) {
  118.             $qb->orderBy('o.create_date''DESC');
  119.         } elseif ( $sortId == ) {
  120.             $qb->orderBy('o.update_date''DESC');
  121.         } else {
  122.             $qb->orderBy('o.create_date''DESC');
  123.         }
  124.         return $this->queries->customize('Blog.getQueryBuilderBySearchData'$qb$searchData);
  125.     }
  126.     public function getQueryBuilderBySearchDataAdmin($searchData)
  127.     {
  128.         $qb $this->createQueryBuilder('o')->select('o');
  129.         // id/タイトル
  130.         if (isset($searchData['id']) && StringUtil::isNotBlank($searchData['id'])) {
  131.             $id preg_match('/^\d{0,10}$/'$searchData['id']) ? $searchData['id'] : null;
  132.             $qb
  133.                 ->andWhere('o.id = :id OR o.title LIKE :likeid')
  134.                 ->setParameter('id'$id)
  135.                 ->setParameter('likeid''%'.str_replace(['%''_'], ['\\%''\\_'], $searchData['id']).'%');
  136.         }
  137.         // ステータス
  138.         if (!empty($searchData['status']) && $searchData['status']) {
  139.             $qb
  140.                 ->andWhere($qb->expr()->in('o.Status'':Status'))
  141.                 ->setParameter('Status'$searchData['status']);
  142.         }        
  143.         // リリース日付
  144.         if (!empty($searchData['releaseStart']) && $searchData['releaseStart']) {
  145.             $date $searchData['releaseStart'];
  146.             $qb
  147.                 ->andWhere('o.release_date >= :releaseStart')
  148.                 ->setParameter('releaseStart'$date);
  149.         }
  150.         if (!empty($searchData['releaseEnd']) && $searchData['releaseEnd']) {
  151.             $date = clone $searchData['releaseEnd'];
  152.             $date $date
  153.                 ->modify('+1 days');
  154.             $qb
  155.                 ->andWhere('o.release_date < :releaseEnd')
  156.                 ->setParameter('releaseEnd'$date);
  157.         }
  158.         // create_date
  159.         if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
  160.             $date $searchData['create_date_start'];
  161.             $qb
  162.                 ->andWhere('o.create_date >= :create_date_start')
  163.                 ->setParameter('create_date_start'$date);
  164.         }
  165.         if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
  166.             $date = clone $searchData['create_date_end'];
  167.             $date $date
  168.                 ->modify('+1 days');
  169.             $qb
  170.                 ->andWhere('o.create_date < :create_date_end')
  171.                 ->setParameter('create_date_end'$date);
  172.         }
  173.         // update_date
  174.         if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
  175.             $date $searchData['update_date_start'];
  176.             $qb
  177.                 ->andWhere('o.update_date >= :update_date_start')
  178.                 ->setParameter('update_date_start'$date);
  179.         }
  180.         if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
  181.             $date = clone $searchData['update_date_end'];
  182.             $date $date
  183.                 ->modify('+1 days');
  184.             $qb
  185.                 ->andWhere('o.update_date < :update_date_end')
  186.                 ->setParameter('update_date_end'$date);
  187.         }
  188.         // カテゴリ
  189.         if (!empty($searchData['category_id']) && $searchData['category_id']) {
  190.             // Not a proper category class so there's no such function
  191.             //$Categories = $searchData['category_id']->getSelfAndDescendants();
  192.             
  193.             $Categories = [$searchData['category_id']];
  194.             if ($Categories) {
  195.                 $qb
  196.                     ->innerJoin('o.BlogCategories''bct')
  197.                     ->innerJoin('bct.Category''c')
  198.                     ->andWhere($qb->expr()->in('bct.Category'':Categories'))
  199.                     ->setParameter('Categories'$Categories);
  200.             }
  201.         }
  202.         // Order By
  203.         $qb->orderBy('o.create_date''DESC');
  204.         return $this->queries->customize('Blog.getQueryBuilderBySearchDataAdmin'$qb$searchData);
  205.     }
  206.     /**
  207.      * @return Blog[]|ArrayCollection
  208.      */
  209.     public function getReleaseDate($searchData)
  210.     {
  211.         $qb $this->createQueryBuilder('o');
  212.         // カテゴリ
  213.         if (isset($searchData['categories']) && StringUtil::isNotBlank($searchData['categories'])) {
  214.             $qb
  215.                 ->innerJoin('o.BlogCategories''bct')
  216.                 ->innerJoin('bct.Category''c')
  217.                 ->andWhere($qb->expr()->in('bct.Category'':categories'))
  218.                 ->setParameter('categories'$searchData['categories']);
  219.         }
  220.         
  221.         $qb
  222.             ->andWhere('o.release_date <= :date OR o.release_date is NULL AND o.create_date <= :date')
  223.             ->setParameter('date', new \DateTime())
  224.             ->andWhere('o.Status = 1')
  225.             ->orderBy('o.release_date''DESC')
  226.             ->select('o.release_date');
  227.         return $qb->getQuery()->getResult();
  228.     }
  229.     /**
  230.      * @return Blog
  231.      */
  232.     public function getPrev$currentId null )
  233.     {        
  234.         $currentDate = new \DateTime();
  235.         $query $this
  236.                 ->createQueryBuilder('o')->select('o')
  237.                 ->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  238.                 ->setParameter('date'$currentDate)
  239.                 ->andWhere('o.Status = 1')
  240.                 ->andWhere('o.id < :current_id')
  241.                 ->setParameter('current_id'$currentId)
  242.                 ->setMaxResults(1);
  243.                 
  244.         return $query->getQuery()->getResult();
  245.     }
  246.     /**
  247.      * @return Blog
  248.      */
  249.     public function getNext$currentId null )
  250.     {        
  251.         $currentDate = new \DateTime();
  252.         $query $this
  253.                 ->createQueryBuilder('o')->select('o')
  254.                 ->andWhere('o.release_date < :date OR o.release_date is NULL AND o.create_date < :date')
  255.                 ->setParameter('date'$currentDate)
  256.                 ->andWhere('o.Status = 1')
  257.                 ->andWhere('o.id > :current_id')
  258.                 ->setParameter('current_id'$currentId)
  259.                 ->setMaxResults(1);
  260.                 
  261.         return $query->getQuery()->getResult();
  262.     }
  263. }