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) --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 61 ++++++++++++++++++---- 1 file changed, 52 insertions(+), 9 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig') 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 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: Switched enableCache visibility to private --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/CoreBundle/Twig') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index ed6dadc5..59eb57ec 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); -- cgit v1.2.3 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: Returned 0 instead of returning empty array for Twig Extension --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 59eb57ec..3780b13e 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()); -- cgit v1.2.3