aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-20 14:38:31 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit893f5159c64e5bcff505c8367e6dc22cc2a7b14d (patch)
treeba07dc71bb17ef143fb2038fcb68f4bc4a62a090 /application/front
parentdd09ec52b20b4a548ecf5c847627575e506e3a50 (diff)
downloadShaarli-893f5159c64e5bcff505c8367e6dc22cc2a7b14d.tar.gz
Shaarli-893f5159c64e5bcff505c8367e6dc22cc2a7b14d.tar.zst
Shaarli-893f5159c64e5bcff505c8367e6dc22cc2a7b14d.zip
Process remove tag endpoint through Slim controller
Diffstat (limited to 'application/front')
-rw-r--r--application/front/controllers/TagController.php48
1 files changed, 47 insertions, 1 deletions
diff --git a/application/front/controllers/TagController.php b/application/front/controllers/TagController.php
index 598275b0..a1d5ad5b 100644
--- a/application/front/controllers/TagController.php
+++ b/application/front/controllers/TagController.php
@@ -35,7 +35,7 @@ class TagController extends ShaarliController
35 return $response->withRedirect('./'); 35 return $response->withRedirect('./');
36 } 36 }
37 37
38 $currentUrl = parse_url($this->container->environment['HTTP_REFERER']); 38 $currentUrl = parse_url($referer);
39 parse_str($currentUrl['query'] ?? '', $params); 39 parse_str($currentUrl['query'] ?? '', $params);
40 40
41 if (null === $newTag) { 41 if (null === $newTag) {
@@ -71,4 +71,50 @@ class TagController extends ShaarliController
71 71
72 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params)); 72 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
73 } 73 }
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)) {
86 return $response->withRedirect('./');
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 }
74} 120}