]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Twig; | |
4 | ||
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 | |
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 | ||
25 | public function getFilters() | |
26 | { | |
27 | return [ | |
28 | new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), | |
29 | ]; | |
30 | } | |
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 | ||
40 | public function removeWww($url) | |
41 | { | |
42 | return preg_replace('/^www\./i', '', $url); | |
43 | } | |
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 []; | |
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 []; | |
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 | ||
131 | public function getName() | |
132 | { | |
133 | return 'wallabag_extension'; | |
134 | } | |
135 | } |