]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/front/controllers/TagCloudController.php
1ff7c2e696792d4442388900a5eac0aeadf3a5b0
3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
8 use Slim\Http\Response
;
13 * Slim controller used to render the tag cloud and tag list pages.
15 * @package Front\Controller
17 class TagCloudController
extends ShaarliController
19 protected const TYPE_CLOUD
= 'cloud';
20 protected const TYPE_LIST
= 'list';
23 * Display the tag cloud through the template engine.
24 * This controller a few filters:
25 * - Visibility stored in the session for logged in users
26 * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark
28 public function cloud(Request
$request, Response
$response): Response
30 return $this->processRequest(static::TYPE_CLOUD
, $request, $response);
34 * Display the tag list through the template engine.
35 * This controller a few filters:
36 * - Visibility stored in the session for logged in users
37 * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark
38 * - `sort` query parameters:
39 * + `usage` (default): most used tags first
40 * + `alpha`: alphabetical order
42 public function list(Request
$request, Response
$response): Response
44 return $this->processRequest(static::TYPE_LIST
, $request, $response);
48 * Process the request for both tag cloud and tag list endpoints.
50 protected function processRequest(string $type, Request
$request, Response
$response): Response
52 if ($this->container
->loginManager
->isLoggedIn() === true) {
53 $visibility = $this->container
->sessionManager
->getSessionParameter('visibility');
56 $sort = $request->getQueryParam('sort');
57 $searchTags = $request->getQueryParam('searchtags');
58 $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : [];
60 $tags = $this->container
->bookmarkService
->bookmarksCountPerTag($filteringTags, $visibility ?? null);
62 if (static::TYPE_CLOUD
=== $type || 'alpha' === $sort) {
63 // TODO: the sorting should be handled by bookmarkService instead of the controller
64 alphabetical_sort($tags, false, true);
67 if (static::TYPE_CLOUD
=== $type) {
68 $tags = $this->formatTagsForCloud($tags);
71 $searchTags = implode(' ', escape($filteringTags));
73 'search_tags' => $searchTags,
76 $data = $this->executeHooks('tag' . $type, $data);
77 foreach ($data as $key => $value) {
78 $this->assignView($key, $value);
81 $searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
84 $searchTags . t('Tag '. $type) .' - '. $this->container
->conf
->get('general.title', 'Shaarli')
87 return $response->write($this->render('tag.'. $type));
91 * Format the tags array for the tag cloud template.
93 * @param array<string, int> $tags List of tags as key with count as value
95 * @return mixed[] List of tags as key, with count and expected font size in a subarray
97 protected function formatTagsForCloud(array $tags): array
99 // We sort tags alphabetically, then choose a font size according to count.
100 // First, find max value.
101 $maxCount = count($tags) > 0 ? max($tags) : 0;
102 $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1;
104 foreach ($tags as $key => $value) {
105 // Tag font size scaling:
106 // default 15 and 30 logarithm bases affect scaling,
107 // 2.2 and 0.8 are arbitrary font sizes in em.
108 $size = log($value, 15) / $logMaxCount * 2.2 +
0.8;
111 'size' => number_format($size, 2, '.', ''),
119 * @param mixed[] $data Template data
121 * @return mixed[] Template data after active plugins hook execution.
123 protected function executeHooks(string $template, array $data): array
125 $this->container
->pluginManager
->executeHooks(
126 'render_'. $template,
128 ['loggedin' => $this->container
->loginManager
->isLoggedIn()]