7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\L10n;
9 use Friendica\Database\DBA;
11 use Friendica\Model\Item;
12 use Friendica\Model\Term;
13 use Friendica\Util\Strings;
15 function tagrm_post(App $a)
18 DI::baseUrl()->redirect($_SESSION['photo_return']);
21 if (!empty($_POST['submit']) && ($_POST['submit'] === L10n::t('Cancel'))) {
22 DI::baseUrl()->redirect($_SESSION['photo_return']);
26 foreach ($_POST['tag'] ?? [] as $tag) {
27 $tags[] = hex2bin(Strings::escapeTags(trim($tag)));
30 $item_id = $_POST['item'] ?? 0;
31 update_tags($item_id, $tags);
32 info(L10n::t('Tag(s) removed') . EOL);
34 DI::baseUrl()->redirect($_SESSION['photo_return']);
39 * Updates tags from an item
45 function update_tags($item_id, $tags){
46 if (empty($item_id) || empty($tags)){
50 $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
51 if (!DBA::isResult($item)) {
55 $old_tags = explode(',', $item['tag']);
57 foreach ($tags as $new_tag) {
58 foreach ($old_tags as $index => $old_tag) {
59 if (strcmp($old_tag, $new_tag) == 0) {
60 unset($old_tags[$index]);
66 $tag_str = implode(',', $old_tags);
67 Term::insertFromTagFieldByItemId($item_id, $tag_str);
70 function tagrm_content(App $a)
75 DI::baseUrl()->redirect($_SESSION['photo_return']);
80 update_tags($a->argv[1], [Strings::escapeTags(trim(hex2bin($a->argv[2])))]);
81 DI::baseUrl()->redirect($_SESSION['photo_return']);
84 $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
86 DI::baseUrl()->redirect($_SESSION['photo_return']);
90 $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
91 if (!DBA::isResult($item)) {
92 DI::baseUrl()->redirect($_SESSION['photo_return']);
95 $arr = explode(',', $item['tag']);
98 if (empty($item['tag'])) {
99 DI::baseUrl()->redirect($_SESSION['photo_return']);
102 $o .= '<h3>' . L10n::t('Remove Item Tag') . '</h3>';
104 $o .= '<p id="tag-remove-desc">' . L10n::t('Select a tag to remove: ') . '</p>';
106 $o .= '<form id="tagrm" action="tagrm" method="post" >';
107 $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
110 foreach ($arr as $x) {
111 $o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
115 $o .= '<input id="tagrm-submit" type="submit" name="submit" value="' . L10n::t('Remove') .'" />';
116 $o .= '<input id="tagrm-cancel" type="submit" name="submit" value="' . L10n::t('Cancel') .'" />';