]> git.mxchange.org Git - friendica.git/blob - mod/tagrm.php
Avoid warning "Undefined namespace prefix"
[friendica.git] / mod / tagrm.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Post;
27 use Friendica\Model\Tag;
28
29 function tagrm_post(App $a)
30 {
31         if (!local_user()) {
32                 DI::baseUrl()->redirect($_SESSION['photo_return']);
33         }
34
35         if (!empty($_POST['submit']) && ($_POST['submit'] === DI::l10n()->t('Cancel'))) {
36                 DI::baseUrl()->redirect($_SESSION['photo_return']);
37         }
38
39         $tags = [];
40         foreach ($_POST['tag'] ?? [] as $tag) {
41                 $tags[] = hex2bin(trim($tag));
42         }
43
44         $item_id = $_POST['item'] ?? 0;
45         update_tags($item_id, $tags);
46
47         DI::baseUrl()->redirect($_SESSION['photo_return']);
48         // NOTREACHED
49 }
50
51 /**
52  * Updates tags from an item
53  *
54  * @param $item_id
55  * @param $tags array
56  * @throws Exception
57  */
58 function update_tags($item_id, $tags)
59 {
60         if (empty($item_id) || empty($tags)) {
61                 return;
62         }
63
64         $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => local_user()]);
65         if (!DBA::isResult($item)) {
66                 return;
67         }
68
69         foreach ($tags as $new_tag) {
70                 if (preg_match_all('/([#@!])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism', $new_tag, $results, PREG_SET_ORDER)) {
71                         foreach ($results as $tag) {
72                                 Tag::removeByHash($item['uri-id'], $tag[1], $tag[3], $tag[2]);
73                         }
74                 }
75         }
76 }
77
78 function tagrm_content(App $a)
79 {
80         $o = '';
81
82         $photo_return = $_SESSION['photo_return'] ?? '';
83
84         if (!local_user()) {
85                 DI::baseUrl()->redirect($photo_return);
86                 // NOTREACHED
87         }
88
89         if (DI::args()->getArgc()== 3) {
90                 update_tags(DI::args()->getArgv()[1], [trim(hex2bin(DI::args()->getArgv()[2]))]);
91                 DI::baseUrl()->redirect($photo_return);
92         }
93
94         $item_id = ((DI::args()->getArgc()> 1) ? intval(DI::args()->getArgv()[1]) : 0);
95         if (!$item_id) {
96                 DI::baseUrl()->redirect($photo_return);
97                 // NOTREACHED
98         }
99
100         $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => local_user()]);
101         if (!DBA::isResult($item)) {
102                 DI::baseUrl()->redirect($photo_return);
103         }
104
105         $tag_text = Tag::getCSVByURIId($item['uri-id']);
106
107         $arr = explode(',', $tag_text);
108
109         if (empty($arr)) {
110                 DI::baseUrl()->redirect($photo_return);
111         }
112
113         $o .= '<h3>' . DI::l10n()->t('Remove Item Tag') . '</h3>';
114
115         $o .= '<p id="tag-remove-desc">' . DI::l10n()->t('Select a tag to remove: ') . '</p>';
116
117         $o .= '<form id="tagrm" action="tagrm" method="post" >';
118         $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
119         $o .= '<ul>';
120
121         foreach ($arr as $x) {
122                 $o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
123         }
124
125         $o .= '</ul>';
126         $o .= '<input id="tagrm-submit" type="submit" name="submit" value="' . DI::l10n()->t('Remove') .'" />';
127         $o .= '<input id="tagrm-cancel" type="submit" name="submit" value="' . DI::l10n()->t('Cancel') .'" />';
128         $o .= '</form>';
129
130         return $o;
131 }