Merge pull request #6003 from JonnyTischbein/issue_phototag

Fix adding / removing photo tags
This commit is contained in:
Hypolite Petovan 2018-10-25 20:48:03 -04:00 committed by GitHub
commit 884ab0d71d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 38 deletions

View File

@ -612,7 +612,7 @@ function photos_post(App $a)
} }
} elseif (strpos($tag, '#') === 0) { } elseif (strpos($tag, '#') === 0) {
$tagname = substr($tag, 1); $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'])) { if (count($linked_items) && strlen($link_item['tag'])) {
$arr = explode(',', $link_item['tag']); $arr = explode(',', $link_item['tag']);
// parse tags and add links // parse tags and add links
$tag_str = ''; $tag_arr = [];
foreach ($arr as $t) { foreach ($arr as $tag) {
if (strlen($tag_str)) { $tag_arr[] = [
$tag_str .= ', '; 'name' => BBCode::convert($tag),
} 'removeurl' => '/tagrm/'.$link_item['id'] . '/' . bin2hex($tag)
$tag_str .= BBCode::convert($t); ];
} }
$tags = [L10n::t('Tags: '), $tag_str]; $tags = ['title' => L10n::t('Tags: '), 'tags' => $tag_arr];
if ($cmd === 'edit') { if ($cmd === 'edit') {
$tags[] = 'tagrm/' . $link_item['id']; $tags['removeanyurl'] = 'tagrm/' . $link_item['id'];
$tags[] = L10n::t('[Remove any tag]'); $tags['removetitle'] = L10n::t('[Select tags to remove]');
} }
} }

View File

@ -6,9 +6,9 @@
use Friendica\App; use Friendica\App;
use Friendica\Content\Text\BBCode; use Friendica\Content\Text\BBCode;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Term;
function tagrm_post(App $a) function tagrm_post(App $a)
{ {
@ -20,33 +20,48 @@ function tagrm_post(App $a)
$a->internalRedirect($_SESSION['photo_return']); $a->internalRedirect($_SESSION['photo_return']);
} }
$tag = (x($_POST,'tag') ? hex2bin(notags(trim($_POST['tag']))) : ''); $tags = [];
$item_id = (x($_POST,'item') ? intval($_POST['item']) : 0); foreach (defaults($_POST, 'tag', []) as $tag) {
$tags[] = hex2bin(notags(trim($tag)));
$item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
if (!DBA::isResult($item)) {
$a->internalRedirect($_SESSION['photo_return']);
} }
$arr = explode(',', $item['tag']); $item_id = defaults($_POST,'item', 0);
for ($x = 0; $x < count($arr); $x ++) { update_tags($item_id, $tags);
if ($arr[$x] === $tag) { info(L10n::t('Tag(s) removed') . EOL);
unset($arr[$x]);
break;
}
}
$tag_str = implode(',',$arr);
Item::update(['tag' => $tag_str], ['id' => $item_id]);
info(L10n::t('Tag removed') . EOL );
$a->internalRedirect($_SESSION['photo_return']); $a->internalRedirect($_SESSION['photo_return']);
// NOTREACHED // 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) function tagrm_content(App $a)
{ {
@ -57,6 +72,11 @@ function tagrm_content(App $a)
// NOTREACHED // 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); $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
if (!$item_id) { if (!$item_id) {
$a->internalRedirect($_SESSION['photo_return']); $a->internalRedirect($_SESSION['photo_return']);
@ -70,7 +90,8 @@ function tagrm_content(App $a)
$arr = explode(',', $item['tag']); $arr = explode(',', $item['tag']);
if (!count($arr)) {
if (empty($item['tag'])) {
$a->internalRedirect($_SESSION['photo_return']); $a->internalRedirect($_SESSION['photo_return']);
} }
@ -83,7 +104,7 @@ function tagrm_content(App $a)
$o .= '<ul>'; $o .= '<ul>';
foreach ($arr as $x) { foreach ($arr as $x) {
$o .= '<li><input type="checkbox" name="tag" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>'; $o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
} }
$o .= '</ul>'; $o .= '</ul>';

View File

@ -816,7 +816,7 @@ class Item extends BaseObject
$tags = $fields['tag']; $tags = $fields['tag'];
$fields['tag'] = null; $fields['tag'] = null;
} else { } else {
$tags = ''; $tags = null;
} }
if (array_key_exists('file', $fields)) { if (array_key_exists('file', $fields)) {
@ -895,7 +895,7 @@ class Item extends BaseObject
} }
} }
if (!empty($tags)) { if (!is_null($tags)) {
Term::insertFromTagFieldByItemId($item['id'], $tags); Term::insertFromTagFieldByItemId($item['id'], $tags);
if (!empty($item['tag'])) { if (!empty($item['tag'])) {
DBA::update('item', ['tag' => ''], ['id' => $item['id']]); DBA::update('item', ['tag' => ''], ['id' => $item['id']]);

View File

@ -76,7 +76,7 @@ class Term
$message['tag'] = $tags; $message['tag'] = $tags;
// Clean up all tags // Clean up all tags
DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]]); self::deleteByItemId($itemid);
if ($message['deleted']) { if ($message['deleted']) {
return; return;
@ -290,4 +290,20 @@ class Term
return $return; return $return;
} }
/**
* Delete all tags from an item
* @param int itemid - choose from which item the tags will be removed
* @param array type - items type. default is [TERM_HASHTAG, TERM_MENTION]
*/
public static function deleteByItemId($itemid, $type = [TERM_HASHTAG, TERM_MENTION])
{
if (empty($itemid)) {
return;
}
// Clean up all tags
DBA::delete('term', ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => $type]);
}
} }

View File

@ -51,12 +51,19 @@
{{* Tags and mentions *}} {{* Tags and mentions *}}
{{if $tags}} {{if $tags}}
<div id="photo-tags">{{$tags.1}}</div> <div id="photo-tags">{{$tags.title}}
{{foreach $tags.tags as $t}}
<span class="category label btn-success sm">
<span class="p-category">{{$t.name}}</span>
{{if $t.removeurl}} <a href="{{$t.removeurl}}">(X)</a> {{/if}}
</span>
{{/foreach}}
</div>
{{/if}} {{/if}}
{{if $tags.2}} {{if $tags.removeanyurl}}
<div id="tag-remove"> <div id="tag-remove">
<a href="{{$tags.2}}">{{$tags.3}}</a> <a href="{{$tags.removeanyurl}}">{{$tags.removetitle}}</a>
</div> </div>
{{/if}} {{/if}}