]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Twig/WallabagExtension.php
Merge remote-tracking branch 'origin/master' into 2.4
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Twig / WallabagExtension.php
CommitLineData
72fcaf8a
NL
1<?php
2
3namespace Wallabag\CoreBundle\Twig;
4
8315130a 5use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
f808b016 6use Symfony\Component\Translation\TranslatorInterface;
8315130a 7use Wallabag\CoreBundle\Repository\EntryRepository;
429d86f3 8use Wallabag\CoreBundle\Repository\TagRepository;
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']),
531c8d0a 31 new \Twig_SimpleFilter('removeScheme', [$this, 'removeScheme']),
e50e45d6 32 new \Twig_SimpleFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']),
4094ea47 33 ];
72fcaf8a
NL
34 }
35
59ddb9ae
JB
36 public function getFunctions()
37 {
f808b016 38 return [
59ddb9ae 39 new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
429d86f3 40 new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
1264029c 41 new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']),
f808b016 42 ];
59ddb9ae
JB
43 }
44
72fcaf8a
NL
45 public function removeWww($url)
46 {
cfb28c9d 47 return preg_replace('/^www\./i', '', $url);
72fcaf8a
NL
48 }
49
531c8d0a
TC
50 public function removeScheme($url)
51 {
52 return preg_replace('#^https?://#i', '', $url);
53 }
54
e50e45d6
KD
55 public function removeSchemeAndWww($url)
56 {
f277bc04 57 return $this->removeWww($this->removeScheme($url));
e50e45d6
KD
58 }
59
59ddb9ae 60 /**
234ad944 61 * Return number of entries depending of the type (unread, archive, starred or all).
59ddb9ae 62 *
234ad944 63 * @param string $type Type of entries to count
59ddb9ae
JB
64 *
65 * @return int
66 */
67 public function countEntries($type)
8315130a
NL
68 {
69 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
70
2a1ceb67 71 if (null === $user || !\is_object($user)) {
5173fd1c 72 return 0;
8315130a
NL
73 }
74
59ddb9ae
JB
75 switch ($type) {
76 case 'starred':
429d86f3 77 $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
59ddb9ae 78 break;
59ddb9ae 79 case 'archive':
429d86f3 80 $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
59ddb9ae 81 break;
59ddb9ae 82 case 'unread':
429d86f3 83 $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
59ddb9ae 84 break;
59ddb9ae 85 case 'all':
429d86f3 86 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
59ddb9ae 87 break;
59ddb9ae
JB
88 default:
89 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
90 }
91
92 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
93 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
94 $query = $qb
95 ->select('e.id')
96 ->groupBy('e.id')
97 ->getQuery();
98
faa86e06
JB
99 $query->useQueryCache(true);
100 $query->useResultCache(true);
101 $query->setResultCacheLifetime($this->lifeTime);
59ddb9ae 102
2a1ceb67 103 return \count($query->getArrayResult());
8315130a
NL
104 }
105
429d86f3
NL
106 /**
107 * Return number of tags.
108 *
109 * @return int
110 */
111 public function countTags()
112 {
113 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
114
2a1ceb67 115 if (null === $user || !\is_object($user)) {
5173fd1c 116 return 0;
429d86f3
NL
117 }
118
28987583 119 return $this->tagRepository->countAllTags($user->getId());
429d86f3
NL
120 }
121
1264029c
JB
122 /**
123 * Display a single line about reading stats.
124 *
125 * @return string
126 */
127 public function displayStats()
128 {
129 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
130
2a1ceb67 131 if (null === $user || !\is_object($user)) {
1264029c
JB
132 return 0;
133 }
134
135 $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId())
136 ->select('e.id')
137 ->groupBy('e.id')
138 ->getQuery();
139
140 $query->useQueryCache(true);
141 $query->useResultCache(true);
142 $query->setResultCacheLifetime($this->lifeTime);
143
2a1ceb67 144 $nbArchives = \count($query->getArrayResult());
1264029c
JB
145
146 $interval = $user->getCreatedAt()->diff(new \DateTime('now'));
147 $nbDays = (int) $interval->format('%a') ?: 1;
148
576d285d 149 // force setlocale for date translation
f808b016 150 setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
576d285d 151
1264029c 152 return $this->translator->trans('footer.stats', [
576d285d 153 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
1264029c
JB
154 '%nb_archives%' => $nbArchives,
155 '%per_day%' => round($nbArchives / $nbDays, 2),
156 ]);
157 }
158
72fcaf8a
NL
159 public function getName()
160 {
161 return 'wallabag_extension';
162 }
163}