Fixed fatal error
[friendica.git/.git] / mod / tagrm.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Content\Text\BBCode;
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Item;
27 use Friendica\Model\Term;
28 use Friendica\Util\Strings;
29
30 function tagrm_post(App $a)
31 {
32         if (!local_user()) {
33                 DI::baseUrl()->redirect($_SESSION['photo_return']);
34         }
35
36         if (!empty($_POST['submit']) && ($_POST['submit'] === DI::l10n()->t('Cancel'))) {
37                 DI::baseUrl()->redirect($_SESSION['photo_return']);
38         }
39
40         $tags = [];
41         foreach ($_POST['tag'] ?? [] as $tag) {
42                 $tags[] = hex2bin(Strings::escapeTags(trim($tag)));
43         }
44
45         $item_id = $_POST['item'] ?? 0;
46         update_tags($item_id, $tags);
47         info(DI::l10n()->t('Tag(s) removed') . EOL);
48
49         DI::baseUrl()->redirect($_SESSION['photo_return']);
50         // NOTREACHED
51 }
52
53 /**
54  * Updates tags from an item
55  *
56  * @param $item_id
57  * @param $tags array
58  * @throws Exception
59  */
60 function update_tags($item_id, $tags){
61         if (empty($item_id) || empty($tags)){
62                 return;
63         }
64
65         $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
66         if (!DBA::isResult($item)) {
67                 return;
68         }
69
70         $old_tags = explode(',', $item['tag']);
71
72         foreach ($tags as $new_tag) {
73                 foreach ($old_tags as $index => $old_tag) {
74                         if (strcmp($old_tag, $new_tag) == 0) {
75                                 unset($old_tags[$index]);
76                                 break;
77                         }
78                 }
79         }
80
81         $tag_str = implode(',', $old_tags);
82         Term::insertFromTagFieldByItemId($item_id, $tag_str);
83 }
84
85 function tagrm_content(App $a)
86 {
87         $o = '';
88
89         if (!local_user()) {
90                 DI::baseUrl()->redirect($_SESSION['photo_return']);
91                 // NOTREACHED
92         }
93
94         if ($a->argc == 3) {
95                 update_tags($a->argv[1], [Strings::escapeTags(trim(hex2bin($a->argv[2])))]);
96                 DI::baseUrl()->redirect($_SESSION['photo_return']);
97         }
98
99         $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
100         if (!$item_id) {
101                 DI::baseUrl()->redirect($_SESSION['photo_return']);
102                 // NOTREACHED
103         }
104
105         $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
106         if (!DBA::isResult($item)) {
107                 DI::baseUrl()->redirect($_SESSION['photo_return']);
108         }
109
110         $arr = explode(',', $item['tag']);
111
112
113         if (empty($item['tag'])) {
114                 DI::baseUrl()->redirect($_SESSION['photo_return']);
115         }
116
117         $o .= '<h3>' . DI::l10n()->t('Remove Item Tag') . '</h3>';
118
119         $o .= '<p id="tag-remove-desc">' . DI::l10n()->t('Select a tag to remove: ') . '</p>';
120
121         $o .= '<form id="tagrm" action="tagrm" method="post" >';
122         $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
123         $o .= '<ul>';
124
125         foreach ($arr as $x) {
126                 $o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
127         }
128
129         $o .= '</ul>';
130         $o .= '<input id="tagrm-submit" type="submit" name="submit" value="' . DI::l10n()->t('Remove') .'" />';
131         $o .= '<input id="tagrm-cancel" type="submit" name="submit" value="' . DI::l10n()->t('Cancel') .'" />';
132         $o .= '</form>';
133
134         return $o;
135 }