From 97d2e3d5b5231906aa4ebf61f11f7ecc62125702 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 1 Feb 2022 17:21:49 +0100 Subject: [PATCH] counter created for workflows for which user is a destuser and that are not finalized --- .../Workflow/EntityWorkflowStepRepository.php | 82 +++++++++++++++++++ .../Routing/MenuBuilder/UserMenuBuilder.php | 9 +- .../Counter/WorkflowByUserCounter.php | 41 ++++++++++ .../translations/messages+intl-icu.fr.yaml | 9 ++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowStepRepository.php create mode 100644 src/Bundle/ChillMainBundle/Workflow/Counter/WorkflowByUserCounter.php diff --git a/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowStepRepository.php b/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowStepRepository.php new file mode 100644 index 000000000..bf45d3888 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowStepRepository.php @@ -0,0 +1,82 @@ +repository = $entityManager->getRepository(EntityWorkflowStep::class); + } + + public function countUnreadByUser(User $user, ?array $orderBy = null, $limit = null, $offset = null): int + { + $qb = $this->buildQueryByUser($user)->select('count(e)'); + + return (int) $qb->getQuery()->getSingleScalarResult(); + } + + private function buildQueryByUser(User $user): QueryBuilder + { + $qb = $this->repository->createQueryBuilder('e'); + + $qb->where( + $qb->expr()->andX( + $qb->expr()->isMemberOf(':user', 'e.destUser'), + $qb->expr()->isNull('e.transitionAt'), + $qb->expr()->eq('e.isFinal', ':bool'), + ) + ); + + $qb->setParameter('user', $user); + $qb->setParameter('bool', false); + + dump($qb->getQuery()); + + return $qb; + } + + public function find($id): ?EntityWorkflowStep + { + return $this->repository->find($id); + } + + public function findAll(): array + { + return $this->repository->findAll(); + } + + public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array + { + return $this->repository->findBy($criteria, $orderBy, $limit, $offset); + } + + public function findOneBy(array $criteria): ?EntityWorkflowStep + { + return $this->repository->findOneBy($criteria); + } + + public function getClassName() + { + return EntityWorkflow::class; + } + +} \ No newline at end of file diff --git a/src/Bundle/ChillMainBundle/Routing/MenuBuilder/UserMenuBuilder.php b/src/Bundle/ChillMainBundle/Routing/MenuBuilder/UserMenuBuilder.php index 9b6d89e83..b2fd5180e 100644 --- a/src/Bundle/ChillMainBundle/Routing/MenuBuilder/UserMenuBuilder.php +++ b/src/Bundle/ChillMainBundle/Routing/MenuBuilder/UserMenuBuilder.php @@ -14,6 +14,7 @@ namespace Chill\MainBundle\Routing\MenuBuilder; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Notification\Counter\NotificationByUserCounter; use Chill\MainBundle\Routing\LocalMenuBuilderInterface; +use Chill\MainBundle\Workflow\Counter\WorkflowByUserCounter; use Symfony\Component\Security\Core\Security; use Symfony\Contracts\Translation\TranslatorInterface; @@ -25,12 +26,16 @@ class UserMenuBuilder implements LocalMenuBuilderInterface private TranslatorInterface $translator; + private WorkflowByUserCounter $workflowByUserCounter; + public function __construct( NotificationByUserCounter $notificationByUserCounter, + WorkflowByUserCounter $workflowByUserCounter, Security $security, TranslatorInterface $translator ) { $this->notificationByUserCounter = $notificationByUserCounter; + $this->workflowByUserCounter = $workflowByUserCounter; $this->security = $security; $this->translator = $translator; } @@ -69,9 +74,11 @@ class UserMenuBuilder implements LocalMenuBuilderInterface 'counter' => $nbNotifications, ]); + $workflowCount = $this->workflowByUserCounter->getCountUnreadByUser($user); + $menu ->addChild( - $this->translator->trans('workflow.My workflows'), + $this->translator->trans('workflow.My workflows with counter', ['wc' => $workflowCount]), ['route' => 'chill_main_workflow_list_dest'] ) ->setExtras([ diff --git a/src/Bundle/ChillMainBundle/Workflow/Counter/WorkflowByUserCounter.php b/src/Bundle/ChillMainBundle/Workflow/Counter/WorkflowByUserCounter.php new file mode 100644 index 000000000..7c1717d52 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Workflow/Counter/WorkflowByUserCounter.php @@ -0,0 +1,41 @@ +workflowStepRepository = $workflowStepRepository; + } + + public function addNotification(UserInterface $u): int + { + if (!$u instanceof User) { + return 0; + } + + return $this->getCountUnreadByUser($u); + } + + public function getCountUnreadByUser(User $user): int + { + return $this->workflowStepRepository->countUnreadByUser($user); + } +} diff --git a/src/Bundle/ChillMainBundle/translations/messages+intl-icu.fr.yaml b/src/Bundle/ChillMainBundle/translations/messages+intl-icu.fr.yaml index 7a5b328f4..9ec3644ef 100644 --- a/src/Bundle/ChillMainBundle/translations/messages+intl-icu.fr.yaml +++ b/src/Bundle/ChillMainBundle/translations/messages+intl-icu.fr.yaml @@ -29,3 +29,12 @@ notification: few {# non-lues} other {# non-lues} } + +workflow: + My workflows with counter: >- + {wc, plural, + =0 {Mes workflows} + one {Une workflow} + few {# workflows} + other {# workflows} + }