diff options
author | Thomas Citharel <tcit@tcit.fr> | 2017-06-15 09:43:48 +0200 |
---|---|---|
committer | Thomas Citharel <tcit@tcit.fr> | 2017-06-23 09:42:20 +0200 |
commit | e0f9010ec2a558f6cf7d16fb96a2c4cdb34e3f37 (patch) | |
tree | 904f42fa442188bf456b9773295fb9582634ba6c /src/Wallabag/CoreBundle/Twig | |
parent | 29714661b1df78871ceaf0e079f11041a8641d4b (diff) | |
download | wallabag-e0f9010ec2a558f6cf7d16fb96a2c4cdb34e3f37.tar.gz wallabag-e0f9010ec2a558f6cf7d16fb96a2c4cdb34e3f37.tar.zst wallabag-e0f9010ec2a558f6cf7d16fb96a2c4cdb34e3f37.zip |
Notifications
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig')
-rw-r--r-- | src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index a305c53f..034b3fa2 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php | |||
@@ -3,7 +3,9 @@ | |||
3 | namespace Wallabag\CoreBundle\Twig; | 3 | namespace Wallabag\CoreBundle\Twig; |
4 | 4 | ||
5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | 5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
6 | use Wallabag\CoreBundle\Notifications\NotificationInterface; | ||
6 | use Wallabag\CoreBundle\Repository\EntryRepository; | 7 | use Wallabag\CoreBundle\Repository\EntryRepository; |
8 | use Wallabag\CoreBundle\Repository\NotificationRepository; | ||
7 | use Wallabag\CoreBundle\Repository\TagRepository; | 9 | use Wallabag\CoreBundle\Repository\TagRepository; |
8 | use Symfony\Component\Translation\TranslatorInterface; | 10 | use Symfony\Component\Translation\TranslatorInterface; |
9 | 11 | ||
@@ -12,15 +14,19 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
12 | private $tokenStorage; | 14 | private $tokenStorage; |
13 | private $entryRepository; | 15 | private $entryRepository; |
14 | private $tagRepository; | 16 | private $tagRepository; |
17 | private $notificationRepository; | ||
15 | private $lifeTime; | 18 | private $lifeTime; |
19 | private $nbNotifications; | ||
16 | private $translator; | 20 | private $translator; |
17 | 21 | ||
18 | public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator) | 22 | public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, NotificationRepository $notificationRepository, TokenStorageInterface $tokenStorage, $lifeTime, $nbNotifications, TranslatorInterface $translator) |
19 | { | 23 | { |
20 | $this->entryRepository = $entryRepository; | 24 | $this->entryRepository = $entryRepository; |
21 | $this->tagRepository = $tagRepository; | 25 | $this->tagRepository = $tagRepository; |
26 | $this->notificationRepository = $notificationRepository; | ||
22 | $this->tokenStorage = $tokenStorage; | 27 | $this->tokenStorage = $tokenStorage; |
23 | $this->lifeTime = $lifeTime; | 28 | $this->lifeTime = $lifeTime; |
29 | $this->nbNotifications = $nbNotifications; | ||
24 | $this->translator = $translator; | 30 | $this->translator = $translator; |
25 | } | 31 | } |
26 | 32 | ||
@@ -28,6 +34,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
28 | { | 34 | { |
29 | return [ | 35 | return [ |
30 | new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), | 36 | new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), |
37 | new \Twig_SimpleFilter('unread_notif', [$this, 'unreadNotif']), | ||
31 | ]; | 38 | ]; |
32 | } | 39 | } |
33 | 40 | ||
@@ -37,6 +44,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
37 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), | 44 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), |
38 | new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), | 45 | new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), |
39 | new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']), | 46 | new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']), |
47 | new \Twig_SimpleFunction('get_notifications', [$this, 'getNotifications']) | ||
40 | ); | 48 | ); |
41 | } | 49 | } |
42 | 50 | ||
@@ -46,6 +54,17 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
46 | } | 54 | } |
47 | 55 | ||
48 | /** | 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 | /** | ||
49 | * Return number of entries depending of the type (unread, archive, starred or all). | 68 | * Return number of entries depending of the type (unread, archive, starred or all). |
50 | * | 69 | * |
51 | * @param string $type Type of entries to count | 70 | * @param string $type Type of entries to count |
@@ -111,6 +130,21 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
111 | return $this->tagRepository->countAllTags($user->getId()); | 130 | return $this->tagRepository->countAllTags($user->getId()); |
112 | } | 131 | } |
113 | 132 | ||
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 | |||
114 | /** | 148 | /** |
115 | * Display a single line about reading stats. | 149 | * Display a single line about reading stats. |
116 | * | 150 | * |
@@ -147,9 +181,4 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
147 | '%per_day%' => round($nbArchives / $nbDays, 2), | 181 | '%per_day%' => round($nbArchives / $nbDays, 2), |
148 | ]); | 182 | ]); |
149 | } | 183 | } |
150 | |||
151 | public function getName() | ||
152 | { | ||
153 | return 'wallabag_extension'; | ||
154 | } | ||
155 | } | 184 | } |