Merge pull request #2266 from wallabag/add-tags-counter

Added tags counter in sidebar (material theme)
This commit is contained in:
Jeremy Benoist 2016-09-05 22:20:27 +02:00 committed by GitHub
commit 6f23289e72
8 changed files with 63 additions and 41 deletions

View File

@ -18,7 +18,9 @@ services:
public: false
arguments:
- "@wallabag_core.entry_repository"
- "@wallabag_core.tag_repository"
- "@security.token_storage"
- "%wallabag_core.cache_lifetime%"
tags:
- { name: twig.extension }

View File

@ -322,7 +322,9 @@ class WallabagRestController extends FOSRestController
$tags = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Tag')
->findAllTags($this->getUser()->getId());
->findAllTags($this->getUser()->getId())
->getQuery()
->getResult();
$json = $this->get('serializer')->serialize($tags, 'json');

View File

@ -84,7 +84,9 @@ class TagController extends Controller
{
$tags = $this->getDoctrine()
->getRepository('WallabagCoreBundle:Tag')
->findAllTags($this->getUser()->getId());
->findAllTags($this->getUser()->getId())
->getQuery()
->getResult();
return $this->render(
'WallabagCoreBundle:Tag:tags.html.twig',

View File

@ -10,8 +10,6 @@ use Wallabag\CoreBundle\Entity\Tag;
class EntryRepository extends EntityRepository
{
private $lifeTime;
/**
* Return a query builder to used by other getBuilderFor* method.
*
@ -311,25 +309,4 @@ class EntryRepository extends EntityRepository
return $qb->getQuery()->getSingleScalarResult();
}
public function setLifeTime($lifeTime)
{
$this->lifeTime = $lifeTime;
}
/**
* Enable cache for a query.
*
* @param Query $query
*
* @return Query
*/
public function enableCache(Query $query)
{
$query->useQueryCache(true);
$query->useResultCache(true);
$query->setResultCacheLifetime($this->lifeTime);
return $query;
}
}

View File

@ -17,9 +17,7 @@ class TagRepository extends EntityRepository
{
return $this->createQueryBuilder('t')
->leftJoin('t.entries', 'e')
->where('e.user = :userId')->setParameter('userId', $userId)
->getQuery()
->getResult();
->where('e.user = :userId')->setParameter('userId', $userId);
}
/**

View File

@ -81,8 +81,6 @@ services:
factory: [ "@doctrine.orm.default_entity_manager", getRepository ]
arguments:
- WallabagCoreBundle:Entry
calls:
- [ setLifeTime, [ "%wallabag_core.cache_lifetime%" ] ]
wallabag_core.tag_repository:
class: Wallabag\CoreBundle\Repository\TagRepository

View File

@ -47,7 +47,7 @@
<a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ count_entries('all') }}</span></a>
</li>
<li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}">
<a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a>
<a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }} <span class="numberItems grey-text">{{ count_tags() }}</span></a>
</li>
<li class="bold {% if currentRoute == 'config' %}active{% endif %}">
<a class="waves-effect" href="{{ path('config') }}">{{ 'menu.left.config'|trans }}</a>

View File

@ -2,18 +2,24 @@
namespace Wallabag\CoreBundle\Twig;
use Doctrine\ORM\Query;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Wallabag\CoreBundle\Repository\EntryRepository;
use Wallabag\CoreBundle\Repository\TagRepository;
class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
private $tokenStorage;
private $repository;
private $entryRepository;
private $tagRepository;
private $lifeTime;
public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null)
public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0)
{
$this->repository = $repository;
$this->entryRepository = $entryRepository;
$this->tagRepository = $tagRepository;
$this->tokenStorage = $tokenStorage;
$this->lifeTime = $lifeTime;
}
public function getFilters()
@ -27,6 +33,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
{
return array(
new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
);
}
@ -47,24 +54,24 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
if (null === $user || !is_object($user)) {
return [];
return 0;
}
switch ($type) {
case 'starred':
$qb = $this->repository->getBuilderForStarredByUser($user->getId());
$qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
break;
case 'archive':
$qb = $this->repository->getBuilderForArchiveByUser($user->getId());
$qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
break;
case 'unread':
$qb = $this->repository->getBuilderForUnreadByUser($user->getId());
$qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
break;
case 'all':
$qb = $this->repository->getBuilderForAllByUser($user->getId());
$qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
break;
default:
@ -78,13 +85,49 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
->groupBy('e.id')
->getQuery();
$data = $this->repository
->enableCache($query)
$data = $this->enableCache($query)
->getArrayResult();
return count($data);
}
/**
* Return number of tags.
*
* @return int
*/
public function countTags()
{
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
if (null === $user || !is_object($user)) {
return 0;
}
$qb = $this->tagRepository->findAllTags($user->getId());
$data = $this->enableCache($qb->getQuery())
->getArrayResult();
return count($data);
}
/**
* Enable cache for a query.
*
* @param Query $query
*
* @return Query
*/
private function enableCache(Query $query)
{
$query->useQueryCache(true);
$query->useResultCache(true);
$query->setResultCacheLifetime($this->lifeTime);
return $query;
}
public function getName()
{
return 'wallabag_extension';