diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-26 14:35:25 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | c266a89d0fbb0d60d2d7df0ec171b7cb022224f6 (patch) | |
tree | ef7b34378e19332f0fa44800caa78108aec1a6c7 /application/front | |
parent | 03340c18ead651ef9e11f883745695f2edafbae3 (diff) | |
download | Shaarli-c266a89d0fbb0d60d2d7df0ec171b7cb022224f6.tar.gz Shaarli-c266a89d0fbb0d60d2d7df0ec171b7cb022224f6.tar.zst Shaarli-c266a89d0fbb0d60d2d7df0ec171b7cb022224f6.zip |
Process tag cloud page through Slim controller
Diffstat (limited to 'application/front')
-rw-r--r-- | application/front/controllers/TagCloudController.php | 89 |
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 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller; | ||
6 | |||
7 | use Slim\Http\Request; | ||
8 | use 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 | */ | ||
17 | class 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 | } | ||