diff --git a/src/Wallabag/CoreBundle/Controller/DeveloperController.php b/src/Wallabag/CoreBundle/Controller/DeveloperController.php
index 1ea220e52..63386db0c 100644
--- a/src/Wallabag/CoreBundle/Controller/DeveloperController.php
+++ b/src/Wallabag/CoreBundle/Controller/DeveloperController.php
@@ -49,7 +49,7 @@ class DeveloperController extends Controller
$this->get('session')->getFlashBag()->add(
'notice',
- $this->get('translator')->trans('flashes.developer.notice.client_created', array('%name%' => $client->getName()))
+ $this->get('translator')->trans('flashes.developer.notice.client_created', ['%name%' => $client->getName()])
);
return $this->render('WallabagCoreBundle:Developer:client_parameters.html.twig', [
@@ -81,7 +81,7 @@ class DeveloperController extends Controller
$this->get('session')->getFlashBag()->add(
'notice',
- $this->get('translator')->trans('flashes.developer.notice.client_deleted', array('%name%' => $client->getName()))
+ $this->get('translator')->trans('flashes.developer.notice.client_deleted', ['%name%' => $client->getName()])
);
return $this->redirect($this->generateUrl('developer'));
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 f64c3da28..06ecbf3d2 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 @@
{% set currentRoute = app.request.attributes.get('_route') %}
- {{ 'menu.left.unread'|trans }} {{ unreadEntries }}
+ {{ 'menu.left.unread'|trans }} {{ count_entries('unread') }}
- {{ 'menu.left.starred'|trans }} {{ starredEntries }}
+ {{ 'menu.left.starred'|trans }} {{ count_entries('starred') }}
- {{ 'menu.left.archive'|trans }} {{ archivedEntries }}
+ {{ 'menu.left.archive'|trans }} {{ count_entries('archive') }}
- {{ 'menu.left.all_articles'|trans }} {{ allEntries }}
+ {{ 'menu.left.all_articles'|trans }} {{ count_entries('all') }}
{{ 'menu.left.tags'|trans }}
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
index 5c475d614..974b86a97 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
];
}
+ public function getFunctions()
+ {
+ return array(
+ new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
+ );
+ }
+
public function removeWww($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;
if (null === $user || !is_object($user)) {
- return array();
+ return [];
}
- $unreadEntries = $this->repository->enableCache(
- $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()
- );
+ switch ($type) {
+ case 'starred':
+ $qb = $this->repository->getBuilderForStarredByUser($user->getId());
+ break;
- $starredEntries = $this->repository->enableCache(
- $this->repository->getBuilderForStarredByUser($user->getId())->getQuery()
- );
+ case 'archive':
+ $qb = $this->repository->getBuilderForArchiveByUser($user->getId());
+ break;
- $archivedEntries = $this->repository->enableCache(
- $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()
- );
+ case 'unread':
+ $qb = $this->repository->getBuilderForUnreadByUser($user->getId());
+ break;
- $allEntries = $this->repository->enableCache(
- $this->repository->getBuilderForAllByUser($user->getId())->getQuery()
- );
+ case 'all':
+ $qb = $this->repository->getBuilderForAllByUser($user->getId());
+ break;
- return array(
- 'unreadEntries' => count($unreadEntries->getResult()),
- 'starredEntries' => count($starredEntries->getResult()),
- 'archivedEntries' => count($archivedEntries->getResult()),
- 'allEntries' => count($allEntries->getResult()),
- );
+ default:
+ throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
+ }
+
+ // 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()