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: [PATCH 1/4] Added tags counter in sidebar (material theme) --- app/config/services.yml | 2 + .../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 +- .../CoreBundle/Twig/WallabagExtension.php | 61 ++++++++++++++++--- 8 files changed, 62 insertions(+), 38 deletions(-) diff --git a/app/config/services.yml b/app/config/services.yml index 95b8f26f5..76bbce27c 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -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 } diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 869fdc56a..07fedeb01 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 1cbc413d6..bc95a4d34 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 24d1a57af..719803a18 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 abf915fe5..41f616079 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 1c1457e70..e95ef4520 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 06ecbf3d2..b70198da6 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 974b86a97..ed6dadc5a 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'; From 13d9f7c96c131b93c1172d8bdc69098da9b68f68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sun, 4 Sep 2016 21:06:52 +0200 Subject: [PATCH 2/4] Switched enableCache visibility to private --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index ed6dadc5a..59eb57ec5 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -119,7 +119,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa * * @return Query */ - public function enableCache(Query $query) + private function enableCache(Query $query) { $query->useQueryCache(true); $query->useResultCache(true); From a1d22ea7d0cc8c51ca5480aa41488f2c6373f1a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 5 Sep 2016 10:23:40 +0200 Subject: [PATCH 3/4] Remove useless variable --- src/Wallabag/CoreBundle/Repository/EntryRepository.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 719803a18..302e5a538 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -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. * From 5173fd1c3d05d1fadfbdd4db862cd744209e13ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 5 Sep 2016 14:17:44 +0200 Subject: [PATCH 4/4] Returned 0 instead of returning empty array for Twig Extension --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 59eb57ec5..3780b13e7 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -54,7 +54,7 @@ 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) { @@ -101,7 +101,7 @@ 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; } $qb = $this->tagRepository->findAllTags($user->getId());