src/Repository/PostPublishesRepository.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Categories;
  4. use App\Entity\GroupBoxItems;
  5. use App\Entity\PostDatas;
  6. use App\Entity\PostPublishes;
  7. use App\Entity\PostsCates;
  8. use App\Entity\PostsTags;
  9. use App\Entity\PostViews;
  10. use App\Entity\Tags;
  11. use App\Utils\Constants;
  12. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  13. use Symfony\Bridge\Doctrine\RegistryInterface;
  14. /**
  15.  * @method PostPublishes|null find($id, $lockMode = null, $lockVersion = null)
  16.  * @method PostPublishes|null findOneBy(array $criteria, array $orderBy = null)
  17.  * @method PostPublishes[]    findAll()
  18.  * @method PostPublishes[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  19.  */
  20. class PostPublishesRepository extends ServiceEntityRepository
  21. {
  22.     public function __construct(RegistryInterface $registry)
  23.     {
  24.         parent::__construct($registryPostPublishes::class);
  25.     }
  26.     
  27.     /**
  28.      * @param $groupBoxKey
  29.      * @param $limit
  30.      * @return mixed
  31.      */
  32.     public function getTopPostsInGroupBox($groupBoxKey$limit)
  33.     {
  34.         $posts $this->createQueryBuilder('p')
  35.             ->select('p.postId, p.title, p.slug, p.sapo, p.cateId, p.publishedDate, p.publishedTimestamp, p.avatar, p.focusAvatar, p.authorName, c.name cateName, c.slug cateSlug')
  36.             ->innerJoin(GroupBoxItems::class, 'gi''WITH''gi.dataId = p.postId')
  37.             ->innerJoin(Categories::class, 'c''WITH''c.cateId = p.cateId')
  38.             ->where('gi.key = :key AND p.publishedTimestamp <= :currentDate')
  39.             ->setParameters([':key' => $groupBoxKey':currentDate' => time()])
  40.             ->orderBy('gi.order')
  41.             ->setMaxResults($limit)
  42.             ->getQuery()
  43.             ->getResult();
  44.         
  45.         return $posts;
  46.     }
  47.     
  48.     /**
  49.      * Get Rss article in cate paging
  50.      * author: ThanhDT
  51.      * date:   2020-01-10 02:45 PM
  52.      * @param $cateId
  53.      * @param $pageSize
  54.      * @return array
  55.      */
  56.     public function getRssArticleInCatePaging($cateId$pageSize)
  57.     {
  58.         $data $this->createQueryBuilder('p')
  59.             ->select('p.postId, p.cateId, p.title, p.slug, p.sapo, p.publishedDate, p.publishedTimestamp, p.avatar, p.publishedTimestamp, p.authorName, pd.contentMobile AS content')
  60.             ->innerJoin(PostDatas::class, 'pd''WITH''p.postId = pd.postId')
  61.             ->innerJoin(PostsCates::class, 'c''WITH''c.postId = p.postId')
  62.             ->where('c.cateId IN (:cateId)')
  63.             ->setParameters([':cateId' => $cateId])
  64.             ->orderBy('p.publishedDate''DESC')
  65.             ->setMaxResults($pageSize)
  66.             ->getQuery()->getArrayResult();
  67.         
  68.         return $data;
  69.     }
  70.     
  71.     /**
  72.      * Get lastest Rss articles
  73.      * author: ThanhDT
  74.      * date:   2020-01-10 03:09 PM
  75.      * @param int $pageSize
  76.      * @return array
  77.      * @throws \Exception
  78.      */
  79.     public function getRssArticleLatest($pageSize 9)
  80.     {
  81.         $data $this->createQueryBuilder('p')
  82.             ->select('p.postId, p.cateId, p.title, p.slug, p.sapo, p.publishedDate, p.avatar, p.publishedTimestamp, p.authorName, pd.contentMobile AS content')
  83.             ->innerJoin(PostDatas::class, 'pd''WITH''p.postId = pd.postId')
  84.             ->where('p.publishedDate <= :currentDate')
  85.             ->setParameters([':currentDate' => new \DateTime()])
  86.             ->orderBy('p.publishedDate''DESC')
  87.             ->setMaxResults($pageSize)
  88.             ->getQuery()->getArrayResult();
  89.         
  90.         return $data;
  91.     }
  92.     
  93.     /**
  94.      * Get post in today
  95.      * author: ThanhDT
  96.      * date:   2020-01-09 12:31 AM
  97.      * @param int $cateId
  98.      * @return mixed
  99.      * @throws \Exception
  100.      */
  101.     public function getPostByCurrentTime($cateId '')
  102.     {
  103.         $qp $this->createQueryBuilder('a')
  104.             ->select('a.postId')
  105.             ->where('a.publishedDate BETWEEN :currentMonth AND :currentDate')
  106.             ->setParameters([':currentMonth' => date('Y-m'), ':currentDate' => new \DateTime()])
  107.             ->setMaxResults(1);
  108.             if ($cateId) {
  109.                 $qp->andWhere('a.cateId = :cateId ')
  110.                     ->setParameter(':cateId'$cateId);
  111.             }
  112.         return $qp->getQuery()->getResult();
  113.     }//end getPostByCurrentTime()
  114.     /**
  115.      * @param $start
  116.      * @param $end
  117.      * @param $cateId
  118.      * @return array
  119.      */
  120.     public function getPostsBetweenDates($start$end$cateId='')
  121.     {
  122.         $queryBuilder $this->createQueryBuilder('p');
  123.         $selectFields = [
  124.             'p.postId',
  125.             'p.cateId',
  126.             'p.avatar',
  127.             'p.slug',
  128.             'p.sapo',
  129.             'p.title',
  130.             'c.slug as slugCate',
  131.             'p.publishedDate'
  132.         ];
  133.         $condition $queryBuilder
  134.             ->select($selectFields)
  135.             ->innerJoin(Categories::class, 'c''WITH''c.cateId = p.cateId')
  136.             ->orderBy('p.publishedTimestamp''DESC')
  137.             ->where('p.publishedTimestamp BETWEEN :startDate AND :endDate')
  138.             ->setParameter('startDate'$start)
  139.             //->andWhere('p.publishedDate <= :endDate')
  140.             ->setParameter('endDate'$end);
  141.         if ($cateId) {
  142.             $condition->andWhere('p.cateId = :cateId ')
  143.                 ->setParameter(':cateId'$cateId);
  144.         }
  145.         
  146.         $postsQuery $condition->getQuery();
  147.         $posts $postsQuery->getArrayResult();
  148.         
  149.         return $posts;
  150.     }
  151.     
  152.     /**
  153.      * @param $cateId
  154.      * @param int $limit
  155.      * @param array $exceptIds
  156.      * @return array
  157.      */
  158.     public function getLatestPostsByCate($arrayCateId = [], $limit 12$exceptIds = [])
  159.     {
  160.         $query $this->createQueryBuilder('p')
  161.             ->select('p.postId, p.cateId, p.title, p.slug, p.sapo, p.publishedDate, p.publishedTimestamp, p.avatar, p.authorName')
  162.             ->where('p.publishedTimestamp <= :now ')
  163.             ->setParameter('now'time());
  164.         
  165.         if ($arrayCateId) {
  166.             $query->innerJoin(PostsCates::class, 'c''WITH''c.postId = p.postId')
  167.                 ->andWhere('c.cateId IN (:cateId) ')
  168.                 ->setParameter(':cateId'$arrayCateId);
  169.         }
  170.         
  171.         if (!empty($exceptIds)) {
  172.             $query->andWhere('p.postId not in (:exceptIds)')
  173.                 ->setParameter(':exceptIds'$exceptIds);
  174.         }
  175.         
  176.         $data $query->orderBy('p.publishedDate''DESC')
  177.             ->setMaxResults($limit)
  178.             ->getQuery()
  179.             ->getArrayResult();
  180.         
  181.         return $data;
  182.     }
  183.     
  184.     /**
  185.      * @param int $limit
  186.      * @param int $fromModify
  187.      * @param array $exceptIds
  188.      * @param array $arrayCateId
  189.      * @return array
  190.      */
  191.     public function getMostViewPosts($limit 5$fromModify 172800$exceptIds = [], $arrayCateId = [])
  192.     {
  193.         $now time();
  194.         $from = ($now $fromModify);
  195.         $queryBuilder $this->createQueryBuilder('p');
  196.         $selectFields = [
  197.             'SUM(v.viewCount) AS totalView',
  198.             'p.postId',
  199.             'p.avatar',
  200.             'p.slug',
  201.             'p.sapo',
  202.             'p.title',
  203.             'p.cateId',
  204.             'p.viewCount',
  205.             'p.publishedDate',
  206.             'p.publishedTimestamp'
  207.         ];
  208.         
  209.         $condition $queryBuilder
  210.             ->select($selectFields)
  211.             ->innerJoin(PostViews::class, 'v''WITH''p.postId = v.postId')
  212.             ->where('p.publishedTimestamp <= :now and v.timestamp >= :from and v.timestamp <= :now')
  213.             ->setParameter('now'$now)
  214.             ->setParameter('from'$from)
  215.             ->setParameter('now'$now)
  216.             ->groupBy('v.postId')
  217.             ->addOrderBy('totalView''DESC')
  218.             ->addOrderBy('p.publishedDate''DESC')
  219.             ->setMaxResults($limit);
  220.         
  221.         if ($arrayCateId) { // Anhpt4 : add list cate id
  222.             $condition->andWhere('p.cateId IN (:cateId)')->setParameter(':cateId'$arrayCateId);
  223.         }
  224.         
  225.         if ($exceptIds) {
  226.             $condition->andWhere(
  227.                 $queryBuilder->expr()->notIn('p.postId'$exceptIds)
  228.             );
  229.         }
  230.         
  231.         return $condition
  232.             ->getQuery()
  233.             ->getArrayResult();
  234.     }
  235.     
  236.     /**
  237.      * @param $tagId
  238.      * @param $limit
  239.      * @param int $timeStamp
  240.      * @param int $lastId
  241.      * @param array $ignoreIds
  242.      * @return array
  243.      */
  244.     public function getPostsByTagId($tagId$limit$timeStamp 0$lastId 0$ignoreIds = [])
  245.     {
  246.         if ($timeStamp) {
  247.             $date = new \DateTime();
  248.             $date->setTimestamp($timeStamp);
  249.         } else {
  250.             $date date('Y-m-d H:i:s');
  251.         }
  252.         
  253.         $queryBuilder $this->createQueryBuilder('p');
  254.         $condition $queryBuilder
  255.             ->select('p.postId, p.title, p.slug, p.sapo, p.cateId, p.publishedDate, p.publishedTimestamp, p.avatar, p.authorName')
  256.             ->leftJoin(PostsTags::class, 'p_tag''WITH''p.postId = p_tag.postId')
  257.             ->where('p_tag.tagId = :tagId AND p.publishedDate <= :currentDate')
  258.             ->setParameters(
  259.                 [
  260.                     ':tagId' => $tagId,
  261.                     ':currentDate' => $date,
  262.                 ]
  263.             )
  264.             ->orderBy('p.publishedDate''DESC')
  265.             ->addOrderBy('p.postId''DESC');
  266.         if ($lastId) {
  267.             $condition
  268.                 ->andWhere('p.postId <> :lastId')
  269.                 ->setParameter('lastId'$lastId);
  270.         }
  271.         if ($ignoreIds) {
  272.             $condition->andWhere(
  273.                 $condition->expr()->notIn('p.postId'$ignoreIds)
  274.             );
  275.         }
  276.         
  277.         return $condition
  278.             ->setMaxResults($limit)
  279.             ->getQuery()
  280.             ->getArrayResult();
  281.     }//end getPostsByTagId()
  282.     
  283.     /**
  284.      * @param int $cateId
  285.      * @param array $excludePostIds
  286.      * @param int $limit
  287.      * @return array
  288.      */
  289.     public function getPostNewInCate($arrayCateId = [], $excludePostIds = [], $limit Constants::LIMIT_FEATURED$focusStatus 0)
  290.     {
  291.         $query $this->createQueryBuilder('p')
  292.             ->select('p.postId, p.title, p.slug, p.cateId, p.publishedDate, p.publishedTimestamp, p.focusStatus, p.avatar, p.sapo,p.authorName, c.name cateName, c.slug cateSlug')
  293.             ->innerJoin(Categories::class, 'c''WITH''p.cateId = c.cateId');
  294.         
  295.         if ($excludePostIds) {
  296.             $query->where('p.postId NOT IN (:postIds)')->setParameter(':postIds'$excludePostIds);
  297.         }
  298.         
  299.         if ($arrayCateId) {
  300.             $query->andWhere('p.cateId IN (:cateId)')->setParameter(':cateId'$arrayCateId);
  301.         }
  302.         
  303.         if (isset($focusStatus)) {
  304.             $query->andWhere('p.focusStatus = :focusStatus')->setParameter(':focusStatus'$focusStatus);
  305.         }
  306.         
  307.         $posts $query->andWhere('p.publishedTimestamp <= :currentDate ')
  308.             ->setParameter(':currentDate'time())
  309.             ->addOrderBy('p.publishedTimestamp''DESC')
  310.             ->setMaxResults($limit)
  311.             ->getQuery()
  312.             ->getArrayResult();
  313.         
  314.         return $posts;
  315.     }//end getPostNewCate()
  316.     
  317.     /**
  318.      * Get paging posts in category
  319.      * @param $cateIds
  320.      * @param $limit
  321.      * @param $lastPublishedStamp
  322.      * @param null $excludePostIds
  323.      * @return array
  324.      */
  325.     public function getCatePosts($cateIds$limit$lastPublishedStamp$excludePostIds null)
  326.     {
  327.         $query $this->createQueryBuilder('p')
  328.             ->select('p.postId, p.title, p.slug, p.sapo,p.reviewPoints, p.cateId, p.publishedDate, p.publishedTimestamp, p.avatar,p.authorName, c.name cateName, c.slug cateSlug')
  329.             ->innerJoin(PostsCates::class, 'pc''WITH''pc.postId = p.postId')
  330.             ->innerJoin(Categories::class, 'c''WITH''c.cateId = p.cateId');
  331.         $parameters = [];
  332.         if ($excludePostIds) {
  333.             $query->where('p.postId NOT IN (:postIds)');//->setParameter(':postIds', $excludePostIds);
  334.             $parameters[':postIds'] = $excludePostIds;
  335.         }
  336.         $query->andWhere('pc.cateId IN (:cateIds) AND p.publishedTimestamp <= :currentDate');
  337.         $parameters[':currentDate'] = $lastPublishedStamp;
  338.         $parameters[':cateIds'] = $cateIds;
  339.         $posts $query->setParameters($parameters)
  340.             ->orderBy('p.publishedDate''DESC')
  341.             ->groupBy('p.postId')
  342.             ->setMaxResults($limit)
  343.             ->getQuery()
  344.             ->getArrayResult();
  345.         
  346.         return $posts;
  347.     }
  348.     
  349.     /**
  350.      * get detail post
  351.      * @param int $postId
  352.      * @param string $slug
  353.      * @return array
  354.      * author : ThangPD
  355.      * date : 2018-25-21 14:25 PM
  356.      */
  357.     public function getDetailPost($postId 0)
  358.     {
  359.         $query $this->createQueryBuilder('p')
  360.             ->select('p.postId, p.title, p.slug, p.avatar, p.sapo, p.cateId, p.authorId, p.authorName, p.publishedDate,
  361.             p.reviewId, p.otherTags, p.reviewPoints, p.seoMetadesc , p.seoTitle, p.seoFocusKeyword,
  362.             pd.content, pd.contentAmp, pd.cates, pd.tags, pd.relatedPosts,
  363.             pd.sourceTags, c.name cateName, c.slug cateSlug')
  364.             ->leftJoin(PostDatas::class, 'pd''WITH''p.postId = pd.postId')
  365.             ->leftJoin(Categories::class, 'c''WITH''p.cateId = c.cateId')
  366.             ->where('p.postId = ' $postId)
  367.             ->andWhere('p.publishedDate <= :currentDate')
  368.             ->setParameter(':currentDate', new \DateTime());
  369.         $result $query->getQuery()->getOneOrNullResult();
  370.         
  371.         return $result;
  372.     }
  373.     
  374.     /**
  375.      * modifier: AnhPT4
  376.      * modifier date: 2019-47-2 16:47 PM
  377.      * get related post by id
  378.      * @param string $ids
  379.      * @return array
  380.      * author : ThangPD
  381.      * date : 2018-20-22 10:20 AM
  382.      */
  383.     public function getRelatedPosts($ids ""$limit Constants::LIMIT_RELATED_POST)
  384.     {
  385.         $ids trim($ids);
  386.         if (empty($ids))
  387.             return [];
  388.         
  389.         $ids explode(','$ids);
  390.         $result $this->createQueryBuilder('p')
  391.             ->select('p.postId, p.cateId, p.title, p.slug, p.avatar, p.publishedDate, p.sapo, c.name cateName, c.slug cateSlug')
  392.             ->leftJoin(Categories::class, 'c''WITH''p.cateId = c.cateId')
  393.             ->where('p.postId IN (:ids) AND p.publishedDate <= :currentDate')
  394.             ->setParameters([':ids' => $ids':currentDate' => new \DateTime()])
  395.             ->setMaxResults(Constants::LIMIT_RELATED)
  396.             ->orderBy('p.publishedDate''DESC')
  397.             ->setMaxResults($limit)
  398.             ->getQuery()
  399.             ->getArrayResult();
  400.         
  401.         return $result;
  402.     }
  403.     
  404.     /**
  405.      * get Post In Tag
  406.      * @param array $tagsId
  407.      * @param int $limit
  408.      * @return array
  409.      */
  410.     public function getPostInTag($postId$tagsId = [], $limit Constants::LIMIT_RELATED_POST)
  411.     {
  412.         if (empty($tagsId))
  413.             return null;
  414.         
  415.         $result $this->createQueryBuilder('p')
  416.             ->select('p.postId, p.title,p.slug, c.slug cateSlug')
  417.             ->innerJoin(Categories::class, 'c''WITH''p.cateId = c.cateId')
  418.             ->innerJoin(PostsTags::class, 'pc''WITH''p.postId = pc.postId')
  419.             ->innerJoin(Tags::class, 't''WITH''pc.tagId = t.tagId')
  420.             ->where('p.postId NOT IN(:postId) AND t.tagId IN (:tagId) AND p.publishedTimestamp <= :currentDate')
  421.             ->setParameters([':postId' => $postId':tagId' => $tagsId':currentDate' => time()])
  422.             ->setMaxResults(Constants::LIMIT_RELATED)
  423.             ->orderBy('p.publishedTimestamp''DESC')
  424.             ->setMaxResults($limit)
  425.             ->groupBy('p.postId')
  426.             ->getQuery()
  427.             ->getArrayResult();
  428.         
  429.         return $result;
  430.     }
  431.     
  432.     /**
  433.      * Get paging posts in category
  434.      * @param $cateIds
  435.      * @param $limit
  436.      * @param $lastPublishedStamp
  437.      * @param null $excludePostIds
  438.      * @return array
  439.      */
  440.     public function getPostsInCates($cateIds$limit$lastPublishedStamp$fromModify$excludePostIds null)
  441.     {
  442.         $parameters = [];
  443.         $time $lastPublishedStamp $fromModify;
  444.         $parameters[':currentDate'] = $lastPublishedStamp;
  445.         $parameters[':cateIds'] = $cateIds;
  446.         $parameters[':fromDate'] = $time;
  447.         
  448.         $query $this->createQueryBuilder('p')
  449.             ->select('p.postId, p.title, p.slug, p.sapo,p.reviewPoints, p.cateId, p.publishedDate, p.publishedTimestamp,
  450.             p.avatar,p.authorName, c.name cateName, c.slug cateSlug')
  451.             ->innerJoin(PostsCates::class, 'pc''WITH''pc.postId = p.postId')
  452.             ->innerJoin(Categories::class, 'c''WITH''c.cateId = p.cateId')
  453.             ->where('pc.cateId IN (:cateIds) AND p.publishedTimestamp <= :currentDate AND p.publishedTimestamp >= :fromDate ')
  454.             ->setParameters($parameters);
  455.         
  456.         if ($excludePostIds) {
  457.             $query->andWhere('p.postId NOT IN (:postIds)')->setParameter(':postIds'$excludePostIds);
  458.         }
  459.         
  460.         $posts $query->orderBy('p.publishedDate''DESC')
  461.             ->setMaxResults($limit)
  462.             ->getQuery()
  463.             ->getArrayResult();
  464.         
  465.         return $posts;
  466.     }
  467.     /**
  468.      * Get top post for instant articles
  469.      * author: ThanhDT
  470.      * date:   2020-04-01 04:55 PM
  471.      * @param $limit
  472.      * @return array
  473.      */
  474.     public function getPostsForInstantArticle($limit)
  475.     {
  476.         $date date('Y-m-d H:i:s');
  477.         $queryBuilder $this->createQueryBuilder('p');
  478.         $condition $queryBuilder
  479.             //->select('p.postId, p.title, p.slug, p.sapo, p.cateId, p.publishedDate, p.avatar')
  480.             ->select('p.postId, p.title, p.slug, p.sapo, pd.contentMobile AS content, p.cateId, p.publishedDate, p.modifiedDate, p.avatar, p.authorName')
  481.             ->innerJoin(PostDatas::class, 'pd''WITH''p.postId = pd.postId')
  482.             ->where('p.publishedDate <= :currentDate AND p.fbInstantArticle = :fbInstantArticle')
  483.             ->setParameters([':currentDate' => $date':fbInstantArticle' => 1])
  484.             ->orderBy('p.publishedDate''DESC')
  485.             ->setMaxResults($limit);
  486.         $data $condition
  487.             ->getQuery()
  488.             ->getArrayResult();
  489.         return $data;
  490.     }//end getPostsForRss()
  491. }