diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-16 13:33:39 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 3772298ee7d8d0708f4e72798600accafa17740b (patch) | |
tree | cec625b67642264973bbe856ddf4ff97187aabea /application/front | |
parent | c79473bd84ab5aba7836d2caaf61847cabaf1e53 (diff) | |
download | Shaarli-3772298ee7d8d0708f4e72798600accafa17740b.tar.gz Shaarli-3772298ee7d8d0708f4e72798600accafa17740b.tar.zst Shaarli-3772298ee7d8d0708f4e72798600accafa17740b.zip |
Few optimizations and code readability for tag cloud controller
Diffstat (limited to 'application/front')
-rw-r--r-- | application/front/controllers/TagCloudController.php | 52 |
1 files changed, 31 insertions, 21 deletions
diff --git a/application/front/controllers/TagCloudController.php b/application/front/controllers/TagCloudController.php index 9389c2b0..93e3ae27 100644 --- a/application/front/controllers/TagCloudController.php +++ b/application/front/controllers/TagCloudController.php | |||
@@ -16,7 +16,13 @@ use Slim\Http\Response; | |||
16 | */ | 16 | */ |
17 | class TagCloudController extends ShaarliController | 17 | class TagCloudController extends ShaarliController |
18 | { | 18 | { |
19 | public function index(Request $request, Response $response): Response | 19 | /** |
20 | * Display the tag cloud through the template engine. | ||
21 | * This controller a few filters: | ||
22 | * - Visibility stored in the session for logged in users | ||
23 | * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark | ||
24 | */ | ||
25 | public function cloud(Request $request, Response $response): Response | ||
20 | { | 26 | { |
21 | if ($this->container->loginManager->isLoggedIn() === true) { | 27 | if ($this->container->loginManager->isLoggedIn() === true) { |
22 | $visibility = $this->container->sessionManager->getSessionParameter('visibility'); | 28 | $visibility = $this->container->sessionManager->getSessionParameter('visibility'); |
@@ -27,27 +33,10 @@ class TagCloudController extends ShaarliController | |||
27 | 33 | ||
28 | $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null); | 34 | $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null); |
29 | 35 | ||
30 | // We sort tags alphabetically, then choose a font size according to count. | 36 | // TODO: the sorting should be handled by bookmarkService instead of the controller |
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); | 37 | alphabetical_sort($tags, false, true); |
38 | 38 | ||
39 | $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1; | 39 | $tagList = $this->formatTagsForCloud($tags); |
40 | $tagList = []; | ||
41 | foreach ($tags as $key => $value) { | ||
42 | // Tag font size scaling: | ||
43 | // default 15 and 30 logarithm bases affect scaling, | ||
44 | // 2.2 and 0.8 are arbitrary font sizes in em. | ||
45 | $size = log($value, 15) / $logMaxCount * 2.2 + 0.8; | ||
46 | $tagList[$key] = [ | ||
47 | 'count' => $value, | ||
48 | 'size' => number_format($size, 2, '.', ''), | ||
49 | ]; | ||
50 | } | ||
51 | 40 | ||
52 | $searchTags = implode(' ', escape($filteringTags)); | 41 | $searchTags = implode(' ', escape($filteringTags)); |
53 | $data = [ | 42 | $data = [ |
@@ -62,12 +51,33 @@ class TagCloudController extends ShaarliController | |||
62 | $searchTags = !empty($searchTags) ? $searchTags .' - ' : ''; | 51 | $searchTags = !empty($searchTags) ? $searchTags .' - ' : ''; |
63 | $this->assignView( | 52 | $this->assignView( |
64 | 'pagetitle', | 53 | 'pagetitle', |
65 | $searchTags. t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli') | 54 | $searchTags . t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli') |
66 | ); | 55 | ); |
67 | 56 | ||
68 | return $response->write($this->render('tag.cloud')); | 57 | return $response->write($this->render('tag.cloud')); |
69 | } | 58 | } |
70 | 59 | ||
60 | protected function formatTagsForCloud(array $tags): array | ||
61 | { | ||
62 | // We sort tags alphabetically, then choose a font size according to count. | ||
63 | // First, find max value. | ||
64 | $maxCount = count($tags) > 0 ? max($tags) : 0; | ||
65 | $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1; | ||
66 | $tagList = []; | ||
67 | foreach ($tags as $key => $value) { | ||
68 | // Tag font size scaling: | ||
69 | // default 15 and 30 logarithm bases affect scaling, | ||
70 | // 2.2 and 0.8 are arbitrary font sizes in em. | ||
71 | $size = log($value, 15) / $logMaxCount * 2.2 + 0.8; | ||
72 | $tagList[$key] = [ | ||
73 | 'count' => $value, | ||
74 | 'size' => number_format($size, 2, '.', ''), | ||
75 | ]; | ||
76 | } | ||
77 | |||
78 | return $tagList; | ||
79 | } | ||
80 | |||
71 | /** | 81 | /** |
72 | * @param mixed[] $data Template data | 82 | * @param mixed[] $data Template data |
73 | * | 83 | * |