3 namespace Wallabag\CoreBundle\Twig
;
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
;
13 class WallabagExtension
extends \Twig_Extension
implements \Twig_Extension_GlobalsInterface
15 private $tokenStorage;
16 private $entryRepository;
17 private $tagRepository;
18 private $notificationRepository;
20 private $nbNotifications;
23 public function __construct(EntryRepository
$entryRepository, TagRepository
$tagRepository, NotificationRepository
$notificationRepository, TokenStorageInterface
$tokenStorage, $lifeTime, $nbNotifications, TranslatorInterface
$translator)
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;
34 public function getFilters()
37 new \
Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
38 new \
Twig_SimpleFilter('unread_notif', [$this, 'unreadNotif']),
42 public function getFunctions()
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'])
52 public function removeWww($url)
54 return preg_replace('/^www\./i', '', $url);
61 public function unreadNotif($notifs)
63 return array_filter($notifs, function (NotificationInterface
$notif) {
64 return !$notif->isRead();
69 * Return number of entries depending of the type (unread, archive, starred or all).
71 * @param string $type Type of entries to count
75 public function countEntries($type)
77 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
79 if (null === $user || !is_object($user)) {
85 $qb = $this->entryRepository
->getBuilderForStarredByUser($user->getId());
89 $qb = $this->entryRepository
->getBuilderForArchiveByUser($user->getId());
93 $qb = $this->entryRepository
->getBuilderForUnreadByUser($user->getId());
97 $qb = $this->entryRepository
->getBuilderForAllByUser($user->getId());
101 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
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
111 $query->useQueryCache(true);
112 $query->useResultCache(true);
113 $query->setResultCacheLifetime($this->lifeTime
);
115 return count($query->getArrayResult());
119 * Return number of tags.
123 public function countTags()
125 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
127 if (null === $user || !is_object($user)) {
131 return $this->tagRepository
->countAllTags($user->getId());
134 public function getNotifications()
136 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
138 if (null === $user || !is_object($user)) {
142 return $this->notificationRepository
->findBy(
143 ['user' => $user->getId()],
144 ['timestamp' => 'DESC'],
145 $this->nbNotifications
150 * Display a single line about reading stats.
154 public function displayStats()
156 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
158 if (null === $user || !is_object($user)) {
162 $query = $this->entryRepository
->getBuilderForArchiveByUser($user->getId())
167 $query->useQueryCache(true);
168 $query->useResultCache(true);
169 $query->setResultCacheLifetime($this->lifeTime
);
171 $nbArchives = count($query->getArrayResult());
173 $interval = $user->getCreatedAt()->diff(new \
DateTime('now'));
174 $nbDays = (int) $interval->format('%a') ?: 1;
176 // force setlocale for date translation
177 setlocale(LC_TIME
, strtolower($user->getConfig()->getLanguage()).'_'.strtoupper(strtolower($user->getConfig()->getLanguage())));
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),