diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 5c475d61..974b86a9 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php | |||
@@ -23,41 +23,66 @@ 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 | ||
35 | if (null === $user || !is_object($user)) { | 49 | if (null === $user || !is_object($user)) { |
36 | return array(); | 50 | return []; |
37 | } | 51 | } |
38 | 52 | ||
39 | $unreadEntries = $this->repository->enableCache( | 53 | switch ($type) { |
40 | $this->repository->getBuilderForUnreadByUser($user->getId())->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())->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())->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())->getQuery() | 67 | $qb = $this->repository->getBuilderForAllByUser($user->getId()); |
53 | ); | 68 | break; |
54 | 69 | ||
55 | return array( | 70 | default: |
56 | 'unreadEntries' => count($unreadEntries->getResult()), | 71 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); |
57 | 'starredEntries' => count($starredEntries->getResult()), | 72 | } |
58 | 'archivedEntries' => count($archivedEntries->getResult()), | 73 | |
59 | 'allEntries' => count($allEntries->getResult()), | 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() |