]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Twig/WallabagExtension.php
Moved total messages from import
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Twig / WallabagExtension.php
CommitLineData
72fcaf8a
NL
1<?php
2
3namespace Wallabag\CoreBundle\Twig;
4
8315130a
NL
5use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
6use Wallabag\CoreBundle\Repository\EntryRepository;
429d86f3 7use Wallabag\CoreBundle\Repository\TagRepository;
1264029c 8use Symfony\Component\Translation\TranslatorInterface;
8315130a 9
f997ae6a 10class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
72fcaf8a 11{
8315130a 12 private $tokenStorage;
429d86f3
NL
13 private $entryRepository;
14 private $tagRepository;
15 private $lifeTime;
1264029c 16 private $translator;
8315130a 17
1264029c 18 public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator)
8315130a 19 {
429d86f3
NL
20 $this->entryRepository = $entryRepository;
21 $this->tagRepository = $tagRepository;
8315130a 22 $this->tokenStorage = $tokenStorage;
429d86f3 23 $this->lifeTime = $lifeTime;
1264029c 24 $this->translator = $translator;
8315130a
NL
25 }
26
72fcaf8a
NL
27 public function getFilters()
28 {
4094ea47
JB
29 return [
30 new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
31 ];
72fcaf8a
NL
32 }
33
59ddb9ae
JB
34 public function getFunctions()
35 {
36 return array(
37 new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
429d86f3 38 new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
1264029c 39 new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']),
59ddb9ae
JB
40 );
41 }
42
72fcaf8a
NL
43 public function removeWww($url)
44 {
cfb28c9d 45 return preg_replace('/^www\./i', '', $url);
72fcaf8a
NL
46 }
47
59ddb9ae 48 /**
234ad944 49 * Return number of entries depending of the type (unread, archive, starred or all).
59ddb9ae 50 *
234ad944 51 * @param string $type Type of entries to count
59ddb9ae
JB
52 *
53 * @return int
54 */
55 public function countEntries($type)
8315130a
NL
56 {
57 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
58
59 if (null === $user || !is_object($user)) {
5173fd1c 60 return 0;
8315130a
NL
61 }
62
59ddb9ae
JB
63 switch ($type) {
64 case 'starred':
429d86f3 65 $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
59ddb9ae 66 break;
0b0233b1 67
59ddb9ae 68 case 'archive':
429d86f3 69 $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
59ddb9ae 70 break;
0b0233b1 71
59ddb9ae 72 case 'unread':
429d86f3 73 $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
59ddb9ae 74 break;
0b0233b1 75
59ddb9ae 76 case 'all':
429d86f3 77 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
59ddb9ae 78 break;
8315130a 79
59ddb9ae
JB
80 default:
81 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
82 }
83
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
86 $query = $qb
87 ->select('e.id')
88 ->groupBy('e.id')
89 ->getQuery();
90
faa86e06
JB
91 $query->useQueryCache(true);
92 $query->useResultCache(true);
93 $query->setResultCacheLifetime($this->lifeTime);
59ddb9ae 94
faa86e06 95 return count($query->getArrayResult());
8315130a
NL
96 }
97
429d86f3
NL
98 /**
99 * Return number of tags.
100 *
101 * @return int
102 */
103 public function countTags()
104 {
105 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
106
107 if (null === $user || !is_object($user)) {
5173fd1c 108 return 0;
429d86f3
NL
109 }
110
28987583 111 return $this->tagRepository->countAllTags($user->getId());
429d86f3
NL
112 }
113
1264029c
JB
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
576d285d
JB
141 // force setlocale for date translation
142 setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()).'_'.strtoupper(strtolower($user->getConfig()->getLanguage())));
143
1264029c 144 return $this->translator->trans('footer.stats', [
576d285d 145 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
1264029c
JB
146 '%nb_archives%' => $nbArchives,
147 '%per_day%' => round($nbArchives / $nbDays, 2),
148 ]);
149 }
150
72fcaf8a
NL
151 public function getName()
152 {
153 return 'wallabag_extension';
154 }
155}