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