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