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/WallabagExtension.php') 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