diff --git a/mod/photos.php b/mod/photos.php index 008d59cd9c..b80350221b 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -612,7 +612,7 @@ function photos_post(App $a) } } elseif (strpos($tag, '#') === 0) { $tagname = substr($tag, 1); - $str_tags .= '#[url=' . System::baseUrl() . "/search?tag=" . $tagname . ']' . $tagname . '[/url]'; + $str_tags .= '#[url=' . System::baseUrl() . "/search?tag=" . $tagname . ']' . $tagname . '[/url],'; } } } @@ -1417,17 +1417,17 @@ function photos_content(App $a) if (count($linked_items) && strlen($link_item['tag'])) { $arr = explode(',', $link_item['tag']); // parse tags and add links - $tag_str = ''; - foreach ($arr as $t) { - if (strlen($tag_str)) { - $tag_str .= ', '; - } - $tag_str .= BBCode::convert($t); + $tag_arr = []; + foreach ($arr as $tag) { + $tag_arr[] = [ + 'name' => BBCode::convert($tag), + 'removeurl' => '/tagrm/'.$link_item['id'] . '/' . bin2hex($tag) + ]; } - $tags = [L10n::t('Tags: '), $tag_str]; + $tags = ['title' => L10n::t('Tags: '), 'tags' => $tag_arr]; if ($cmd === 'edit') { - $tags[] = 'tagrm/' . $link_item['id']; - $tags[] = L10n::t('[Remove any tag]'); + $tags['removeanyurl'] = 'tagrm/' . $link_item['id']; + $tags['removetitle'] = L10n::t('[Select tags to remove]'); } } diff --git a/mod/tagrm.php b/mod/tagrm.php index 105cc0b3d5..2678748de5 100644 --- a/mod/tagrm.php +++ b/mod/tagrm.php @@ -6,9 +6,9 @@ use Friendica\App; use Friendica\Content\Text\BBCode; use Friendica\Core\L10n; -use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Model\Item; +use Friendica\Model\Term; function tagrm_post(App $a) { @@ -20,33 +20,48 @@ function tagrm_post(App $a) $a->internalRedirect($_SESSION['photo_return']); } - $tag = (x($_POST,'tag') ? hex2bin(notags(trim($_POST['tag']))) : ''); - $item_id = (x($_POST,'item') ? intval($_POST['item']) : 0); - - $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]); - if (!DBA::isResult($item)) { - $a->internalRedirect($_SESSION['photo_return']); + $tags = []; + foreach (defaults($_POST, 'tag', []) as $tag) { + $tags[] = hex2bin(notags(trim($tag))); } - $arr = explode(',', $item['tag']); - for ($x = 0; $x < count($arr); $x ++) { - if ($arr[$x] === $tag) { - unset($arr[$x]); - break; - } - } + $item_id = defaults($_POST,'item', 0); + update_tags($item_id, $tags); + info(L10n::t('Tag(s) removed') . EOL); - $tag_str = implode(',',$arr); - - Item::update(['tag' => $tag_str], ['id' => $item_id]); - - info(L10n::t('Tag removed') . EOL ); $a->internalRedirect($_SESSION['photo_return']); - // NOTREACHED } +/** + * Updates tags from an item + * @param $item_id + * @param $tags array + */ +function update_tags($item_id, $tags){ + if (empty($item_id) || empty($tags)){ + return; + } + $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]); + if (!DBA::isResult($item)) { + return; + } + + $old_tags = explode(',', $item['tag']); + + foreach ($tags as $new_tag) { + foreach ($old_tags as $index => $old_tag) { + if (strcmp($old_tag, $new_tag) == 0) { + unset($old_tags[$index]); + break; + } + } + } + + $tag_str = implode(',', $old_tags); + Term::insertFromTagFieldByItemId($item_id, $tag_str); +} function tagrm_content(App $a) { @@ -57,6 +72,11 @@ function tagrm_content(App $a) // NOTREACHED } + if ($a->argc == 3) { + update_tags($a->argv[1], [notags(trim(hex2bin($a->argv[2])))]); + $a->internalRedirect($_SESSION['photo_return']); + } + $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0); if (!$item_id) { $a->internalRedirect($_SESSION['photo_return']); @@ -70,7 +90,8 @@ function tagrm_content(App $a) $arr = explode(',', $item['tag']); - if (!count($arr)) { + + if (empty($item['tag'])) { $a->internalRedirect($_SESSION['photo_return']); } @@ -83,7 +104,7 @@ function tagrm_content(App $a) $o .= '