diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 114 |
1 files changed, 113 insertions, 1 deletions
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 1a308070..3780b13e 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php | |||
@@ -2,8 +2,26 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Twig; | 3 | namespace Wallabag\CoreBundle\Twig; |
4 | 4 | ||
5 | class WallabagExtension extends \Twig_Extension | 5 | use Doctrine\ORM\Query; |
6 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
7 | use Wallabag\CoreBundle\Repository\EntryRepository; | ||
8 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
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 | |||
17 | public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0) | ||
18 | { | ||
19 | $this->entryRepository = $entryRepository; | ||
20 | $this->tagRepository = $tagRepository; | ||
21 | $this->tokenStorage = $tokenStorage; | ||
22 | $this->lifeTime = $lifeTime; | ||
23 | } | ||
24 | |||
7 | public function getFilters() | 25 | public function getFilters() |
8 | { | 26 | { |
9 | return [ | 27 | return [ |
@@ -11,11 +29,105 @@ class WallabagExtension extends \Twig_Extension | |||
11 | ]; | 29 | ]; |
12 | } | 30 | } |
13 | 31 | ||
32 | public function getFunctions() | ||
33 | { | ||
34 | return array( | ||
35 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), | ||
36 | new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), | ||
37 | ); | ||
38 | } | ||
39 | |||
14 | public function removeWww($url) | 40 | public function removeWww($url) |
15 | { | 41 | { |
16 | return preg_replace('/^www\./i', '', $url); | 42 | return preg_replace('/^www\./i', '', $url); |
17 | } | 43 | } |
18 | 44 | ||
45 | /** | ||
46 | * Return number of entries depending of the type (unread, archive, starred or all). | ||
47 | * | ||
48 | * @param string $type Type of entries to count | ||
49 | * | ||
50 | * @return int | ||
51 | */ | ||
52 | public function countEntries($type) | ||
53 | { | ||
54 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | ||
55 | |||
56 | if (null === $user || !is_object($user)) { | ||
57 | return 0; | ||
58 | } | ||
59 | |||
60 | switch ($type) { | ||
61 | case 'starred': | ||
62 | $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); | ||
63 | break; | ||
64 | |||
65 | case 'archive': | ||
66 | $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); | ||
67 | break; | ||
68 | |||
69 | case 'unread': | ||
70 | $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); | ||
71 | break; | ||
72 | |||
73 | case 'all': | ||
74 | $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); | ||
75 | break; | ||
76 | |||
77 | default: | ||
78 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | ||
79 | } | ||
80 | |||
81 | // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id) | ||
82 | // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function | ||
83 | $query = $qb | ||
84 | ->select('e.id') | ||
85 | ->groupBy('e.id') | ||
86 | ->getQuery(); | ||
87 | |||
88 | $data = $this->enableCache($query) | ||
89 | ->getArrayResult(); | ||
90 | |||
91 | return count($data); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Return number of tags. | ||
96 | * | ||
97 | * @return int | ||
98 | */ | ||
99 | public function countTags() | ||
100 | { | ||
101 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | ||
102 | |||
103 | if (null === $user || !is_object($user)) { | ||
104 | return 0; | ||
105 | } | ||
106 | |||
107 | $qb = $this->tagRepository->findAllTags($user->getId()); | ||
108 | |||
109 | $data = $this->enableCache($qb->getQuery()) | ||
110 | ->getArrayResult(); | ||
111 | |||
112 | return count($data); | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Enable cache for a query. | ||
117 | * | ||
118 | * @param Query $query | ||
119 | * | ||
120 | * @return Query | ||
121 | */ | ||
122 | private function enableCache(Query $query) | ||
123 | { | ||
124 | $query->useQueryCache(true); | ||
125 | $query->useResultCache(true); | ||
126 | $query->setResultCacheLifetime($this->lifeTime); | ||
127 | |||
128 | return $query; | ||
129 | } | ||
130 | |||
19 | public function getName() | 131 | public function getName() |
20 | { | 132 | { |
21 | return 'wallabag_extension'; | 133 | return 'wallabag_extension'; |