From fa19ef21801d76ee84b8d853f66dab7e90dacfa5 Mon Sep 17 00:00:00 2001 From: kayliz Date: Wed, 9 Aug 2023 19:58:27 +0200 Subject: [PATCH] update contact repository --- src/Controller/DashboardController.php | 4 +- src/Controller/KontaktController.php | 9 +-- src/Repository/KontakteRepository.php | 103 ++++++++++++++++--------- src/Repository/PoliciesRepository.php | 2 +- src/Repository/TomRepository.php | 2 +- src/Service/InheritanceService.php | 10 +-- templates/kontakt/edit.html.twig | 4 + translations/kontakt/kontakt.de.yaml | 1 + 8 files changed, 83 insertions(+), 52 deletions(-) diff --git a/src/Controller/DashboardController.php b/src/Controller/DashboardController.php index 0ccd8e02..586c65c1 100644 --- a/src/Controller/DashboardController.php +++ b/src/Controller/DashboardController.php @@ -86,7 +86,7 @@ public function dashboard(Request $request, $av = $transferRepository->findActiveOrderProcessingsByTeamPath($teamPath); $processes = $processRepository->findActiveByTeam($currentTeam); $vvtDsfa = $impactAssessmentRepository->findActiveByTeam($currentTeam); - $kontakte = $contactRepository->findActiveByTeamPath($teamPath); + $contacts = $contactRepository->findActiveByTeam($currentTeam); $tom = $tomRepository->findActiveByTeam($currentTeam); $forms = $formRepository->findPublicByTeam($currentTeam); $policies = $policyRepository->findPublicByTeam($currentTeam); @@ -111,7 +111,7 @@ public function dashboard(Request $request, 'daten' => $daten, 'vvt' => $processes, 'dsfa' => $vvtDsfa, - 'kontakte' => $kontakte, + 'kontakte' => $contacts, 'kAudit' => $kritischeAudits, 'kVvt' => $criticalProcesses, 'openDsfa' => $openDsfa, diff --git a/src/Controller/KontaktController.php b/src/Controller/KontaktController.php index 2e4b1ac7..21f1d6d2 100644 --- a/src/Controller/KontaktController.php +++ b/src/Controller/KontaktController.php @@ -156,9 +156,10 @@ public function editKontakt( 'form' => $form->createView(), 'kontakt' => $kontakt, 'errors' => $errors, - 'title' => $this->translator->trans(id: 'contact.create', domain: 'kontakt'), + 'title' => $this->translator->trans(id: 'contact.edit', domain: 'kontakt'), 'snack' => $request->get('snack'), 'isEditable' => $isEditable, + 'currentTeam' => $team, ]); } @@ -167,7 +168,6 @@ public function index( SecurityService $securityService, CurrentTeamService $currentTeamService, KontakteRepository $contactRepository, - TeamRepository $teamRepository, ): Response { $team = $currentTeamService->getCurrentTeam($this->getUser()); @@ -175,11 +175,10 @@ public function index( return $this->redirectToRoute('dashboard'); } - $teamPath = $teamRepository->getPath($team); - $kontakte = $contactRepository->findActiveByTeamPath($teamPath); + $contacts = $contactRepository->findAllByTeam($team); return $this->render('kontakt/index.html.twig', [ - 'kontakte' => $kontakte, + 'kontakte' => $contacts, 'title' => $this->translator->trans(id: 'contact', domain: 'general'), 'currentTeam' => $team, ]); diff --git a/src/Repository/KontakteRepository.php b/src/Repository/KontakteRepository.php index 6f9b2191..d03c144c 100644 --- a/src/Repository/KontakteRepository.php +++ b/src/Repository/KontakteRepository.php @@ -3,7 +3,9 @@ namespace App\Repository; use App\Entity\Kontakte; +use App\Entity\Team; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; /** @@ -14,59 +16,84 @@ */ class KontakteRepository extends ServiceEntityRepository { - public function __construct(ManagerRegistry $registry) + public function __construct( + ManagerRegistry $registry, + private readonly TeamRepository $teamRepository, + ) { parent::__construct($registry, Kontakte::class); } - // /** - // * @return Kontakte[] Returns an array of Kontakte objects - // */ - /* - public function findByExampleField($value) + public function findActiveByTeam(Team $team) { - return $this->createQueryBuilder('k') - ->andWhere('k.exampleField = :val') - ->setParameter('val', $value) - ->orderBy('k.id', 'ASC') - ->setMaxResults(10) - ->getQuery() - ->getResult() - ; + $queryBuilder = $this->getBaseQueryBuilder(); + $this->filterByTeam(queryBuilder: $queryBuilder, team: $team); + return $queryBuilder->getQuery()->getResult(); } - */ - /* - public function findOneBySomeField($value): ?Kontakte + public function findAllByTeam(Team $team) { - return $this->createQueryBuilder('k') - ->andWhere('k.exampleField = :val') - ->setParameter('val', $value) - ->getQuery() - ->getOneOrNullResult() - ; + $queryBuilder = $this->getBaseQueryBuilder(); + $this->filterByTeam(queryBuilder: $queryBuilder, team: $team, all: true); + return $queryBuilder->getQuery()->getResult(); + } + + public function findIsInheritedById(string $id) : bool + { + $queryBuilder = $this->getBaseQueryBuilder(); + $this->filterById(queryBuilder: $queryBuilder, id: $id); + $this->filterByInherited(queryBuilder: $queryBuilder); + $result = $queryBuilder->getQuery()->getResult(); + return count($result) > 0; } - */ - public function findActiveByTeam($value) + public function findIsUsedByTeamAndId(Team $team, string $id) : bool { - return $this->createQueryBuilder('a') - ->andWhere('a.team = :val') - ->andWhere('a.activ = 1') - ->setParameter('val', $value) - ->getQuery() - ->getResult() + $queryBuilder = $this->getBaseQueryBuilder(); + $this->filterByTeam(queryBuilder: $queryBuilder, team: $team); + $this->filterById(queryBuilder: $queryBuilder, id: $id); + $result = $queryBuilder->getQuery()->getResult(); + return count($result) > 0; + } + + private function getBaseQueryBuilder() :QueryBuilder + { + return $this->createQueryBuilder('c') + ->leftJoin('c.datenweitergaben', 'dw') + ->leftJoin('dw.verfahren', 'process') + ->andWhere('c.activ = 1') ; } - public function findActiveByTeamPath(array $teamPath) + private function filterByTeam(QueryBuilder $queryBuilder, Team $team, bool $all = false) :void { - return $this->createQueryBuilder('a') - ->andWhere('a.team IN (:teamPath)') - ->andWhere('a.activ = 1') + $teamPath = $this->teamRepository->getPath($team); + $queryBuilder + ->andWhere('c.team = :team OR (process.activ = 1 AND process.inherited = 1 AND process.team IN (:teamPath))') ->setParameter('teamPath', $teamPath) - ->getQuery() - ->getResult() - ; + ->setParameter('team', $team) + ; + + if (!$all) { + $ignored = $team->getIgnoredInheritances(); + if (count($ignored)) { + $queryBuilder + ->andWhere('process NOT IN (:ignored) OR c.team = :team') + ->setParameter('ignored', $ignored); + } + } + } + + private function filterById(QueryBuilder $queryBuilder, string $id) :void + { + $queryBuilder + ->andWhere('c.id = :id') + ->setParameter('id', $id); + } + + private function filterByInherited(QueryBuilder $queryBuilder) :void + { + $queryBuilder + ->andWhere('process.activ = 1 AND process.inherited = 1'); } } diff --git a/src/Repository/PoliciesRepository.php b/src/Repository/PoliciesRepository.php index d50f8f45..a6d701a8 100644 --- a/src/Repository/PoliciesRepository.php +++ b/src/Repository/PoliciesRepository.php @@ -72,7 +72,7 @@ private function getBaseQueryBuilder(Team $team) :QueryBuilder return $this->createQueryBuilder('a') ->innerJoin('a.processes', 'process') - ->andWhere('a.team = :team OR (process.team = :team OR process.inherited = 1) AND process.activ = 1 AND process.team IN (:teamPath)') + ->andWhere('a.team = :team OR process.inherited = 1 AND process.activ = 1 AND process.team IN (:teamPath)') ->andWhere('a.activ = 1') ->setParameter('teamPath', $teamPath) ->setParameter('team', $team) diff --git a/src/Repository/TomRepository.php b/src/Repository/TomRepository.php index 5b981bb4..f018b347 100644 --- a/src/Repository/TomRepository.php +++ b/src/Repository/TomRepository.php @@ -53,7 +53,7 @@ private function getBaseQueryBuilder(Team $team) :QueryBuilder return $this->createQueryBuilder('a') ->innerJoin('a.vvts', 'process') - ->andWhere('a.team = :team OR (process.team = :team OR process.inherited = 1) AND process.activ = 1 AND process.team IN (:teamPath)') + ->andWhere('a.team = :team OR process.inherited = 1 AND process.activ = 1 AND process.team IN (:teamPath)') ->andWhere('a.activ = 1') ->setParameter('teamPath', $teamPath) ->setParameter('team', $team) diff --git a/src/Service/InheritanceService.php b/src/Service/InheritanceService.php index b7c71991..071729c2 100644 --- a/src/Service/InheritanceService.php +++ b/src/Service/InheritanceService.php @@ -7,13 +7,15 @@ use App\Entity\Software; use App\Entity\Team; use App\Entity\Tom; +use App\Repository\KontakteRepository; use App\Repository\SoftwareRepository; use Doctrine\Common\Collections\Collection; class InheritanceService { public function __construct( - private readonly SoftwareRepository $softwareRepository + private readonly SoftwareRepository $softwareRepository, + private readonly KontakteRepository $contactRepository, ) { @@ -51,14 +53,12 @@ public function checkTeamUsesPolicy(Team $team, Policies $policy): bool public function checkContactIsInherited(Kontakte $contact): bool { - // TODO: implement - return true; + return $this->contactRepository->findIsInheritedById($contact->getId()); } public function checkTeamUsesContact(Team $team, Kontakte $contact): bool { - // TODO: implement - return true; + return $this->contactRepository->findIsUsedByTeamAndId($team, $contact->getId()); } public function checkTransferIsInherited(Datenweitergabe $transfer): bool diff --git a/templates/kontakt/edit.html.twig b/templates/kontakt/edit.html.twig index 6fd82596..4bcb0977 100644 --- a/templates/kontakt/edit.html.twig +++ b/templates/kontakt/edit.html.twig @@ -9,6 +9,10 @@

{{ title }}

{{ include('base/__approvedBy.html.twig', {'data':kontakt}) }} + {% set inherited = contactInherited(kontakt) %} + {% set used = teamUsesContact(currentTeam, kontakt) %} + {% include 'base/__inheritanceInfo.html.twig' with {team:kontakt.team, currentTeam:currentTeam, used:used, inherited:inherited} %} + {% include 'base/__flashMessage.html.twig' with {app:app} %}
{{ form_start(form) }} diff --git a/translations/kontakt/kontakt.de.yaml b/translations/kontakt/kontakt.de.yaml index 8cb36583..7fff2cbc 100644 --- a/translations/kontakt/kontakt.de.yaml +++ b/translations/kontakt/kontakt.de.yaml @@ -6,3 +6,4 @@ client: Auftraggeber contractor: Auftragnehmer contact: create: Kontakt erstellen + edit: Kontakt bearbeiten