]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Twig/WallabagExtension.php
530e0492486feae9679350438f09585cd6bcd1a5
[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\Repository\EntryRepository;
8 use Wallabag\CoreBundle\Repository\TagRepository;
9
10 class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
11 {
12 private $tokenStorage;
13 private $entryRepository;
14 private $tagRepository;
15 private $lifeTime;
16 private $translator;
17
18 public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator)
19 {
20 $this->entryRepository = $entryRepository;
21 $this->tagRepository = $tagRepository;
22 $this->tokenStorage = $tokenStorage;
23 $this->lifeTime = $lifeTime;
24 $this->translator = $translator;
25 }
26
27 public function getFilters()
28 {
29 return [
30 new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
31 new \Twig_SimpleFilter('removeScheme', [$this, 'removeScheme']),
32 ];
33 }
34
35 public function getFunctions()
36 {
37 return [
38 new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
39 new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
40 new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']),
41 ];
42 }
43
44 public function removeWww($url)
45 {
46 return preg_replace('/^www\./i', '', $url);
47 }
48
49 public function removeScheme($url)
50 {
51 return preg_replace('#^https?://#', '', $url);
52 }
53
54 /**
55 * Return number of entries depending of the type (unread, archive, starred or all).
56 *
57 * @param string $type Type of entries to count
58 *
59 * @return int
60 */
61 public function countEntries($type)
62 {
63 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
64
65 if (null === $user || !is_object($user)) {
66 return 0;
67 }
68
69 switch ($type) {
70 case 'starred':
71 $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
72 break;
73 case 'archive':
74 $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
75 break;
76 case 'unread':
77 $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
78 break;
79 case 'all':
80 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
81 break;
82 default:
83 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
84 }
85
86 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
87 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
88 $query = $qb
89 ->select('e.id')
90 ->groupBy('e.id')
91 ->getQuery();
92
93 $query->useQueryCache(true);
94 $query->useResultCache(true);
95 $query->setResultCacheLifetime($this->lifeTime);
96
97 return count($query->getArrayResult());
98 }
99
100 /**
101 * Return number of tags.
102 *
103 * @return int
104 */
105 public function countTags()
106 {
107 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
108
109 if (null === $user || !is_object($user)) {
110 return 0;
111 }
112
113 return $this->tagRepository->countAllTags($user->getId());
114 }
115
116 /**
117 * Display a single line about reading stats.
118 *
119 * @return string
120 */
121 public function displayStats()
122 {
123 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
124
125 if (null === $user || !is_object($user)) {
126 return 0;
127 }
128
129 $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId())
130 ->select('e.id')
131 ->groupBy('e.id')
132 ->getQuery();
133
134 $query->useQueryCache(true);
135 $query->useResultCache(true);
136 $query->setResultCacheLifetime($this->lifeTime);
137
138 $nbArchives = count($query->getArrayResult());
139
140 $interval = $user->getCreatedAt()->diff(new \DateTime('now'));
141 $nbDays = (int) $interval->format('%a') ?: 1;
142
143 // force setlocale for date translation
144 setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
145
146 return $this->translator->trans('footer.stats', [
147 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
148 '%nb_archives%' => $nbArchives,
149 '%per_day%' => round($nbArchives / $nbDays, 2),
150 ]);
151 }
152
153 public function getName()
154 {
155 return 'wallabag_extension';
156 }
157 }