]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controller/admin/ManageTagController.php
Process main page (linklist) through Slim controller
[github/shaarli/Shaarli.git] / application / front / controller / admin / ManageTagController.php
CommitLineData
8eac2e54
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Shaarli\Bookmark\BookmarkFilter;
1a8ac737 8use Shaarli\Render\TemplatePage;
8eac2e54
A
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12/**
13 * Class ManageTagController
14 *
15 * Slim controller used to handle Shaarli manage tags page (rename and delete tags).
16 */
17class ManageTagController extends ShaarliAdminController
18{
19 /**
9c75f877 20 * GET /admin/tags - Displays the manage tags page
8eac2e54
A
21 */
22 public function index(Request $request, Response $response): Response
23 {
24 $fromTag = $request->getParam('fromtag') ?? '';
25
26 $this->assignView('fromtag', escape($fromTag));
27 $this->assignView(
28 'pagetitle',
29 t('Manage tags') .' - '. $this->container->conf->get('general.title', 'Shaarli')
30 );
31
1a8ac737 32 return $response->write($this->render(TemplatePage::CHANGE_TAG));
8eac2e54
A
33 }
34
35 /**
9c75f877 36 * POST /admin/tags - Update or delete provided tag
8eac2e54
A
37 */
38 public function save(Request $request, Response $response): Response
39 {
40 $this->checkToken($request);
41
42 $isDelete = null !== $request->getParam('deletetag') && null === $request->getParam('renametag');
43
44 $fromTag = escape(trim($request->getParam('fromtag') ?? ''));
45 $toTag = escape(trim($request->getParam('totag') ?? ''));
46
47 if (0 === strlen($fromTag) || false === $isDelete && 0 === strlen($toTag)) {
48 $this->saveWarningMessage(t('Invalid tags provided.'));
49
9c75f877 50 return $this->redirect($response, '/admin/tags');
8eac2e54
A
51 }
52
53 // TODO: move this to bookmark service
54 $count = 0;
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);
59 } else {
60 $bookmark->deleteTag($fromTag);
61 }
62
63 $this->container->bookmarkService->set($bookmark, false);
64 $this->container->history->updateLink($bookmark);
65 $count++;
66 }
67
68 $this->container->bookmarkService->save();
69
70 if (true === $isDelete) {
71 $alert = sprintf(
72 t('The tag was removed from %d bookmark.', 'The tag was removed from %d bookmarks.', $count),
73 $count
74 );
75 } else {
76 $alert = sprintf(
77 t('The tag was renamed in %d bookmark.', 'The tag was renamed in %d bookmarks.', $count),
78 $count
79 );
80 }
81
82 $this->saveSuccessMessage($alert);
83
9c75f877 84 $redirect = true === $isDelete ? '/admin/tags' : '/?searchtags='. urlencode($toTag);
8eac2e54 85
9c75f877 86 return $this->redirect($response, $redirect);
8eac2e54
A
87 }
88}