diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index fb4c7412..783cde3e 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php | |||
@@ -5,6 +5,7 @@ namespace Wallabag\CoreBundle\Twig; | |||
5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | 5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
6 | use Wallabag\CoreBundle\Repository\EntryRepository; | 6 | use Wallabag\CoreBundle\Repository\EntryRepository; |
7 | use Wallabag\CoreBundle\Repository\TagRepository; | 7 | use Wallabag\CoreBundle\Repository\TagRepository; |
8 | use Symfony\Component\Translation\TranslatorInterface; | ||
8 | 9 | ||
9 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface | 10 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface |
10 | { | 11 | { |
@@ -12,13 +13,15 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
12 | private $entryRepository; | 13 | private $entryRepository; |
13 | private $tagRepository; | 14 | private $tagRepository; |
14 | private $lifeTime; | 15 | private $lifeTime; |
16 | private $translator; | ||
15 | 17 | ||
16 | public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0) | 18 | public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator) |
17 | { | 19 | { |
18 | $this->entryRepository = $entryRepository; | 20 | $this->entryRepository = $entryRepository; |
19 | $this->tagRepository = $tagRepository; | 21 | $this->tagRepository = $tagRepository; |
20 | $this->tokenStorage = $tokenStorage; | 22 | $this->tokenStorage = $tokenStorage; |
21 | $this->lifeTime = $lifeTime; | 23 | $this->lifeTime = $lifeTime; |
24 | $this->translator = $translator; | ||
22 | } | 25 | } |
23 | 26 | ||
24 | public function getFilters() | 27 | public function getFilters() |
@@ -33,6 +36,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
33 | return array( | 36 | return array( |
34 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), | 37 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), |
35 | new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), | 38 | new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), |
39 | new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']), | ||
36 | ); | 40 | ); |
37 | } | 41 | } |
38 | 42 | ||
@@ -107,6 +111,40 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
107 | return $this->tagRepository->countAllTags($user->getId()); | 111 | return $this->tagRepository->countAllTags($user->getId()); |
108 | } | 112 | } |
109 | 113 | ||
114 | /** | ||
115 | * Display a single line about reading stats. | ||
116 | * | ||
117 | * @return string | ||
118 | */ | ||
119 | public function displayStats() | ||
120 | { | ||
121 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | ||
122 | |||
123 | if (null === $user || !is_object($user)) { | ||
124 | return 0; | ||
125 | } | ||
126 | |||
127 | $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId()) | ||
128 | ->select('e.id') | ||
129 | ->groupBy('e.id') | ||
130 | ->getQuery(); | ||
131 | |||
132 | $query->useQueryCache(true); | ||
133 | $query->useResultCache(true); | ||
134 | $query->setResultCacheLifetime($this->lifeTime); | ||
135 | |||
136 | $nbArchives = count($query->getArrayResult()); | ||
137 | |||
138 | $interval = $user->getCreatedAt()->diff(new \DateTime('now')); | ||
139 | $nbDays = (int) $interval->format('%a') ?: 1; | ||
140 | |||
141 | return $this->translator->trans('footer.stats', [ | ||
142 | '%user_creation%' => $user->getCreatedAt()->format('F jS, Y'), | ||
143 | '%nb_archives%' => $nbArchives, | ||
144 | '%per_day%' => round($nbArchives / $nbDays, 2), | ||
145 | ]); | ||
146 | } | ||
147 | |||
110 | public function getName() | 148 | public function getName() |
111 | { | 149 | { |
112 | return 'wallabag_extension'; | 150 | return 'wallabag_extension'; |