1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-09-15 18:57:05 +00:00

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 ...
This commit is contained in:
Jeremy Benoist 2016-09-03 19:09:28 +02:00
parent 543da3e0b7
commit 59ddb9ae99
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
2 changed files with 48 additions and 23 deletions

View file

@ -35,16 +35,16 @@
{% set currentRoute = app.request.attributes.get('_route') %} {% set currentRoute = app.request.attributes.get('_route') %}
<li class="bold {% if currentRoute == 'unread' or currentRoute == 'homepage' %}active{% endif %}"> <li class="bold {% if currentRoute == 'unread' or currentRoute == 'homepage' %}active{% endif %}">
<a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }} <span class="numberItems grey-text">{{ unreadEntries }}</span></a> <a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }} <span class="numberItems grey-text">{{ count_entries('unread') }}</span></a>
</li> </li>
<li class="bold {% if currentRoute == 'starred' %}active{% endif %}"> <li class="bold {% if currentRoute == 'starred' %}active{% endif %}">
<a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }} <span class="numberItems grey-text">{{ starredEntries }}</span></a> <a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }} <span class="numberItems grey-text">{{ count_entries('starred') }}</span></a>
</li> </li>
<li class="bold {% if currentRoute == 'archive' %}active{% endif %}"> <li class="bold {% if currentRoute == 'archive' %}active{% endif %}">
<a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }} <span class="numberItems grey-text">{{ archivedEntries }}</span></a> <a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }} <span class="numberItems grey-text">{{ count_entries('archive') }}</span></a>
</li> </li>
<li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}"> <li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}">
<a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ allEntries }}</span></a> <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ count_entries('all') }}</span></a>
</li> </li>
<li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}"> <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}">
<a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a> <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a>

View file

@ -23,12 +23,26 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
]; ];
} }
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
);
}
public function removeWww($url) public function removeWww($url)
{ {
return preg_replace('/^www\./i', '', $url); return preg_replace('/^www\./i', '', $url);
} }
public function getGlobals() /**
* Return number of entries depending of the type (unread, archive, starred or all)
*
* @param string $type Type of entries to count
*
* @return int
*/
public function countEntries($type)
{ {
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
@ -36,28 +50,39 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
return []; return [];
} }
$unreadEntries = $this->repository->enableCache( switch ($type) {
$this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery() case 'starred':
); $qb = $this->repository->getBuilderForStarredByUser($user->getId());
break;
$starredEntries = $this->repository->enableCache( case 'archive':
$this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery() $qb = $this->repository->getBuilderForArchiveByUser($user->getId());
); break;
$archivedEntries = $this->repository->enableCache( case 'unread':
$this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery() $qb = $this->repository->getBuilderForUnreadByUser($user->getId());
); break;
$allEntries = $this->repository->enableCache( case 'all':
$this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery() $qb = $this->repository->getBuilderForAllByUser($user->getId());
); break;
return [ default:
'unreadEntries' => $unreadEntries->getSingleScalarResult(), throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
'starredEntries' => $starredEntries->getSingleScalarResult(), }
'archivedEntries' => $archivedEntries->getSingleScalarResult(),
'allEntries' => $allEntries->getSingleScalarResult(), // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
]; // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
$query = $qb
->select('e.id')
->groupBy('e.id')
->getQuery();
$data =$this->repository
->enableCache($query)
->getArrayResult();
return count($data);
} }
public function getName() public function getName()