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