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