3 * @copyright Copyright (C) 2010-2021, 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/>.
23 use Friendica\Content\Text\BBCode;
24 use Friendica\Database\DBA;
26 use Friendica\Model\Item;
27 use Friendica\Model\Post;
28 use Friendica\Model\Tag;
29 use Friendica\Util\Strings;
31 function tagrm_post(App $a)
34 DI::baseUrl()->redirect($_SESSION['photo_return']);
37 if (!empty($_POST['submit']) && ($_POST['submit'] === DI::l10n()->t('Cancel'))) {
38 DI::baseUrl()->redirect($_SESSION['photo_return']);
42 foreach ($_POST['tag'] ?? [] as $tag) {
43 $tags[] = hex2bin(Strings::escapeTags(trim($tag)));
46 $item_id = $_POST['item'] ?? 0;
47 update_tags($item_id, $tags);
49 DI::baseUrl()->redirect($_SESSION['photo_return']);
54 * Updates tags from an item
60 function update_tags($item_id, $tags)
62 if (empty($item_id) || empty($tags)) {
66 $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => local_user()]);
67 if (!DBA::isResult($item)) {
71 foreach ($tags as $new_tag) {
72 if (preg_match_all('/([#@!])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism', $new_tag, $results, PREG_SET_ORDER)) {
73 foreach ($results as $tag) {
74 Tag::removeByHash($item['uri-id'], $tag[1], $tag[3], $tag[2]);
80 function tagrm_content(App $a)
84 $photo_return = $_SESSION['photo_return'] ?? '';
87 DI::baseUrl()->redirect($photo_return);
91 if (DI::args()->getArgc()== 3) {
92 update_tags(DI::args()->getArgv()[1], [Strings::escapeTags(trim(hex2bin(DI::args()->getArgv()[2])))]);
93 DI::baseUrl()->redirect($photo_return);
96 $item_id = ((DI::args()->getArgc()> 1) ? intval(DI::args()->getArgv()[1]) : 0);
98 DI::baseUrl()->redirect($photo_return);
102 $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => local_user()]);
103 if (!DBA::isResult($item)) {
104 DI::baseUrl()->redirect($photo_return);
107 $tag_text = Tag::getCSVByURIId($item['uri-id']);
109 $arr = explode(',', $tag_text);
112 DI::baseUrl()->redirect($photo_return);
115 $o .= '<h3>' . DI::l10n()->t('Remove Item Tag') . '</h3>';
117 $o .= '<p id="tag-remove-desc">' . DI::l10n()->t('Select a tag to remove: ') . '</p>';
119 $o .= '<form id="tagrm" action="tagrm" method="post" >';
120 $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
123 foreach ($arr as $x) {
124 $o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
128 $o .= '<input id="tagrm-submit" type="submit" name="submit" value="' . DI::l10n()->t('Remove') .'" />';
129 $o .= '<input id="tagrm-cancel" type="submit" name="submit" value="' . DI::l10n()->t('Cancel') .'" />';