aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag
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
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')
-rw-r--r--src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig8
-rw-r--r--src/Wallabag/CoreBundle/Twig/WallabagExtension.php63
2 files changed, 48 insertions, 23 deletions
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig
index f64c3da2..06ecbf3d 100644
--- a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig
+++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig
@@ -35,16 +35,16 @@
35 {% set currentRoute = app.request.attributes.get('_route') %} 35 {% set currentRoute = app.request.attributes.get('_route') %}
36 36
37 <li class="bold {% if currentRoute == 'unread' or currentRoute == 'homepage' %}active{% endif %}"> 37 <li class="bold {% if currentRoute == 'unread' or currentRoute == 'homepage' %}active{% endif %}">
38 <a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }} <span class="numberItems grey-text">{{ unreadEntries }}</span></a> 38 <a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }} <span class="numberItems grey-text">{{ count_entries('unread') }}</span></a>
39 </li> 39 </li>
40 <li class="bold {% if currentRoute == 'starred' %}active{% endif %}"> 40 <li class="bold {% if currentRoute == 'starred' %}active{% endif %}">
41 <a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }} <span class="numberItems grey-text">{{ starredEntries }}</span></a> 41 <a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }} <span class="numberItems grey-text">{{ count_entries('starred') }}</span></a>
42 </li> 42 </li>
43 <li class="bold {% if currentRoute == 'archive' %}active{% endif %}"> 43 <li class="bold {% if currentRoute == 'archive' %}active{% endif %}">
44 <a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }} <span class="numberItems grey-text">{{ archivedEntries }}</span></a> 44 <a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }} <span class="numberItems grey-text">{{ count_entries('archive') }}</span></a>
45 </li> 45 </li>
46 <li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}"> 46 <li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}">
47 <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ allEntries }}</span></a> 47 <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ count_entries('all') }}</span></a>
48 </li> 48 </li>
49 <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}"> 49 <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}">
50 <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a> 50 <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a>
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()