src/Repository/ContactsRepository.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Contacts;
  4. use App\Service\Securizer;
  5. use Doctrine\ORM\ORMException;
  6. use Doctrine\ORM\QueryBuilder;
  7. use Doctrine\ORM\OptimisticLockException;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  10. /**
  11.  * @method Contacts|null find($id, $lockMode = null, $lockVersion = null)
  12.  * @method Contacts|null findOneBy(array $criteria, array $orderBy = null)
  13.  * @method Contacts[]    findAll()
  14.  * @method Contacts[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15.  */
  16. class ContactsRepository extends ServiceEntityRepository
  17. {
  18.     public function __construct(ManagerRegistry $registry)
  19.     {
  20.         parent::__construct($registryContacts::class);
  21.     }
  22.     /**
  23.      * @throws ORMException
  24.      * @throws OptimisticLockException
  25.      */
  26.     public function add(Contacts $entitybool $flush true): void
  27.     {
  28.         $this->_em->persist($entity);
  29.         if ($flush) {
  30.             $this->_em->flush();
  31.         }
  32.     }
  33.     /**
  34.      * @throws ORMException
  35.      * @throws OptimisticLockException
  36.      */
  37.     public function remove(Contacts $entitybool $flush true): void
  38.     {
  39.         $this->_em->remove($entity);
  40.         if ($flush) {
  41.             $this->_em->flush();
  42.         }
  43.     }
  44.     protected function genericRequestOrder(QueryBuilder $queryBuilder)
  45.     {
  46.         $response $queryBuilder->select('c')
  47.             ->getQuery()
  48.             ->getResult();
  49.         return $response;
  50.     }
  51.     public function findContactTous($userSecurizer $securizer): array
  52.     {
  53.         $idClientUser $user->getIdClient();
  54.         if ($securizer->isGranted($user"ROLE_TECH")) {
  55.             $contacts $this->findAll();
  56.         } else if ($securizer->isGranted($user"ROLE_CLIENT")) {
  57.             $contacts $this->findBy(['idClient' => $idClientUser]);
  58.         } else {
  59.             $contacts = [$this->find($idClientUser)];
  60.         }
  61.         return $contacts;
  62.     }
  63.     public function findContactActif($userSecurizer $securizer): array
  64.     {
  65.         $idClientUser $user->getIdClient();
  66.         if ($securizer->isGranted($user"ROLE_TECH")) {
  67.             $contacts $this->findby(['archive' => false]);
  68.         } else {
  69.             $contacts $this->findBy(['idClient' => $idClientUser'archive' => false]);
  70.         }
  71.         return $contacts;
  72.     }
  73.     public function findContactArchiver($userSecurizer $securizer): array
  74.     {
  75.         $idClientUser $user->getIdClient();
  76.         if ($securizer->isGranted($user"ROLE_TECH")) {
  77.             $contacts $this->findby(['archive' => true]);
  78.         } else {
  79.             $contacts $this->findBy(['idClient' => $idClientUser'archive' => true]);
  80.         }
  81.         return $contacts;
  82.     }
  83.     public function globalSearch(String $search$userSecurizer $securizer): array
  84.     {
  85.         if ($securizer->isGranted($user"ROLE_TECH")) {
  86.             $queryBuilder = ($this->createQueryBuilder('c')
  87.                 ->where('c.nom LIKE :search')
  88.                 ->orWhere('c.prenom LIKE :search')
  89.                 ->andWhere('c.archive = false')
  90.                 ->setParameter('search''%' $search '%')
  91.             );
  92.         }
  93.         return $this->genericRequestOrder($queryBuilder);
  94.     }
  95. }