← Back
Editing: ReportItemRepository.php
<?php namespace App\Repository; use App\Entity\ReportItem; use App\Entity\User; use App\Enum\ReportType; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Form\FormInterface; /** * @extends ServiceEntityRepository<ReportItem> * * @method ReportItem|null find($id, $lockMode = null, $lockVersion = null) * @method ReportItem|null findOneBy(array $criteria, array $orderBy = null) * @method ReportItem[] findAll() * @method ReportItem[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ class ReportItemRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry, private readonly ReportRepository $reportRepository) { parent::__construct($registry, ReportItem::class); } public function save(ReportItem $entity, bool $flush = false): void { $this->getEntityManager()->persist($entity); if ($flush) { $this->getEntityManager()->flush(); } } public function remove(ReportItem $entity, bool $flush = false): void { $this->getEntityManager()->remove($entity); if ($flush) { $this->getEntityManager()->flush(); } } public function getReportItems(ReportType $reportType): array { $qb = $this->createQueryBuilder('ri') ->where('ri.type = :type') ->setParameter('type', $reportType); if (ReportType::INCIVILITY === $reportType) { $qb->andWhere('ri.parent IS NULL'); } $results = $qb->orderBy('ri.label', 'ASC')->getQuery()->getResult(); if (ReportType::INCIVILITY === $reportType) { $array = []; if (ReportType::INCIVILITY === $reportType) { foreach ($results as $result) { $array[$result->getLabel()] = $this->createQueryBuilder('ri') ->where('ri.type = :type') ->setParameter('type', $reportType) ->andWhere('ri.parent = :parent') ->setParameter('parent', $result) ->orderBy('ri.label', 'ASC')->getQuery()->getResult(); } } return $array; } return $results; } /** * @return ReportItem[] */ public function findParentReportItems(ReportType $reportType): array { return $this->createQueryBuilder('ri') ->where('ri.type = :reportType') ->andWhere('ri.parent is null') ->setParameter('reportType', $reportType) ->getQuery() ->getResult(); } public function countReportWithFilter( ReportType $reportType, FormInterface $filter, ?User $manager = null, ?User $supervisor = null ): array { $qb = $this->createQueryBuilder('ri') ->select('ri') ->addSelect('COUNT(r) as cnt') ->leftJoin('ri.reports', 'r') ->where('ri.type = :type') ->groupBy('ri') ->setParameter('type', $reportType); if (null !== $manager) { $qb->andWhere('r.manager = :manager') ->setParameter('manager', $manager); } if (null !== $supervisor) { $qb->andWhere('r.manager IN (:managers)') ->setParameter('managers', $supervisor->getAgency()->getManagers()); } $this->reportRepository->filterReports($qb, $filter); return $qb->getQuery()->getResult(); } public function countReportWithFilterByAgencies( ReportType $reportType, FormInterface $filter, array $agenciesId = [] ): array { $qb = $this->createQueryBuilder('ri') ->select('ri') ->addSelect('COUNT(r) as cnt') ->leftJoin('ri.reports', 'r') ->leftJoin('r.agent', 'a') ->where('ri.type = :type') ->groupBy('ri') ->setParameter('type', $reportType); if (!empty($agenciesId)) { $qb->andWhere('a.agency IN ('.implode(',', $agenciesId).')'); } $this->reportRepository->filterReports($qb, $filter); return $qb->getQuery()->getResult(); } }
Save File
Cancel