aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorNicolas Lœuillet <nicolas@loeuillet.org>2016-10-09 21:27:47 +0200
committerGitHub <noreply@github.com>2016-10-09 21:27:47 +0200
commit47508f004fe9a17a8012187f37032f4155d507f4 (patch)
tree69e089f1bafa49a553b28cf2aefc4f5c0192697d /src
parente39aec3e38144f1a2ddb0027ec724e3dfd6f53f1 (diff)
parente08477803079d0097885e026797256da7fd30f6c (diff)
downloadwallabag-47508f004fe9a17a8012187f37032f4155d507f4.tar.gz
wallabag-47508f004fe9a17a8012187f37032f4155d507f4.tar.zst
wallabag-47508f004fe9a17a8012187f37032f4155d507f4.zip
Merge pull request #2410 from wallabag/tag-optim
Optimize tag list display
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php2
-rw-r--r--src/Wallabag/CoreBundle/Command/TagAllCommand.php5
-rw-r--r--src/Wallabag/CoreBundle/Controller/TagController.php19
-rw-r--r--src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php8
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php20
-rw-r--r--src/Wallabag/CoreBundle/Repository/TagRepository.php10
-rw-r--r--src/Wallabag/CoreBundle/Resources/views/themes/baggy/Tag/tags.html.twig2
-rw-r--r--src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig17
8 files changed, 69 insertions, 14 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index ed31c536..9997913d 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -387,7 +387,7 @@ class WallabagRestController extends FOSRestController
387 387
388 $tags = $this->getDoctrine() 388 $tags = $this->getDoctrine()
389 ->getRepository('WallabagCoreBundle:Tag') 389 ->getRepository('WallabagCoreBundle:Tag')
390 ->findAllTagsWithEntries($this->getUser()->getId()); 390 ->findAllTags($this->getUser()->getId());
391 391
392 $json = $this->get('serializer')->serialize($tags, 'json'); 392 $json = $this->get('serializer')->serialize($tags, 'json');
393 393
diff --git a/src/Wallabag/CoreBundle/Command/TagAllCommand.php b/src/Wallabag/CoreBundle/Command/TagAllCommand.php
index db1a9ab7..3f9bb04d 100644
--- a/src/Wallabag/CoreBundle/Command/TagAllCommand.php
+++ b/src/Wallabag/CoreBundle/Command/TagAllCommand.php
@@ -34,10 +34,13 @@ class TagAllCommand extends ContainerAwareCommand
34 } 34 }
35 $tagger = $this->getContainer()->get('wallabag_core.rule_based_tagger'); 35 $tagger = $this->getContainer()->get('wallabag_core.rule_based_tagger');
36 36
37 $output->write(sprintf('Tagging entries for user « <info>%s</info> »... ', $user->getUserName())); 37 $output->write(sprintf('Tagging entries for user « <comment>%s</comment> »... ', $user->getUserName()));
38 38
39 $entries = $tagger->tagAllForUser($user); 39 $entries = $tagger->tagAllForUser($user);
40 40
41 $output->writeln('<info>Done.</info>');
42 $output->write(sprintf('Persist entries ... ', $user->getUserName()));
43
41 $em = $this->getDoctrine()->getManager(); 44 $em = $this->getDoctrine()->getManager();
42 foreach ($entries as $entry) { 45 foreach ($entries as $entry) {
43 $em->persist($entry); 46 $em->persist($entry);
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php
index c5746734..5acc6852 100644
--- a/src/Wallabag/CoreBundle/Controller/TagController.php
+++ b/src/Wallabag/CoreBundle/Controller/TagController.php
@@ -86,10 +86,25 @@ class TagController extends Controller
86 { 86 {
87 $tags = $this->getDoctrine() 87 $tags = $this->getDoctrine()
88 ->getRepository('WallabagCoreBundle:Tag') 88 ->getRepository('WallabagCoreBundle:Tag')
89 ->findAllTagsWithEntries($this->getUser()->getId()); 89 ->findAllTags($this->getUser()->getId());
90
91 $flatTags = [];
92
93 foreach ($tags as $key => $tag) {
94 $nbEntries = $this->getDoctrine()
95 ->getRepository('WallabagCoreBundle:Entry')
96 ->countAllEntriesByUserIdAndTagId($this->getUser()->getId(), $tag['id']);
97
98 $flatTags[] = [
99 'id' => $tag['id'],
100 'label' => $tag['label'],
101 'slug' => $tag['slug'],
102 'nbEntries' => $nbEntries,
103 ];
104 }
90 105
91 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [ 106 return $this->render('WallabagCoreBundle:Tag:tags.html.twig', [
92 'tags' => $tags, 107 'tags' => $flatTags,
93 ]); 108 ]);
94 } 109 }
95 110
diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
index 239d09ae..b490e209 100644
--- a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
+++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
@@ -55,6 +55,7 @@ class RuleBasedTagger
55 { 55 {
56 $rules = $this->getRulesForUser($user); 56 $rules = $this->getRulesForUser($user);
57 $entries = []; 57 $entries = [];
58 $tagsCache = [];
58 59
59 foreach ($rules as $rule) { 60 foreach ($rules as $rule) {
60 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); 61 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
@@ -62,7 +63,12 @@ class RuleBasedTagger
62 63
63 foreach ($entries as $entry) { 64 foreach ($entries as $entry) {
64 foreach ($rule->getTags() as $label) { 65 foreach ($rule->getTags() as $label) {
65 $tag = $this->getTag($label); 66 // avoid new tag duplicate by manually caching them
67 if (!isset($tagsCache[$label])) {
68 $tagsCache[$label] = $this->getTag($label);
69 }
70
71 $tag = $tagsCache[$label];
66 72
67 $entry->addTag($tag); 73 $entry->addTag($tag);
68 } 74 }
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 75127b7d..cd2b47b9 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -309,4 +309,24 @@ class EntryRepository extends EntityRepository
309 309
310 return $qb->getQuery()->getSingleScalarResult(); 310 return $qb->getQuery()->getSingleScalarResult();
311 } 311 }
312
313 /**
314 * Count all entries for a tag and a user.
315 *
316 * @param int $userId
317 * @param int $tagId
318 *
319 * @return int
320 */
321 public function countAllEntriesByUserIdAndTagId($userId, $tagId)
322 {
323 $qb = $this->createQueryBuilder('e')
324 ->select('count(e.id)')
325 ->leftJoin('e.tags', 't')
326 ->where('e.user=:userId')->setParameter('userId', $userId)
327 ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
328 ;
329
330 return $qb->getQuery()->getSingleScalarResult();
331 }
312} 332}
diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php
index 9d127da7..e76878d4 100644
--- a/src/Wallabag/CoreBundle/Repository/TagRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php
@@ -33,19 +33,23 @@ class TagRepository extends EntityRepository
33 } 33 }
34 34
35 /** 35 /**
36 * Find all tags with associated entries per user. 36 * Find all tags per user.
37 * 37 *
38 * @param int $userId 38 * @param int $userId
39 * 39 *
40 * @return array 40 * @return array
41 */ 41 */
42 public function findAllTagsWithEntries($userId) 42 public function findAllTags($userId)
43 { 43 {
44 return $this->createQueryBuilder('t') 44 return $this->createQueryBuilder('t')
45 ->select('t.slug', 't.label', 't.id')
45 ->leftJoin('t.entries', 'e') 46 ->leftJoin('t.entries', 'e')
46 ->where('e.user = :userId')->setParameter('userId', $userId) 47 ->where('e.user = :userId')->setParameter('userId', $userId)
48 ->groupBy('t.slug')
49 ->addGroupBy('t.label')
50 ->addGroupBy('t.id')
47 ->getQuery() 51 ->getQuery()
48 ->getResult(); 52 ->getArrayResult();
49 } 53 }
50 54
51 /** 55 /**
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Tag/tags.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Tag/tags.html.twig
index 50043907..1e2c6b42 100644
--- a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Tag/tags.html.twig
+++ b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Tag/tags.html.twig
@@ -9,7 +9,7 @@
9 9
10 <ul> 10 <ul>
11 {% for tag in tags %} 11 {% for tag in tags %}
12 <li id="tag-{{ tag.id|e }}"><a href="{{ path('tag_entries', {'slug': tag.slug}) }}">{{tag.label}} ({{ tag.entries.getValues | length }})</a></li> 12 <li id="tag-{{ tag.id|e }}"><a href="{{ path('tag_entries', {'slug': tag.slug}) }}">{{tag.label}} ({{ tag.nbEntries | length }})</a></li>
13 {% endfor %} 13 {% endfor %}
14 </ul> 14 </ul>
15 15
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig
index 1690633a..96f4f990 100644
--- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig
+++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig
@@ -6,12 +6,19 @@
6 <div class="results clearfix"> 6 <div class="results clearfix">
7 <div class="nb-results left">{{ 'tag.list.number_on_the_page'|transchoice(tags|length) }}</div> 7 <div class="nb-results left">{{ 'tag.list.number_on_the_page'|transchoice(tags|length) }}</div>
8 </div> 8 </div>
9
9 <br /> 10 <br />
10 <ul class="row data"> 11
11 {% for tag in tags %} 12 <div class="row">
12 <li id="tag-{{ tag.id|e }}" class="col l4 m6 s12"><a href="{{ path('tag_entries', {'slug': tag.slug}) }}">{{tag.label}} ({{ tag.entries.getValues | length }})</a></li> 13 <ul class="card-tag-labels">
13 {% endfor %} 14 {% for tag in tags %}
14 </ul> 15 <li title="{{tag.label}} ({{ tag.nbEntries }})" id="tag-{{ tag.id }}" class="col l2 m2 s2">
16 <a href="{{ path('tag_entries', {'slug': tag.slug}) }}">{{tag.label}} ({{ tag.nbEntries }})</a>
17 </li>
18 {% endfor %}
19 </ul>
20 </div>
21
15 <div> 22 <div>
16 <a href="{{ path('untagged') }}">{{ 'tag.list.see_untagged_entries'|trans }}</a> 23 <a href="{{ path('untagged') }}">{{ 'tag.list.see_untagged_entries'|trans }}</a>
17 </div> 24 </div>