aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Twig
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-10-01 15:58:26 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-10-01 16:03:50 +0200
commit1264029cd413a4e29642a219e2647d886f0de0d6 (patch)
treef257817f70c1169d442d4563a53533124ed28f94 /src/Wallabag/CoreBundle/Twig
parent114c55c0a6eade2ba6c53fe25f61cc58cca91620 (diff)
downloadwallabag-1264029cd413a4e29642a219e2647d886f0de0d6.tar.gz
wallabag-1264029cd413a4e29642a219e2647d886f0de0d6.tar.zst
wallabag-1264029cd413a4e29642a219e2647d886f0de0d6.zip
Add simple stats in footer
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig')
-rw-r--r--src/Wallabag/CoreBundle/Twig/WallabagExtension.php40
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;
5use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; 5use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
6use Wallabag\CoreBundle\Repository\EntryRepository; 6use Wallabag\CoreBundle\Repository\EntryRepository;
7use Wallabag\CoreBundle\Repository\TagRepository; 7use Wallabag\CoreBundle\Repository\TagRepository;
8use Symfony\Component\Translation\TranslatorInterface;
8 9
9class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface 10class 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';