]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controller/visitor/TagCloudController.php
Initialize admin Slim controllers
[github/shaarli/Shaarli.git] / application / front / controller / visitor / TagCloudController.php
CommitLineData
c266a89d
A
1<?php
2
3declare(strict_types=1);
4
2899ebb5 5namespace Shaarli\Front\Controller\Visitor;
c266a89d
A
6
7use Slim\Http\Request;
8use Slim\Http\Response;
9
10/**
11 * Class TagCloud
12 *
60ae2412 13 * Slim controller used to render the tag cloud and tag list pages.
c266a89d 14 */
2899ebb5 15class TagCloudController extends ShaarliVisitorController
c266a89d 16{
60ae2412
A
17 protected const TYPE_CLOUD = 'cloud';
18 protected const TYPE_LIST = 'list';
19
3772298e
A
20 /**
21 * Display the tag cloud through the template engine.
22 * This controller a few filters:
23 * - Visibility stored in the session for logged in users
24 * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark
25 */
26 public function cloud(Request $request, Response $response): Response
60ae2412
A
27 {
28 return $this->processRequest(static::TYPE_CLOUD, $request, $response);
29 }
30
31 /**
32 * Display the tag list through the template engine.
33 * This controller a few filters:
34 * - Visibility stored in the session for logged in users
35 * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark
36 * - `sort` query parameters:
37 * + `usage` (default): most used tags first
38 * + `alpha`: alphabetical order
39 */
40 public function list(Request $request, Response $response): Response
41 {
42 return $this->processRequest(static::TYPE_LIST, $request, $response);
43 }
44
45 /**
46 * Process the request for both tag cloud and tag list endpoints.
47 */
48 protected function processRequest(string $type, Request $request, Response $response): Response
c266a89d
A
49 {
50 if ($this->container->loginManager->isLoggedIn() === true) {
51 $visibility = $this->container->sessionManager->getSessionParameter('visibility');
52 }
53
60ae2412 54 $sort = $request->getQueryParam('sort');
c266a89d
A
55 $searchTags = $request->getQueryParam('searchtags');
56 $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : [];
57
58 $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null);
59
60ae2412
A
60 if (static::TYPE_CLOUD === $type || 'alpha' === $sort) {
61 // TODO: the sorting should be handled by bookmarkService instead of the controller
62 alphabetical_sort($tags, false, true);
63 }
c266a89d 64
60ae2412
A
65 if (static::TYPE_CLOUD === $type) {
66 $tags = $this->formatTagsForCloud($tags);
67 }
c266a89d
A
68
69 $searchTags = implode(' ', escape($filteringTags));
70 $data = [
71 'search_tags' => $searchTags,
60ae2412 72 'tags' => $tags,
c266a89d 73 ];
60ae2412 74 $data = $this->executeHooks('tag' . $type, $data);
c266a89d
A
75 foreach ($data as $key => $value) {
76 $this->assignView($key, $value);
77 }
78
79 $searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
80 $this->assignView(
81 'pagetitle',
60ae2412 82 $searchTags . t('Tag '. $type) .' - '. $this->container->conf->get('general.title', 'Shaarli')
c266a89d
A
83 );
84
60ae2412 85 return $response->write($this->render('tag.'. $type));
c266a89d
A
86 }
87
60ae2412
A
88 /**
89 * Format the tags array for the tag cloud template.
90 *
91 * @param array<string, int> $tags List of tags as key with count as value
92 *
93 * @return mixed[] List of tags as key, with count and expected font size in a subarray
94 */
3772298e
A
95 protected function formatTagsForCloud(array $tags): array
96 {
97 // We sort tags alphabetically, then choose a font size according to count.
98 // First, find max value.
99 $maxCount = count($tags) > 0 ? max($tags) : 0;
100 $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1;
101 $tagList = [];
102 foreach ($tags as $key => $value) {
103 // Tag font size scaling:
104 // default 15 and 30 logarithm bases affect scaling,
105 // 2.2 and 0.8 are arbitrary font sizes in em.
106 $size = log($value, 15) / $logMaxCount * 2.2 + 0.8;
107 $tagList[$key] = [
108 'count' => $value,
109 'size' => number_format($size, 2, '.', ''),
110 ];
111 }
112
113 return $tagList;
114 }
115
c266a89d
A
116 /**
117 * @param mixed[] $data Template data
118 *
60ae2412 119 * @return mixed[] Template data after active plugins hook execution.
c266a89d 120 */
60ae2412 121 protected function executeHooks(string $template, array $data): array
c266a89d
A
122 {
123 $this->container->pluginManager->executeHooks(
60ae2412 124 'render_'. $template,
c266a89d
A
125 $data,
126 ['loggedin' => $this->container->loginManager->isLoggedIn()]
127 );
128
129 return $data;
130 }
131}