]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Process tag cloud page through Slim controller
authorArthurHoaro <arthur@hoa.ro>
Sun, 26 Jan 2020 13:35:25 +0000 (14:35 +0100)
committerArthurHoaro <arthur@hoa.ro>
Thu, 23 Jul 2020 19:19:21 +0000 (21:19 +0200)
application/Utils.php
application/front/controllers/TagCloudController.php [new file with mode: 0644]
application/security/SessionManager.php
doc/md/Translations.md
index.php
tpl/default/page.header.html
tpl/default/tag.cloud.html
tpl/default/tag.sort.html
tpl/vintage/page.header.html

index 4b7fc5464916495ec9f9189270edbd7d43d2b53c..4e97cddae26267f6e3c9b4e8a472c7a56aaf597b 100644 (file)
@@ -87,7 +87,7 @@ function endsWith($haystack, $needle, $case = true)
  *
  * @param mixed $input Data to escape: a single string or an array of strings.
  *
- * @return string escaped.
+ * @return string|array escaped.
  */
 function escape($input)
 {
diff --git a/application/front/controllers/TagCloudController.php b/application/front/controllers/TagCloudController.php
new file mode 100644 (file)
index 0000000..b6f4a0c
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Front\Controller;
+
+use Slim\Http\Request;
+use Slim\Http\Response;
+
+/**
+ * Class TagCloud
+ *
+ * Slim controller used to render the tag cloud page.
+ *
+ * @package Front\Controller
+ */
+class TagCloudController extends ShaarliController
+{
+    public function index(Request $request, Response $response): Response
+    {
+        if ($this->container->loginManager->isLoggedIn() === true) {
+            $visibility = $this->container->sessionManager->getSessionParameter('visibility');
+        }
+
+        $searchTags = $request->getQueryParam('searchtags');
+        $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : [];
+
+        $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);
+        }
+
+        alphabetical_sort($tags, false, true);
+
+        $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1;
+        $tagList = [];
+        foreach ($tags as $key => $value) {
+            if (in_array($key, $filteringTags)) {
+                continue;
+            }
+            // 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, '.', ''),
+            ];
+        }
+
+        $searchTags = implode(' ', escape($filteringTags));
+        $data = [
+            'search_tags' => $searchTags,
+            'tags' => $tagList,
+        ];
+        $data = $this->executeHooks($data);
+        foreach ($data as $key => $value) {
+            $this->assignView($key, $value);
+        }
+
+        $searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
+        $this->assignView(
+            'pagetitle',
+            $searchTags. t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli')
+        );
+
+        return $response->write($this->render('tag.cloud'));
+    }
+
+    /**
+     * @param mixed[] $data Template data
+     *
+     * @return mixed[] Template data after active plugins render_picwall hook execution.
+     */
+    protected function executeHooks(array $data): array
+    {
+        $this->container->pluginManager->executeHooks(
+            'render_tagcloud',
+            $data,
+            ['loggedin' => $this->container->loginManager->isLoggedIn()]
+        );
+
+        return $data;
+    }
+}
index 994fcbe52ceb30cd4086369c46c89b9d66832260..4ae991684ac5ffff6b3fd259077caa3db513a23e 100644 (file)
@@ -202,4 +202,14 @@ class SessionManager
     {
         return $this->session;
     }
+
+    /**
+     * @param mixed $default value which will be returned if the $key is undefined
+     *
+     * @return mixed Content stored in session
+     */
+    public function getSessionParameter(string $key, $default = null)
+    {
+        return $this->session[$key] ?? $default;
+    }
 }
index dfdd021e22e35f15b80d37a64fc7e12908384332..b8b7053ffc5ba9445f9997b257ccad76da8b6bf7 100644 (file)
@@ -44,7 +44,7 @@ http://<replace_domain>/?do=import
 http://<replace_domain>/login
 http://<replace_domain>/picture-wall
 http://<replace_domain>/?do=pluginadmin
-http://<replace_domain>/?do=tagcloud
+http://<replace_domain>/tag-cloud
 http://<replace_domain>/?do=taglist
 ```
 
index a42c844a8bac754d1e4fccf3118cb75f1460d5f3..83f1264f5264a4960d839d83ae579f72224440f4 100644 (file)
--- a/index.php
+++ b/index.php
@@ -616,49 +616,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
 
     // -------- Tag cloud
     if ($targetPage == Router::$PAGE_TAGCLOUD) {
-        $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
-        $filteringTags = isset($_GET['searchtags']) ? explode(' ', $_GET['searchtags']) : [];
-        $tags = $bookmarkService->bookmarksCountPerTag($filteringTags, $visibility);
-
-        // We sort tags alphabetically, then choose a font size according to count.
-        // First, find max value.
-        $maxcount = 0;
-        foreach ($tags as $value) {
-            $maxcount = max($maxcount, $value);
-        }
-
-        alphabetical_sort($tags, false, true);
-
-        $logMaxCount = $maxcount > 1 ? log($maxcount, 30) : 1;
-        $tagList = array();
-        foreach ($tags as $key => $value) {
-            if (in_array($key, $filteringTags)) {
-                continue;
-            }
-            // 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] = array(
-                'count' => $value,
-                'size' => number_format($size, 2, '.', ''),
-            );
-        }
-
-        $searchTags = implode(' ', escape($filteringTags));
-        $data = array(
-            'search_tags' => $searchTags,
-            'tags' => $tagList,
-        );
-        $pluginManager->executeHooks('render_tagcloud', $data, array('loggedin' => $loginManager->isLoggedIn()));
-
-        foreach ($data as $key => $value) {
-            $PAGE->assign($key, $value);
-        }
-
-        $searchTags = ! empty($searchTags) ? $searchTags .' - ' : '';
-        $PAGE->assign('pagetitle', $searchTags. t('Tag cloud') .' - '. $conf->get('general.title', 'Shaarli'));
-        $PAGE->renderPage('tag.cloud');
+        header('Location: ./tag-cloud');
         exit;
     }
 
@@ -1916,6 +1874,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('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag');
 })->add('\Shaarli\Front\ShaarliMiddleware');
 
index 2086aeb014f7a91a6c00de81fb4daf8c63d7e47b..ea89a2095db33cc738ad4325ed3876a075042633 100644 (file)
@@ -30,7 +30,7 @@
           </li>
         {/if}
         <li class="pure-menu-item" id="shaarli-menu-tags">
-          <a href="./?do=tagcloud" class="pure-menu-link">{'Tag cloud'|t}</a>
+          <a href="./tag-cloud" class="pure-menu-link">{'Tag cloud'|t}</a>
         </li>
         {if="$thumbnails_enabled"}
           <li class="pure-menu-item" id="shaarli-menu-picwall">
index 80dcebf5b753a444d72c3f91131ea41717a898f5..547d50188b31fcb35223da352fddb64bb0249bb8 100644 (file)
@@ -15,7 +15,7 @@
     <h2 class="window-title">{'Tag cloud'|t} - {$countTags} {'tags'|t}</h2>
     {if="!empty($search_tags)"}
     <p class="center">
-      <a href="?searchtags={$search_tags|urlencode}" class="pure-button pure-button-shaarli">
+      <a href="./?searchtags={$search_tags|urlencode}" class="pure-button pure-button-shaarli">
         {'List all links with those tags'|t}
       </a>
     </p>
index 7af4723d8393c891c57382c59f2c689c1d68040e..b7aa7d80dab337a5343e9deb60e13ba597bf480d 100644 (file)
@@ -1,7 +1,7 @@
 <div class="pure-g">
   <div class="pure-u-1 pure-alert pure-alert-success tag-sort">
     {'Sort by:'|t}
-    <a href="./?do=tagcloud">{'Cloud'|t}</a> &middot;
+    <a href="./tag-cloud">{'Cloud'|t}</a> &middot;
     <a href="./?do=taglist&sort=usage">{'Most used'|t}</a> &middot;
     <a href="./?do=taglist&sort=alpha">{'Alphabetical'|t}</a>
   </div>
index 8b9db353997c6c1d58fe694d4f6cdaf7874d1295..971fac9abdf7185c750796cfcf3daa340adeeaf4 100644 (file)
@@ -31,7 +31,7 @@
     {if="$showatom"}
     <li><a href="{$feedurl}?do=atom{$searchcrits}" class="nomobile">ATOM Feed</a></li>
     {/if}
-    <li><a href="./?do=tagcloud">Tag cloud</a></li>
+    <li><a href="./tag-cloud">Tag cloud</a></li>
     <li><a href="./picture-wall{function="ltrim($searchcrits, '&')"}">Picture wall</a></li>
     <li><a href="./?do=daily">Daily</a></li>
     {loop="$plugins_header.buttons_toolbar"}