]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #2002 from wallabag/feature-display-itemsNumber
authorJeremy Benoist <j0k3r@users.noreply.github.com>
Sat, 3 Sep 2016 14:13:08 +0000 (16:13 +0200)
committerGitHub <noreply@github.com>
Sat, 3 Sep 2016 14:13:08 +0000 (16:13 +0200)
Feature display items number

app/Resources/static/themes/material/css/main.css
app/config/config.yml
app/config/config_dev.yml
app/config/services.yml
src/Wallabag/CoreBundle/DependencyInjection/Configuration.php
src/Wallabag/CoreBundle/DependencyInjection/WallabagCoreExtension.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
src/Wallabag/CoreBundle/Resources/config/services.yml
src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig
src/Wallabag/CoreBundle/Twig/WallabagExtension.php

index 68f3f2ee57e55c84605eba8ad3c15b7fd7bd1172..f4230734cf4e674d0c206fec20e47ce50812fbdd 100755 (executable)
@@ -308,6 +308,10 @@ nav input {
   margin: 0 1rem;
 }
 
+span.numberItems {
+  float: right;
+}
+
 /* ==========================================================================
  * 3 = Filters slider
  * ========================================================================== */
index 30fd60636376415c91c0da22cfa638090cf2ac8e..31bd8a8c0597a96cc533a7e7a27ccc402504694a 100644 (file)
@@ -49,6 +49,7 @@ wallabag_core:
     language: en
     rss_limit: 50
     reading_speed: 1
+    cache_lifetime: 10
 
 wallabag_user:
     registration_enabled: "%fosuser_registration%"
index 77840682dfb273d0fac99a5d3ad40190e498ac59..3b67d8f6965b50a14b1460c9bea2a9416c25de8f 100644 (file)
@@ -40,3 +40,11 @@ swiftmailer:
     transport: smtp
     host: 'localhost'
     port: 1025
+
+# If you want to use cache for queries used in WallabagExtension
+# Uncomment the following lines
+#doctrine:
+#    orm:
+#        metadata_cache_driver: apcu
+#        result_cache_driver: apcu
+#        query_cache_driver: apcu
index 480408d918c3e01ea34cc74ea61fd919ff7b82b0..95b8f26f5b7b0509239b03f9e2ac48d5cd0415c2 100644 (file)
@@ -16,6 +16,9 @@ services:
     wallabag.twig_extension:
         class: Wallabag\CoreBundle\Twig\WallabagExtension
         public: false
+        arguments:
+            - "@wallabag_core.entry_repository"
+            - "@security.token_storage"
         tags:
             - { name: twig.extension }
 
index d1bb9820beb3a4a498ca3a7b0b383a35e63e0683..d8141eea993ffbb277bf0954407ddeb2d774466e 100644 (file)
@@ -36,6 +36,9 @@ class Configuration implements ConfigurationInterface
                 ->end()
                 ->scalarNode('paypal_url')
                 ->end()
+                ->integerNode('cache_lifetime')
+                    ->defaultValue(10)
+                ->end()
             ->end()
         ;
 
index 7d08b73b78ceacf095e8c34a1ce61242e32f62d8..0cbde908d0f205f8a2f6e798316c18837866289e 100644 (file)
@@ -22,6 +22,7 @@ class WallabagCoreExtension extends Extension
         $container->setParameter('wallabag_core.reading_speed', $config['reading_speed']);
         $container->setParameter('wallabag_core.version', $config['version']);
         $container->setParameter('wallabag_core.paypal_url', $config['paypal_url']);
+        $container->setParameter('wallabag_core.cache_lifetime', $config['cache_lifetime']);
 
         $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
         $loader->load('services.yml');
index e5c21679935a51035a9df97a88486a27a6864e0e..24d1a57af0550eda5782c3fdd40d86f22c5aca24 100644 (file)
@@ -3,12 +3,15 @@
 namespace Wallabag\CoreBundle\Repository;
 
 use Doctrine\ORM\EntityRepository;
+use Doctrine\ORM\Query;
 use Pagerfanta\Adapter\DoctrineORMAdapter;
 use Pagerfanta\Pagerfanta;
 use Wallabag\CoreBundle\Entity\Tag;
 
 class EntryRepository extends EntityRepository
 {
+    private $lifeTime;
+
     /**
      * Return a query builder to used by other getBuilderFor* method.
      *
@@ -308,4 +311,25 @@ class EntryRepository extends EntityRepository
 
         return $qb->getQuery()->getSingleScalarResult();
     }
+
+    public function setLifeTime($lifeTime)
+    {
+        $this->lifeTime = $lifeTime;
+    }
+
+    /**
+     * Enable cache for a query.
+     *
+     * @param Query $query
+     *
+     * @return Query
+     */
+    public function enableCache(Query $query)
+    {
+        $query->useQueryCache(true);
+        $query->useResultCache(true);
+        $query->setResultCacheLifetime($this->lifeTime);
+
+        return $query;
+    }
 }
index e95ef4520f990cf174765179eda7dc197d53cd21..1c1457e705fee2200503b3a53128b22ab4c6a69f 100644 (file)
@@ -81,6 +81,8 @@ services:
         factory: [ "@doctrine.orm.default_entity_manager", getRepository ]
         arguments:
             - WallabagCoreBundle:Entry
+        calls:
+            - [ setLifeTime, [ "%wallabag_core.cache_lifetime%" ] ]
 
     wallabag_core.tag_repository:
         class: Wallabag\CoreBundle\Repository\TagRepository
index 50134357f942a47327a10b8c146f80c3244d94ea..f64c3da281bf5e19edf6a3dad50a06ec567c3160 100644 (file)
             {% set currentRoute = app.request.attributes.get('_route') %}
 
             <li class="bold {% if currentRoute == 'unread' or currentRoute == 'homepage' %}active{% endif %}">
-                <a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }}</a>
+                <a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }} <span class="numberItems grey-text">{{ unreadEntries }}</span></a>
             </li>
             <li class="bold {% if currentRoute == 'starred' %}active{% endif %}">
-                <a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }}</a>
+                <a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }} <span class="numberItems grey-text">{{ starredEntries }}</span></a>
             </li>
             <li class="bold {% if currentRoute == 'archive' %}active{% endif %}">
-                <a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }}</a>
+                <a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }} <span class="numberItems grey-text">{{ archivedEntries }}</span></a>
             </li>
             <li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}">
-                <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }}</a>
+                <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ allEntries }}</span></a>
             </li>
             <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}">
                 <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a>
index 1a308070672e057bea8277288c46684ca6e5eb29..5c475d614b5712e313f4b0a60d00717631c1c01d 100644 (file)
@@ -2,8 +2,20 @@
 
 namespace Wallabag\CoreBundle\Twig;
 
-class WallabagExtension extends \Twig_Extension
+use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
+use Wallabag\CoreBundle\Repository\EntryRepository;
+
+class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
 {
+    private $tokenStorage;
+    private $repository;
+
+    public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null)
+    {
+        $this->repository = $repository;
+        $this->tokenStorage = $tokenStorage;
+    }
+
     public function getFilters()
     {
         return [
@@ -16,6 +28,38 @@ class WallabagExtension extends \Twig_Extension
         return preg_replace('/^www\./i', '', $url);
     }
 
+    public function getGlobals()
+    {
+        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
+
+        if (null === $user || !is_object($user)) {
+            return array();
+        }
+
+        $unreadEntries = $this->repository->enableCache(
+            $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()
+        );
+
+        $starredEntries = $this->repository->enableCache(
+            $this->repository->getBuilderForStarredByUser($user->getId())->getQuery()
+        );
+
+        $archivedEntries = $this->repository->enableCache(
+            $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()
+        );
+
+        $allEntries = $this->repository->enableCache(
+            $this->repository->getBuilderForAllByUser($user->getId())->getQuery()
+        );
+
+        return array(
+            'unreadEntries' => count($unreadEntries->getResult()),
+            'starredEntries' => count($starredEntries->getResult()),
+            'archivedEntries' => count($archivedEntries->getResult()),
+            'allEntries' => count($allEntries->getResult()),
+        );
+    }
+
     public function getName()
     {
         return 'wallabag_extension';