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