]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Added tags counter in sidebar (material theme)
authorNicolas Lœuillet <nicolas@loeuillet.org>
Sun, 4 Sep 2016 18:53:28 +0000 (20:53 +0200)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Sun, 4 Sep 2016 18:53:28 +0000 (20:53 +0200)
app/config/services.yml
src/Wallabag/ApiBundle/Controller/WallabagRestController.php
src/Wallabag/CoreBundle/Controller/TagController.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
src/Wallabag/CoreBundle/Repository/TagRepository.php
src/Wallabag/CoreBundle/Resources/config/services.yml
src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig
src/Wallabag/CoreBundle/Twig/WallabagExtension.php

index 95b8f26f5b7b0509239b03f9e2ac48d5cd0415c2..76bbce27c741b49aeae4898e87ca469305630d35 100644 (file)
@@ -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 }
 
index 869fdc56ab1ba54dbf787d3f281fc357a180fa44..07fedeb01ee428b5842caa6be6cc0a1e35e1770e 100644 (file)
@@ -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');
 
index 1cbc413d644e616727ce0dca82af9b6f5582cd15..bc95a4d34ac4e6bda9e265c120b43d63397b4e3e 100644 (file)
@@ -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',
index 24d1a57af0550eda5782c3fdd40d86f22c5aca24..719803a1884ce0af8f0a92e6c2813e1ea5c1c599 100644 (file)
@@ -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;
-    }
 }
index abf915fe5c7f23629af5b746530dc6d6c868d792..41f616079dd53f69032512adf6a1bfb176b98657 100644 (file)
@@ -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);
     }
 
     /**
index 1c1457e705fee2200503b3a53128b22ab4c6a69f..e95ef4520f990cf174765179eda7dc197d53cd21 100644 (file)
@@ -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
index 06ecbf3d243743d209e4af7636536b8ff5f52f77..b70198da6bc68f3a9586e583c9fcbd14a2da19ce 100644 (file)
@@ -47,7 +47,7 @@
                 <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ count_entries('all') }}</span></a>
             </li>
             <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}">
-                <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a>
+                <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }} <span class="numberItems grey-text">{{ count_tags() }}</span></a>
             </li>
             <li class="bold {% if currentRoute == 'config' %}active{% endif %}">
                 <a class="waves-effect" href="{{ path('config') }}">{{ 'menu.left.config'|trans }}</a>
index 974b86a97aab2c9da636ad7603e0feae2aef16ce..ed6dadc5a40ed6d002c94ef7f333f09b9bafaa45 100644 (file)
@@ -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';