]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controller/visitor/TagController.php
Apply PHP Code Beautifier on source code for linter automatic fixes
[github/shaarli/Shaarli.git] / application / front / controller / visitor / TagController.php
CommitLineData
03340c18
A
1<?php
2
3declare(strict_types=1);
4
2899ebb5 5namespace Shaarli\Front\Controller\Visitor;
03340c18
A
6
7use Slim\Http\Request;
8use Slim\Http\Response;
9
10/**
11 * Class TagController
12 *
13 * Slim controller handle tags.
03340c18 14 */
2899ebb5 15class TagController extends ShaarliVisitorController
03340c18
A
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) {
53054b2b 30 return $this->redirect($response, '/?searchtags=' . urlencode($newTag));
03340c18
A
31 }
32
9c75f877 33 return $this->redirect($response, '/');
03340c18
A
34 }
35
893f5159 36 $currentUrl = parse_url($referer);
03340c18
A
37 parse_str($currentUrl['query'] ?? '', $params);
38
39 if (null === $newTag) {
53054b2b 40 return $response->withRedirect(($currentUrl['path'] ?? './') . '?' . http_build_query($params));
03340c18
A
41 }
42
43 // Prevent redirection loop
44 if (isset($params['addtag'])) {
45 unset($params['addtag']);
46 }
47
b3bd8c3e 48 $tagsSeparator = $this->container->conf->get('general.tags_separator', ' ');
03340c18
A
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
b3bd8c3e 51 $currentTags = tags_str2array($params['searchtags'] ?? '', $tagsSeparator);
03340c18
A
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
b3bd8c3e 66 $params['searchtags'] = tags_array2str($currentTags, $tagsSeparator);
03340c18
A
67
68 // We also remove page (keeping the same page has no sense, since the results are different)
69 unset($params['page']);
70
53054b2b 71 return $response->withRedirect(($currentUrl['path'] ?? './') . '?' . http_build_query($params));
03340c18 72 }
893f5159
A
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)) {
9c75f877 85 return $this->redirect($response, '/');
893f5159
A
86 }
87
88 $tagToRemove = $args['tag'] ?? null;
89 $currentUrl = parse_url($referer);
90 parse_str($currentUrl['query'] ?? '', $params);
91
92 if (null === $tagToRemove) {
53054b2b 93 return $response->withRedirect(($currentUrl['path'] ?? './') . '?' . http_build_query($params));
893f5159
A
94 }
95
96 // Prevent redirection loop
97 if (isset($params['removetag'])) {
98 unset($params['removetag']);
99 }
100
101 if (isset($params['searchtags'])) {
b3bd8c3e
A
102 $tagsSeparator = $this->container->conf->get('general.tags_separator', ' ');
103 $tags = tags_str2array($params['searchtags'] ?? '', $tagsSeparator);
893f5159
A
104 // Remove value from array $tags.
105 $tags = array_diff($tags, [$tagToRemove]);
b3bd8c3e 106 $params['searchtags'] = tags_array2str($tags, $tagsSeparator);
893f5159
A
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 }
03340c18 120}