aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-16 14:56:22 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit60ae241251b753fc052e50ebd95277dfcb074cb0 (patch)
treedfa474c1df6a5f123a31c953d7cb0d157b040b8b /application
parent3772298ee7d8d0708f4e72798600accafa17740b (diff)
downloadShaarli-60ae241251b753fc052e50ebd95277dfcb074cb0.tar.gz
Shaarli-60ae241251b753fc052e50ebd95277dfcb074cb0.tar.zst
Shaarli-60ae241251b753fc052e50ebd95277dfcb074cb0.zip
Process tag list page through Slim controller
Diffstat (limited to 'application')
-rw-r--r--application/front/controllers/TagCloudController.php59
1 files changed, 48 insertions, 11 deletions
diff --git a/application/front/controllers/TagCloudController.php b/application/front/controllers/TagCloudController.php
index 93e3ae27..1ff7c2e6 100644
--- a/application/front/controllers/TagCloudController.php
+++ b/application/front/controllers/TagCloudController.php
@@ -10,12 +10,15 @@ use Slim\Http\Response;
10/** 10/**
11 * Class TagCloud 11 * Class TagCloud
12 * 12 *
13 * Slim controller used to render the tag cloud page. 13 * Slim controller used to render the tag cloud and tag list pages.
14 * 14 *
15 * @package Front\Controller 15 * @package Front\Controller
16 */ 16 */
17class TagCloudController extends ShaarliController 17class TagCloudController extends ShaarliController
18{ 18{
19 protected const TYPE_CLOUD = 'cloud';
20 protected const TYPE_LIST = 'list';
21
19 /** 22 /**
20 * Display the tag cloud through the template engine. 23 * Display the tag cloud through the template engine.
21 * This controller a few filters: 24 * This controller a few filters:
@@ -24,26 +27,53 @@ class TagCloudController extends ShaarliController
24 */ 27 */
25 public function cloud(Request $request, Response $response): Response 28 public function cloud(Request $request, Response $response): Response
26 { 29 {
30 return $this->processRequest(static::TYPE_CLOUD, $request, $response);
31 }
32
33 /**
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
41 */
42 public function list(Request $request, Response $response): Response
43 {
44 return $this->processRequest(static::TYPE_LIST, $request, $response);
45 }
46
47 /**
48 * Process the request for both tag cloud and tag list endpoints.
49 */
50 protected function processRequest(string $type, Request $request, Response $response): Response
51 {
27 if ($this->container->loginManager->isLoggedIn() === true) { 52 if ($this->container->loginManager->isLoggedIn() === true) {
28 $visibility = $this->container->sessionManager->getSessionParameter('visibility'); 53 $visibility = $this->container->sessionManager->getSessionParameter('visibility');
29 } 54 }
30 55
56 $sort = $request->getQueryParam('sort');
31 $searchTags = $request->getQueryParam('searchtags'); 57 $searchTags = $request->getQueryParam('searchtags');
32 $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : []; 58 $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : [];
33 59
34 $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null); 60 $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null);
35 61
36 // TODO: the sorting should be handled by bookmarkService instead of the controller 62 if (static::TYPE_CLOUD === $type || 'alpha' === $sort) {
37 alphabetical_sort($tags, false, true); 63 // TODO: the sorting should be handled by bookmarkService instead of the controller
64 alphabetical_sort($tags, false, true);
65 }
38 66
39 $tagList = $this->formatTagsForCloud($tags); 67 if (static::TYPE_CLOUD === $type) {
68 $tags = $this->formatTagsForCloud($tags);
69 }
40 70
41 $searchTags = implode(' ', escape($filteringTags)); 71 $searchTags = implode(' ', escape($filteringTags));
42 $data = [ 72 $data = [
43 'search_tags' => $searchTags, 73 'search_tags' => $searchTags,
44 'tags' => $tagList, 74 'tags' => $tags,
45 ]; 75 ];
46 $data = $this->executeHooks($data); 76 $data = $this->executeHooks('tag' . $type, $data);
47 foreach ($data as $key => $value) { 77 foreach ($data as $key => $value) {
48 $this->assignView($key, $value); 78 $this->assignView($key, $value);
49 } 79 }
@@ -51,12 +81,19 @@ class TagCloudController extends ShaarliController
51 $searchTags = !empty($searchTags) ? $searchTags .' - ' : ''; 81 $searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
52 $this->assignView( 82 $this->assignView(
53 'pagetitle', 83 'pagetitle',
54 $searchTags . t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli') 84 $searchTags . t('Tag '. $type) .' - '. $this->container->conf->get('general.title', 'Shaarli')
55 ); 85 );
56 86
57 return $response->write($this->render('tag.cloud')); 87 return $response->write($this->render('tag.'. $type));
58 } 88 }
59 89
90 /**
91 * Format the tags array for the tag cloud template.
92 *
93 * @param array<string, int> $tags List of tags as key with count as value
94 *
95 * @return mixed[] List of tags as key, with count and expected font size in a subarray
96 */
60 protected function formatTagsForCloud(array $tags): array 97 protected function formatTagsForCloud(array $tags): array
61 { 98 {
62 // We sort tags alphabetically, then choose a font size according to count. 99 // We sort tags alphabetically, then choose a font size according to count.
@@ -81,12 +118,12 @@ class TagCloudController extends ShaarliController
81 /** 118 /**
82 * @param mixed[] $data Template data 119 * @param mixed[] $data Template data
83 * 120 *
84 * @return mixed[] Template data after active plugins render_picwall hook execution. 121 * @return mixed[] Template data after active plugins hook execution.
85 */ 122 */
86 protected function executeHooks(array $data): array 123 protected function executeHooks(string $template, array $data): array
87 { 124 {
88 $this->container->pluginManager->executeHooks( 125 $this->container->pluginManager->executeHooks(
89 'render_tagcloud', 126 'render_'. $template,
90 $data, 127 $data,
91 ['loggedin' => $this->container->loginManager->isLoggedIn()] 128 ['loggedin' => $this->container->loginManager->isLoggedIn()]
92 ); 129 );