]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Twig/WallabagExtension.php
Merge remote-tracking branch 'origin/master' into 2.1
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Twig / WallabagExtension.php
index 93640dc68811a896b5b2d776c0443d3a327f5580..fb4c7412395f4affcb3b4b73f844ea06d498d4cc 100644 (file)
@@ -4,16 +4,21 @@ namespace Wallabag\CoreBundle\Twig;
 
 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()
@@ -23,41 +28,83 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
         ];
     }
 
+    public function getFunctions()
+    {
+        return array(
+            new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
+            new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
+        );
+    }
+
     public function removeWww($url)
     {
         return preg_replace('/^www\./i', '', $url);
     }
 
-    public function getGlobals()
+    /**
+     * Return number of entries depending of the type (unread, archive, starred or all).
+     *
+     * @param string $type Type of entries to count
+     *
+     * @return int
+     */
+    public function countEntries($type)
     {
         $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
 
         if (null === $user || !is_object($user)) {
-            return [];
+            return 0;
         }
 
-        $unreadEntries = $this->repository->enableCache(
-            $this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery()
-        );
+        switch ($type) {
+            case 'starred':
+                $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
+                break;
 
-        $starredEntries = $this->repository->enableCache(
-            $this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery()
-        );
+            case 'archive':
+                $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
+                break;
 
-        $archivedEntries = $this->repository->enableCache(
-            $this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery()
-        );
+            case 'unread':
+                $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
+                break;
 
-        $allEntries = $this->repository->enableCache(
-            $this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery()
-        );
+            case 'all':
+                $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
+                break;
 
-        return [
-            'unreadEntries' => $unreadEntries->getSingleScalarResult(),
-            'starredEntries' => $starredEntries->getSingleScalarResult(),
-            'archivedEntries' => $archivedEntries->getSingleScalarResult(),
-            'allEntries' => $allEntries->getSingleScalarResult(),
-        ];
+            default:
+                throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
+        }
+
+        // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
+        // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
+        $query = $qb
+            ->select('e.id')
+            ->groupBy('e.id')
+            ->getQuery();
+
+        $query->useQueryCache(true);
+        $query->useResultCache(true);
+        $query->setResultCacheLifetime($this->lifeTime);
+
+        return count($query->getArrayResult());
+    }
+
+    /**
+     * 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 0;
+        }
+
+        return $this->tagRepository->countAllTags($user->getId());
     }
 
     public function getName()