3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
8 use Slim\Http\Response
;
13 * Slim controller used to render the tag cloud and tag list pages.
15 class TagCloudController
extends ShaarliVisitorController
17 protected const TYPE_CLOUD
= 'cloud';
18 protected const TYPE_LIST
= 'list';
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
26 public function cloud(Request
$request, Response
$response): Response
28 return $this->processRequest(static::TYPE_CLOUD
, $request, $response);
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
40 public function list(Request
$request, Response
$response): Response
42 return $this->processRequest(static::TYPE_LIST
, $request, $response);
46 * Process the request for both tag cloud and tag list endpoints.
48 protected function processRequest(string $type, Request
$request, Response
$response): Response
50 if ($this->container
->loginManager
->isLoggedIn() === true) {
51 $visibility = $this->container
->sessionManager
->getSessionParameter('visibility');
54 $sort = $request->getQueryParam('sort');
55 $searchTags = $request->getQueryParam('searchtags');
56 $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : [];
58 $tags = $this->container
->bookmarkService
->bookmarksCountPerTag($filteringTags, $visibility ?? null);
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);
65 if (static::TYPE_CLOUD
=== $type) {
66 $tags = $this->formatTagsForCloud($tags);
69 $searchTags = implode(' ', escape($filteringTags));
71 'search_tags' => $searchTags,
74 $this->executePageHooks('render_tag' . $type, $data, 'tag.' . $type);
75 $this->assignAllView($data);
77 $searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
80 $searchTags . t('Tag '. $type) .' - '. $this->container
->conf
->get('general.title', 'Shaarli')
83 return $response->write($this->render('tag.' . $type));
87 * Format the tags array for the tag cloud template.
89 * @param array<string, int> $tags List of tags as key with count as value
91 * @return mixed[] List of tags as key, with count and expected font size in a subarray
93 protected function formatTagsForCloud(array $tags): array
95 // We sort tags alphabetically, then choose a font size according to count.
96 // First, find max value.
97 $maxCount = count($tags) > 0 ? max($tags) : 0;
98 $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1;
100 foreach ($tags as $key => $value) {
101 // Tag font size scaling:
102 // default 15 and 30 logarithm bases affect scaling,
103 // 2.2 and 0.8 are arbitrary font sizes in em.
104 $size = log($value, 15) / $logMaxCount * 2.2 +
0.8;
107 'size' => number_format($size, 2, '.', ''),