aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-16 13:33:39 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit3772298ee7d8d0708f4e72798600accafa17740b (patch)
treecec625b67642264973bbe856ddf4ff97187aabea
parentc79473bd84ab5aba7836d2caaf61847cabaf1e53 (diff)
downloadShaarli-3772298ee7d8d0708f4e72798600accafa17740b.tar.gz
Shaarli-3772298ee7d8d0708f4e72798600accafa17740b.tar.zst
Shaarli-3772298ee7d8d0708f4e72798600accafa17740b.zip
Few optimizations and code readability for tag cloud controller
-rw-r--r--application/front/controllers/TagCloudController.php52
-rw-r--r--index.php2
-rw-r--r--tests/front/controller/TagCloudControllerTest.php6
-rw-r--r--tpl/default/tag.cloud.html2
-rw-r--r--tpl/vintage/tag.cloud.html2
5 files changed, 37 insertions, 27 deletions
diff --git a/application/front/controllers/TagCloudController.php b/application/front/controllers/TagCloudController.php
index 9389c2b0..93e3ae27 100644
--- a/application/front/controllers/TagCloudController.php
+++ b/application/front/controllers/TagCloudController.php
@@ -16,7 +16,13 @@ use Slim\Http\Response;
16 */ 16 */
17class TagCloudController extends ShaarliController 17class TagCloudController extends ShaarliController
18{ 18{
19 public function index(Request $request, Response $response): Response 19 /**
20 * Display the tag cloud through the template engine.
21 * This controller a few filters:
22 * - Visibility stored in the session for logged in users
23 * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark
24 */
25 public function cloud(Request $request, Response $response): Response
20 { 26 {
21 if ($this->container->loginManager->isLoggedIn() === true) { 27 if ($this->container->loginManager->isLoggedIn() === true) {
22 $visibility = $this->container->sessionManager->getSessionParameter('visibility'); 28 $visibility = $this->container->sessionManager->getSessionParameter('visibility');
@@ -27,27 +33,10 @@ class TagCloudController extends ShaarliController
27 33
28 $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null); 34 $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null);
29 35
30 // We sort tags alphabetically, then choose a font size according to count. 36 // TODO: the sorting should be handled by bookmarkService instead of the controller
31 // First, find max value.
32 $maxCount = 0;
33 foreach ($tags as $count) {
34 $maxCount = max($maxCount, $count);
35 }
36
37 alphabetical_sort($tags, false, true); 37 alphabetical_sort($tags, false, true);
38 38
39 $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1; 39 $tagList = $this->formatTagsForCloud($tags);
40 $tagList = [];
41 foreach ($tags as $key => $value) {
42 // Tag font size scaling:
43 // default 15 and 30 logarithm bases affect scaling,
44 // 2.2 and 0.8 are arbitrary font sizes in em.
45 $size = log($value, 15) / $logMaxCount * 2.2 + 0.8;
46 $tagList[$key] = [
47 'count' => $value,
48 'size' => number_format($size, 2, '.', ''),
49 ];
50 }
51 40
52 $searchTags = implode(' ', escape($filteringTags)); 41 $searchTags = implode(' ', escape($filteringTags));
53 $data = [ 42 $data = [
@@ -62,12 +51,33 @@ class TagCloudController extends ShaarliController
62 $searchTags = !empty($searchTags) ? $searchTags .' - ' : ''; 51 $searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
63 $this->assignView( 52 $this->assignView(
64 'pagetitle', 53 'pagetitle',
65 $searchTags. t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli') 54 $searchTags . t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli')
66 ); 55 );
67 56
68 return $response->write($this->render('tag.cloud')); 57 return $response->write($this->render('tag.cloud'));
69 } 58 }
70 59
60 protected function formatTagsForCloud(array $tags): array
61 {
62 // We sort tags alphabetically, then choose a font size according to count.
63 // First, find max value.
64 $maxCount = count($tags) > 0 ? max($tags) : 0;
65 $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1;
66 $tagList = [];
67 foreach ($tags as $key => $value) {
68 // Tag font size scaling:
69 // default 15 and 30 logarithm bases affect scaling,
70 // 2.2 and 0.8 are arbitrary font sizes in em.
71 $size = log($value, 15) / $logMaxCount * 2.2 + 0.8;
72 $tagList[$key] = [
73 'count' => $value,
74 'size' => number_format($size, 2, '.', ''),
75 ];
76 }
77
78 return $tagList;
79 }
80
71 /** 81 /**
72 * @param mixed[] $data Template data 82 * @param mixed[] $data Template data
73 * 83 *
diff --git a/index.php b/index.php
index bf7090e3..6ecb9a67 100644
--- a/index.php
+++ b/index.php
@@ -1869,7 +1869,7 @@ $app->group('', function () {
1869 $this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login'); 1869 $this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login');
1870 $this->get('/logout', '\Shaarli\Front\Controller\LogoutController:index')->setName('logout'); 1870 $this->get('/logout', '\Shaarli\Front\Controller\LogoutController:index')->setName('logout');
1871 $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall'); 1871 $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall');
1872 $this->get('/tag-cloud', '\Shaarli\Front\Controller\TagCloudController:index')->setName('tagcloud'); 1872 $this->get('/tag-cloud', '\Shaarli\Front\Controller\TagCloudController:cloud')->setName('tagcloud');
1873 $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag'); 1873 $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag');
1874})->add('\Shaarli\Front\ShaarliMiddleware'); 1874})->add('\Shaarli\Front\ShaarliMiddleware');
1875 1875
diff --git a/tests/front/controller/TagCloudControllerTest.php b/tests/front/controller/TagCloudControllerTest.php
index 5cbf06a9..352bdee2 100644
--- a/tests/front/controller/TagCloudControllerTest.php
+++ b/tests/front/controller/TagCloudControllerTest.php
@@ -73,7 +73,7 @@ class TagCloudControllerTest extends TestCase
73 }) 73 })
74 ; 74 ;
75 75
76 $result = $this->controller->index($request, $response); 76 $result = $this->controller->cloud($request, $response);
77 77
78 static::assertSame(200, $result->getStatusCode()); 78 static::assertSame(200, $result->getStatusCode());
79 static::assertSame('tag.cloud', (string) $result->getBody()); 79 static::assertSame('tag.cloud', (string) $result->getBody());
@@ -147,7 +147,7 @@ class TagCloudControllerTest extends TestCase
147 }) 147 })
148 ; 148 ;
149 149
150 $result = $this->controller->index($request, $response); 150 $result = $this->controller->cloud($request, $response);
151 151
152 static::assertSame(200, $result->getStatusCode()); 152 static::assertSame(200, $result->getStatusCode());
153 static::assertSame('tag.cloud', (string) $result->getBody()); 153 static::assertSame('tag.cloud', (string) $result->getBody());
@@ -198,7 +198,7 @@ class TagCloudControllerTest extends TestCase
198 }) 198 })
199 ; 199 ;
200 200
201 $result = $this->controller->index($request, $response); 201 $result = $this->controller->cloud($request, $response);
202 202
203 static::assertSame(200, $result->getStatusCode()); 203 static::assertSame(200, $result->getStatusCode());
204 static::assertSame('tag.cloud', (string) $result->getBody()); 204 static::assertSame('tag.cloud', (string) $result->getBody());
diff --git a/tpl/default/tag.cloud.html b/tpl/default/tag.cloud.html
index 547d5018..bf543357 100644
--- a/tpl/default/tag.cloud.html
+++ b/tpl/default/tag.cloud.html
@@ -48,7 +48,7 @@
48 48
49 <div id="cloudtag" class="cloudtag-container"> 49 <div id="cloudtag" class="cloudtag-container">
50 {loop="tags"} 50 {loop="tags"}
51 <a href="?searchtags={$key|urlencode} {$search_tags|urlencode}" style="font-size:{$value.size}em;">{$key}</a 51 <a href="./?searchtags={$key|urlencode} {$search_tags|urlencode}" style="font-size:{$value.size}em;">{$key}</a
52 ><a href="./add-tag/{$key|urlencode}" title="{'Filter by tag'|t}" class="count">{$value.count}</a> 52 ><a href="./add-tag/{$key|urlencode}" title="{'Filter by tag'|t}" class="count">{$value.count}</a>
53 {loop="$value.tag_plugin"} 53 {loop="$value.tag_plugin"}
54 {$value} 54 {$value}
diff --git a/tpl/vintage/tag.cloud.html b/tpl/vintage/tag.cloud.html
index 23aa381e..4bc4bf88 100644
--- a/tpl/vintage/tag.cloud.html
+++ b/tpl/vintage/tag.cloud.html
@@ -13,7 +13,7 @@
13 <div id="cloudtag"> 13 <div id="cloudtag">
14 {loop="$tags"} 14 {loop="$tags"}
15 <a href="./add-tag/{$key|urlencode}" class="count">{$value.count}</a><a 15 <a href="./add-tag/{$key|urlencode}" class="count">{$value.count}</a><a
16 href="?searchtags={$key|urlencode}" style="font-size:{$value.size}em;">{$key}</a> 16 href="./?searchtags={$key|urlencode}" style="font-size:{$value.size}em;">{$key}</a>
17 {loop="$value.tag_plugin"} 17 {loop="$value.tag_plugin"}
18 {$value} 18 {$value}
19 {/loop} 19 {/loop}