]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Twig/WallabagExtension.php
First draft for notifications
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Twig / WallabagExtension.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Twig;
4
5 use Doctrine\Common\Collections\Collection;
6 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7 use Wallabag\CoreBundle\Notifications\NotificationInterface;
8 use Wallabag\CoreBundle\Repository\EntryRepository;
9 use Wallabag\CoreBundle\Repository\NotificationRepository;
10 use Wallabag\CoreBundle\Repository\TagRepository;
11 use Symfony\Component\Translation\TranslatorInterface;
12
13 class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
14 {
15 private $tokenStorage;
16 private $entryRepository;
17 private $tagRepository;
18 private $notificationRepository;
19 private $lifeTime;
20 private $nbNotifications;
21 private $translator;
22
23 public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, NotificationRepository $notificationRepository, TokenStorageInterface $tokenStorage, $lifeTime, $nbNotifications, TranslatorInterface $translator)
24 {
25 $this->entryRepository = $entryRepository;
26 $this->tagRepository = $tagRepository;
27 $this->notificationRepository = $notificationRepository;
28 $this->tokenStorage = $tokenStorage;
29 $this->lifeTime = $lifeTime;
30 $this->nbNotifications = $nbNotifications;
31 $this->translator = $translator;
32 }
33
34 public function getFilters()
35 {
36 return [
37 new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
38 new \Twig_SimpleFilter('unread_notif', [$this, 'unreadNotif']),
39 ];
40 }
41
42 public function getFunctions()
43 {
44 return array(
45 new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
46 new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
47 new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']),
48 new \Twig_SimpleFunction('get_notifications', [$this, 'getNotifications'])
49 );
50 }
51
52 public function removeWww($url)
53 {
54 return preg_replace('/^www\./i', '', $url);
55 }
56
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
68 /**
69 * Return number of entries depending of the type (unread, archive, starred or all).
70 *
71 * @param string $type Type of entries to count
72 *
73 * @return int
74 */
75 public function countEntries($type)
76 {
77 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
78
79 if (null === $user || !is_object($user)) {
80 return 0;
81 }
82
83 switch ($type) {
84 case 'starred':
85 $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
86 break;
87
88 case 'archive':
89 $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
90 break;
91
92 case 'unread':
93 $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
94 break;
95
96 case 'all':
97 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
98 break;
99
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
111 $query->useQueryCache(true);
112 $query->useResultCache(true);
113 $query->setResultCacheLifetime($this->lifeTime);
114
115 return count($query->getArrayResult());
116 }
117
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)) {
128 return 0;
129 }
130
131 return $this->tagRepository->countAllTags($user->getId());
132 }
133
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
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
176 // force setlocale for date translation
177 setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()).'_'.strtoupper(strtolower($user->getConfig()->getLanguage())));
178
179 return $this->translator->trans('footer.stats', [
180 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
181 '%nb_archives%' => $nbArchives,
182 '%per_day%' => round($nbArchives / $nbDays, 2),
183 ]);
184 }
185 }