<?php
namespace App\Repository;
use App\Entity\Post;
use App\Entity\PostTranslation;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Intl\Locale;
use App\Pagination\Paginator;
/**
* @method Post|null find($id, $lockMode = null, $lockVersion = null)
* @method Post|null findOneBy(array $criteria, array $orderBy = null)
* @method Post[] findAll()
* @method Post[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PostRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Post::class);
}
/** find conut nb resultat post **/
public function findCountPost($type = null)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->select('count(p.id)');
if($type != null){
$sql->join('p.type_post','typePost')
->andWhere('typePost.id = :type')
->setParameter('type', $type);
}
return $sql->getQuery()->getSingleScalarResult();
}
/** end off method **/
public function findPostBySlug($slug)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale);
if($slug){
$sql->andWhere('t.slug = :slug')
->setParameter('slug', $slug);
}
return $sql->getQuery()->getOneOrNullResult();
}
public function findAllPostByType($locale,$type,$IdSite)
{
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->andWhere('p.post_archive = 0 OR p.post_archive is null')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale);
if($type){
$sql->join('p.type_post','tp')
->andWhere('tp.id = :type')
->setParameter('type', $type);
// if($type == '6'){
// $sql->orderBy('p.date_publication','DESC');
// }
}
$sql->orderBy('p.position','ASC');
return $sql->getQuery()->getResult();
}
public function findPostByType($type,$option,$nb)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typePost')
->andWhere('typePost.id = :type')
->andWhere('t.actif = true')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('type', $type)
->setMaxResults($nb);
if($option == '1'){
$sql->orderBy('p.id', 'ASC');
}elseif($option == '2'){
$sql->orderBy('p.id', 'DESC');
}
return $sql->getQuery()->getResult();
}
public function findPostActifsHomeByType($type,$option,$nb = 1,$all_post = false)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typePost')
->andWhere('typePost.id = :type')
->andWhere('t.actif = true')
->andWhere('p.actif_home = 1')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('type', $type);
if($all_post == false){
$sql->setMaxResults($nb);
}
if($option == '1'){
$sql->orderBy('p.id', 'ASC');
}elseif($option == '2'){
$sql->orderBy('p.id', 'DESC');
}elseif($option == '3'){
$sql->orderBy('p.date_publication','DESC');
}elseif($option == '4'){
$sql->orderBy('p.position', 'ASC');
}elseif($option == '5'){
$sql->orderBy('RAND()');
}
return $sql->getQuery()->getResult();
}
public function findPostByTypeInterne($type,$option,$nb)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typePost')
->andWhere('typePost.id = :type')
->andWhere('t.actif = true')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('type', $type)
->setMaxResults($nb);
if($option == '1'){
$sql->orderBy('p.id', 'ASC');
}elseif($option == '2'){
$sql->orderBy('p.id', 'DESC');
}elseif($option == '3'){
$sql->orderBy('p.date_publication','DESC');
}elseif($option == '4'){
$sql->orderBy('p.position', 'ASC');
}elseif($option == '5'){
$sql->orderBy('RAND()');
}
return $sql->getQuery()->getResult();
}
public function findPostBySystemName($name)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.type_post','typePost')
->join('typePost.translations','translation')
->join('p.translations','t')
->andWhere('t.actif = true')
->andWhere('translation.system_name = :name')
->andWhere('translation.locale = :locale')
->setParameter('locale', $locale)
->setParameter('name', $name)
->getQuery()
->getResult();
return $sql;
}
public function findPostBySystemNames($system_name)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.type_post','typePost')
->join('typePost.translations','translation')
->join('p.translations','t')
->andWhere('t.actif = true')
->andWhere("translation.system_name IN(:system_name)")
->andWhere('translation.locale = :locale')
->setParameter('locale', $locale)
->setParameter('system_name', $system_name)
->getQuery()
->getResult();
return $sql;
}
public function findPostByCategory($categorie,$pagination,$page = 1,$page_size = 10)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.PostCategory','post_categorie')
->andWhere('t.actif = true')
->andWhere('post_categorie.Category =:categorie_id')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('categorie_id', $categorie)
->orderBy('p.position', 'ASC');
if ($pagination == true) {
return (new Paginator($sql))->paginate($page,$page_size);
}else{
return $sql->getQuery()->getResult();
}
}
/**
* Retourne les posts par type et par id avec limit
*/
public function getPostsWithLimit($locale,$type,$limit,$order,$IdSite)
{
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale);
if($type){
$sql->join('p.type_post','tp')
->andWhere('tp.id = :type')
->setParameter('type', $type);
/* if($type == '6'){
$sql->orderBy('p.date_publication','DESC');
} */
}
switch ($order) {
case 0:
$sql->orderBy('RAND()');
break;
case 1:
$sql->orderBy('p.id','ASC');
break;
case 2:
$sql->orderBy('p.id','DESC');
break;
case 3:
$sql->orderBy('p.date_publication','DESC');
break;
default:
$sql->orderBy('p.id','ASC');
break;
}
if($limit){
$sql->setMaxResults($limit);
}
return $sql->getQuery()->getResult();
}
/**
* Retourne la liste des catégories du post
*/
public function getCategoryPost($post_category, $_locale){
$categories = [];
if($post_category){
foreach ($post_category as $key => $post_cat) {
if($post_cat->getCategory()){
$cat = $post_cat->getCategory();
$categories[] = ['id'=> $cat->getId(),'title'=> $cat->translate($_locale)->getTitreCategorie()];
}
}
}
return $categories;
}
// get article prev or next
public function findPostPrevOrNext($ids_categories,$id_post,$action = 'next')
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.PostCategory','post_categorie')
->andWhere('t.actif = true')
->andWhere('p.id != :id_post')
->andWhere('post_categorie.Category IN (:ids_categories)')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('ids_categories', $ids_categories)
->setMaxResults(1);
if($action == 'next') {
$sql->andWhere('p.id > :id_post')
->orderBy('p.id', 'asc');
}else{
$sql->andWhere('p.id < :id_post')
->orderBy('p.id', 'DESC');
}
$sql->setParameter('id_post', $id_post);
return $sql->getQuery()->getOneOrNullResult();
}
}