3 namespace Wallabag\CoreBundle\Twig
;
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
;
12 class WallabagExtension
extends \Twig_Extension
implements \Twig_Extension_GlobalsInterface
14 private $tokenStorage;
15 private $entryRepository;
16 private $tagRepository;
17 private $notificationRepository;
19 private $nbNotifications;
22 public function __construct(EntryRepository
$entryRepository, TagRepository
$tagRepository, NotificationRepository
$notificationRepository, TokenStorageInterface
$tokenStorage, $lifeTime, $nbNotifications, TranslatorInterface
$translator)
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;
33 public function getFilters()
36 new \
Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
37 new \
Twig_SimpleFilter('unread_notif', [$this, 'unreadNotif']),
41 public function getFunctions()
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']),
51 public function removeWww($url)
53 return preg_replace('/^www\./i', '', $url);
60 public function unreadNotif($notifs)
62 return array_filter($notifs, function (NotificationInterface
$notif) {
63 return !$notif->isRead();
68 * Return number of entries depending of the type (unread, archive, starred or all).
70 * @param string $type Type of entries to count
74 public function countEntries($type)
76 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
78 if (null === $user || !is_object($user)) {
84 $qb = $this->entryRepository
->getBuilderForStarredByUser($user->getId());
87 $qb = $this->entryRepository
->getBuilderForArchiveByUser($user->getId());
90 $qb = $this->entryRepository
->getBuilderForUnreadByUser($user->getId());
93 $qb = $this->entryRepository
->getBuilderForAllByUser($user->getId());
96 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
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
106 $query->useQueryCache(true);
107 $query->useResultCache(true);
108 $query->setResultCacheLifetime($this->lifeTime
);
110 return count($query->getArrayResult());
114 * Return number of tags.
118 public function countTags()
120 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
122 if (null === $user || !is_object($user)) {
126 return $this->tagRepository
->countAllTags($user->getId());
129 public function getNotifications()
131 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
133 if (null === $user || !is_object($user)) {
137 return $this->notificationRepository
->findBy(
138 ['user' => $user->getId()],
139 ['timestamp' => 'DESC'],
140 $this->nbNotifications
145 * Display a single line about reading stats.
149 public function displayStats()
151 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
153 if (null === $user || !is_object($user)) {
157 $query = $this->entryRepository
->getBuilderForArchiveByUser($user->getId())
162 $query->useQueryCache(true);
163 $query->useResultCache(true);
164 $query->setResultCacheLifetime($this->lifeTime
);
166 $nbArchives = count($query->getArrayResult());
168 $interval = $user->getCreatedAt()->diff(new \
DateTime('now'));
169 $nbDays = (int) $interval->format('%a') ?: 1;
171 // force setlocale for date translation
172 setlocale(LC_TIME
, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
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),