add link when task are near deadline in user menu

This commit is contained in:
Julien Fastré 2018-04-30 17:37:20 +02:00
parent 399f99ebfc
commit 63168818ec
6 changed files with 145 additions and 6 deletions

View File

@ -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)

View File

@ -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
]);
}
}
}

77
Menu/UserMenuBuilder.php Normal file
View File

@ -0,0 +1,77 @@
<?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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é <julien.fastre@champs-libres.coop>
*/
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
]
]
]);
}
}
}

View File

@ -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' }

View File

@ -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 }

View File

@ -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());
}
}