diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php')
-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 351172c4..fa63ba24 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php | |||
@@ -4,7 +4,9 @@ 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 Symfony\Component\Translation\TranslatorInterface; | 6 | use Symfony\Component\Translation\TranslatorInterface; |
7 | use Wallabag\CoreBundle\Notifications\NotificationInterface; | ||
7 | use Wallabag\CoreBundle\Repository\EntryRepository; | 8 | use Wallabag\CoreBundle\Repository\EntryRepository; |
9 | use Wallabag\CoreBundle\Repository\NotificationRepository; | ||
8 | use Wallabag\CoreBundle\Repository\TagRepository; | 10 | use Wallabag\CoreBundle\Repository\TagRepository; |
9 | 11 | ||
10 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface | 12 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface |
@@ -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 |
@@ -107,6 +126,21 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
107 | return $this->tagRepository->countAllTags($user->getId()); | 126 | return $this->tagRepository->countAllTags($user->getId()); |
108 | } | 127 | } |
109 | 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 | |||
110 | /** | 144 | /** |
111 | * Display a single line about reading stats. | 145 | * Display a single line about reading stats. |
112 | * | 146 | * |
@@ -143,9 +177,4 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
143 | '%per_day%' => round($nbArchives / $nbDays, 2), | 177 | '%per_day%' => round($nbArchives / $nbDays, 2), |
144 | ]); | 178 | ]); |
145 | } | 179 | } |
146 | |||
147 | public function getName() | ||
148 | { | ||
149 | return 'wallabag_extension'; | ||
150 | } | ||
151 | } | 180 | } |