]>
Commit | Line | Data |
---|---|---|
72fcaf8a NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Twig; | |
4 | ||
8315130a NL |
5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
6 | use Wallabag\CoreBundle\Repository\EntryRepository; | |
429d86f3 | 7 | use Wallabag\CoreBundle\Repository\TagRepository; |
8315130a | 8 | |
f997ae6a | 9 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface |
72fcaf8a | 10 | { |
8315130a | 11 | private $tokenStorage; |
429d86f3 NL |
12 | private $entryRepository; |
13 | private $tagRepository; | |
14 | private $lifeTime; | |
8315130a | 15 | |
429d86f3 | 16 | public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0) |
8315130a | 17 | { |
429d86f3 NL |
18 | $this->entryRepository = $entryRepository; |
19 | $this->tagRepository = $tagRepository; | |
8315130a | 20 | $this->tokenStorage = $tokenStorage; |
429d86f3 | 21 | $this->lifeTime = $lifeTime; |
8315130a NL |
22 | } |
23 | ||
72fcaf8a NL |
24 | public function getFilters() |
25 | { | |
4094ea47 JB |
26 | return [ |
27 | new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), | |
28 | ]; | |
72fcaf8a NL |
29 | } |
30 | ||
59ddb9ae JB |
31 | public function getFunctions() |
32 | { | |
33 | return array( | |
34 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), | |
429d86f3 | 35 | new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), |
59ddb9ae JB |
36 | ); |
37 | } | |
38 | ||
72fcaf8a NL |
39 | public function removeWww($url) |
40 | { | |
cfb28c9d | 41 | return preg_replace('/^www\./i', '', $url); |
72fcaf8a NL |
42 | } |
43 | ||
59ddb9ae | 44 | /** |
234ad944 | 45 | * Return number of entries depending of the type (unread, archive, starred or all). |
59ddb9ae | 46 | * |
234ad944 | 47 | * @param string $type Type of entries to count |
59ddb9ae JB |
48 | * |
49 | * @return int | |
50 | */ | |
51 | public function countEntries($type) | |
8315130a NL |
52 | { |
53 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
54 | ||
55 | if (null === $user || !is_object($user)) { | |
5173fd1c | 56 | return 0; |
8315130a NL |
57 | } |
58 | ||
59ddb9ae JB |
59 | switch ($type) { |
60 | case 'starred': | |
429d86f3 | 61 | $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); |
59ddb9ae | 62 | break; |
0b0233b1 | 63 | |
59ddb9ae | 64 | case 'archive': |
429d86f3 | 65 | $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); |
59ddb9ae | 66 | break; |
0b0233b1 | 67 | |
59ddb9ae | 68 | case 'unread': |
429d86f3 | 69 | $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); |
59ddb9ae | 70 | break; |
0b0233b1 | 71 | |
59ddb9ae | 72 | case 'all': |
429d86f3 | 73 | $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); |
59ddb9ae | 74 | break; |
8315130a | 75 | |
59ddb9ae JB |
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 | ||
faa86e06 JB |
87 | $query->useQueryCache(true); |
88 | $query->useResultCache(true); | |
89 | $query->setResultCacheLifetime($this->lifeTime); | |
59ddb9ae | 90 | |
faa86e06 | 91 | return count($query->getArrayResult()); |
8315130a NL |
92 | } |
93 | ||
429d86f3 NL |
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)) { | |
5173fd1c | 104 | return 0; |
429d86f3 NL |
105 | } |
106 | ||
28987583 | 107 | return $this->tagRepository->countAllTags($user->getId()); |
429d86f3 NL |
108 | } |
109 | ||
72fcaf8a NL |
110 | public function getName() |
111 | { | |
112 | return 'wallabag_extension'; | |
113 | } | |
114 | } |