]>
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 | ||
26 | public function removeWww($url) | |
27 | { | |
cfb28c9d | 28 | return preg_replace('/^www\./i', '', $url); |
72fcaf8a NL |
29 | } |
30 | ||
8315130a NL |
31 | public function getGlobals() |
32 | { | |
33 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
34 | ||
35 | if (null === $user || !is_object($user)) { | |
543da3e0 | 36 | return []; |
8315130a NL |
37 | } |
38 | ||
0b0233b1 | 39 | $unreadEntries = $this->repository->enableCache( |
543da3e0 | 40 | $this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery() |
0b0233b1 NL |
41 | ); |
42 | ||
43 | $starredEntries = $this->repository->enableCache( | |
543da3e0 | 44 | $this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery() |
0b0233b1 NL |
45 | ); |
46 | ||
47 | $archivedEntries = $this->repository->enableCache( | |
543da3e0 | 48 | $this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery() |
0b0233b1 NL |
49 | ); |
50 | ||
51 | $allEntries = $this->repository->enableCache( | |
543da3e0 | 52 | $this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery() |
0b0233b1 | 53 | ); |
8315130a | 54 | |
543da3e0 JB |
55 | return [ |
56 | 'unreadEntries' => $unreadEntries->getSingleScalarResult(), | |
57 | 'starredEntries' => $starredEntries->getSingleScalarResult(), | |
58 | 'archivedEntries' => $archivedEntries->getSingleScalarResult(), | |
59 | 'allEntries' => $allEntries->getSingleScalarResult(), | |
60 | ]; | |
8315130a NL |
61 | } |
62 | ||
72fcaf8a NL |
63 | public function getName() |
64 | { | |
65 | return 'wallabag_extension'; | |
66 | } | |
67 | } |