X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=application%2Ffront%2Fcontrollers%2FTagCloudController.php;h=93e3ae27c46ff2b423c1250f3b5b2fc2faa73346;hb=3772298ee7d8d0708f4e72798600accafa17740b;hp=b6f4a0ce99d30fd5d6f968eda0aef1c5064bbfc0;hpb=c266a89d0fbb0d60d2d7df0ec171b7cb022224f6;p=github%2Fshaarli%2FShaarli.git diff --git a/application/front/controllers/TagCloudController.php b/application/front/controllers/TagCloudController.php index b6f4a0ce..93e3ae27 100644 --- a/application/front/controllers/TagCloudController.php +++ b/application/front/controllers/TagCloudController.php @@ -16,7 +16,13 @@ use Slim\Http\Response; */ class TagCloudController extends ShaarliController { - public function index(Request $request, Response $response): Response + /** + * Display the tag cloud through the template engine. + * This controller a few filters: + * - Visibility stored in the session for logged in users + * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark + */ + public function cloud(Request $request, Response $response): Response { if ($this->container->loginManager->isLoggedIn() === true) { $visibility = $this->container->sessionManager->getSessionParameter('visibility'); @@ -27,30 +33,10 @@ class TagCloudController extends ShaarliController $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null); - // We sort tags alphabetically, then choose a font size according to count. - // First, find max value. - $maxCount = 0; - foreach ($tags as $count) { - $maxCount = max($maxCount, $count); - } - + // TODO: the sorting should be handled by bookmarkService instead of the controller alphabetical_sort($tags, false, true); - $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1; - $tagList = []; - foreach ($tags as $key => $value) { - if (in_array($key, $filteringTags)) { - continue; - } - // Tag font size scaling: - // default 15 and 30 logarithm bases affect scaling, - // 2.2 and 0.8 are arbitrary font sizes in em. - $size = log($value, 15) / $logMaxCount * 2.2 + 0.8; - $tagList[$key] = [ - 'count' => $value, - 'size' => number_format($size, 2, '.', ''), - ]; - } + $tagList = $this->formatTagsForCloud($tags); $searchTags = implode(' ', escape($filteringTags)); $data = [ @@ -65,12 +51,33 @@ class TagCloudController extends ShaarliController $searchTags = !empty($searchTags) ? $searchTags .' - ' : ''; $this->assignView( 'pagetitle', - $searchTags. t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli') + $searchTags . t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli') ); return $response->write($this->render('tag.cloud')); } + protected function formatTagsForCloud(array $tags): array + { + // We sort tags alphabetically, then choose a font size according to count. + // First, find max value. + $maxCount = count($tags) > 0 ? max($tags) : 0; + $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1; + $tagList = []; + foreach ($tags as $key => $value) { + // Tag font size scaling: + // default 15 and 30 logarithm bases affect scaling, + // 2.2 and 0.8 are arbitrary font sizes in em. + $size = log($value, 15) / $logMaxCount * 2.2 + 0.8; + $tagList[$key] = [ + 'count' => $value, + 'size' => number_format($size, 2, '.', ''), + ]; + } + + return $tagList; + } + /** * @param mixed[] $data Template data *