aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/Utils.php2
-rw-r--r--application/front/controllers/TagCloudController.php89
-rw-r--r--application/security/SessionManager.php10
3 files changed, 100 insertions, 1 deletions
diff --git a/application/Utils.php b/application/Utils.php
index 4b7fc546..4e97cdda 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -87,7 +87,7 @@ function endsWith($haystack, $needle, $case = true)
87 * 87 *
88 * @param mixed $input Data to escape: a single string or an array of strings. 88 * @param mixed $input Data to escape: a single string or an array of strings.
89 * 89 *
90 * @return string escaped. 90 * @return string|array escaped.
91 */ 91 */
92function escape($input) 92function escape($input)
93{ 93{
diff --git a/application/front/controllers/TagCloudController.php b/application/front/controllers/TagCloudController.php
new file mode 100644
index 00000000..b6f4a0ce
--- /dev/null
+++ b/application/front/controllers/TagCloudController.php
@@ -0,0 +1,89 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Slim\Http\Request;
8use Slim\Http\Response;
9
10/**
11 * Class TagCloud
12 *
13 * Slim controller used to render the tag cloud page.
14 *
15 * @package Front\Controller
16 */
17class TagCloudController extends ShaarliController
18{
19 public function index(Request $request, Response $response): Response
20 {
21 if ($this->container->loginManager->isLoggedIn() === true) {
22 $visibility = $this->container->sessionManager->getSessionParameter('visibility');
23 }
24
25 $searchTags = $request->getQueryParam('searchtags');
26 $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : [];
27
28 $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null);
29
30 // We sort tags alphabetically, then choose a font size according to count.
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);
38
39 $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1;
40 $tagList = [];
41 foreach ($tags as $key => $value) {
42 if (in_array($key, $filteringTags)) {
43 continue;
44 }
45 // Tag font size scaling:
46 // default 15 and 30 logarithm bases affect scaling,
47 // 2.2 and 0.8 are arbitrary font sizes in em.
48 $size = log($value, 15) / $logMaxCount * 2.2 + 0.8;
49 $tagList[$key] = [
50 'count' => $value,
51 'size' => number_format($size, 2, '.', ''),
52 ];
53 }
54
55 $searchTags = implode(' ', escape($filteringTags));
56 $data = [
57 'search_tags' => $searchTags,
58 'tags' => $tagList,
59 ];
60 $data = $this->executeHooks($data);
61 foreach ($data as $key => $value) {
62 $this->assignView($key, $value);
63 }
64
65 $searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
66 $this->assignView(
67 'pagetitle',
68 $searchTags. t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli')
69 );
70
71 return $response->write($this->render('tag.cloud'));
72 }
73
74 /**
75 * @param mixed[] $data Template data
76 *
77 * @return mixed[] Template data after active plugins render_picwall hook execution.
78 */
79 protected function executeHooks(array $data): array
80 {
81 $this->container->pluginManager->executeHooks(
82 'render_tagcloud',
83 $data,
84 ['loggedin' => $this->container->loginManager->isLoggedIn()]
85 );
86
87 return $data;
88 }
89}
diff --git a/application/security/SessionManager.php b/application/security/SessionManager.php
index 994fcbe5..4ae99168 100644
--- a/application/security/SessionManager.php
+++ b/application/security/SessionManager.php
@@ -202,4 +202,14 @@ class SessionManager
202 { 202 {
203 return $this->session; 203 return $this->session;
204 } 204 }
205
206 /**
207 * @param mixed $default value which will be returned if the $key is undefined
208 *
209 * @return mixed Content stored in session
210 */
211 public function getSessionParameter(string $key, $default = null)
212 {
213 return $this->session[$key] ?? $default;
214 }
205} 215}