src/Repository/PostRepository.php line 137

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Post;
  4. use App\Entity\PostTranslation;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Intl\Locale;
  8. use App\Pagination\Paginator;
  9. /**
  10.  * @method Post|null find($id, $lockMode = null, $lockVersion = null)
  11.  * @method Post|null findOneBy(array $criteria, array $orderBy = null)
  12.  * @method Post[]    findAll()
  13.  * @method Post[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14.  */
  15. class PostRepository extends ServiceEntityRepository
  16. {
  17.     public function __construct(ManagerRegistry $registry)
  18.     {
  19.         parent::__construct($registryPost::class);
  20.     }
  21.     /** find conut nb resultat post **/
  22.     public function findCountPost($type null)
  23.     {
  24.         $locale Locale::getDefault();
  25.         $sql =  $this->createQueryBuilder('p')
  26.                      ->select('count(p.id)');
  27.       
  28.         if($type != null){
  29.             $sql->join('p.type_post','typePost')
  30.                 ->andWhere('typePost.id = :type')
  31.                 ->setParameter('type'$type);
  32.         }
  33.         return $sql->getQuery()->getSingleScalarResult();
  34.     }
  35.     /** end off method **/
  36.     public function findPostBySlug($slug)
  37.     {
  38.         $locale Locale::getDefault();
  39.         $sql =  $this->createQueryBuilder('p')
  40.                      ->join('p.translations','t')
  41.                      ->andWhere('t.locale = :locale')
  42.                      ->setParameter('locale'$locale);
  43.       
  44.         if($slug){
  45.             $sql->andWhere('t.slug = :slug')
  46.                 ->setParameter('slug'$slug);
  47.         }
  48.         return $sql->getQuery()->getOneOrNullResult();
  49.     }
  50.         
  51.     public function findAllPostByType($locale,$type,$IdSite)
  52.     {
  53.        
  54.         $sql =  $this->createQueryBuilder('p')
  55.                      ->join('p.translations','t')
  56.                      ->andWhere('p.post_archive = 0 OR p.post_archive is null')
  57.                      ->andWhere('t.locale = :locale')
  58.                      ->setParameter('locale'$locale);
  59.         if($type){
  60.             $sql->join('p.type_post','tp')
  61.                 ->andWhere('tp.id = :type')
  62.                 ->setParameter('type'$type);
  63.             // if($type == '6'){
  64.             //     $sql->orderBy('p.date_publication','DESC');
  65.             // }
  66.         }
  67.      
  68.         $sql->orderBy('p.position','ASC');
  69.         return $sql->getQuery()->getResult();
  70.     }
  71.     public function findPostByType($type,$option,$nb)
  72.     {
  73.         $locale Locale::getDefault();
  74.         $sql =  $this->createQueryBuilder('p')
  75.                      ->join('p.translations','t')
  76.                      ->join('p.type_post','typePost')
  77.                      ->andWhere('typePost.id = :type')
  78.                      ->andWhere('t.actif = true')
  79.                      ->andWhere('t.locale = :locale')
  80.                      ->setParameter('locale'$locale)
  81.                      ->setParameter('type'$type)
  82.                      ->setMaxResults($nb);
  83.         if($option == '1'){
  84.             $sql->orderBy('p.id''ASC');
  85.         }elseif($option == '2'){
  86.             $sql->orderBy('p.id''DESC');
  87.         }
  88.       
  89.         return $sql->getQuery()->getResult();
  90.     }
  91.     public function findPostActifsHomeByType($type,$option,$nb 1,$all_post false)
  92.     {
  93.         $locale Locale::getDefault();
  94.         $sql =  $this->createQueryBuilder('p')
  95.                      ->join('p.translations','t')
  96.                      ->join('p.type_post','typePost')
  97.                      ->andWhere('typePost.id = :type')
  98.                      ->andWhere('t.actif = true')
  99.                      ->andWhere('p.actif_home = 1')
  100.                      ->andWhere('t.locale = :locale')
  101.                      ->setParameter('locale'$locale)
  102.                      ->setParameter('type'$type);
  103.                      
  104.         if($all_post == false){
  105.            $sql->setMaxResults($nb);
  106.         }
  107.         if($option == '1'){
  108.             $sql->orderBy('p.id''ASC');
  109.         }elseif($option == '2'){
  110.             $sql->orderBy('p.id''DESC');
  111.         }elseif($option == '3'){
  112.             $sql->orderBy('p.date_publication','DESC');
  113.         }elseif($option == '4'){
  114.             $sql->orderBy('p.position''ASC');
  115.         }elseif($option == '5'){
  116.             $sql->orderBy('RAND()');
  117.         }
  118.       
  119.         return $sql->getQuery()->getResult();
  120.     }
  121.     public function findPostByTypeInterne($type,$option,$nb)
  122.     {
  123.         $locale Locale::getDefault();
  124.         $sql =  $this->createQueryBuilder('p')
  125.                      ->join('p.translations','t')
  126.                      ->join('p.type_post','typePost')
  127.                      ->andWhere('typePost.id = :type')
  128.                      ->andWhere('t.actif = true')
  129.                      ->andWhere('t.locale = :locale')
  130.                      ->setParameter('locale'$locale)
  131.                      ->setParameter('type'$type)
  132.                      ->setMaxResults($nb);
  133.         if($option == '1'){
  134.             $sql->orderBy('p.id''ASC');
  135.         }elseif($option == '2'){
  136.             $sql->orderBy('p.id''DESC');
  137.         }elseif($option == '3'){
  138.             $sql->orderBy('p.date_publication','DESC');
  139.         }elseif($option == '4'){
  140.             $sql->orderBy('p.position''ASC');
  141.         }elseif($option == '5'){
  142.             $sql->orderBy('RAND()');
  143.         }
  144.       
  145.         return $sql->getQuery()->getResult();
  146.     }
  147.     
  148.     public function findPostBySystemName($name)
  149.     {
  150.         $locale Locale::getDefault();
  151.         $sql =  $this->createQueryBuilder('p')
  152.                      ->join('p.type_post','typePost')
  153.                      ->join('typePost.translations','translation')
  154.                      ->join('p.translations','t')
  155.                      ->andWhere('t.actif = true')
  156.                      ->andWhere('translation.system_name = :name')
  157.                      ->andWhere('translation.locale = :locale')
  158.                      ->setParameter('locale'$locale)
  159.                      ->setParameter('name'$name)
  160.                      ->getQuery()
  161.                      ->getResult();
  162.      
  163.         return $sql;
  164.     }
  165.     public function findPostBySystemNames($system_name)
  166.     {
  167.         $locale Locale::getDefault();
  168.         $sql =  $this->createQueryBuilder('p')
  169.                      ->join('p.type_post','typePost')
  170.                      ->join('typePost.translations','translation')
  171.                      ->join('p.translations','t')
  172.                      ->andWhere('t.actif = true')
  173.                      ->andWhere("translation.system_name IN(:system_name)")
  174.                      ->andWhere('translation.locale = :locale')
  175.                      ->setParameter('locale'$locale)
  176.                      ->setParameter('system_name'$system_name)
  177.                      ->getQuery()
  178.                      ->getResult();
  179.      
  180.         return $sql;
  181.     }
  182.     public function findPostByCategory($categorie,$pagination,$page 1,$page_size 10)
  183.     {
  184.         $locale Locale::getDefault();
  185.         $sql =  $this->createQueryBuilder('p')
  186.                      ->join('p.translations','t')
  187.                      ->join('p.PostCategory','post_categorie')
  188.                      ->andWhere('t.actif = true')
  189.                      ->andWhere('post_categorie.Category =:categorie_id')
  190.                      ->andWhere('t.locale = :locale')
  191.                      ->setParameter('locale'$locale)
  192.                      ->setParameter('categorie_id'$categorie)
  193.                      ->orderBy('p.position''ASC');
  194.         if ($pagination == true) {
  195.             return (new Paginator($sql))->paginate($page,$page_size);
  196.         }else{
  197.             return $sql->getQuery()->getResult();
  198.         }
  199.     }
  200.     /**
  201.      * Retourne les posts par type et par id avec limit
  202.      */
  203.     public function getPostsWithLimit($locale,$type,$limit,$order,$IdSite)
  204.     {
  205.        
  206.         $sql =  $this->createQueryBuilder('p')
  207.                      ->join('p.translations','t')
  208.                      ->andWhere('t.locale = :locale')
  209.                      ->setParameter('locale'$locale);
  210.         if($type){
  211.             $sql->join('p.type_post','tp')
  212.                 ->andWhere('tp.id = :type')
  213.                 ->setParameter('type'$type);
  214.            /* if($type == '6'){
  215.               $sql->orderBy('p.date_publication','DESC');
  216.            } */
  217.         }
  218.         switch ($order) {
  219.             case 0:
  220.                 $sql->orderBy('RAND()');
  221.                 break;
  222.             case 1:
  223.                 $sql->orderBy('p.id','ASC');
  224.                 break;
  225.             case 2:
  226.                 $sql->orderBy('p.id','DESC');
  227.                 break;
  228.             case 3:
  229.                 $sql->orderBy('p.date_publication','DESC');
  230.                 break;
  231.             
  232.             default:
  233.                 $sql->orderBy('p.id','ASC');
  234.                 break;
  235.         }
  236.         if($limit){
  237.             $sql->setMaxResults($limit);
  238.         }
  239.      
  240.         return $sql->getQuery()->getResult();
  241.     }
  242.     /**
  243.      * Retourne la liste des catégories du post
  244.      */
  245.     public function getCategoryPost($post_category$_locale){
  246.         $categories = [];
  247.         if($post_category){
  248.             foreach ($post_category as $key => $post_cat) {
  249.               if($post_cat->getCategory()){
  250.                 $cat $post_cat->getCategory();
  251.                 $categories[] = ['id'=> $cat->getId(),'title'=> $cat->translate($_locale)->getTitreCategorie()];
  252.               }
  253.             }
  254.          }
  255.          return $categories;
  256.     }
  257.     // get article prev or next   
  258.     public function findPostPrevOrNext($ids_categories,$id_post,$action 'next')
  259.     {
  260.         $locale Locale::getDefault();
  261.         $sql =  $this->createQueryBuilder('p')
  262.                      ->join('p.translations','t')
  263.                      ->join('p.PostCategory','post_categorie')
  264.                      ->andWhere('t.actif = true')
  265.                      ->andWhere('p.id != :id_post')
  266.                      ->andWhere('post_categorie.Category IN (:ids_categories)')
  267.                      ->andWhere('t.locale = :locale')
  268.                      ->setParameter('locale'$locale)
  269.                      ->setParameter('ids_categories'$ids_categories)
  270.                      ->setMaxResults(1);
  271.                     
  272.         if($action == 'next') {
  273.              $sql->andWhere('p.id > :id_post')
  274.                  ->orderBy('p.id''asc');
  275.         }else{
  276.              $sql->andWhere('p.id < :id_post')
  277.                  ->orderBy('p.id''DESC');
  278.         }
  279.         $sql->setParameter('id_post'$id_post);
  280.         return $sql->getQuery()->getOneOrNullResult();
  281.     }
  282. }