]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/front/controller/visitor/TagController.php
7a3377a718b3c7696b1577ace45aaf70ee3eae51
[github/shaarli/Shaarli.git] / application / front / controller / visitor / TagController.php
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 TagController
12 *
13 * Slim controller handle tags.
14 */
15 class TagController extends ShaarliVisitorController
16 {
17 /**
18 * Add another tag in the current search through an HTTP redirection.
19 *
20 * @param array $args Should contain `newTag` key as tag to add to current search
21 */
22 public function addTag(Request $request, Response $response, array $args): Response
23 {
24 $newTag = $args['newTag'] ?? null;
25 $referer = $this->container->environment['HTTP_REFERER'] ?? null;
26
27 // In case browser does not send HTTP_REFERER, we search a single tag
28 if (null === $referer) {
29 if (null !== $newTag) {
30 return $this->redirect($response, '/?searchtags='. urlencode($newTag));
31 }
32
33 return $this->redirect($response, '/');
34 }
35
36 $currentUrl = parse_url($referer);
37 parse_str($currentUrl['query'] ?? '', $params);
38
39 if (null === $newTag) {
40 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
41 }
42
43 // Prevent redirection loop
44 if (isset($params['addtag'])) {
45 unset($params['addtag']);
46 }
47
48 $tagsSeparator = $this->container->conf->get('general.tags_separator', ' ');
49 // Check if this tag is already in the search query and ignore it if it is.
50 // Each tag is always separated by a space
51 $currentTags = tags_str2array($params['searchtags'] ?? '', $tagsSeparator);
52
53 $addtag = true;
54 foreach ($currentTags as $value) {
55 if ($value === $newTag) {
56 $addtag = false;
57 break;
58 }
59 }
60
61 // Append the tag if necessary
62 if (true === $addtag) {
63 $currentTags[] = trim($newTag);
64 }
65
66 $params['searchtags'] = tags_array2str($currentTags, $tagsSeparator);
67
68 // We also remove page (keeping the same page has no sense, since the results are different)
69 unset($params['page']);
70
71 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
72 }
73
74 /**
75 * Remove a tag from the current search through an HTTP redirection.
76 *
77 * @param array $args Should contain `tag` key as tag to remove from current search
78 */
79 public function removeTag(Request $request, Response $response, array $args): Response
80 {
81 $referer = $this->container->environment['HTTP_REFERER'] ?? null;
82
83 // If the referrer is not provided, we can update the search, so we failback on the bookmark list
84 if (empty($referer)) {
85 return $this->redirect($response, '/');
86 }
87
88 $tagToRemove = $args['tag'] ?? null;
89 $currentUrl = parse_url($referer);
90 parse_str($currentUrl['query'] ?? '', $params);
91
92 if (null === $tagToRemove) {
93 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
94 }
95
96 // Prevent redirection loop
97 if (isset($params['removetag'])) {
98 unset($params['removetag']);
99 }
100
101 if (isset($params['searchtags'])) {
102 $tagsSeparator = $this->container->conf->get('general.tags_separator', ' ');
103 $tags = tags_str2array($params['searchtags'] ?? '', $tagsSeparator);
104 // Remove value from array $tags.
105 $tags = array_diff($tags, [$tagToRemove]);
106 $params['searchtags'] = tags_array2str($tags, $tagsSeparator);
107
108 if (empty($params['searchtags'])) {
109 unset($params['searchtags']);
110 }
111
112 // We also remove page (keeping the same page has no sense, since the results are different)
113 unset($params['page']);
114 }
115
116 $queryParams = count($params) > 0 ? '?' . http_build_query($params) : '';
117
118 return $response->withRedirect(($currentUrl['path'] ?? './') . $queryParams);
119 }
120 }