aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'application/front/controllers')
-rw-r--r--application/front/controllers/TagCloudController.php89
1 files changed, 89 insertions, 0 deletions
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}