Added tags counter in sidebar (material theme)

This commit is contained in:
Nicolas Lœuillet 2016-09-04 20:53:28 +02:00
parent c3b53188d7
commit 429d86f388
No known key found for this signature in database
GPG key ID: 5656BE27E1E34D0A
8 changed files with 62 additions and 38 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

@ -311,25 +311,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']),
);
}
@ -52,19 +59,19 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
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 [];
}
$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
*/
public function enableCache(Query $query)
{
$query->useQueryCache(true);
$query->useResultCache(true);
$query->setResultCacheLifetime($this->lifeTime);
return $query;
}
public function getName()
{
return 'wallabag_extension';