]>
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; | |
7 | ||
f997ae6a | 8 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface |
72fcaf8a | 9 | { |
8315130a NL |
10 | private $tokenStorage; |
11 | private $repository; | |
12 | ||
13 | public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null) | |
14 | { | |
15 | $this->repository = $repository; | |
16 | $this->tokenStorage = $tokenStorage; | |
17 | } | |
18 | ||
72fcaf8a NL |
19 | public function getFilters() |
20 | { | |
4094ea47 JB |
21 | return [ |
22 | new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), | |
23 | ]; | |
72fcaf8a NL |
24 | } |
25 | ||
59ddb9ae JB |
26 | public function getFunctions() |
27 | { | |
28 | return array( | |
29 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), | |
30 | ); | |
31 | } | |
32 | ||
72fcaf8a NL |
33 | public function removeWww($url) |
34 | { | |
cfb28c9d | 35 | return preg_replace('/^www\./i', '', $url); |
72fcaf8a NL |
36 | } |
37 | ||
59ddb9ae JB |
38 | /** |
39 | * Return number of entries depending of the type (unread, archive, starred or all) | |
40 | * | |
41 | * @param string $type Type of entries to count | |
42 | * | |
43 | * @return int | |
44 | */ | |
45 | public function countEntries($type) | |
8315130a NL |
46 | { |
47 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
48 | ||
49 | if (null === $user || !is_object($user)) { | |
543da3e0 | 50 | return []; |
8315130a NL |
51 | } |
52 | ||
59ddb9ae JB |
53 | switch ($type) { |
54 | case 'starred': | |
55 | $qb = $this->repository->getBuilderForStarredByUser($user->getId()); | |
56 | break; | |
0b0233b1 | 57 | |
59ddb9ae JB |
58 | case 'archive': |
59 | $qb = $this->repository->getBuilderForArchiveByUser($user->getId()); | |
60 | break; | |
0b0233b1 | 61 | |
59ddb9ae JB |
62 | case 'unread': |
63 | $qb = $this->repository->getBuilderForUnreadByUser($user->getId()); | |
64 | break; | |
0b0233b1 | 65 | |
59ddb9ae JB |
66 | case 'all': |
67 | $qb = $this->repository->getBuilderForAllByUser($user->getId()); | |
68 | break; | |
8315130a | 69 | |
59ddb9ae JB |
70 | default: |
71 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
72 | } | |
73 | ||
74 | // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id) | |
75 | // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function | |
76 | $query = $qb | |
77 | ->select('e.id') | |
78 | ->groupBy('e.id') | |
79 | ->getQuery(); | |
80 | ||
81 | $data =$this->repository | |
82 | ->enableCache($query) | |
83 | ->getArrayResult(); | |
84 | ||
85 | return count($data); | |
8315130a NL |
86 | } |
87 | ||
72fcaf8a NL |
88 | public function getName() |
89 | { | |
90 | return 'wallabag_extension'; | |
91 | } | |
92 | } |