src/Repository/ArticleRepository.php line 250

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\User;
  4. use App\Entity\Article;
  5. use App\Entity\ArticleSearch;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  8. /**
  9.  * @method Article|null find($id, $lockMode = null, $lockVersion = null)
  10.  * @method Article|null findOneBy(array $criteria, array $orderBy = null)
  11.  * @method Article[]    findAll()
  12.  * @method Article[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  13.  */
  14. class ArticleRepository extends ServiceEntityRepository
  15. {
  16.     public function __construct(ManagerRegistry $registry)
  17.     {
  18.         parent::__construct($registryArticle::class);
  19.     }
  20.     public function findCategoryCount(string $category2string $categorystring $etat '')
  21.     {
  22.         $query $this->findQueryBuilder();
  23.         $query->select('count(p.id) ');
  24.         $query->leftJoin('p.category''c');
  25.         $query->andWhere('c.title = :title');
  26.         $query->setParameter('title'$category);
  27.         $query->leftJoin('c.category2''c2');
  28.         $query->andWhere('c2.title = :title2');
  29.         $query->setParameter('title2'$category2);
  30.         if (!empty($etat)) {
  31.             $query->andWhere('p.etat = :etat');
  32.             $query->setParameter('etat'$etat);
  33.         }
  34.         $resulat =  $query->getQuery()->getResult();
  35.         return $resulat[0][1];
  36.     }
  37.     public function findCategoryTitleLimit(string $categoryint $maxResults 10)
  38.     {
  39.         $query $this->findQueryBuilder();
  40.         $query->leftJoin('p.category''c');
  41.         $query->andWhere('c.title = :title');
  42.         $query->setParameter('title'$category)
  43.             ->setMaxResults($maxResults);
  44.         return $query->getQuery()->getResult();
  45.     }
  46.     /**
  47.      * Recheche les articles en fonctions du formulaire
  48.      *
  49.      * @param  mixed $var
  50.      * @return void
  51.      */
  52.     public function search($mots null$category3 null$category2 null$category null$min null$max null$brand null$etat null$order = [])
  53.     {
  54.         $query $this->findQueryBuilder();
  55.         // Séparer les mots de recherche en un tableau
  56.         $termsArray explode(' '$mots);
  57.         if ($mots != null) {
  58.             // Construire les conditions LIKE pour chaque mot de recherche
  59.             foreach ($termsArray as $index => $term) {
  60.                 $query->andWhere('(p.title LIKE :term' $index ' OR p.description LIKE :term' $index ')')
  61.                     ->setParameter('term' $index'%' $term '%');
  62.             }
  63.         }
  64.         if ($min != null) {
  65.             $query
  66.                 ->andWhere("p.price >= :minprix")
  67.                 ->setParameter("minprix"$min);
  68.         }
  69.         if ($max != null) {
  70.             $query
  71.                 ->andWhere("p.price <= :maxprix")
  72.                 ->setParameter("maxprix"$max);
  73.         }
  74.         if ($etat != null) {
  75.             $query
  76.                 ->andWhere("p.status = :etat")
  77.                 ->setParameter("etat"$etat);
  78.         }
  79.         if ($brand != null) {
  80.             $query
  81.                 ->leftJoin('p.brand''b')
  82.                 ->andWhere("b.name = :brand")
  83.                 ->setParameter("brand"$brand);
  84.         }
  85.         if ($category != null) {
  86.             $query->leftJoin('p.category''c');
  87.             $query->andWhere('c.slug = :category_slug');
  88.             $query->andWhere('c.is_active = 1')
  89.                 ->setParameter('category_slug'$category);
  90.         } else {
  91.             $query->leftJoin('p.category''c');
  92.             $query->andWhere('c.is_active = 1');
  93.         }
  94.         if ($category3 != null) {
  95.             if ($category3 == 'reductions') {
  96.                 $query->andWhere('p.reduction > 0');
  97.             } else {
  98.                 $query->leftJoin('c.category2''c2');
  99.                 $query->leftJoin('c2.category3''c3');
  100.                 $query->andWhere('c3.slug = :category3')
  101.                     ->setParameter('category3'$category3);
  102.             }
  103.         }
  104.         if ($category2 != null) {
  105.             $query->leftJoin('p.category''c');
  106.             $query->leftJoin('c.category2''c2');
  107.             $query->andWhere("c2.slug = :category2 ");
  108.             $query->setParameter('category2'$category2);
  109.         }
  110.         if (!empty($order)) {
  111.             $query->orderBy('p.' $order[0], $order[1]);
  112.         }
  113.         return $query->getQuery();
  114.     }
  115.     public function findCategoryTitle(string $categorystring $etat '')
  116.     {
  117.         $query $this->findQueryBuilder();
  118.         $query->leftJoin('p.category''c');
  119.         $query->andWhere('c.title = :title');
  120.         $query->setParameter('title'$category);
  121.         if (!empty($etat)) {
  122.             $query->andWhere('p.etat = :etat');
  123.             $query->setParameter('etat'$etat);
  124.         }
  125.         return $query->getQuery()->getResult();
  126.     }
  127.     public function showPagination()
  128.     {
  129.         $query $this->findQueryBuilder();
  130.         // $query->
  131.         return $query->getQuery();
  132.     }
  133.     /**
  134.      * Recheche les articles en fonctions du formulaire
  135.      *
  136.      * @param  mixed $var
  137.      * @return void
  138.      */
  139.     public function searchJson($mots null)
  140.     {
  141.         $query $this->findQueryBuilder()
  142.             ->AndWhere('p.enabled = true');
  143.         if ($mots != null) {
  144.             $query->andWhere('MATCH_AGAINST(p.title, p.description) AGAINST(:mots boolean) > 0')
  145.                 ->setParameter('mots'$mots);
  146.         }
  147.         $articles $query->getQuery()->getResult();
  148.         $suggestions = [];
  149.         foreach ($articles as $v) {
  150.             $suggestions[] = [
  151.                 'id' => $v->getId(),
  152.                 'name' => $v->getTitle(),
  153.                 'link' => '',
  154.                 'image' => ''
  155.             ];
  156.         }
  157.         return $suggestions;
  158.     }
  159.     /**
  160.      * @return Query[]
  161.      */
  162.     public function findAllOnQuery(ArticleSearch $Search)
  163.     {
  164.         // $Search->setMaxPrice(10000);
  165.         $query =  $this->findQueryBuilder()
  166.             ->andwhere("p.enabled = true");
  167.         // ->setParameter('activer',true);
  168.         if ($Search->getMaxPrice()) {
  169.             $query
  170.                 ->andWhere("p.price <= :maxprice ")
  171.                 ->setParameter("maxprix"$Search->getMaxPrice());
  172.         }
  173.         if ($Search->getMinPrice()) {
  174.             $query
  175.                 ->andWhere("p.price >= :minprice ")
  176.                 ->setParameter("minprix"$Search->getMinPrice());
  177.         }
  178.         return $query->getQuery();
  179.     }
  180.     public function recently()
  181.     {
  182.         return $this->findQueryBuilder()
  183.             ->orderBy('p.id''DESC')
  184.             ->setMaxResults(5)
  185.             ->getQuery()
  186.             ->getResult();
  187.     }
  188.     public function findAllOff()
  189.     {
  190.         return $this->findQueryBuilder()
  191.             ->where("p.enabled = false ")
  192.             ->getQuery()
  193.             ->getResult();
  194.     }
  195.     public function findCountOnline()
  196.     {
  197.         return count($this->findAllOn());
  198.     }
  199.     public function findAllOn()
  200.     {
  201.         return $this->findQueryBuilder()
  202.             ->where("p.enabled = true ")
  203.             ->orderBy('p.id''DESC')
  204.             ->getQuery()
  205.             ->getResult();
  206.     }
  207.     public function findEtat($etat null)
  208.     {
  209.         return $this->findQueryBuilder()
  210.             ->where("p.etat = :etat ")
  211.             ->setParameter('etat'$etat)
  212.             ->setMaxResults(10)
  213.             ->getQuery()
  214.             ->getResult();
  215.     }
  216.     public function findRand(int $maxResults 10Article $article null)
  217.     {
  218.         return $this->findQueryBuilder()
  219.             ->addSelect('RAND() as HIDDEN rand')
  220.             ->where("p.enabled = true ")
  221.             ->andWhere('p.id <> :id')
  222.             ->setParameter('id'$article == null $article->getId())
  223.             ->orderBy('rand')
  224.             ->setMaxResults($maxResults)
  225.             ->getQuery()
  226.             ->getResult();
  227.     }
  228.     public function findAllOnArray()
  229.     {
  230.         return $this->findQueryBuilder()
  231.             ->where("p.enabled = true ")
  232.             ->getQuery()
  233.             ->getArrayResult();
  234.         // ->getResult();
  235.     }
  236.     public function findQueryBuilder()
  237.     {
  238.         $query $this->createQueryBuilder('p');
  239.         $query->andWhere('p.enabled = true');
  240.         return $query;
  241.     }
  242.     /**
  243.      * Vérifie si l'article est en favoris en fonction du user
  244.      *
  245.      */
  246.     public function isFavoris(User $userArticle $article)
  247.     {
  248.         $conn $this->getEntityManager()->getConnection();
  249.         $sql '
  250.             SELECT * FROM article_user f
  251.             WHERE f.article_id = :article_id and f.user_id = :user_id
  252.             ';
  253.         $stmt $conn->prepare($sql);
  254.         $resultSet $stmt->executeQuery([
  255.             'article_id' => $article->getId(),
  256.             'user_id' => $user->getId()
  257.         ]);
  258.         if (empty($resultSet->fetchAllAssociative())) {
  259.             return false;
  260.         } else {
  261.             return true;
  262.         }
  263.         return false;
  264.     }
  265.     // /**
  266.     //  * @return Article[] Returns an array of Article objects
  267.     //  */
  268.     /*
  269.     public function findByExampleField($value)
  270.     {
  271.         return $this->createQueryBuilder('a')
  272.             ->andWhere('a.exampleField = :val')
  273.             ->setParameter('val', $value)
  274.             ->orderBy('a.id', 'ASC')
  275.             ->setMaxResults(10)
  276.             ->getQuery()
  277.             ->getResult()
  278.         ;
  279.     }
  280.     */
  281.     /*
  282.     public function findOneBySomeField($value): ?Article
  283.     {
  284.         return $this->createQueryBuilder('a')
  285.             ->andWhere('a.exampleField = :val')
  286.             ->setParameter('val', $value)
  287.             ->getQuery()
  288.             ->getOneOrNullResult()
  289.         ;
  290.     }
  291.     */
  292. }