3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Bookmark\BookmarkFilter
;
8 use Shaarli\Render\TemplatePage
;
10 use Slim\Http\Response
;
13 * Class ManageTagController
15 * Slim controller used to handle Shaarli manage tags page (rename and delete tags).
17 class ManageTagController
extends ShaarliAdminController
20 * GET /admin/tags - Displays the manage tags page
22 public function index(Request
$request, Response
$response): Response
24 $fromTag = $request->getParam('fromtag') ?? '';
26 $this->assignView('fromtag', escape($fromTag));
29 t('Manage tags') .' - '. $this->container
->conf
->get('general.title', 'Shaarli')
32 return $response->write($this->render(TemplatePage
::CHANGE_TAG
));
36 * POST /admin/tags - Update or delete provided tag
38 public function save(Request
$request, Response
$response): Response
40 $this->checkToken($request);
42 $isDelete = null !== $request->getParam('deletetag') && null === $request->getParam('renametag');
44 $fromTag = escape(trim($request->getParam('fromtag') ?? ''));
45 $toTag = escape(trim($request->getParam('totag') ?? ''));
47 if (0 === strlen($fromTag) || false === $isDelete && 0 === strlen($toTag)) {
48 $this->saveWarningMessage(t('Invalid tags provided.'));
50 return $this->redirect($response, '/admin/tags');
53 // TODO: move this to bookmark service
55 $bookmarks = $this->container
->bookmarkService
->search(['searchtags' => $fromTag], BookmarkFilter
::$ALL, true);
56 foreach ($bookmarks as $bookmark) {
57 if (false === $isDelete) {
58 $bookmark->renameTag($fromTag, $toTag);
60 $bookmark->deleteTag($fromTag);
63 $this->container
->bookmarkService
->set($bookmark, false);
64 $this->container
->history
->updateLink($bookmark);
68 $this->container
->bookmarkService
->save();
70 if (true === $isDelete) {
72 t('The tag was removed from %d bookmark.', 'The tag was removed from %d bookmarks.', $count),
77 t('The tag was renamed in %d bookmark.', 'The tag was renamed in %d bookmarks.', $count),
82 $this->saveSuccessMessage($alert);
84 $redirect = true === $isDelete ? '/admin/tags' : '/?searchtags='. urlencode($toTag);
86 return $this->redirect($response, $redirect);