diff --git a/DependencyInjection/ChillTaskExtension.php b/DependencyInjection/ChillTaskExtension.php
index 31dcd3d4a..acc4257a1 100644
--- a/DependencyInjection/ChillTaskExtension.php
+++ b/DependencyInjection/ChillTaskExtension.php
@@ -31,6 +31,7 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface
$loader->load('services/repositories.yml');
$loader->load('services/workflow.yml');
$loader->load('services/templating.yml');
+ $loader->load('services/menu.yml');
}
public function prepend(ContainerBuilder $container)
diff --git a/DependencyInjection/Compiler/TaskWorkflowDefinitionCompilerPass.php b/DependencyInjection/Compiler/TaskWorkflowDefinitionCompilerPass.php
index fb1a9e2aa..81d387922 100644
--- a/DependencyInjection/Compiler/TaskWorkflowDefinitionCompilerPass.php
+++ b/DependencyInjection/Compiler/TaskWorkflowDefinitionCompilerPass.php
@@ -21,6 +21,7 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Chill\TaskBundle\Workflow\TaskWorkflowManager;
use Symfony\Component\DependencyInjection\Reference;
+use Chill\TaskBundle\Templating\UI\CountNotificationTask;
/**
*
@@ -37,6 +38,7 @@ class TaskWorkflowDefinitionCompilerPass implements CompilerPassInterface
}
$workflowManagerDefinition = $container->getDefinition(TaskWorkflowManager::class);
+ $counterDefinition = $container->getDefinition(CountNotificationTask::class);
foreach ($container->findTaggedServiceIds('chill_task.workflow_definition') as $id => $tags) {
// registering the definition to manager
@@ -50,6 +52,12 @@ class TaskWorkflowDefinitionCompilerPass implements CompilerPassInterface
'method' => 'onTaskStateEntered',
'priority' => -255
]);
+ $counterDefinition
+ ->addTag('kernel.event_listener', [
+ 'event' => sprintf('workflow.%s.entered', $definition->getClass()::getAssociatedWorkflowName()),
+ 'method' => 'resetCacheOnNewStates',
+ 'priority' => 0
+ ]);
}
}
}
diff --git a/Menu/UserMenuBuilder.php b/Menu/UserMenuBuilder.php
new file mode 100644
index 000000000..6e0319b61
--- /dev/null
+++ b/Menu/UserMenuBuilder.php
@@ -0,0 +1,77 @@
+
+ *
+ * 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\Menu;
+
+use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
+use Knp\Menu\MenuItem;
+use Chill\TaskBundle\Templating\UI\CountNotificationTask;
+use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
+use Chill\TaskBundle\Repository\SingleTaskRepository;
+
+/**
+ *
+ *
+ * @author Julien Fastré
+ */
+class UserMenuBuilder implements LocalMenuBuilderInterface
+{
+
+ /**
+ *
+ * @var CountNotificationTask
+ */
+ public $counter;
+
+ /*
+ * @var TokenStorageInterface
+ */
+ public $tokenStorage;
+
+ public function __construct(
+ CountNotificationTask $counter,
+ TokenStorageInterface $tokenStorage
+ ) {
+ $this->counter = $counter;
+ $this->tokenStorage = $tokenStorage;
+ }
+
+ public static function getMenuIds(): array
+ {
+ return [ 'user' ];
+ }
+
+ public function buildMenu($menuId, MenuItem $menu, array $parameters)
+ {
+ if ($this->counter->countNotification($this->tokenStorage->getToken()
+ ->getUser()) > 0) {
+ $menu->addChild('Task near deadlines', [
+ 'route' => 'chill_task_singletask_list',
+ 'routeParameters' => [
+ 'user_id' => $this->tokenStorage
+ ->getToken()
+ ->getUser()
+ ->getId(),
+ 'status' => [
+ SingleTaskRepository::DATE_STATUS_WARNING,
+ SingleTaskRepository::DATE_STATUS_ENDED
+ ]
+ ]
+ ]);
+ }
+ }
+}
diff --git a/Resources/config/services/menu.yml b/Resources/config/services/menu.yml
new file mode 100644
index 000000000..7c15d2aae
--- /dev/null
+++ b/Resources/config/services/menu.yml
@@ -0,0 +1,7 @@
+services:
+ Chill\TaskBundle\Menu\UserMenuBuilder:
+ arguments:
+ $tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
+ $counter: '@Chill\TaskBundle\Templating\UI\CountNotificationTask'
+ tags:
+ - { name: 'chill.menu_builder' }
diff --git a/Resources/config/services/templating.yml b/Resources/config/services/templating.yml
index c6bc1b51e..401bef6d5 100644
--- a/Resources/config/services/templating.yml
+++ b/Resources/config/services/templating.yml
@@ -8,5 +8,6 @@ services:
Chill\TaskBundle\Templating\UI\CountNotificationTask:
arguments:
$singleTaskRepository: '@Chill\TaskBundle\Repository\SingleTaskRepository'
+ $cachePool: '@cache.user_data'
tags:
- { name: chill.count_notification.user }
diff --git a/Templating/UI/CountNotificationTask.php b/Templating/UI/CountNotificationTask.php
index 6ad2dd7f7..76a350ae2 100644
--- a/Templating/UI/CountNotificationTask.php
+++ b/Templating/UI/CountNotificationTask.php
@@ -20,6 +20,8 @@ namespace Chill\TaskBundle\Templating\UI;
use Chill\MainBundle\Templating\UI\NotificationCounterInterface;
use Chill\MainBundle\Entity\User;
use Chill\TaskBundle\Repository\SingleTaskRepository;
+use Psr\Cache\CacheItemPoolInterface;
+use Symfony\Component\Workflow\Event\Event;
/**
*
@@ -34,14 +36,30 @@ class CountNotificationTask implements NotificationCounterInterface
*/
protected $singleTaskRepository;
- public function __construct(SingleTaskRepository $singleTaskRepository)
- {
- $this->singleTaskRepository = $singleTaskRepository;
- }
-
+ /**
+ *
+ * @var CacheItempPoolInterface
+ */
+ protected $cachePool;
- public function addNotification(User $u): int
+ const CACHE_KEY = 'chill_task.count_notifications.user.%d';
+
+ public function __construct(
+ SingleTaskRepository $singleTaskRepository,
+ CacheItemPoolInterface $cachePool
+ ) {
+ $this->singleTaskRepository = $singleTaskRepository;
+ $this->cachePool = $cachePool;
+ }
+
+ public function countNotification(User $u): int
{
+ $sumCache = $this->cachePool->getItem($this->getCacheKey($u));
+
+ if ($sumCache->isHit()) {
+ return $sumCache->get();
+ }
+
$params = [
'user' => $u,
'is_closed' => false
@@ -58,6 +76,33 @@ class CountNotificationTask implements NotificationCounterInterface
);
}
+ $sumCache->set($sum);
+ $this->cachePool->save($sumCache);
+
return $sum;
}
+
+ public function addNotification(User $u): int
+ {
+ return $this->countNotification($u);
+ }
+
+ public function resetCacheOnNewStates(Event $e)
+ {
+ /* @var $task \Chill\TaskBundle\Entity\SingleTask */
+ $task = $e->getSubject();
+
+ if (NULL !== $task->getAssignee()) {
+ $sumCache = $this->cachePool->getItem($this->getCacheKey($task->getAssignee()));
+
+ if ($sumCache->isHit()) {
+ $this->cachePool->deleteItem($this->getCacheKey($task->getAssignee()));
+ }
+ }
+ }
+
+ private function getCacheKey(User $u)
+ {
+ return sprintf(self::CACHE_KEY, $u->getId());
+ }
}