3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Post\Tag;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Model\Post;
30 use Friendica\Model\Tag;
31 use Friendica\Module\Response;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
35 class Remove extends \Friendica\BaseModule
37 /** @var IHandleUserSessions */
40 public function __construct(IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
42 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
44 $this->session = $session;
47 protected function post(array $request = [])
49 if (!$this->session->getLocalUserId()) {
50 $this->baseUrl->redirect($request['return'] ?? '');
54 if (isset($request['cancel'])) {
55 $this->baseUrl->redirect($request['return'] ?? '');
59 foreach ($request['tag'] ?? [] as $tag => $checked) {
61 $tags[] = hex2bin(trim($tag));
65 $this->removeTagsFromItem($this->parameters['item_id'], $tags);
66 $this->baseUrl->redirect($request['return'] ?? '');
69 protected function content(array $request = []): string
71 $returnUrl = $request['return'] ?? '';
73 if (!$this->session->getLocalUserId()) {
74 $this->baseUrl->redirect($returnUrl);
77 if (isset($this->parameters['tag_name'])) {
78 $this->removeTagsFromItem($this->parameters['item_id'], [trim(hex2bin($this->parameters['tag_name']))]);
79 $this->baseUrl->redirect($returnUrl);
82 $item_id = intval($this->parameters['item_id']);
84 $this->baseUrl->redirect($returnUrl);
87 $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]);
89 $this->baseUrl->redirect($returnUrl);
92 $tag_text = Tag::getCSVByURIId($item['uri-id']);
94 $tags = explode(',', $tag_text);
96 $this->baseUrl->redirect($returnUrl);
99 $tag_checkboxes = array_map(function ($tag_text) {
100 return ['tag[' . bin2hex($tag_text) . ']', BBCode::toPlaintext($tag_text)];
103 $tpl = Renderer::getMarkupTemplate('post/tag/remove.tpl');
104 return Renderer::replaceMacros($tpl, [
106 'header' => $this->t('Remove Item Tag'),
107 'desc' => $this->t('Select a tag to remove: '),
108 'remove' => $this->t('Remove'),
109 'cancel' => $this->t('Cancel'),
112 '$item_id' => $item_id,
113 '$return' => $returnUrl,
114 '$tag_checkboxes' => $tag_checkboxes,
119 * @param int $item_id
123 private function removeTagsFromItem(int $item_id, array $tags)
125 if (empty($item_id) || empty($tags)) {
129 $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $this->session->getLocalUserId()]);
134 foreach ($tags as $tag) {
135 if (preg_match('~([#@!])\[url=([^\[\]]*)]([^\[\]]*)\[/url]~im', $tag, $results)) {
136 Tag::removeByHash($item['uri-id'], $results[1], $results[3], $results[2]);