From fd2e06781ddb4203c0d1254b9d3e4b9a8e9934e9 Mon Sep 17 00:00:00 2001 From: Jonny Tischbein Date: Tue, 23 Oct 2018 11:40:20 +0200 Subject: [PATCH] Fix adding / removing photo tags + tagrm delete via GET + Item::update / add Term::deleteAllTags --- mod/photos.php | 16 ++++---- mod/tagrm.php | 51 +++++++++++++++++++----- src/Model/Item.php | 14 ++++--- src/Model/Term.php | 15 +++++++ view/theme/frio/templates/photo_view.tpl | 13 ++++-- 5 files changed, 81 insertions(+), 28 deletions(-) diff --git a/mod/photos.php b/mod/photos.php index 008d59cd9..65ad9b5d4 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,15 @@ 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 = ''; + $tag_arr = []; foreach ($arr as $t) { - if (strlen($tag_str)) { - $tag_str .= ', '; - } - $tag_str .= BBCode::convert($t); + array_push($tag_arr, ['name' => BBCode::convert($t), + 'removeurl' => '/tagrm/'.$link_item['id'] . '/' . bin2hex($t)]); } - $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('[Remove any tag]')]; } } diff --git a/mod/tagrm.php b/mod/tagrm.php index 105cc0b3d..39581efbb 100644 --- a/mod/tagrm.php +++ b/mod/tagrm.php @@ -20,8 +20,27 @@ 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); + $tags = []; + if (defaults($_POST, 'tag', '')){ + foreach ($_POST['tag'] as $t){ + array_push($tags, hex2bin(notags(trim($t)))); + } + } + + $item_id = defaults($_POST,'item', 0); + update_tags($item_id, $tags); + + info(L10n::t('Tag(s) removed') . EOL ); + + $a->internalRedirect($_SESSION['photo_return']); + + // NOTREACHED +} + +function update_tags($item_id, $tags){ + if (empty($item_id) || empty($tags)){ + $a->internalRedirect($_SESSION['photo_return']); + } $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]); if (!DBA::isResult($item)) { @@ -29,25 +48,29 @@ function tagrm_post(App $a) } $arr = explode(',', $item['tag']); - for ($x = 0; $x < count($arr); $x ++) { - if ($arr[$x] === $tag) { - unset($arr[$x]); - break; + + foreach ($tags as $t) { + foreach ($arr as $i => $x) { + if (strcmp($x, $t) == 0) { + unset($arr[$i]); + break; + } } } $tag_str = implode(',',$arr); + if(empty($tag_str)){ + $tag_str = ''; + } Item::update(['tag' => $tag_str], ['id' => $item_id]); - info(L10n::t('Tag removed') . EOL ); + info(L10n::t('Tag(s) removed') . EOL ); $a->internalRedirect($_SESSION['photo_return']); // NOTREACHED } - - function tagrm_content(App $a) { $o = ''; @@ -57,6 +80,11 @@ function tagrm_content(App $a) // NOTREACHED } + if ($a->argc == 3){ + update_tags($a->argv[1], [hex2bin(notags(trim($a->argv[2])))]); + goaway('/' . $_SESSION['photo_return']); + } + $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0); if (!$item_id) { $a->internalRedirect($_SESSION['photo_return']); @@ -70,7 +98,8 @@ function tagrm_content(App $a) $arr = explode(',', $item['tag']); - if (!count($arr)) { + + if (empty($item['tag'])) { $a->internalRedirect($_SESSION['photo_return']); } @@ -83,7 +112,7 @@ function tagrm_content(App $a) $o .= ''; diff --git a/src/Model/Item.php b/src/Model/Item.php index 1c0b11db4..9b016295e 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -816,7 +816,7 @@ class Item extends BaseObject $tags = $fields['tag']; $fields['tag'] = null; } else { - $tags = ''; + $tags = null; } if (array_key_exists('file', $fields)) { @@ -895,10 +895,14 @@ class Item extends BaseObject } } - if (!empty($tags)) { - Term::insertFromTagFieldByItemId($item['id'], $tags); - if (!empty($item['tag'])) { - DBA::update('item', ['tag' => ''], ['id' => $item['id']]); + if (!is_null($tags)) { + Term::deleteAllTags($item['id']); + + if ($tags) { + Term::insertFromTagFieldByItemId($item['id'], $tags); + if (!empty($item['tag'])) { + DBA::update('item', ['tag' => ''], ['id' => $item['id']]); + } } } diff --git a/src/Model/Term.php b/src/Model/Term.php index 854861ccb..6c89b5d00 100644 --- a/src/Model/Term.php +++ b/src/Model/Term.php @@ -290,4 +290,19 @@ class Term return $return; } + + /* + * Deletes all Tags from an item + */ + public static function deleteAllTags($itemid) + { + $message = Item::selectFirst(['id'], ['id' => $itemid]); + if (!DBA::isResult($message)) { + return; + } + + // Clean up all tags + DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]); + + } } diff --git a/view/theme/frio/templates/photo_view.tpl b/view/theme/frio/templates/photo_view.tpl index e610926d4..cc72a10fa 100644 --- a/view/theme/frio/templates/photo_view.tpl +++ b/view/theme/frio/templates/photo_view.tpl @@ -51,12 +51,19 @@ {{* Tags and mentions *}} {{if $tags}} -
{{$tags.1}}
+
{{$tags.title}} + {{foreach $tags.tags as $t}} + + {{$t.name}} + {{if $t.removeurl}} (x) {{/if}} + + {{/foreach}} +
{{/if}} - {{if $tags.2}} + {{if $tags.removeanyurl}}
- {{$tags.3}} + {{$tags.removetitle}}
{{/if}}