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