diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig')
-rw-r--r-- | src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 63 |
1 files changed, 44 insertions, 19 deletions
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 93640dc6..d6ac6117 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php | |||
@@ -23,12 +23,26 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
23 | ]; | 23 | ]; |
24 | } | 24 | } |
25 | 25 | ||
26 | public function getFunctions() | ||
27 | { | ||
28 | return array( | ||
29 | new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), | ||
30 | ); | ||
31 | } | ||
32 | |||
26 | public function removeWww($url) | 33 | public function removeWww($url) |
27 | { | 34 | { |
28 | return preg_replace('/^www\./i', '', $url); | 35 | return preg_replace('/^www\./i', '', $url); |
29 | } | 36 | } |
30 | 37 | ||
31 | public function getGlobals() | 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) | ||
32 | { | 46 | { |
33 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | 47 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; |
34 | 48 | ||
@@ -36,28 +50,39 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa | |||
36 | return []; | 50 | return []; |
37 | } | 51 | } |
38 | 52 | ||
39 | $unreadEntries = $this->repository->enableCache( | 53 | switch ($type) { |
40 | $this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery() | 54 | case 'starred': |
41 | ); | 55 | $qb = $this->repository->getBuilderForStarredByUser($user->getId()); |
56 | break; | ||
42 | 57 | ||
43 | $starredEntries = $this->repository->enableCache( | 58 | case 'archive': |
44 | $this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery() | 59 | $qb = $this->repository->getBuilderForArchiveByUser($user->getId()); |
45 | ); | 60 | break; |
46 | 61 | ||
47 | $archivedEntries = $this->repository->enableCache( | 62 | case 'unread': |
48 | $this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery() | 63 | $qb = $this->repository->getBuilderForUnreadByUser($user->getId()); |
49 | ); | 64 | break; |
50 | 65 | ||
51 | $allEntries = $this->repository->enableCache( | 66 | case 'all': |
52 | $this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery() | 67 | $qb = $this->repository->getBuilderForAllByUser($user->getId()); |
53 | ); | 68 | break; |
54 | 69 | ||
55 | return [ | 70 | default: |
56 | 'unreadEntries' => $unreadEntries->getSingleScalarResult(), | 71 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); |
57 | 'starredEntries' => $starredEntries->getSingleScalarResult(), | 72 | } |
58 | 'archivedEntries' => $archivedEntries->getSingleScalarResult(), | 73 | |
59 | 'allEntries' => $allEntries->getSingleScalarResult(), | 74 | // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id) |
60 | ]; | 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); | ||
61 | } | 86 | } |
62 | 87 | ||
63 | public function getName() | 88 | public function getName() |