From e0dc7a471e036cd9caa2f46266ea3358306805f3 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 1 Nov 2022 23:42:20 -0400 Subject: [PATCH] Create Post/Tag/Remove module class and route - Fix tag name parameter in single tag removal link in mod/photos - Remove "/post" from backend routes as it was preventing sessions from working in this module. No existing module had a route starting with "/post". --- mod/photos.php | 14 +-- src/App/Mode.php | 1 - src/Module/Post/Tag/Remove.php | 140 +++++++++++++++++++++++++++++ static/routes.config.php | 5 ++ view/templates/post/tag/remove.tpl | 19 ++++ 5 files changed, 172 insertions(+), 7 deletions(-) create mode 100644 src/Module/Post/Tag/Remove.php create mode 100644 view/templates/post/tag/remove.tpl diff --git a/mod/photos.php b/mod/photos.php index 2d7516be93..ccf0525cb0 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -1250,15 +1250,17 @@ function photos_content(App $a) if (!empty($link_item['id'])) { // parse tags and add links $tag_arr = []; - foreach (Tag::getByURIId($link_item['uri-id']) as $tag) { - $tag_arr[] = [ - 'name' => $tag['name'], - 'removeurl' => '/tagrm/' . $link_item['id'] . '/' . bin2hex($tag['name']) - ]; + foreach (explode(',', Tag::getCSVByURIId($link_item['uri-id'])) as $tag_name) { + if ($tag_name) { + $tag_arr[] = [ + 'name' => BBCode::toPlaintext($tag_name), + 'removeurl' => 'post/' . $link_item['id'] . '/tag/remove/' . bin2hex($tag_name) . '?return=' . urlencode(DI::args()->getCommand()), + ]; + } } $tags = ['title' => DI::l10n()->t('Tags: '), 'tags' => $tag_arr]; if ($cmd === 'edit') { - $tags['removeanyurl'] = 'tagrm/' . $link_item['id']; + $tags['removeanyurl'] = 'post/' . $link_item['id'] . '/tag/remove?return=' . urlencode(DI::args()->getCommand()); $tags['removetitle'] = DI::l10n()->t('[Select tags to remove]'); } } diff --git a/src/App/Mode.php b/src/App/Mode.php index 5d6bd759f2..59a47d8284 100644 --- a/src/App/Mode.php +++ b/src/App/Mode.php @@ -68,7 +68,6 @@ class Mode 'objects', 'outbox', 'poco', - 'post', 'pubsub', 'pubsubhubbub', 'receive', diff --git a/src/Module/Post/Tag/Remove.php b/src/Module/Post/Tag/Remove.php new file mode 100644 index 0000000000..94a1ecfe49 --- /dev/null +++ b/src/Module/Post/Tag/Remove.php @@ -0,0 +1,140 @@ +. + * + */ + +namespace Friendica\Module\Post\Tag; + +use Friendica\App; +use Friendica\Content\Text\BBCode; +use Friendica\Core\L10n; +use Friendica\Core\Renderer; +use Friendica\Core\Session\Capability\IHandleUserSessions; +use Friendica\Model\Post; +use Friendica\Model\Tag; +use Friendica\Module\Response; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +class Remove extends \Friendica\BaseModule +{ + /** @var IHandleUserSessions */ + private $session; + + public function __construct(IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) + { + parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->session = $session; + } + + protected function post(array $request = []) + { + if (!$this->session->getLocalUserId()) { + $this->baseUrl->redirect($request['return'] ?? ''); + } + + + if (isset($request['cancel'])) { + $this->baseUrl->redirect($request['return'] ?? ''); + } + + $tags = []; + foreach ($request['tag'] ?? [] as $tag => $checked) { + if ($checked) { + $tags[] = hex2bin(trim($tag)); + } + } + + $this->removeTagsFromItem($this->parameters['item_id'], $tags); + $this->baseUrl->redirect($request['return'] ?? ''); + } + + protected function content(array $request = []): string + { + $returnUrl = $request['return'] ?? ''; + + if (!$this->session->getLocalUserId()) { + $this->baseUrl->redirect($returnUrl); + } + + if (isset($this->parameters['tag_name'])) { + $this->removeTagsFromItem($this->parameters['item_id'], [trim(hex2bin($this->parameters['tag_name']))]); + $this->baseUrl->redirect($returnUrl); + } + + $item_id = intval($this->parameters['item_id']); + if (!$item_id) { + $this->baseUrl->redirect($returnUrl); + } + + $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]); + if (!$item) { + $this->baseUrl->redirect($returnUrl); + } + + $tag_text = Tag::getCSVByURIId($item['uri-id']); + + $tags = explode(',', $tag_text); + if (empty($tags)) { + $this->baseUrl->redirect($returnUrl); + } + + $tag_checkboxes = array_map(function ($tag_text) { + return ['tag[' . bin2hex($tag_text) . ']', BBCode::toPlaintext($tag_text)]; + }, $tags); + + $tpl = Renderer::getMarkupTemplate('post/tag/remove.tpl'); + return Renderer::replaceMacros($tpl, [ + '$l10n' => [ + 'header' => $this->t('Remove Item Tag'), + 'desc' => $this->t('Select a tag to remove: '), + 'remove' => $this->t('Remove'), + 'cancel' => $this->t('Cancel'), + ], + + '$item_id' => $item_id, + '$return' => $returnUrl, + '$tag_checkboxes' => $tag_checkboxes, + ]); + } + + /** + * @param int $item_id + * @param array $tags + * @throws \Exception + */ + private function removeTagsFromItem(int $item_id, array $tags) + { + if (empty($item_id) || empty($tags)) { + return; + } + + $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]); + if (empty($item)) { + return; + } + + foreach ($tags as $tag) { + if (preg_match('~([#@!])\[url=([^\[\]]*)]([^\[\]]*)\[/url]~im', $tag, $results)) { + Tag::removeByHash($item['uri-id'], $results[1], $results[3], $results[2]); + } + } + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 53bbf3eb7f..9066c48762 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -531,6 +531,11 @@ return [ ], '/ping' => [Module\Notifications\Ping::class, [R::GET]], + + '/post' => [ + '/{item_id}/tag/remove[/{tag_name}]' => [Module\Post\Tag\Remove::class, [R::GET, R::POST]], + ], + '/pretheme' => [Module\ThemeDetails::class, [R::GET]], '/probe' => [Module\Debug\Probe::class, [R::GET]], diff --git a/view/templates/post/tag/remove.tpl b/view/templates/post/tag/remove.tpl new file mode 100644 index 0000000000..86c54af911 --- /dev/null +++ b/view/templates/post/tag/remove.tpl @@ -0,0 +1,19 @@ +
+

{{$l10n.header}}

+ +

{{$l10n.desc}}

+ +
+
    +{{foreach $tag_checkboxes as $tag_checkbox}} +
  • + {{include file="field_checkbox.tpl" field=$tag_checkbox}} +
  • +{{/foreach}} +
+

+ + +

+
+
-- 2.39.5