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