]>
Commit | Line | Data |
---|---|---|
03340c18 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
2899ebb5 | 5 | namespace Shaarli\Front\Controller\Visitor; |
03340c18 A |
6 | |
7 | use Slim\Http\Request; | |
8 | use Slim\Http\Response; | |
9 | ||
10 | /** | |
11 | * Class TagController | |
12 | * | |
13 | * Slim controller handle tags. | |
9c75f877 A |
14 | * |
15 | * TODO: check redirections with new helper | |
03340c18 | 16 | */ |
2899ebb5 | 17 | class TagController extends ShaarliVisitorController |
03340c18 A |
18 | { |
19 | /** | |
20 | * Add another tag in the current search through an HTTP redirection. | |
21 | * | |
22 | * @param array $args Should contain `newTag` key as tag to add to current search | |
23 | */ | |
24 | public function addTag(Request $request, Response $response, array $args): Response | |
25 | { | |
26 | $newTag = $args['newTag'] ?? null; | |
27 | $referer = $this->container->environment['HTTP_REFERER'] ?? null; | |
28 | ||
29 | // In case browser does not send HTTP_REFERER, we search a single tag | |
30 | if (null === $referer) { | |
31 | if (null !== $newTag) { | |
9c75f877 | 32 | return $this->redirect($response, '/?searchtags='. urlencode($newTag)); |
03340c18 A |
33 | } |
34 | ||
9c75f877 | 35 | return $this->redirect($response, '/'); |
03340c18 A |
36 | } |
37 | ||
893f5159 | 38 | $currentUrl = parse_url($referer); |
03340c18 A |
39 | parse_str($currentUrl['query'] ?? '', $params); |
40 | ||
41 | if (null === $newTag) { | |
42 | return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params)); | |
43 | } | |
44 | ||
45 | // Prevent redirection loop | |
46 | if (isset($params['addtag'])) { | |
47 | unset($params['addtag']); | |
48 | } | |
49 | ||
50 | // Check if this tag is already in the search query and ignore it if it is. | |
51 | // Each tag is always separated by a space | |
52 | $currentTags = isset($params['searchtags']) ? explode(' ', $params['searchtags']) : []; | |
53 | ||
54 | $addtag = true; | |
55 | foreach ($currentTags as $value) { | |
56 | if ($value === $newTag) { | |
57 | $addtag = false; | |
58 | break; | |
59 | } | |
60 | } | |
61 | ||
62 | // Append the tag if necessary | |
63 | if (true === $addtag) { | |
64 | $currentTags[] = trim($newTag); | |
65 | } | |
66 | ||
67 | $params['searchtags'] = trim(implode(' ', $currentTags)); | |
68 | ||
69 | // We also remove page (keeping the same page has no sense, since the results are different) | |
70 | unset($params['page']); | |
71 | ||
72 | return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params)); | |
73 | } | |
893f5159 A |
74 | |
75 | /** | |
76 | * Remove a tag from the current search through an HTTP redirection. | |
77 | * | |
78 | * @param array $args Should contain `tag` key as tag to remove from current search | |
79 | */ | |
80 | public function removeTag(Request $request, Response $response, array $args): Response | |
81 | { | |
82 | $referer = $this->container->environment['HTTP_REFERER'] ?? null; | |
83 | ||
84 | // If the referrer is not provided, we can update the search, so we failback on the bookmark list | |
85 | if (empty($referer)) { | |
9c75f877 | 86 | return $this->redirect($response, '/'); |
893f5159 A |
87 | } |
88 | ||
89 | $tagToRemove = $args['tag'] ?? null; | |
90 | $currentUrl = parse_url($referer); | |
91 | parse_str($currentUrl['query'] ?? '', $params); | |
92 | ||
93 | if (null === $tagToRemove) { | |
94 | return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params)); | |
95 | } | |
96 | ||
97 | // Prevent redirection loop | |
98 | if (isset($params['removetag'])) { | |
99 | unset($params['removetag']); | |
100 | } | |
101 | ||
102 | if (isset($params['searchtags'])) { | |
103 | $tags = explode(' ', $params['searchtags']); | |
104 | // Remove value from array $tags. | |
105 | $tags = array_diff($tags, [$tagToRemove]); | |
106 | $params['searchtags'] = implode(' ', $tags); | |
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 | } |