3 namespace Wallabag\CoreBundle\Twig
;
5 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
;
6 use Wallabag\CoreBundle\Repository\EntryRepository
;
7 use Wallabag\CoreBundle\Repository\TagRepository
;
9 class WallabagExtension
extends \Twig_Extension
implements \Twig_Extension_GlobalsInterface
11 private $tokenStorage;
12 private $entryRepository;
13 private $tagRepository;
16 public function __construct(EntryRepository
$entryRepository = null, TagRepository
$tagRepository = null, TokenStorageInterface
$tokenStorage = null, $lifeTime = 0)
18 $this->entryRepository
= $entryRepository;
19 $this->tagRepository
= $tagRepository;
20 $this->tokenStorage
= $tokenStorage;
21 $this->lifeTime
= $lifeTime;
24 public function getFilters()
27 new \
Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
31 public function getFunctions()
34 new \
Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
35 new \
Twig_SimpleFunction('count_tags', [$this, 'countTags']),
39 public function removeWww($url)
41 return preg_replace('/^www\./i', '', $url);
45 * Return number of entries depending of the type (unread, archive, starred or all).
47 * @param string $type Type of entries to count
51 public function countEntries($type)
53 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
55 if (null === $user || !is_object($user)) {
61 $qb = $this->entryRepository
->getBuilderForStarredByUser($user->getId());
65 $qb = $this->entryRepository
->getBuilderForArchiveByUser($user->getId());
69 $qb = $this->entryRepository
->getBuilderForUnreadByUser($user->getId());
73 $qb = $this->entryRepository
->getBuilderForAllByUser($user->getId());
77 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
80 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
81 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
87 $query->useQueryCache(true);
88 $query->useResultCache(true);
89 $query->setResultCacheLifetime($this->lifeTime
);
91 return count($query->getArrayResult());
95 * Return number of tags.
99 public function countTags()
101 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
103 if (null === $user || !is_object($user)) {
107 return $this->tagRepository
->countAllTags($user->getId());
110 public function getName()
112 return 'wallabag_extension';