From 429d86f388da856c9d8d9a649147c5212bee4258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sun, 4 Sep 2016 20:53:28 +0200 Subject: Added tags counter in sidebar (material theme) --- .../Controller/WallabagRestController.php | 4 +- .../CoreBundle/Controller/TagController.php | 4 +- .../CoreBundle/Repository/EntryRepository.php | 21 -------- .../CoreBundle/Repository/TagRepository.php | 4 +- .../CoreBundle/Resources/config/services.yml | 2 - .../views/themes/material/layout.html.twig | 2 +- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 61 ++++++++++++++++++---- 7 files changed, 60 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 869fdc56..07fedeb0 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php @@ -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'); diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index 1cbc413d..bc95a4d3 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php @@ -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', diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 24d1a57a..719803a1 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -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; - } } diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index abf915fe..41f61607 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php @@ -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); } /** diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 1c1457e7..e95ef452 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -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 diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig index 06ecbf3d..b70198da 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig @@ -47,7 +47,7 @@ {{ 'menu.left.all_articles'|trans }} {{ count_entries('all') }}
  • - {{ 'menu.left.tags'|trans }} + {{ 'menu.left.tags'|trans }} {{ count_tags() }}
  • {{ 'menu.left.config'|trans }} diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 974b86a9..ed6dadc5 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -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'; -- cgit v1.2.3