]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Few optimizations and code readability for tag cloud controller
authorArthurHoaro <arthur@hoa.ro>
Sat, 16 May 2020 11:33:39 +0000 (13:33 +0200)
committerArthurHoaro <arthur@hoa.ro>
Thu, 23 Jul 2020 19:19:21 +0000 (21:19 +0200)
application/front/controllers/TagCloudController.php
index.php
tests/front/controller/TagCloudControllerTest.php
tpl/default/tag.cloud.html
tpl/vintage/tag.cloud.html

index 9389c2b03ad3ea783a1171190cbbac45869d4f39..93e3ae27c46ff2b423c1250f3b5b2fc2faa73346 100644 (file)
@@ -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
      *
index bf7090e39c1a3560cf192ae9862710fa4a6f5834..6ecb9a67448b9ef7efa9c9184859d04489120deb 100644 (file)
--- 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');
 
index 5cbf06a9ee9458b579ef27232b6411dafcfdc764..352bdee2344697379ea990c31fd87031c4cecedf 100644 (file)
@@ -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());
index 547d50188b31fcb35223da352fddb64bb0249bb8..bf5433573179cce250661fb45329525449d9ec2a 100644 (file)
@@ -48,7 +48,7 @@
 
     <div id="cloudtag" class="cloudtag-container">
       {loop="tags"}
-        <a href="?searchtags={$key|urlencode} {$search_tags|urlencode}" style="font-size:{$value.size}em;">{$key}</a
+        <a href="./?searchtags={$key|urlencode} {$search_tags|urlencode}" style="font-size:{$value.size}em;">{$key}</a
         ><a href="./add-tag/{$key|urlencode}" title="{'Filter by tag'|t}" class="count">{$value.count}</a>
         {loop="$value.tag_plugin"}
           {$value}
index 23aa381eb7c0a2f5dfe800288c22c63bff16de3e..4bc4bf88aaa69772863b4c04cf82b07767d6e6d4 100644 (file)
@@ -13,7 +13,7 @@
     <div id="cloudtag">
         {loop="$tags"}
             <a href="./add-tag/{$key|urlencode}" class="count">{$value.count}</a><a
-                href="?searchtags={$key|urlencode}" style="font-size:{$value.size}em;">{$key}</a>
+                href="./?searchtags={$key|urlencode}" style="font-size:{$value.size}em;">{$key}</a>
             {loop="$value.tag_plugin"}
                 {$value}
             {/loop}