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
;
8 use Symfony\Component\Translation\TranslatorInterface
;
10 class WallabagExtension
extends \Twig_Extension
implements \Twig_Extension_GlobalsInterface
12 private $tokenStorage;
13 private $entryRepository;
14 private $tagRepository;
18 public function __construct(EntryRepository
$entryRepository, TagRepository
$tagRepository, TokenStorageInterface
$tokenStorage, $lifeTime, TranslatorInterface
$translator)
20 $this->entryRepository
= $entryRepository;
21 $this->tagRepository
= $tagRepository;
22 $this->tokenStorage
= $tokenStorage;
23 $this->lifeTime
= $lifeTime;
24 $this->translator
= $translator;
27 public function getFilters()
30 new \
Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
34 public function getFunctions()
37 new \
Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
38 new \
Twig_SimpleFunction('count_tags', [$this, 'countTags']),
39 new \
Twig_SimpleFunction('display_stats', [$this, 'displayStats']),
43 public function removeWww($url)
45 return preg_replace('/^www\./i', '', $url);
49 * Return number of entries depending of the type (unread, archive, starred or all).
51 * @param string $type Type of entries to count
55 public function countEntries($type)
57 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
59 if (null === $user || !is_object($user)) {
65 $qb = $this->entryRepository
->getBuilderForStarredByUser($user->getId());
69 $qb = $this->entryRepository
->getBuilderForArchiveByUser($user->getId());
73 $qb = $this->entryRepository
->getBuilderForUnreadByUser($user->getId());
77 $qb = $this->entryRepository
->getBuilderForAllByUser($user->getId());
81 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
84 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
85 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
91 $query->useQueryCache(true);
92 $query->useResultCache(true);
93 $query->setResultCacheLifetime($this->lifeTime
);
95 return count($query->getArrayResult());
99 * Return number of tags.
103 public function countTags()
105 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
107 if (null === $user || !is_object($user)) {
111 return $this->tagRepository
->countAllTags($user->getId());
115 * Display a single line about reading stats.
119 public function displayStats()
121 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
123 if (null === $user || !is_object($user)) {
127 $query = $this->entryRepository
->getBuilderForArchiveByUser($user->getId())
132 $query->useQueryCache(true);
133 $query->useResultCache(true);
134 $query->setResultCacheLifetime($this->lifeTime
);
136 $nbArchives = count($query->getArrayResult());
138 $interval = $user->getCreatedAt()->diff(new \
DateTime('now'));
139 $nbDays = (int) $interval->format('%a') ?: 1;
141 // force setlocale for date translation
142 setlocale(LC_TIME
, strtolower($user->getConfig()->getLanguage()).'_'.strtoupper(strtolower($user->getConfig()->getLanguage())));
144 return $this->translator
->trans('footer.stats', [
145 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
146 '%nb_archives%' => $nbArchives,
147 '%per_day%' => round($nbArchives / $nbDays, 2),
151 public function getName()
153 return 'wallabag_extension';