]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/front/controllers/TagController.php
3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
8 use Slim\Http\Response
;
13 * Slim controller handle tags.
15 * @package Front\Controller
17 class TagController
extends ShaarliController
20 * Add another tag in the current search through an HTTP redirection.
22 * @param array $args Should contain `newTag` key as tag to add to current search
24 public function addTag(Request
$request, Response
$response, array $args): Response
26 $newTag = $args['newTag'] ?? null;
27 $referer = $this->container
->environment
['HTTP_REFERER'] ?? null;
29 // In case browser does not send HTTP_REFERER, we search a single tag
30 if (null === $referer) {
31 if (null !== $newTag) {
32 return $response->withRedirect('./?searchtags='. urlencode($newTag));
35 return $response->withRedirect('./');
38 $currentUrl = parse_url($this->container
->environment
['HTTP_REFERER']);
39 parse_str($currentUrl['query'] ?? '', $params);
41 if (null === $newTag) {
42 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
45 // Prevent redirection loop
46 if (isset($params['addtag'])) {
47 unset($params['addtag']);
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']) : [];
55 foreach ($currentTags as $value) {
56 if ($value === $newTag) {
62 // Append the tag if necessary
63 if (true === $addtag) {
64 $currentTags[] = trim($newTag);
67 $params['searchtags'] = trim(implode(' ', $currentTags));
69 // We also remove page (keeping the same page has no sense, since the results are different)
70 unset($params['page']);
72 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));