diff options
author | Paulino Michelazzo <paulino@michelazzo.com.br> | 2016-10-18 22:48:23 +0200 |
---|---|---|
committer | Paulino Michelazzo <paulino@michelazzo.com.br> | 2016-10-18 22:48:23 +0200 |
commit | 99731f0bb1f6fd2815eeb9af504ce86df927657b (patch) | |
tree | b080efc608d2bbd52b77a4a0067402007f50c5a8 /src/Wallabag/CoreBundle/Twig/WallabagExtension.php | |
parent | 3a3c6b866b52721431bed22426d9abfcd0d2dfe0 (diff) | |
parent | 7180aaed45dce62e40620a9e4b202526ebd6a3bb (diff) | |
download | wallabag-99731f0bb1f6fd2815eeb9af504ce86df927657b.tar.gz wallabag-99731f0bb1f6fd2815eeb9af504ce86df927657b.tar.zst wallabag-99731f0bb1f6fd2815eeb9af504ce86df927657b.zip |
Merge remote-tracking branch 'wallabag/master'
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 131 |
1 files changed, 130 insertions, 1 deletions
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 1a308070..783cde3e 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php | |||
@@ -2,8 +2,28 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Twig; | 3 | namespace Wallabag\CoreBundle\Twig; |
4 | 4 | ||
5 | class WallabagExtension extends \Twig_Extension | 5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
6 | use Wallabag\CoreBundle\Repository\EntryRepository; | ||
7 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
8 | use Symfony\Component\Translation\TranslatorInterface; | ||
9 | |||
10 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface | ||
6 | { | 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 | |||
7 | public function getFilters() | 27 | public function getFilters() |
8 | { | 28 | { |
9 | return [ | 29 | return [ |
@@ -11,11 +31,120 @@ class WallabagExtension extends \Twig_Extension | |||
11 | ]; | 31 | ]; |
12 | } | 32 | } |
13 | 33 | ||
34 | public function getFunctions() | ||
35 | { | ||
36 | return array( | ||
37 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), | ||
38 | new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), | ||
39 | new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']), | ||
40 | ); | ||
41 | } | ||
42 | |||
14 | public function removeWww($url) | 43 | public function removeWww($url) |
15 | { | 44 | { |
16 | return preg_replace('/^www\./i', '', $url); | 45 | return preg_replace('/^www\./i', '', $url); |
17 | } | 46 | } |
18 | 47 | ||
48 | /** | ||
49 | * Return number of entries depending of the type (unread, archive, starred or all). | ||
50 | * | ||
51 | * @param string $type Type of entries to count | ||
52 | * | ||
53 | * @return int | ||
54 | */ | ||
55 | public function countEntries($type) | ||
56 | { | ||
57 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | ||
58 | |||
59 | if (null === $user || !is_object($user)) { | ||
60 | return 0; | ||
61 | } | ||
62 | |||
63 | switch ($type) { | ||
64 | case 'starred': | ||
65 | $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); | ||
66 | break; | ||
67 | |||
68 | case 'archive': | ||
69 | $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); | ||
70 | break; | ||
71 | |||
72 | case 'unread': | ||
73 | $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); | ||
74 | break; | ||
75 | |||
76 | case 'all': | ||
77 | $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); | ||
78 | break; | ||
79 | |||
80 | default: | ||
81 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | ||
82 | } | ||
83 | |||
84 | // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id) | ||
85 | // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function | ||
86 | $query = $qb | ||
87 | ->select('e.id') | ||
88 | ->groupBy('e.id') | ||
89 | ->getQuery(); | ||
90 | |||
91 | $query->useQueryCache(true); | ||
92 | $query->useResultCache(true); | ||
93 | $query->setResultCacheLifetime($this->lifeTime); | ||
94 | |||
95 | return count($query->getArrayResult()); | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * Return number of tags. | ||
100 | * | ||
101 | * @return int | ||
102 | */ | ||
103 | public function countTags() | ||
104 | { | ||
105 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | ||
106 | |||
107 | if (null === $user || !is_object($user)) { | ||
108 | return 0; | ||
109 | } | ||
110 | |||
111 | return $this->tagRepository->countAllTags($user->getId()); | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * Display a single line about reading stats. | ||
116 | * | ||
117 | * @return string | ||
118 | */ | ||
119 | public function displayStats() | ||
120 | { | ||
121 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | ||
122 | |||
123 | if (null === $user || !is_object($user)) { | ||
124 | return 0; | ||
125 | } | ||
126 | |||
127 | $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId()) | ||
128 | ->select('e.id') | ||
129 | ->groupBy('e.id') | ||
130 | ->getQuery(); | ||
131 | |||
132 | $query->useQueryCache(true); | ||
133 | $query->useResultCache(true); | ||
134 | $query->setResultCacheLifetime($this->lifeTime); | ||
135 | |||
136 | $nbArchives = count($query->getArrayResult()); | ||
137 | |||
138 | $interval = $user->getCreatedAt()->diff(new \DateTime('now')); | ||
139 | $nbDays = (int) $interval->format('%a') ?: 1; | ||
140 | |||
141 | return $this->translator->trans('footer.stats', [ | ||
142 | '%user_creation%' => $user->getCreatedAt()->format('F jS, Y'), | ||
143 | '%nb_archives%' => $nbArchives, | ||
144 | '%per_day%' => round($nbArchives / $nbDays, 2), | ||
145 | ]); | ||
146 | } | ||
147 | |||
19 | public function getName() | 148 | public function getName() |
20 | { | 149 | { |
21 | return 'wallabag_extension'; | 150 | return 'wallabag_extension'; |