]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Twig/WallabagExtension.php
Load custom.css only if exists
[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;
a2f4efe6
JB
7use Twig\Extension\AbstractExtension;
8use Twig\Extension\GlobalsInterface;
9use Twig\TwigFilter;
10use Twig\TwigFunction;
8315130a 11use Wallabag\CoreBundle\Repository\EntryRepository;
429d86f3 12use Wallabag\CoreBundle\Repository\TagRepository;
8315130a 13
a2f4efe6 14class WallabagExtension extends AbstractExtension implements GlobalsInterface
72fcaf8a 15{
8315130a 16 private $tokenStorage;
429d86f3
NL
17 private $entryRepository;
18 private $tagRepository;
19 private $lifeTime;
1264029c 20 private $translator;
33e3eeae 21 private $rootDir;
8315130a 22
33e3eeae 23 public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator, string $rootDir)
8315130a 24 {
429d86f3
NL
25 $this->entryRepository = $entryRepository;
26 $this->tagRepository = $tagRepository;
8315130a 27 $this->tokenStorage = $tokenStorage;
429d86f3 28 $this->lifeTime = $lifeTime;
1264029c 29 $this->translator = $translator;
33e3eeae 30 $this->rootDir = $rootDir;
8315130a
NL
31 }
32
a2f4efe6
JB
33 public function getGlobals()
34 {
35 return [];
36 }
37
72fcaf8a
NL
38 public function getFilters()
39 {
4094ea47 40 return [
a2f4efe6
JB
41 new TwigFilter('removeWww', [$this, 'removeWww']),
42 new TwigFilter('removeScheme', [$this, 'removeScheme']),
43 new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']),
4094ea47 44 ];
72fcaf8a
NL
45 }
46
59ddb9ae
JB
47 public function getFunctions()
48 {
f808b016 49 return [
a2f4efe6
JB
50 new TwigFunction('count_entries', [$this, 'countEntries']),
51 new TwigFunction('count_tags', [$this, 'countTags']),
52 new TwigFunction('display_stats', [$this, 'displayStats']),
33e3eeae 53 new TwigFunction('asset_file_exists', [$this, 'assetFileExists']),
f808b016 54 ];
59ddb9ae
JB
55 }
56
72fcaf8a
NL
57 public function removeWww($url)
58 {
cfb28c9d 59 return preg_replace('/^www\./i', '', $url);
72fcaf8a
NL
60 }
61
531c8d0a
TC
62 public function removeScheme($url)
63 {
64 return preg_replace('#^https?://#i', '', $url);
65 }
66
e50e45d6
KD
67 public function removeSchemeAndWww($url)
68 {
f277bc04 69 return $this->removeWww($this->removeScheme($url));
e50e45d6
KD
70 }
71
59ddb9ae 72 /**
234ad944 73 * Return number of entries depending of the type (unread, archive, starred or all).
59ddb9ae 74 *
234ad944 75 * @param string $type Type of entries to count
59ddb9ae
JB
76 *
77 * @return int
78 */
79 public function countEntries($type)
8315130a
NL
80 {
81 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
82
2a1ceb67 83 if (null === $user || !\is_object($user)) {
5173fd1c 84 return 0;
8315130a
NL
85 }
86
59ddb9ae
JB
87 switch ($type) {
88 case 'starred':
429d86f3 89 $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
59ddb9ae 90 break;
59ddb9ae 91 case 'archive':
429d86f3 92 $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
59ddb9ae 93 break;
59ddb9ae 94 case 'unread':
429d86f3 95 $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
59ddb9ae 96 break;
59ddb9ae 97 case 'all':
429d86f3 98 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
59ddb9ae 99 break;
59ddb9ae
JB
100 default:
101 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
102 }
103
104 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
105 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
106 $query = $qb
107 ->select('e.id')
108 ->groupBy('e.id')
109 ->getQuery();
110
faa86e06
JB
111 $query->useQueryCache(true);
112 $query->useResultCache(true);
113 $query->setResultCacheLifetime($this->lifeTime);
59ddb9ae 114
2a1ceb67 115 return \count($query->getArrayResult());
8315130a
NL
116 }
117
429d86f3
NL
118 /**
119 * Return number of tags.
120 *
121 * @return int
122 */
123 public function countTags()
124 {
125 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
126
2a1ceb67 127 if (null === $user || !\is_object($user)) {
5173fd1c 128 return 0;
429d86f3
NL
129 }
130
28987583 131 return $this->tagRepository->countAllTags($user->getId());
429d86f3
NL
132 }
133
1264029c
JB
134 /**
135 * Display a single line about reading stats.
136 *
137 * @return string
138 */
139 public function displayStats()
140 {
141 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
142
2a1ceb67 143 if (null === $user || !\is_object($user)) {
1264029c
JB
144 return 0;
145 }
146
147 $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId())
148 ->select('e.id')
149 ->groupBy('e.id')
150 ->getQuery();
151
152 $query->useQueryCache(true);
153 $query->useResultCache(true);
154 $query->setResultCacheLifetime($this->lifeTime);
155
2a1ceb67 156 $nbArchives = \count($query->getArrayResult());
1264029c
JB
157
158 $interval = $user->getCreatedAt()->diff(new \DateTime('now'));
159 $nbDays = (int) $interval->format('%a') ?: 1;
160
576d285d 161 // force setlocale for date translation
f808b016 162 setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
576d285d 163
1264029c 164 return $this->translator->trans('footer.stats', [
576d285d 165 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
1264029c
JB
166 '%nb_archives%' => $nbArchives,
167 '%per_day%' => round($nbArchives / $nbDays, 2),
168 ]);
169 }
170
33e3eeae
S
171 public function assetFileExists($name)
172 {
173 return file_exists(realpath($this->rootDir . '/../web/' . $name));
174 }
175
72fcaf8a
NL
176 public function getName()
177 {
178 return 'wallabag_extension';
179 }
180}