<?php
namespace App\Repository;
use App\Entity\Categories;
use App\Entity\GroupBoxItems;
use App\Entity\PostDatas;
use App\Entity\PostPublishes;
use App\Entity\PostsCates;
use App\Entity\PostsTags;
use App\Entity\PostViews;
use App\Entity\Tags;
use App\Utils\Constants;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* @method PostPublishes|null find($id, $lockMode = null, $lockVersion = null)
* @method PostPublishes|null findOneBy(array $criteria, array $orderBy = null)
* @method PostPublishes[] findAll()
* @method PostPublishes[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PostPublishesRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, PostPublishes::class);
}
/**
* @param $groupBoxKey
* @param $limit
* @return mixed
*/
public function getTopPostsInGroupBox($groupBoxKey, $limit)
{
$posts = $this->createQueryBuilder('p')
->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')
->innerJoin(GroupBoxItems::class, 'gi', 'WITH', 'gi.dataId = p.postId')
->innerJoin(Categories::class, 'c', 'WITH', 'c.cateId = p.cateId')
->where('gi.key = :key AND p.publishedTimestamp <= :currentDate')
->setParameters([':key' => $groupBoxKey, ':currentDate' => time()])
->orderBy('gi.order')
->setMaxResults($limit)
->getQuery()
->getResult();
return $posts;
}
/**
* Get Rss article in cate paging
* author: ThanhDT
* date: 2020-01-10 02:45 PM
* @param $cateId
* @param $pageSize
* @return array
*/
public function getRssArticleInCatePaging($cateId, $pageSize)
{
$data = $this->createQueryBuilder('p')
->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')
->innerJoin(PostDatas::class, 'pd', 'WITH', 'p.postId = pd.postId')
->innerJoin(PostsCates::class, 'c', 'WITH', 'c.postId = p.postId')
->where('c.cateId IN (:cateId)')
->setParameters([':cateId' => $cateId])
->orderBy('p.publishedDate', 'DESC')
->setMaxResults($pageSize)
->getQuery()->getArrayResult();
return $data;
}
/**
* Get lastest Rss articles
* author: ThanhDT
* date: 2020-01-10 03:09 PM
* @param int $pageSize
* @return array
* @throws \Exception
*/
public function getRssArticleLatest($pageSize = 9)
{
$data = $this->createQueryBuilder('p')
->select('p.postId, p.cateId, p.title, p.slug, p.sapo, p.publishedDate, p.avatar, p.publishedTimestamp, p.authorName, pd.contentMobile AS content')
->innerJoin(PostDatas::class, 'pd', 'WITH', 'p.postId = pd.postId')
->where('p.publishedDate <= :currentDate')
->setParameters([':currentDate' => new \DateTime()])
->orderBy('p.publishedDate', 'DESC')
->setMaxResults($pageSize)
->getQuery()->getArrayResult();
return $data;
}
/**
* Get post in today
* author: ThanhDT
* date: 2020-01-09 12:31 AM
* @param int $cateId
* @return mixed
* @throws \Exception
*/
public function getPostByCurrentTime($cateId = '')
{
$qp = $this->createQueryBuilder('a')
->select('a.postId')
->where('a.publishedDate BETWEEN :currentMonth AND :currentDate')
->setParameters([':currentMonth' => date('Y-m'), ':currentDate' => new \DateTime()])
->setMaxResults(1);
if ($cateId) {
$qp->andWhere('a.cateId = :cateId ')
->setParameter(':cateId', $cateId);
}
return $qp->getQuery()->getResult();
}//end getPostByCurrentTime()
/**
* @param $start
* @param $end
* @param $cateId
* @return array
*/
public function getPostsBetweenDates($start, $end, $cateId='')
{
$queryBuilder = $this->createQueryBuilder('p');
$selectFields = [
'p.postId',
'p.cateId',
'p.avatar',
'p.slug',
'p.sapo',
'p.title',
'c.slug as slugCate',
'p.publishedDate'
];
$condition = $queryBuilder
->select($selectFields)
->innerJoin(Categories::class, 'c', 'WITH', 'c.cateId = p.cateId')
->orderBy('p.publishedTimestamp', 'DESC')
->where('p.publishedTimestamp BETWEEN :startDate AND :endDate')
->setParameter('startDate', $start)
//->andWhere('p.publishedDate <= :endDate')
->setParameter('endDate', $end);
if ($cateId) {
$condition->andWhere('p.cateId = :cateId ')
->setParameter(':cateId', $cateId);
}
$postsQuery = $condition->getQuery();
$posts = $postsQuery->getArrayResult();
return $posts;
}
/**
* @param $cateId
* @param int $limit
* @param array $exceptIds
* @return array
*/
public function getLatestPostsByCate($arrayCateId = [], $limit = 12, $exceptIds = [])
{
$query = $this->createQueryBuilder('p')
->select('p.postId, p.cateId, p.title, p.slug, p.sapo, p.publishedDate, p.publishedTimestamp, p.avatar, p.authorName')
->where('p.publishedTimestamp <= :now ')
->setParameter('now', time());
if ($arrayCateId) {
$query->innerJoin(PostsCates::class, 'c', 'WITH', 'c.postId = p.postId')
->andWhere('c.cateId IN (:cateId) ')
->setParameter(':cateId', $arrayCateId);
}
if (!empty($exceptIds)) {
$query->andWhere('p.postId not in (:exceptIds)')
->setParameter(':exceptIds', $exceptIds);
}
$data = $query->orderBy('p.publishedDate', 'DESC')
->setMaxResults($limit)
->getQuery()
->getArrayResult();
return $data;
}
/**
* @param int $limit
* @param int $fromModify
* @param array $exceptIds
* @param array $arrayCateId
* @return array
*/
public function getMostViewPosts($limit = 5, $fromModify = 172800, $exceptIds = [], $arrayCateId = [])
{
$now = time();
$from = ($now - $fromModify);
$queryBuilder = $this->createQueryBuilder('p');
$selectFields = [
'SUM(v.viewCount) AS totalView',
'p.postId',
'p.avatar',
'p.slug',
'p.sapo',
'p.title',
'p.cateId',
'p.viewCount',
'p.publishedDate',
'p.publishedTimestamp'
];
$condition = $queryBuilder
->select($selectFields)
->innerJoin(PostViews::class, 'v', 'WITH', 'p.postId = v.postId')
->where('p.publishedTimestamp <= :now and v.timestamp >= :from and v.timestamp <= :now')
->setParameter('now', $now)
->setParameter('from', $from)
->setParameter('now', $now)
->groupBy('v.postId')
->addOrderBy('totalView', 'DESC')
->addOrderBy('p.publishedDate', 'DESC')
->setMaxResults($limit);
if ($arrayCateId) { // Anhpt4 : add list cate id
$condition->andWhere('p.cateId IN (:cateId)')->setParameter(':cateId', $arrayCateId);
}
if ($exceptIds) {
$condition->andWhere(
$queryBuilder->expr()->notIn('p.postId', $exceptIds)
);
}
return $condition
->getQuery()
->getArrayResult();
}
/**
* @param $tagId
* @param $limit
* @param int $timeStamp
* @param int $lastId
* @param array $ignoreIds
* @return array
*/
public function getPostsByTagId($tagId, $limit, $timeStamp = 0, $lastId = 0, $ignoreIds = [])
{
if ($timeStamp) {
$date = new \DateTime();
$date->setTimestamp($timeStamp);
} else {
$date = date('Y-m-d H:i:s');
}
$queryBuilder = $this->createQueryBuilder('p');
$condition = $queryBuilder
->select('p.postId, p.title, p.slug, p.sapo, p.cateId, p.publishedDate, p.publishedTimestamp, p.avatar, p.authorName')
->leftJoin(PostsTags::class, 'p_tag', 'WITH', 'p.postId = p_tag.postId')
->where('p_tag.tagId = :tagId AND p.publishedDate <= :currentDate')
->setParameters(
[
':tagId' => $tagId,
':currentDate' => $date,
]
)
->orderBy('p.publishedDate', 'DESC')
->addOrderBy('p.postId', 'DESC');
if ($lastId) {
$condition
->andWhere('p.postId <> :lastId')
->setParameter('lastId', $lastId);
}
if ($ignoreIds) {
$condition->andWhere(
$condition->expr()->notIn('p.postId', $ignoreIds)
);
}
return $condition
->setMaxResults($limit)
->getQuery()
->getArrayResult();
}//end getPostsByTagId()
/**
* @param int $cateId
* @param array $excludePostIds
* @param int $limit
* @return array
*/
public function getPostNewInCate($arrayCateId = [], $excludePostIds = [], $limit = Constants::LIMIT_FEATURED, $focusStatus = 0)
{
$query = $this->createQueryBuilder('p')
->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')
->innerJoin(Categories::class, 'c', 'WITH', 'p.cateId = c.cateId');
if ($excludePostIds) {
$query->where('p.postId NOT IN (:postIds)')->setParameter(':postIds', $excludePostIds);
}
if ($arrayCateId) {
$query->andWhere('p.cateId IN (:cateId)')->setParameter(':cateId', $arrayCateId);
}
if (isset($focusStatus)) {
$query->andWhere('p.focusStatus = :focusStatus')->setParameter(':focusStatus', $focusStatus);
}
$posts = $query->andWhere('p.publishedTimestamp <= :currentDate ')
->setParameter(':currentDate', time())
->addOrderBy('p.publishedTimestamp', 'DESC')
->setMaxResults($limit)
->getQuery()
->getArrayResult();
return $posts;
}//end getPostNewCate()
/**
* Get paging posts in category
* @param $cateIds
* @param $limit
* @param $lastPublishedStamp
* @param null $excludePostIds
* @return array
*/
public function getCatePosts($cateIds, $limit, $lastPublishedStamp, $excludePostIds = null)
{
$query = $this->createQueryBuilder('p')
->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')
->innerJoin(PostsCates::class, 'pc', 'WITH', 'pc.postId = p.postId')
->innerJoin(Categories::class, 'c', 'WITH', 'c.cateId = p.cateId');
$parameters = [];
if ($excludePostIds) {
$query->where('p.postId NOT IN (:postIds)');//->setParameter(':postIds', $excludePostIds);
$parameters[':postIds'] = $excludePostIds;
}
$query->andWhere('pc.cateId IN (:cateIds) AND p.publishedTimestamp <= :currentDate');
$parameters[':currentDate'] = $lastPublishedStamp;
$parameters[':cateIds'] = $cateIds;
$posts = $query->setParameters($parameters)
->orderBy('p.publishedDate', 'DESC')
->groupBy('p.postId')
->setMaxResults($limit)
->getQuery()
->getArrayResult();
return $posts;
}
/**
* get detail post
* @param int $postId
* @param string $slug
* @return array
* author : ThangPD
* date : 2018-25-21 14:25 PM
*/
public function getDetailPost($postId = 0)
{
$query = $this->createQueryBuilder('p')
->select('p.postId, p.title, p.slug, p.avatar, p.sapo, p.cateId, p.authorId, p.authorName, p.publishedDate,
p.reviewId, p.otherTags, p.reviewPoints, p.seoMetadesc , p.seoTitle, p.seoFocusKeyword,
pd.content, pd.contentAmp, pd.cates, pd.tags, pd.relatedPosts,
pd.sourceTags, c.name cateName, c.slug cateSlug')
->leftJoin(PostDatas::class, 'pd', 'WITH', 'p.postId = pd.postId')
->leftJoin(Categories::class, 'c', 'WITH', 'p.cateId = c.cateId')
->where('p.postId = ' . $postId)
->andWhere('p.publishedDate <= :currentDate')
->setParameter(':currentDate', new \DateTime());
$result = $query->getQuery()->getOneOrNullResult();
return $result;
}
/**
* modifier: AnhPT4
* modifier date: 2019-47-2 16:47 PM
* get related post by id
* @param string $ids
* @return array
* author : ThangPD
* date : 2018-20-22 10:20 AM
*/
public function getRelatedPosts($ids = "", $limit = Constants::LIMIT_RELATED_POST)
{
$ids = trim($ids);
if (empty($ids))
return [];
$ids = explode(',', $ids);
$result = $this->createQueryBuilder('p')
->select('p.postId, p.cateId, p.title, p.slug, p.avatar, p.publishedDate, p.sapo, c.name cateName, c.slug cateSlug')
->leftJoin(Categories::class, 'c', 'WITH', 'p.cateId = c.cateId')
->where('p.postId IN (:ids) AND p.publishedDate <= :currentDate')
->setParameters([':ids' => $ids, ':currentDate' => new \DateTime()])
->setMaxResults(Constants::LIMIT_RELATED)
->orderBy('p.publishedDate', 'DESC')
->setMaxResults($limit)
->getQuery()
->getArrayResult();
return $result;
}
/**
* get Post In Tag
* @param array $tagsId
* @param int $limit
* @return array
*/
public function getPostInTag($postId, $tagsId = [], $limit = Constants::LIMIT_RELATED_POST)
{
if (empty($tagsId))
return null;
$result = $this->createQueryBuilder('p')
->select('p.postId, p.title,p.slug, c.slug cateSlug')
->innerJoin(Categories::class, 'c', 'WITH', 'p.cateId = c.cateId')
->innerJoin(PostsTags::class, 'pc', 'WITH', 'p.postId = pc.postId')
->innerJoin(Tags::class, 't', 'WITH', 'pc.tagId = t.tagId')
->where('p.postId NOT IN(:postId) AND t.tagId IN (:tagId) AND p.publishedTimestamp <= :currentDate')
->setParameters([':postId' => $postId, ':tagId' => $tagsId, ':currentDate' => time()])
->setMaxResults(Constants::LIMIT_RELATED)
->orderBy('p.publishedTimestamp', 'DESC')
->setMaxResults($limit)
->groupBy('p.postId')
->getQuery()
->getArrayResult();
return $result;
}
/**
* Get paging posts in category
* @param $cateIds
* @param $limit
* @param $lastPublishedStamp
* @param null $excludePostIds
* @return array
*/
public function getPostsInCates($cateIds, $limit, $lastPublishedStamp, $fromModify, $excludePostIds = null)
{
$parameters = [];
$time = $lastPublishedStamp - $fromModify;
$parameters[':currentDate'] = $lastPublishedStamp;
$parameters[':cateIds'] = $cateIds;
$parameters[':fromDate'] = $time;
$query = $this->createQueryBuilder('p')
->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')
->innerJoin(PostsCates::class, 'pc', 'WITH', 'pc.postId = p.postId')
->innerJoin(Categories::class, 'c', 'WITH', 'c.cateId = p.cateId')
->where('pc.cateId IN (:cateIds) AND p.publishedTimestamp <= :currentDate AND p.publishedTimestamp >= :fromDate ')
->setParameters($parameters);
if ($excludePostIds) {
$query->andWhere('p.postId NOT IN (:postIds)')->setParameter(':postIds', $excludePostIds);
}
$posts = $query->orderBy('p.publishedDate', 'DESC')
->setMaxResults($limit)
->getQuery()
->getArrayResult();
return $posts;
}
/**
* Get top post for instant articles
* author: ThanhDT
* date: 2020-04-01 04:55 PM
* @param $limit
* @return array
*/
public function getPostsForInstantArticle($limit)
{
$date = date('Y-m-d H:i:s');
$queryBuilder = $this->createQueryBuilder('p');
$condition = $queryBuilder
//->select('p.postId, p.title, p.slug, p.sapo, p.cateId, p.publishedDate, p.avatar')
->select('p.postId, p.title, p.slug, p.sapo, pd.contentMobile AS content, p.cateId, p.publishedDate, p.modifiedDate, p.avatar, p.authorName')
->innerJoin(PostDatas::class, 'pd', 'WITH', 'p.postId = pd.postId')
->where('p.publishedDate <= :currentDate AND p.fbInstantArticle = :fbInstantArticle')
->setParameters([':currentDate' => $date, ':fbInstantArticle' => 1])
->orderBy('p.publishedDate', 'DESC')
->setMaxResults($limit);
$data = $condition
->getQuery()
->getArrayResult();
return $data;
}//end getPostsForRss()
}