<?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();
dump($locale);
$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 findPostById($id)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->andWhere('p.id = :id')
->andWhere('t.actif = true')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('id', $id);
return $sql->getQuery()->getResult();
}
public function findAllPostByType($locale,$type)
{
$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 findAllOtherPosts($id)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->andWhere('p.post_archive = 0 OR p.post_archive is null')
->andWhere('t.locale = :locale')
->andWhere('t.actif = true')
->setParameter('locale', $locale);
if($id){
$sql->andWhere('p.id != :id')
->setParameter('id', $id);
}
$sql->orderBy('p.position','ASC');
return $sql->getQuery()->getResult();
}
public function findAllActivesPostsByType($locale,$type)
{
$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')
->andWhere('t.actif = true')
->setParameter('type', $type);
}
$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 findPostMaxPosition()
{
$sql = $this->createQueryBuilder('p')
->select('MAX(p.position) as max_position');
return $sql->getQuery()->getOneOrNullResult();
}
public function findPostByCategory($categorie,$page = 1)
{
$id_categorie = $categorie->getId();
$pagination = $categorie->getPagination() != null ? $categorie->getPagination() : false;
$page_size = $categorie->getPageSize() != null ? $categorie->getPageSize() : 10;
$order_by = 1;
$order_by = $categorie->getOrderBy();
$req_order = "p.position";
$asc_desc = "ASC";
if($order_by == 0){
$req_order = "p.date_publication";
$asc_desc = "DESC";
}
$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', $id_categorie)
->orderBy($req_order, $asc_desc);
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();
}
/*****************************************************************
** get liste actualite related with secteur activte ***
*****************************************************************/
public function findActualiteByActivite()
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->andWhere('p.id_activite IS NOT NULL or p.id_sous_activite IS NOT NULL or p.id_secteur IS NOT NULL')
->join('p.translations','t')
->andWhere('t.actif = true')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->andWhere('p.date_publication IS NULL OR p.date_publication <= :currentDate')
->setParameter('currentDate', new \DateTime())
->join('p.type_post','typepost')
->andWhere('typepost.id != 11')
->getQuery()
->getResult();
return $sql;
}
/*****************************************************************
** requete sql ref ***
*****************************************************************/
public function findPostByIdSecteur($id,$id_activite,$id_secteur)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->andWhere('p.id != :id')
->join('p.translations','t')
->andWhere('t.actif = true')
->andWhere('p.id_secteur = :id_secteur')
->andWhere('p.id_activite = :id_activite')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('id_secteur', $id_secteur)
->setParameter('id_activite', $id_activite)
->setParameter('id', $id)
->getQuery()
->getResult();
return $sql;
}
public function findPageRefByIdActivite($id_activite,$id_diff = null)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typepost')
->andWhere('typepost.id = 11')
->andWhere('t.actif = true')
->andWhere('p.id_activite = :id_activite')
->andWhere('t.locale = :locale')
->andWhere('p.date_publication IS NULL OR p.date_publication <= :currentDate')
->setParameter('currentDate', new \DateTime())
->setParameter('locale', $locale)
->setParameter('id_activite', $id_activite)
->andWhere('p.id_sous_activite is NULL')
->setMaxResults(3)
->orderBy('p.id','desc');
if ($id_diff != null) {
$sql->andWhere('p.id != :id_diff')
->setParameter('id_diff', $id_diff);
}
return $sql->getQuery()->getResult();
}
public function findPageRefByIdSousActivite($id_sous_activite,$id_diff = null)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typepost')
->andWhere('typepost.id = 11')
->andWhere('t.actif = true')
->andWhere('p.id_sous_activite = :id_sous_activite')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->andWhere('p.date_publication IS NULL OR p.date_publication <= :currentDate')
->setParameter('currentDate', new \DateTime())
->setParameter('id_sous_activite', $id_sous_activite)
->setMaxResults(3)
->orderBy('p.id','desc');
if ($id_diff != null) {
$sql->andWhere('p.id != :id_diff')
->setParameter('id_diff', $id_diff);
}
return $sql->getQuery()->getResult();
}
public function findPageRefByIdActiviteAndIdSousActivite($id_activite,$id_sous_activite,$id_diff = null)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typepost')
->andWhere('typepost.id = 11')
->andWhere('t.actif = true')
->andWhere('p.id_activite = :id_activite')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('id_activite', $id_activite)
->andWhere('p.id_sous_activite = :id_sous_activite')
->setParameter('id_sous_activite', $id_sous_activite)
->setMaxResults(3)
->orderBy('p.id','desc');
if ($id_diff != null) {
$sql->andWhere('p.id != :id_diff')
->setParameter('id_diff', $id_diff);
}
return $sql->getQuery()->getResult();
}
public function findPageClientByIdActiviteOrSousActivite($id_activite,$type = 'activite')
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typepost')
->andWhere('typepost.id != 11')
->andWhere('t.actif = true')
->andWhere('p.date_publication IS NULL OR p.date_publication <= :currentDate')
->setParameter('currentDate', new \DateTime())
->andWhere('t.locale = :locale')
->setParameter('locale', $locale);
if ($type == 'activite') {
$sql->andWhere('p.id_activite = :id_activite')
->setParameter('id_activite', $id_activite);
}else{
$sql->andWhere('p.id_sous_activite = :id_sous_activite')
->setParameter('id_sous_activite', $id_activite);
}
$sql->setMaxResults(3)->orderBy('p.id','desc');
return $sql->getQuery()->getResult();
}
public function findPageClientByIdActiviteAndSousActivite($id_activite,$sous_activite)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typepost')
->andWhere('typepost.id != 11')
->andWhere('t.actif = true')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->andWhere('p.id_activite = :id_activite')
->setParameter('id_activite', $id_activite)
->andWhere('p.id_sous_activite = :id_sous_activite')
->setParameter('id_sous_activite', $sous_activite);
$sql->setMaxResults(3)->orderBy('p.id','desc');
return $sql->getQuery()->getResult();
}
public function findPageRefByIdsSousActivite($ids_sous_activite = [])
{
$locale = Locale::getDefault();
$req1 = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typepost')
->andWhere('typepost.id = 11')
->andWhere('t.actif = true')
->andWhere('p.actif_home = true')
->andWhere('p.id_sous_activite IN (:ids_sous_activite)')
->andWhere('p.type_page_ref = :type_page_ref')
->andWhere('t.locale = :locale')
->andWhere('p.date_publication IS NULL OR p.date_publication <= :currentDate')
->setParameter('currentDate', new \DateTime())
->setParameter('locale', $locale)
->setParameter('ids_sous_activite', $ids_sous_activite)
->setParameter('type_page_ref', 'activite_secondaire')
->setMaxResults(3)
->orderBy('p.id','desc')->getQuery()->getResult();
if (empty($req1)) {
$req1 = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typepost')
->andWhere('typepost.id = 11')
->andWhere('t.actif = true')
// ->andWhere('p.actif_home = true')
->andWhere('p.id_sous_activite IN (:ids_sous_activite)')
->andWhere('p.type_page_ref = :type_page_ref')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('ids_sous_activite', $ids_sous_activite)
->andWhere('p.date_publication IS NULL OR p.date_publication <= :currentDate')
->setParameter('currentDate', new \DateTime())
->setParameter('type_page_ref', 'activite_secondaire')
->setMaxResults(3)
->orderBy('RAND()')
->orderBy('p.id','desc')->getQuery()->getResult();
}
return $req1;
}
public function findPageRefByIdsSousActiviteAndActivitePrincipal($id_activite,$id_secteur = null)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.type_post','typepost')
->andWhere('typepost.id = 11')
->andWhere('t.actif = true')
->andWhere('p.id_sous_activite IS NOT NULL')
->andWhere('p.date_publication IS NULL OR p.date_publication <= :currentDate')
->setParameter('currentDate', new \DateTime())
//->andWhere('p.id_sous_activite IN (:ids_sous_activite)')
// ->setParameter('ids_sous_activite', $ids_sous_activite)
->andWhere('p.type_page_ref = :type_page_ref')
->andWhere('p.id_activite = :id_activite')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('id_activite', $id_activite)
->setParameter('type_page_ref', 'activite_secondaire');
if ($id_secteur != null) {
$sql->andWhere('p.id_secteur = :id_secteur')
->setParameter('id_secteur', $id_secteur);
}
$sql->groupBy('p.id_sous_activite')
//->distinct('p.id_secteur');
->orderBy('RAND()')
->setMaxResults(3);
//->orderBy('p.id','desc');
return $sql->getQuery()->getResult();
}
public function findAllPostNotArchived($locale)
{
$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);
$sql->orderBy('p.position','ASC');
return $sql->getQuery()->getResult();
}
/********************* Sql gestion des produits suivant categories ***********************/
public function findPostBySpecifiqueCategory($categorie,$page = 1,$categorie_ids = [])
{
// $id_categorie = $categorie->getId();
$pagination = $categorie->getPagination() != null ? $categorie->getPagination() : false;
$page_size = $categorie->getPageSize() != null ? $categorie->getPageSize() : 10;
$order_by = 1;
$order_by = $categorie->getOrderBy();
$req_order = "p.position";
$asc_desc = "ASC";
if($order_by == 0){
$req_order = "p.date_publication";
$asc_desc = "DESC";
}
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->join('p.PostCategory','post_categorie')
->andWhere('t.actif = true')
->andWhere('post_categorie.Category IN (:categorie_ids)')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('categorie_ids', $categorie_ids)
->orderBy($req_order, $asc_desc);
if ($pagination == true) {
return (new Paginator($sql))->paginate($page,$page_size);
}else{
return $sql->getQuery()->getResult();
}
}
/**************** get produit related *************/
public function findProductRelated($type,$diff_id,$id_default_categorie)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->andWhere('t.actif = true')
->andWhere('p.id != :diff_id')
->andWhere('p.default_categorie = :id_default_categorie')
->andWhere('p.post_archive = 0 OR p.post_archive is null')
->join('p.type_post','typePost')
->andWhere('typePost.id = :type')
->andWhere('t.locale = :locale')
->setParameter('id_default_categorie', $id_default_categorie)
->setParameter('locale', $locale)
->setParameter('diff_id', $diff_id)
->setParameter('type', $type);
return $sql->getQuery()->getResult();
}
/**************** get related Annonces *************/
public function findRelatedAnnonces($type,$diff_id)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->andWhere('t.actif = true')
->andWhere('p.id != :diff_id')
->andWhere('p.post_archive = 0 OR p.post_archive is null')
->join('p.type_post','typePost')
->andWhere('typePost.id = :type')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('diff_id', $diff_id)
->setParameter('type', $type);
return $sql->getQuery()->getResult();
}
/*****************************************************************************************/
/********************** Affichage Détails produit pour le panier *************************/
public function getCartProductInfos($id)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->select('p.id', 't.title', 'p.prix_produit', 'p.image')
->join('p.translations','t')
->andWhere('t.actif = true')
->andWhere('p.id = :id')
->andWhere('p.post_archive = 0 OR p.post_archive is null')
->join('p.type_post','typePost')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->setParameter('id', $id)
->getQuery()
->getOneOrNullResult();
return $sql;
}
/*****************************************************************************************/
/* find slug page by alias */
public function findSlugPageByAlias($alias)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->join('p.translations','t')
->andWhere('t.locale = :locale')
->setParameter('locale', $locale)
->andWhere('p.alias = :alias')
->setParameter('alias', $alias);
return $sql->getQuery()->getOneOrNullResult();
}
public function findSystemNameByIdPost($id_post)
{
$locale = Locale::getDefault();
$sql = $this->createQueryBuilder('p')
->select('typePost.id')
->join('p.type_post','typePost')
// ->join('typePost.translations','translation')
->andWhere('p.id = :id_post')
->setParameter('id_post', $id_post)
->getQuery()
->getOneOrNullResult();
return $sql;
}
}