2020-05-30 15:51:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shaarli\Front\Controller\Admin;
|
|
|
|
|
|
|
|
use Shaarli\Bookmark\BookmarkFilter;
|
2020-07-06 08:04:35 +02:00
|
|
|
use Shaarli\Render\TemplatePage;
|
2020-05-30 15:51:14 +02:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ManageTagController
|
|
|
|
*
|
|
|
|
* Slim controller used to handle Shaarli manage tags page (rename and delete tags).
|
|
|
|
*/
|
|
|
|
class ManageTagController extends ShaarliAdminController
|
|
|
|
{
|
|
|
|
/**
|
2020-06-13 13:08:01 +02:00
|
|
|
* GET /admin/tags - Displays the manage tags page
|
2020-05-30 15:51:14 +02:00
|
|
|
*/
|
|
|
|
public function index(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$fromTag = $request->getParam('fromtag') ?? '';
|
|
|
|
|
|
|
|
$this->assignView('fromtag', escape($fromTag));
|
|
|
|
$this->assignView(
|
|
|
|
'pagetitle',
|
|
|
|
t('Manage tags') .' - '. $this->container->conf->get('general.title', 'Shaarli')
|
|
|
|
);
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
return $response->write($this->render(TemplatePage::CHANGE_TAG));
|
2020-05-30 15:51:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-13 13:08:01 +02:00
|
|
|
* POST /admin/tags - Update or delete provided tag
|
2020-05-30 15:51:14 +02:00
|
|
|
*/
|
|
|
|
public function save(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$this->checkToken($request);
|
|
|
|
|
|
|
|
$isDelete = null !== $request->getParam('deletetag') && null === $request->getParam('renametag');
|
|
|
|
|
|
|
|
$fromTag = escape(trim($request->getParam('fromtag') ?? ''));
|
|
|
|
$toTag = escape(trim($request->getParam('totag') ?? ''));
|
|
|
|
|
|
|
|
if (0 === strlen($fromTag) || false === $isDelete && 0 === strlen($toTag)) {
|
|
|
|
$this->saveWarningMessage(t('Invalid tags provided.'));
|
|
|
|
|
2020-06-13 13:08:01 +02:00
|
|
|
return $this->redirect($response, '/admin/tags');
|
2020-05-30 15:51:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move this to bookmark service
|
|
|
|
$count = 0;
|
|
|
|
$bookmarks = $this->container->bookmarkService->search(['searchtags' => $fromTag], BookmarkFilter::$ALL, true);
|
|
|
|
foreach ($bookmarks as $bookmark) {
|
|
|
|
if (false === $isDelete) {
|
|
|
|
$bookmark->renameTag($fromTag, $toTag);
|
|
|
|
} else {
|
|
|
|
$bookmark->deleteTag($fromTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->container->bookmarkService->set($bookmark, false);
|
|
|
|
$this->container->history->updateLink($bookmark);
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->container->bookmarkService->save();
|
|
|
|
|
|
|
|
if (true === $isDelete) {
|
|
|
|
$alert = sprintf(
|
|
|
|
t('The tag was removed from %d bookmark.', 'The tag was removed from %d bookmarks.', $count),
|
|
|
|
$count
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$alert = sprintf(
|
|
|
|
t('The tag was renamed in %d bookmark.', 'The tag was renamed in %d bookmarks.', $count),
|
|
|
|
$count
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->saveSuccessMessage($alert);
|
|
|
|
|
2020-06-13 13:08:01 +02:00
|
|
|
$redirect = true === $isDelete ? '/admin/tags' : '/?searchtags='. urlencode($toTag);
|
2020-05-30 15:51:14 +02:00
|
|
|
|
2020-06-13 13:08:01 +02:00
|
|
|
return $this->redirect($response, $redirect);
|
2020-05-30 15:51:14 +02:00
|
|
|
}
|
|
|
|
}
|