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