diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-09-04 20:53:28 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-09-04 20:53:28 +0200 |
commit | 429d86f388da856c9d8d9a649147c5212bee4258 (patch) | |
tree | 2dde16adfb069d26209a0c01c92a04eb9b502e54 /src/Wallabag/CoreBundle | |
parent | c3b53188d75a01e92f2be7204fc961a7636a1827 (diff) | |
download | wallabag-429d86f388da856c9d8d9a649147c5212bee4258.tar.gz wallabag-429d86f388da856c9d8d9a649147c5212bee4258.tar.zst wallabag-429d86f388da856c9d8d9a649147c5212bee4258.zip |
Added tags counter in sidebar (material theme)
Diffstat (limited to 'src/Wallabag/CoreBundle')
6 files changed, 57 insertions, 37 deletions
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 | |||
84 | { | 84 | { |
85 | $tags = $this->getDoctrine() | 85 | $tags = $this->getDoctrine() |
86 | ->getRepository('WallabagCoreBundle:Tag') | 86 | ->getRepository('WallabagCoreBundle:Tag') |
87 | ->findAllTags($this->getUser()->getId()); | 87 | ->findAllTags($this->getUser()->getId()) |
88 | ->getQuery() | ||
89 | ->getResult(); | ||
88 | 90 | ||
89 | return $this->render( | 91 | return $this->render( |
90 | 'WallabagCoreBundle:Tag:tags.html.twig', | 92 | '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 | |||
311 | 311 | ||
312 | return $qb->getQuery()->getSingleScalarResult(); | 312 | return $qb->getQuery()->getSingleScalarResult(); |
313 | } | 313 | } |
314 | |||
315 | public function setLifeTime($lifeTime) | ||
316 | { | ||
317 | $this->lifeTime = $lifeTime; | ||
318 | } | ||
319 | |||
320 | /** | ||
321 | * Enable cache for a query. | ||
322 | * | ||
323 | * @param Query $query | ||
324 | * | ||
325 | * @return Query | ||
326 | */ | ||
327 | public function enableCache(Query $query) | ||
328 | { | ||
329 | $query->useQueryCache(true); | ||
330 | $query->useResultCache(true); | ||
331 | $query->setResultCacheLifetime($this->lifeTime); | ||
332 | |||
333 | return $query; | ||
334 | } | ||
335 | } | 314 | } |
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 | |||
17 | { | 17 | { |
18 | return $this->createQueryBuilder('t') | 18 | return $this->createQueryBuilder('t') |
19 | ->leftJoin('t.entries', 'e') | 19 | ->leftJoin('t.entries', 'e') |
20 | ->where('e.user = :userId')->setParameter('userId', $userId) | 20 | ->where('e.user = :userId')->setParameter('userId', $userId); |
21 | ->getQuery() | ||
22 | ->getResult(); | ||
23 | } | 21 | } |
24 | 22 | ||
25 | /** | 23 | /** |
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: | |||
81 | factory: [ "@doctrine.orm.default_entity_manager", getRepository ] | 81 | factory: [ "@doctrine.orm.default_entity_manager", getRepository ] |
82 | arguments: | 82 | arguments: |
83 | - WallabagCoreBundle:Entry | 83 | - WallabagCoreBundle:Entry |
84 | calls: | ||
85 | - [ setLifeTime, [ "%wallabag_core.cache_lifetime%" ] ] | ||
86 | 84 | ||
87 | wallabag_core.tag_repository: | 85 | wallabag_core.tag_repository: |
88 | class: Wallabag\CoreBundle\Repository\TagRepository | 86 | 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 @@ | |||
47 | <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ count_entries('all') }}</span></a> | 47 | <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ count_entries('all') }}</span></a> |
48 | </li> | 48 | </li> |
49 | <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}"> | 49 | <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}"> |
50 | <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a> | 50 | <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }} <span class="numberItems grey-text">{{ count_tags() }}</span></a> |
51 | </li> | 51 | </li> |
52 | <li class="bold {% if currentRoute == 'config' %}active{% endif %}"> | 52 | <li class="bold {% if currentRoute == 'config' %}active{% endif %}"> |
53 | <a class="waves-effect" href="{{ path('config') }}">{{ 'menu.left.config'|trans }}</a> | 53 | <a class="waves-effect" href="{{ path('config') }}">{{ 'menu.left.config'|trans }}</a> |
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 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Twig; | 3 | namespace Wallabag\CoreBundle\Twig; |
4 | 4 | ||
5 | use Doctrine\ORM\Query; | ||
5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | 6 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
6 | use Wallabag\CoreBundle\Repository\EntryRepository; | 7 | use Wallabag\CoreBundle\Repository\EntryRepository; |
8 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
7 | 9 | ||
8 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface | 10 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface |
9 | { | 11 | { |
10 | private $tokenStorage; | 12 | private $tokenStorage; |
11 | private $repository; | 13 | private $entryRepository; |
14 | private $tagRepository; | ||
15 | private $lifeTime; | ||
12 | 16 | ||
13 | public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null) | 17 | public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0) |
14 | { | 18 | { |
15 | $this->repository = $repository; | 19 | $this->entryRepository = $entryRepository; |
20 | $this->tagRepository = $tagRepository; | ||
16 | $this->tokenStorage = $tokenStorage; | 21 | $this->tokenStorage = $tokenStorage; |
22 | $this->lifeTime = $lifeTime; | ||
17 | } | 23 | } |
18 | 24 | ||
19 | public function getFilters() | 25 | public function getFilters() |
@@ -27,6 +33,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
27 | { | 33 | { |
28 | return array( | 34 | return array( |
29 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), | 35 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), |
36 | new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), | ||
30 | ); | 37 | ); |
31 | } | 38 | } |
32 | 39 | ||
@@ -52,19 +59,19 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
52 | 59 | ||
53 | switch ($type) { | 60 | switch ($type) { |
54 | case 'starred': | 61 | case 'starred': |
55 | $qb = $this->repository->getBuilderForStarredByUser($user->getId()); | 62 | $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); |
56 | break; | 63 | break; |
57 | 64 | ||
58 | case 'archive': | 65 | case 'archive': |
59 | $qb = $this->repository->getBuilderForArchiveByUser($user->getId()); | 66 | $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); |
60 | break; | 67 | break; |
61 | 68 | ||
62 | case 'unread': | 69 | case 'unread': |
63 | $qb = $this->repository->getBuilderForUnreadByUser($user->getId()); | 70 | $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); |
64 | break; | 71 | break; |
65 | 72 | ||
66 | case 'all': | 73 | case 'all': |
67 | $qb = $this->repository->getBuilderForAllByUser($user->getId()); | 74 | $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); |
68 | break; | 75 | break; |
69 | 76 | ||
70 | default: | 77 | default: |
@@ -78,13 +85,49 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
78 | ->groupBy('e.id') | 85 | ->groupBy('e.id') |
79 | ->getQuery(); | 86 | ->getQuery(); |
80 | 87 | ||
81 | $data = $this->repository | 88 | $data = $this->enableCache($query) |
82 | ->enableCache($query) | ||
83 | ->getArrayResult(); | 89 | ->getArrayResult(); |
84 | 90 | ||
85 | return count($data); | 91 | return count($data); |
86 | } | 92 | } |
87 | 93 | ||
94 | /** | ||
95 | * Return number of tags. | ||
96 | * | ||
97 | * @return int | ||
98 | */ | ||
99 | public function countTags() | ||
100 | { | ||
101 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | ||
102 | |||
103 | if (null === $user || !is_object($user)) { | ||
104 | return []; | ||
105 | } | ||
106 | |||
107 | $qb = $this->tagRepository->findAllTags($user->getId()); | ||
108 | |||
109 | $data = $this->enableCache($qb->getQuery()) | ||
110 | ->getArrayResult(); | ||
111 | |||
112 | return count($data); | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Enable cache for a query. | ||
117 | * | ||
118 | * @param Query $query | ||
119 | * | ||
120 | * @return Query | ||
121 | */ | ||
122 | public function enableCache(Query $query) | ||
123 | { | ||
124 | $query->useQueryCache(true); | ||
125 | $query->useResultCache(true); | ||
126 | $query->setResultCacheLifetime($this->lifeTime); | ||
127 | |||
128 | return $query; | ||
129 | } | ||
130 | |||
88 | public function getName() | 131 | public function getName() |
89 | { | 132 | { |
90 | return 'wallabag_extension'; | 133 | return 'wallabag_extension'; |