diff options
Diffstat (limited to 'application/front/controller')
-rw-r--r-- | application/front/controller/admin/ConfigureController.php | 2 | ||||
-rw-r--r-- | application/front/controller/admin/ManageTagController.php | 87 |
2 files changed, 88 insertions, 1 deletions
diff --git a/application/front/controller/admin/ConfigureController.php b/application/front/controller/admin/ConfigureController.php index b1d32270..5a482d8e 100644 --- a/application/front/controller/admin/ConfigureController.php +++ b/application/front/controller/admin/ConfigureController.php | |||
@@ -12,7 +12,7 @@ use Slim\Http\Response; | |||
12 | use Throwable; | 12 | use Throwable; |
13 | 13 | ||
14 | /** | 14 | /** |
15 | * Class PasswordController | 15 | * Class ConfigureController |
16 | * | 16 | * |
17 | * Slim controller used to handle Shaarli configuration page (display + save new config). | 17 | * Slim controller used to handle Shaarli configuration page (display + save new config). |
18 | */ | 18 | */ |
diff --git a/application/front/controller/admin/ManageTagController.php b/application/front/controller/admin/ManageTagController.php new file mode 100644 index 00000000..e015e613 --- /dev/null +++ b/application/front/controller/admin/ManageTagController.php | |||
@@ -0,0 +1,87 @@ | |||
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 | } | ||