3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
8 use Slim\Http\Response
;
13 * Slim controller handle tags.
15 class TagController
extends ShaarliVisitorController
18 * Add another tag in the current search through an HTTP redirection.
20 * @param array $args Should contain `newTag` key as tag to add to current search
22 public function addTag(Request
$request, Response
$response, array $args): Response
24 $newTag = $args['newTag'] ?? null;
25 $referer = $this->container
->environment
['HTTP_REFERER'] ?? null;
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));
33 return $this->redirect($response, '/');
36 $currentUrl = parse_url($referer);
37 parse_str($currentUrl['query'] ?? '', $params);
39 if (null === $newTag) {
40 return $response->withRedirect(($currentUrl['path'] ?? './') . '?' . http_build_query($params));
43 // Prevent redirection loop
44 if (isset($params['addtag'])) {
45 unset($params['addtag']);
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);
54 foreach ($currentTags as $value) {
55 if ($value === $newTag) {
61 // Append the tag if necessary
62 if (true === $addtag) {
63 $currentTags[] = trim($newTag);
66 $params['searchtags'] = tags_array2str($currentTags, $tagsSeparator);
68 // We also remove page (keeping the same page has no sense, since the results are different)
69 unset($params['page']);
71 return $response->withRedirect(($currentUrl['path'] ?? './') . '?' . http_build_query($params));
75 * Remove a tag from the current search through an HTTP redirection.
77 * @param array $args Should contain `tag` key as tag to remove from current search
79 public function removeTag(Request
$request, Response
$response, array $args): Response
81 $referer = $this->container
->environment
['HTTP_REFERER'] ?? null;
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, '/');
88 $tagToRemove = $args['tag'] ?? null;
89 $currentUrl = parse_url($referer);
90 parse_str($currentUrl['query'] ?? '', $params);
92 if (null === $tagToRemove) {
93 return $response->withRedirect(($currentUrl['path'] ?? './') . '?' . http_build_query($params));
96 // Prevent redirection loop
97 if (isset($params['removetag'])) {
98 unset($params['removetag']);
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);
108 if (empty($params['searchtags'])) {
109 unset($params['searchtags']);
112 // We also remove page (keeping the same page has no sense, since the results are different)
113 unset($params['page']);
116 $queryParams = count($params) > 0 ? '?' . http_build_query($params) : '';
118 return $response->withRedirect(($currentUrl['path'] ?? './') . $queryParams);