From 3772298ee7d8d0708f4e72798600accafa17740b Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 16 May 2020 13:33:39 +0200 Subject: [PATCH] Few optimizations and code readability for tag cloud controller --- .../front/controllers/TagCloudController.php | 52 +++++++++++-------- index.php | 2 +- .../controller/TagCloudControllerTest.php | 6 +-- tpl/default/tag.cloud.html | 2 +- tpl/vintage/tag.cloud.html | 2 +- 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; */ class TagCloudController extends ShaarliController { - public function index(Request $request, Response $response): Response + /** + * Display the tag cloud through the template engine. + * This controller a few filters: + * - Visibility stored in the session for logged in users + * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark + */ + public function cloud(Request $request, Response $response): Response { if ($this->container->loginManager->isLoggedIn() === true) { $visibility = $this->container->sessionManager->getSessionParameter('visibility'); @@ -27,27 +33,10 @@ class TagCloudController extends ShaarliController $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null); - // We sort tags alphabetically, then choose a font size according to count. - // First, find max value. - $maxCount = 0; - foreach ($tags as $count) { - $maxCount = max($maxCount, $count); - } - + // TODO: the sorting should be handled by bookmarkService instead of the controller alphabetical_sort($tags, false, true); - $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1; - $tagList = []; - foreach ($tags as $key => $value) { - // Tag font size scaling: - // default 15 and 30 logarithm bases affect scaling, - // 2.2 and 0.8 are arbitrary font sizes in em. - $size = log($value, 15) / $logMaxCount * 2.2 + 0.8; - $tagList[$key] = [ - 'count' => $value, - 'size' => number_format($size, 2, '.', ''), - ]; - } + $tagList = $this->formatTagsForCloud($tags); $searchTags = implode(' ', escape($filteringTags)); $data = [ @@ -62,12 +51,33 @@ class TagCloudController extends ShaarliController $searchTags = !empty($searchTags) ? $searchTags .' - ' : ''; $this->assignView( 'pagetitle', - $searchTags. t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli') + $searchTags . t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli') ); return $response->write($this->render('tag.cloud')); } + protected function formatTagsForCloud(array $tags): array + { + // We sort tags alphabetically, then choose a font size according to count. + // First, find max value. + $maxCount = count($tags) > 0 ? max($tags) : 0; + $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1; + $tagList = []; + foreach ($tags as $key => $value) { + // Tag font size scaling: + // default 15 and 30 logarithm bases affect scaling, + // 2.2 and 0.8 are arbitrary font sizes in em. + $size = log($value, 15) / $logMaxCount * 2.2 + 0.8; + $tagList[$key] = [ + 'count' => $value, + 'size' => number_format($size, 2, '.', ''), + ]; + } + + return $tagList; + } + /** * @param mixed[] $data Template data * 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 () { $this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login'); $this->get('/logout', '\Shaarli\Front\Controller\LogoutController:index')->setName('logout'); $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall'); - $this->get('/tag-cloud', '\Shaarli\Front\Controller\TagCloudController:index')->setName('tagcloud'); + $this->get('/tag-cloud', '\Shaarli\Front\Controller\TagCloudController:cloud')->setName('tagcloud'); $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag'); })->add('\Shaarli\Front\ShaarliMiddleware'); 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 }) ; - $result = $this->controller->index($request, $response); + $result = $this->controller->cloud($request, $response); static::assertSame(200, $result->getStatusCode()); static::assertSame('tag.cloud', (string) $result->getBody()); @@ -147,7 +147,7 @@ class TagCloudControllerTest extends TestCase }) ; - $result = $this->controller->index($request, $response); + $result = $this->controller->cloud($request, $response); static::assertSame(200, $result->getStatusCode()); static::assertSame('tag.cloud', (string) $result->getBody()); @@ -198,7 +198,7 @@ class TagCloudControllerTest extends TestCase }) ; - $result = $this->controller->index($request, $response); + $result = $this->controller->cloud($request, $response); static::assertSame(200, $result->getStatusCode()); 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 @@
{loop="tags"} - {$key}{$key}{$value.count} {loop="$value.tag_plugin"} {$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 @@
{loop="$tags"} {$value.count}{$key} + href="./?searchtags={$key|urlencode}" style="font-size:{$value.size}em;">{$key} {loop="$value.tag_plugin"} {$value} {/loop} -- 2.41.0