diff options
author | Jeremy Benoist <j0k3r@users.noreply.github.com> | 2016-09-05 22:20:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-05 22:20:27 +0200 |
commit | 6f23289e721bd14710af1acc23466432c1312850 (patch) | |
tree | 58ce6aa0cc70d0a101f3233ca2258592e6d08515 | |
parent | e5b090960bed3eb4a70c29c4acbae2027e17f7d6 (diff) | |
parent | 5173fd1c3d05d1fadfbdd4db862cd744209e13ac (diff) | |
download | wallabag-6f23289e721bd14710af1acc23466432c1312850.tar.gz wallabag-6f23289e721bd14710af1acc23466432c1312850.tar.zst wallabag-6f23289e721bd14710af1acc23466432c1312850.zip |
Merge pull request #2266 from wallabag/add-tags-counter
Added tags counter in sidebar (material theme)
8 files changed, 63 insertions, 41 deletions
diff --git a/app/config/services.yml b/app/config/services.yml index 95b8f26f..76bbce27 100644 --- a/app/config/services.yml +++ b/app/config/services.yml | |||
@@ -18,7 +18,9 @@ services: | |||
18 | public: false | 18 | public: false |
19 | arguments: | 19 | arguments: |
20 | - "@wallabag_core.entry_repository" | 20 | - "@wallabag_core.entry_repository" |
21 | - "@wallabag_core.tag_repository" | ||
21 | - "@security.token_storage" | 22 | - "@security.token_storage" |
23 | - "%wallabag_core.cache_lifetime%" | ||
22 | tags: | 24 | tags: |
23 | - { name: twig.extension } | 25 | - { name: twig.extension } |
24 | 26 | ||
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 | |||
322 | 322 | ||
323 | $tags = $this->getDoctrine() | 323 | $tags = $this->getDoctrine() |
324 | ->getRepository('WallabagCoreBundle:Tag') | 324 | ->getRepository('WallabagCoreBundle:Tag') |
325 | ->findAllTags($this->getUser()->getId()); | 325 | ->findAllTags($this->getUser()->getId()) |
326 | ->getQuery() | ||
327 | ->getResult(); | ||
326 | 328 | ||
327 | $json = $this->get('serializer')->serialize($tags, 'json'); | 329 | $json = $this->get('serializer')->serialize($tags, 'json'); |
328 | 330 | ||
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..302e5a53 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -10,8 +10,6 @@ use Wallabag\CoreBundle\Entity\Tag; | |||
10 | 10 | ||
11 | class EntryRepository extends EntityRepository | 11 | class EntryRepository extends EntityRepository |
12 | { | 12 | { |
13 | private $lifeTime; | ||
14 | |||
15 | /** | 13 | /** |
16 | * Return a query builder to used by other getBuilderFor* method. | 14 | * Return a query builder to used by other getBuilderFor* method. |
17 | * | 15 | * |
@@ -311,25 +309,4 @@ class EntryRepository extends EntityRepository | |||
311 | 309 | ||
312 | return $qb->getQuery()->getSingleScalarResult(); | 310 | return $qb->getQuery()->getSingleScalarResult(); |
313 | } | 311 | } |
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 | } | 312 | } |
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..3780b13e 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 | ||
@@ -47,24 +54,24 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
47 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | 54 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; |
48 | 55 | ||
49 | if (null === $user || !is_object($user)) { | 56 | if (null === $user || !is_object($user)) { |
50 | return []; | 57 | return 0; |
51 | } | 58 | } |
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 0; | ||
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 | private 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'; |