diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /application/front/controller/visitor/TagCloudController.php | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'application/front/controller/visitor/TagCloudController.php')
-rw-r--r-- | application/front/controller/visitor/TagCloudController.php | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/application/front/controller/visitor/TagCloudController.php b/application/front/controller/visitor/TagCloudController.php new file mode 100644 index 00000000..76ed7690 --- /dev/null +++ b/application/front/controller/visitor/TagCloudController.php | |||
@@ -0,0 +1,121 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Visitor; | ||
6 | |||
7 | use Slim\Http\Request; | ||
8 | use Slim\Http\Response; | ||
9 | |||
10 | /** | ||
11 | * Class TagCloud | ||
12 | * | ||
13 | * Slim controller used to render the tag cloud and tag list pages. | ||
14 | */ | ||
15 | class TagCloudController extends ShaarliVisitorController | ||
16 | { | ||
17 | protected const TYPE_CLOUD = 'cloud'; | ||
18 | protected const TYPE_LIST = 'list'; | ||
19 | |||
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 | ||
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 | ||
49 | { | ||
50 | if ($this->container->loginManager->isLoggedIn() === true) { | ||
51 | $visibility = $this->container->sessionManager->getSessionParameter('visibility'); | ||
52 | } | ||
53 | |||
54 | $sort = $request->getQueryParam('sort'); | ||
55 | $searchTags = $request->getQueryParam('searchtags'); | ||
56 | $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : []; | ||
57 | |||
58 | $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null); | ||
59 | |||
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 | } | ||
64 | |||
65 | if (static::TYPE_CLOUD === $type) { | ||
66 | $tags = $this->formatTagsForCloud($tags); | ||
67 | } | ||
68 | |||
69 | $tagsUrl = []; | ||
70 | foreach ($tags as $tag => $value) { | ||
71 | $tagsUrl[escape($tag)] = urlencode((string) $tag); | ||
72 | } | ||
73 | |||
74 | $searchTags = implode(' ', escape($filteringTags)); | ||
75 | $searchTagsUrl = urlencode(implode(' ', $filteringTags)); | ||
76 | $data = [ | ||
77 | 'search_tags' => escape($searchTags), | ||
78 | 'search_tags_url' => $searchTagsUrl, | ||
79 | 'tags' => escape($tags), | ||
80 | 'tags_url' => $tagsUrl, | ||
81 | ]; | ||
82 | $this->executePageHooks('render_tag' . $type, $data, 'tag.' . $type); | ||
83 | $this->assignAllView($data); | ||
84 | |||
85 | $searchTags = !empty($searchTags) ? $searchTags .' - ' : ''; | ||
86 | $this->assignView( | ||
87 | 'pagetitle', | ||
88 | $searchTags . t('Tag '. $type) .' - '. $this->container->conf->get('general.title', 'Shaarli') | ||
89 | ); | ||
90 | |||
91 | return $response->write($this->render('tag.' . $type)); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Format the tags array for the tag cloud template. | ||
96 | * | ||
97 | * @param array<string, int> $tags List of tags as key with count as value | ||
98 | * | ||
99 | * @return mixed[] List of tags as key, with count and expected font size in a subarray | ||
100 | */ | ||
101 | protected function formatTagsForCloud(array $tags): array | ||
102 | { | ||
103 | // We sort tags alphabetically, then choose a font size according to count. | ||
104 | // First, find max value. | ||
105 | $maxCount = count($tags) > 0 ? max($tags) : 0; | ||
106 | $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1; | ||
107 | $tagList = []; | ||
108 | foreach ($tags as $key => $value) { | ||
109 | // Tag font size scaling: | ||
110 | // default 15 and 30 logarithm bases affect scaling, | ||
111 | // 2.2 and 0.8 are arbitrary font sizes in em. | ||
112 | $size = log($value, 15) / $logMaxCount * 2.2 + 0.8; | ||
113 | $tagList[$key] = [ | ||
114 | 'count' => $value, | ||
115 | 'size' => number_format($size, 2, '.', ''), | ||
116 | ]; | ||
117 | } | ||
118 | |||
119 | return $tagList; | ||
120 | } | ||
121 | } | ||