← Back
Editing: UserRepository.php
<?php namespace App\Repository; use App\Entity\Agency; use App\Entity\Organization; use App\Entity\Solution; use App\Entity\User; use App\Service\SolutionService; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\NoResultException; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\HttpFoundation\Request; /** * @method User|null find($id, $lockMode = null, $lockVersion = null) * @method User|null findOneBy(array $criteria, array $orderBy = null) * @method User[] findAll() * @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ class UserRepository extends ServiceEntityRepository { use OptionsTrait; private SolutionService $solutionService; /** * UserRepository constructor. */ public function __construct(ManagerRegistry $registry, SolutionService $solutionService) { $this->solutionService = $solutionService; parent::__construct($registry, User::class); } /** * @return User[] */ public function getAgents(): array { return $this->createQueryBuilder('u') ->where('u.roles LIKE :roles') ->setParameter('roles', '%ROLE_AGENT%') ->getQuery() ->getResult(); } public function getAgentsByManagerQueryBuilder(User $manager, ?Solution $solution = null): QueryBuilder { $qb = $this->createQueryBuilder('u') ->where('u.manager = :manager') ->setParameter('manager', $manager); if (null !== $solution) { $qb ->leftJoin('u.solutions', 's') ->andWhere('s = :solution') ->setParameter('solution', $solution); } return $qb; } public function getAgentsByAgencyQueryBuilder(Agency $agency, array $options = []): QueryBuilder { $qb = $this->createQueryBuilder('u') ->where('u.agency = :agency') ->andWhere('u.roles LIKE :roles') ->setParameter('agency', $agency) ->setParameter('roles', '%ROLE_AGENT%') ->orderBy('u.nom'); if (isset($options['withAddressOnly']) && true === $options['withAddressOnly']) { $qb->leftJoin('u.userAddresses', 'ua') ->groupBy('u') ->having('COUNT(ua.id) > 0'); } return $qb; } public function getAgentsByAgenciesQueryBuilder(array $agenciesId, array $options = []): QueryBuilder { $qb = $this->createQueryBuilder('u') ->where('u.agency IN ('.implode(',', $agenciesId).')') ->andWhere('u.roles LIKE :roles') ->setParameter('roles', '%ROLE_AGENT%') ->orderBy('u.nom'); if (isset($options['withAddressOnly']) && true === $options['withAddressOnly']) { $qb->leftJoin('u.userAddresses', 'ua') ->groupBy('u') ->having('COUNT(ua.id) > 0'); } return $qb; } /** * @throws NoResultException * @throws NonUniqueResultException */ public function countAll(): int { return $this->createQueryBuilder('u') ->select('count(u)') ->leftJoin('u.agency', 'c') ->leftJoin('c.solutions', 's') ->where('s = :solution') ->setParameter('solution', $this->solutionService->getCurrent()) ->getQuery() ->getSingleScalarResult(); } public function findManagersByAgencyQuery(Agency $agency): QueryBuilder { return $this->createQueryBuilder('u') ->where('u.agency = :agency') ->andWhere('u.roles LIKE :roleManager') ->setParameter('agency', $agency) ->setParameter('roleManager', '%ROLE_MANAGER%') ->orderBy('u.nom'); } /** * @return User[] */ public function findManagersByAgency(Agency $agency) { return $this->findManagersByAgencyQuery($agency)->getQuery()->getResult(); } public function findManagersByOrganizationQuery(Organization $organization): QueryBuilder { return $this->createQueryBuilder('u') ->where( 'u.agency IN ('.implode( ',', array_map(function (Agency $agency) { return $agency->getId(); }, $organization->getAgencies()->toArray()) ).')' ) ->andWhere('u.roles LIKE :roleManager') ->setParameter('roleManager', '%ROLE_MANAGER%') ->orderBy('u.nom'); } /** * @return User[] */ public function findManagersByOrganization(Organization $organization): array { return $this->findManagersByOrganizationQuery($organization)->getQuery()->getResult(); } // TODO Corriger les appels a cette methode pour recuperer user hors dashboard public function findAgentsByAgency(Request $request, ?int $agencyId) { $debutAnnee = new \DateTime('midnight first day of this year'); $finAnnee = new \DateTime('midnight last day of this year'); $query = $this->createQueryBuilder('u') ->select('u, f') ->addSelect('COUNT(c.id) as totalControles') ->addSelect('sum(case when c.dateRealisation is not null then 1 else 0 end) AS controlesValides') ->leftJoin('u.controles', 'c') ->andWhere('c.dateRealisation >= :startmonth or c is null') ->setParameter('startmonth', $debutAnnee) ->andWhere('c.dateRealisation < :endmonth or c is null') ->setParameter('endmonth', $finAnnee) ->leftJoin('u.frequenceControle', 'f') ->andWhere('u.agency = :agencyId') ->andWhere('u.roles LIKE :role') ->groupBy('u.id') ->setParameter('agencyId', $agencyId) ->setParameter('role', '%ROLE_AGENT%'); if ($request->get('s')) { $query->where('u.nom LIKE :nom') ->setParameter('nom', '%'.$request->get('s').'%'); } $parPage = 20; if ($request->get('p')) { if ($request->get('p') > 1) { $query->setFirstResult(($request->get('p') - 1) * $parPage); } } return $query->orderBy('u.nom', 'ASC')->setMaxResults($parPage)->getQuery()->getResult(); } public function exportAgentsAdmin() { $debutAnnee = new \DateTime('midnight first day of this year'); $finAnnee = new \DateTime('midnight last day of this year'); return $this->createQueryBuilder('u') ->select('u, f') ->addSelect('COUNT(c.id) as totalControles') ->addSelect('sum(case when c.dateRealisation is not null then 1 else 0 end) AS controlesValides') ->leftJoin('u.controles', 'c') ->leftJoin('u.frequenceControle', 'f') ->andWhere('u.roles LIKE :role') ->andWhere('c.dateRealisation >= :startmonth or c is null') ->andWhere('c.dateRealisation < :endmonth or c is null') ->setParameter('role', '%ROLE_AGENT%') ->setParameter('startmonth', $debutAnnee) ->setParameter('endmonth', $finAnnee) ->groupBy('u.id') ->orderBy('u.nom', 'ASC') ->getQuery() ->getResult(); } /** * @return User[] */ public function getExistingAgentsFromOtherSolutions(User $manager): array { $agents = []; $result = $this->createQueryBuilder('u') ->leftJoin('u.solutions', 's') ->where('s != :currentSolution') ->andWhere('u.manager = :manager') ->setParameter('currentSolution', $this->solutionService->getCurrent()) ->setParameter('manager', $manager) ->getQuery() ->getResult(); foreach ($result as $agent) { if (!$agent->hasSolution($this->solutionService->getCurrent())) { $agents[] = $agent; } } return $agents; } public function getByAgenciesByControl( array $agenciesId, string $role = 'ROLE_MANAGER', bool $previous = false, ?array $sort = null, ?int $limit = null, ?array $options = [] ): array { $qb = $this->createQueryBuilder('u'); $qb ->select('CONCAT(u.prenom, \' \', u.nom) as name') ->addSelect('u.id as id') ->addSelect('u.color as color') ->addSelect('COUNT(c.id) as cnt'); if ('ROLE_MANAGER' === $role) { $qb->leftJoin('u.agents', 'ag2'); $qb = $this->leftJoinControlsWithOptions($qb, 'ag2.controles'); } else { $qb = $this->leftJoinControlsWithOptions($qb, 'u.controles'); } $qb ->leftJoin('c.adresse', 'a') ->where('u.roles LIKE :roles') ->andWhere('u.agency IN ('.implode(',', $agenciesId).')') ->groupBy('u') ->setParameter('roles', '%'.$role.'%'); if (null !== $sort) { $qb->orderBy($sort[0], $sort[1]); } if (null !== $limit) { $qb->setMaxResults($limit); } $qb = $this->addOptions($qb, $previous, $options); return $qb->getQuery()->getResult(); } public function getManagersByAgenciesByMonth(array $agenciesId): array { $qb = $this->createQueryBuilder('u') ->select('CONCAT(u.prenom, \' \', u.nom) as name') ->addSelect('DATE_FORMAT(c.dateRealisation, \'%Y-%m\') as month') ->addSelect('u.id as id') ->addSelect('u.color as color') ->addSelect('COUNT(c.id) as cnt') ->leftJoin('u.agents', 'ag2'); $qb = $this->leftJoinControlsWithOptions($qb, 'ag2.controles'); $qb ->leftJoin('c.adresse', 'a') ->where('u.roles LIKE :roles') ->andWhere('u.agency IN ('.implode(',', $agenciesId).')') ->groupBy('month') ->addGroupBy('u') ->orderBy('month') ->setParameter('roles', '%ROLE_MANAGER%'); $qb = $this->addOptions($qb); return $qb->getQuery()->getResult(); } public function getAgentsByAgenciesByControlTimeQuery(array $agenciesId): QueryBuilder { $qb = $this->createQueryBuilder('u') ->addSelect('SEC_TO_TIME(AVG(TIME_TO_SEC(TIMEDIFF(c.dateRealisation, c.createdAt)))) as doneTime') ->leftJoin('u.controles', 'c') ->leftJoin('u.userAddresses', 'ua') ->leftJoin('ua.address', 'a') ->where('u.agency IN ('.implode(',', $agenciesId).')') ->andWhere('c.createdAt is not null') ->andWhere('c.dateRealisation is not null') ->andWhere('c.valide = :valid') ->orderBy('doneTime') ->groupBy('u') ->setParameter('valid', true); return $this->addOptions($qb); } public function findByRoleQuery(string $role): QueryBuilder { return $this->createQueryBuilder('u') ->andWhere('u.roles LIKE :role') ->orderBy('u.nom') ->setParameter('role', '%'.$role.'%'); } /** * Used to find user by role. */ public function findByRole(string $role) { return $this->findByRoleQuery($role) ->getQuery() ->getResult(); } public function getManagersWithoutColor() { return $this->createQueryBuilder('u') ->andWhere('u.roles LIKE :role') ->andWhere('u.color is NULL') ->setParameter('role', '%ROLE_MANAGER%') ->getQuery() ->getResult(); } public function countManagersWithColor() { return $this->createQueryBuilder('u') ->select('count(u.id)') ->andWhere('u.roles LIKE :role') ->andWhere('u.color is NOT NULL') ->setParameter('role', '%ROLE_MANAGER%') ->getQuery() ->getSingleScalarResult(); } }
Save File
Cancel