]> 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;
f808b016 6use Symfony\Component\Translation\TranslatorInterface;
378aaefb 7use Wallabag\CoreBundle\Notifications\NotificationInterface;
8315130a 8use Wallabag\CoreBundle\Repository\EntryRepository;
378aaefb 9use Wallabag\CoreBundle\Repository\NotificationRepository;
429d86f3 10use Wallabag\CoreBundle\Repository\TagRepository;
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;
378aaefb 17 private $notificationRepository;
429d86f3 18 private $lifeTime;
378aaefb 19 private $nbNotifications;
1264029c 20 private $translator;
8315130a 21
378aaefb 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;
378aaefb 26 $this->notificationRepository = $notificationRepository;
8315130a 27 $this->tokenStorage = $tokenStorage;
429d86f3 28 $this->lifeTime = $lifeTime;
378aaefb 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']),
378aaefb 37 new \Twig_SimpleFilter('unread_notif', [$this, 'unreadNotif']),
4094ea47 38 ];
72fcaf8a
NL
39 }
40
59ddb9ae
JB
41 public function getFunctions()
42 {
f808b016 43 return [
59ddb9ae 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']),
378aaefb 47 new \Twig_SimpleFunction('get_notifications', [$this, 'getNotifications']),
f808b016 48 ];
59ddb9ae
JB
49 }
50
72fcaf8a
NL
51 public function removeWww($url)
52 {
cfb28c9d 53 return preg_replace('/^www\./i', '', $url);
72fcaf8a
NL
54 }
55
378aaefb
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;
59ddb9ae 86 case 'archive':
429d86f3 87 $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
59ddb9ae 88 break;
59ddb9ae 89 case 'unread':
429d86f3 90 $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
59ddb9ae 91 break;
59ddb9ae 92 case 'all':
429d86f3 93 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
59ddb9ae 94 break;
59ddb9ae
JB
95 default:
96 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
97 }
98
99 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
100 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
101 $query = $qb
102 ->select('e.id')
103 ->groupBy('e.id')
104 ->getQuery();
105
faa86e06
JB
106 $query->useQueryCache(true);
107 $query->useResultCache(true);
108 $query->setResultCacheLifetime($this->lifeTime);
59ddb9ae 109
faa86e06 110 return count($query->getArrayResult());
8315130a
NL
111 }
112
429d86f3
NL
113 /**
114 * Return number of tags.
115 *
116 * @return int
117 */
118 public function countTags()
119 {
120 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
121
122 if (null === $user || !is_object($user)) {
5173fd1c 123 return 0;
429d86f3
NL
124 }
125
28987583 126 return $this->tagRepository->countAllTags($user->getId());
429d86f3
NL
127 }
128
378aaefb
TC
129 public function getNotifications()
130 {
131 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
132
133 if (null === $user || !is_object($user)) {
134 return 0;
135 }
136
137 return $this->notificationRepository->findBy(
138 ['user' => $user->getId()],
139 ['timestamp' => 'DESC'],
140 $this->nbNotifications
141 );
142 }
143
1264029c
JB
144 /**
145 * Display a single line about reading stats.
146 *
147 * @return string
148 */
149 public function displayStats()
150 {
151 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
152
153 if (null === $user || !is_object($user)) {
154 return 0;
155 }
156
157 $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId())
158 ->select('e.id')
159 ->groupBy('e.id')
160 ->getQuery();
161
162 $query->useQueryCache(true);
163 $query->useResultCache(true);
164 $query->setResultCacheLifetime($this->lifeTime);
165
166 $nbArchives = count($query->getArrayResult());
167
168 $interval = $user->getCreatedAt()->diff(new \DateTime('now'));
169 $nbDays = (int) $interval->format('%a') ?: 1;
170
576d285d 171 // force setlocale for date translation
f808b016 172 setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
576d285d 173
1264029c 174 return $this->translator->trans('footer.stats', [
576d285d 175 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
1264029c
JB
176 '%nb_archives%' => $nbArchives,
177 '%per_day%' => round($nbArchives / $nbDays, 2),
178 ]);
179 }
72fcaf8a 180}