]> git.mxchange.org Git - friendica.git/blob - mod/tagrm.php
Merge remote-tracking branch 'upstream/develop' into item-notification
[friendica.git] / mod / tagrm.php
1 <?php
2 /**
3  * @file mod/tagrm.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\L10n;
9 use Friendica\Database\DBA;
10 use Friendica\DI;
11 use Friendica\Model\Item;
12 use Friendica\Model\Term;
13 use Friendica\Util\Strings;
14
15 function tagrm_post(App $a)
16 {
17         if (!local_user()) {
18                 DI::baseUrl()->redirect($_SESSION['photo_return']);
19         }
20
21         if (!empty($_POST['submit']) && ($_POST['submit'] === L10n::t('Cancel'))) {
22                 DI::baseUrl()->redirect($_SESSION['photo_return']);
23         }
24
25         $tags = [];
26         foreach ($_POST['tag'] ?? [] as $tag) {
27                 $tags[] = hex2bin(Strings::escapeTags(trim($tag)));
28         }
29
30         $item_id = $_POST['item'] ?? 0;
31         update_tags($item_id, $tags);
32         info(L10n::t('Tag(s) removed') . EOL);
33
34         DI::baseUrl()->redirect($_SESSION['photo_return']);
35         // NOTREACHED
36 }
37
38 /**
39  * Updates tags from an item
40  *
41  * @param $item_id
42  * @param $tags array
43  * @throws Exception
44  */
45 function update_tags($item_id, $tags){
46         if (empty($item_id) || empty($tags)){
47                 return;
48         }
49
50         $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
51         if (!DBA::isResult($item)) {
52                 return;
53         }
54
55         $old_tags = explode(',', $item['tag']);
56
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]);
61                                 break;
62                         }
63                 }
64         }
65
66         $tag_str = implode(',', $old_tags);
67         Term::insertFromTagFieldByItemId($item_id, $tag_str);
68 }
69
70 function tagrm_content(App $a)
71 {
72         $o = '';
73
74         if (!local_user()) {
75                 DI::baseUrl()->redirect($_SESSION['photo_return']);
76                 // NOTREACHED
77         }
78
79         if ($a->argc == 3) {
80                 update_tags($a->argv[1], [Strings::escapeTags(trim(hex2bin($a->argv[2])))]);
81                 DI::baseUrl()->redirect($_SESSION['photo_return']);
82         }
83
84         $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
85         if (!$item_id) {
86                 DI::baseUrl()->redirect($_SESSION['photo_return']);
87                 // NOTREACHED
88         }
89
90         $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
91         if (!DBA::isResult($item)) {
92                 DI::baseUrl()->redirect($_SESSION['photo_return']);
93         }
94
95         $arr = explode(',', $item['tag']);
96
97
98         if (empty($item['tag'])) {
99                 DI::baseUrl()->redirect($_SESSION['photo_return']);
100         }
101
102         $o .= '<h3>' . L10n::t('Remove Item Tag') . '</h3>';
103
104         $o .= '<p id="tag-remove-desc">' . L10n::t('Select a tag to remove: ') . '</p>';
105
106         $o .= '<form id="tagrm" action="tagrm" method="post" >';
107         $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
108         $o .= '<ul>';
109
110         foreach ($arr as $x) {
111                 $o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
112         }
113
114         $o .= '</ul>';
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') .'" />';
117         $o .= '</form>';
118
119         return $o;
120 }