diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 93 |
1 files changed, 92 insertions, 1 deletions
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 1a308070..fb4c7412 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php | |||
@@ -2,8 +2,25 @@ | |||
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 | |||
9 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface | ||
6 | { | 10 | { |
11 | private $tokenStorage; | ||
12 | private $entryRepository; | ||
13 | private $tagRepository; | ||
14 | private $lifeTime; | ||
15 | |||
16 | public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0) | ||
17 | { | ||
18 | $this->entryRepository = $entryRepository; | ||
19 | $this->tagRepository = $tagRepository; | ||
20 | $this->tokenStorage = $tokenStorage; | ||
21 | $this->lifeTime = $lifeTime; | ||
22 | } | ||
23 | |||
7 | public function getFilters() | 24 | public function getFilters() |
8 | { | 25 | { |
9 | return [ | 26 | return [ |
@@ -11,11 +28,85 @@ class WallabagExtension extends \Twig_Extension | |||
11 | ]; | 28 | ]; |
12 | } | 29 | } |
13 | 30 | ||
31 | public function getFunctions() | ||
32 | { | ||
33 | return array( | ||
34 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), | ||
35 | new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), | ||
36 | ); | ||
37 | } | ||
38 | |||
14 | public function removeWww($url) | 39 | public function removeWww($url) |
15 | { | 40 | { |
16 | return preg_replace('/^www\./i', '', $url); | 41 | return preg_replace('/^www\./i', '', $url); |
17 | } | 42 | } |
18 | 43 | ||
44 | /** | ||
45 | * Return number of entries depending of the type (unread, archive, starred or all). | ||
46 | * | ||
47 | * @param string $type Type of entries to count | ||
48 | * | ||
49 | * @return int | ||
50 | */ | ||
51 | public function countEntries($type) | ||
52 | { | ||
53 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | ||
54 | |||
55 | if (null === $user || !is_object($user)) { | ||
56 | return 0; | ||
57 | } | ||
58 | |||
59 | switch ($type) { | ||
60 | case 'starred': | ||
61 | $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); | ||
62 | break; | ||
63 | |||
64 | case 'archive': | ||
65 | $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); | ||
66 | break; | ||
67 | |||
68 | case 'unread': | ||
69 | $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); | ||
70 | break; | ||
71 | |||
72 | case 'all': | ||
73 | $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); | ||
74 | break; | ||
75 | |||
76 | default: | ||
77 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | ||
78 | } | ||
79 | |||
80 | // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id) | ||
81 | // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function | ||
82 | $query = $qb | ||
83 | ->select('e.id') | ||
84 | ->groupBy('e.id') | ||
85 | ->getQuery(); | ||
86 | |||
87 | $query->useQueryCache(true); | ||
88 | $query->useResultCache(true); | ||
89 | $query->setResultCacheLifetime($this->lifeTime); | ||
90 | |||
91 | return count($query->getArrayResult()); | ||
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 | return $this->tagRepository->countAllTags($user->getId()); | ||
108 | } | ||
109 | |||
19 | public function getName() | 110 | public function getName() |
20 | { | 111 | { |
21 | return 'wallabag_extension'; | 112 | return 'wallabag_extension'; |