]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controllers/TagCloudController.php
Handle tag filtering in the Bookmark service
[github/shaarli/Shaarli.git] / application / front / controllers / TagCloudController.php
CommitLineData
c266a89d
A
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) {
c266a89d
A
42 // Tag font size scaling:
43 // default 15 and 30 logarithm bases affect scaling,
44 // 2.2 and 0.8 are arbitrary font sizes in em.
45 $size = log($value, 15) / $logMaxCount * 2.2 + 0.8;
46 $tagList[$key] = [
47 'count' => $value,
48 'size' => number_format($size, 2, '.', ''),
49 ];
50 }
51
52 $searchTags = implode(' ', escape($filteringTags));
53 $data = [
54 'search_tags' => $searchTags,
55 'tags' => $tagList,
56 ];
57 $data = $this->executeHooks($data);
58 foreach ($data as $key => $value) {
59 $this->assignView($key, $value);
60 }
61
62 $searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
63 $this->assignView(
64 'pagetitle',
65 $searchTags. t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli')
66 );
67
68 return $response->write($this->render('tag.cloud'));
69 }
70
71 /**
72 * @param mixed[] $data Template data
73 *
74 * @return mixed[] Template data after active plugins render_picwall hook execution.
75 */
76 protected function executeHooks(array $data): array
77 {
78 $this->container->pluginManager->executeHooks(
79 'render_tagcloud',
80 $data,
81 ['loggedin' => $this->container->loginManager->isLoggedIn()]
82 );
83
84 return $data;
85 }
86}