From 654a5a20196c99a5d0a19a6d097739772adbf0ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 27 Apr 2018 22:21:11 +0200 Subject: [PATCH] add counter for tasks --- Repository/SingleTaskRepository.php | 8 +-- Resources/config/services/templating.yml | 6 +++ Templating/UI/CountNotificationTask.php | 63 ++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Templating/UI/CountNotificationTask.php diff --git a/Repository/SingleTaskRepository.php b/Repository/SingleTaskRepository.php index 9f7d0f0c5..4c802d3d5 100644 --- a/Repository/SingleTaskRepository.php +++ b/Repository/SingleTaskRepository.php @@ -48,7 +48,7 @@ class SingleTaskRepository extends \Doctrine\ORM\EntityRepository * @param User $currentUser * @return int */ - public function countByParameters($params, User $currentUser) + public function countByParameters($params, User $currentUser = null) { $qb = $this->createQueryBuilder('st') ->select('COUNT(st)'); @@ -97,9 +97,11 @@ class SingleTaskRepository extends \Doctrine\ORM\EntityRepository ; } - protected function buildQuery(QueryBuilder $qb, $params, User $currentUser) + protected function buildQuery(QueryBuilder $qb, $params, User $currentUser = null) { - $this->buildACLQuery($qb, $currentUser); + if (NULL !== $currentUser) { + $this->buildACLQuery($qb, $currentUser); + } if (\array_key_exists('person', $params) and !empty($params['person'])) { $qb->andWhere($qb->expr()->eq('st.person', ':person')); diff --git a/Resources/config/services/templating.yml b/Resources/config/services/templating.yml index dca4e36f6..edbe0b1d0 100644 --- a/Resources/config/services/templating.yml +++ b/Resources/config/services/templating.yml @@ -4,3 +4,9 @@ services: $taskWorkflowManager: '@Chill\TaskBundle\Workflow\TaskWorkflowManager' tags: - { name: 'twig.extension' } + + Chill\TaskBundle\Templating\UI\CountNotificationTask: + arguments: + $singleTaskRepository: '@Chill\TaskBundle\Repository\SingleTaskRepository' + tags: + - { name: chill_main.count_notification.user } diff --git a/Templating/UI/CountNotificationTask.php b/Templating/UI/CountNotificationTask.php new file mode 100644 index 000000000..6ad2dd7f7 --- /dev/null +++ b/Templating/UI/CountNotificationTask.php @@ -0,0 +1,63 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +namespace Chill\TaskBundle\Templating\UI; + +use Chill\MainBundle\Templating\UI\NotificationCounterInterface; +use Chill\MainBundle\Entity\User; +use Chill\TaskBundle\Repository\SingleTaskRepository; + +/** + * + * + * @author Julien Fastré + */ +class CountNotificationTask implements NotificationCounterInterface +{ + /** + * + * @var SingleTaskRepository + */ + protected $singleTaskRepository; + + public function __construct(SingleTaskRepository $singleTaskRepository) + { + $this->singleTaskRepository = $singleTaskRepository; + } + + + public function addNotification(User $u): int + { + $params = [ + 'user' => $u, + 'is_closed' => false + ]; + + $sum = 0; + + foreach ([ + SingleTaskRepository::DATE_STATUS_ENDED, + SingleTaskRepository::DATE_STATUS_WARNING] as $status) { + + $sum += $this->singleTaskRepository->countByParameters( + \array_merge($params, [ 'date_status' => $status ]) + ); + } + + return $sum; + } +}