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