aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Twig
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-09-03 19:09:28 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-09-03 19:09:28 +0200
commit59ddb9ae99b97a1a8fa3aa3770a4a2afef333699 (patch)
tree284253835f947c2e7ffefc9b8c09d95bd324f154 /src/Wallabag/CoreBundle/Twig
parent543da3e0b7592b1a00a7c5baec1554460609d63f (diff)
downloadwallabag-59ddb9ae99b97a1a8fa3aa3770a4a2afef333699.tar.gz
wallabag-59ddb9ae99b97a1a8fa3aa3770a4a2afef333699.tar.zst
wallabag-59ddb9ae99b97a1a8fa3aa3770a4a2afef333699.zip
Remove Twig globals
Twig Global function are called globally. This means even on a query to the api. Using a function we can decide when we want to call it. Also, remove previous `COUNT(e.id)` since it doesn't work on PostgreSQL ...
Diffstat (limited to 'src/Wallabag/CoreBundle/Twig')
-rw-r--r--src/Wallabag/CoreBundle/Twig/WallabagExtension.php63
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()